Skip to content
Snippets Groups Projects
Commit 2b225255 authored by Demian Katz's avatar Demian Katz
Browse files

Added console tool to regenerate hierarchy tree cache. Added more flexible...

Added console tool to regenerate hierarchy tree cache.  Added more flexible cache lifetime settings to support new tool.
parent cb70ff78
No related merge requests found
......@@ -4,7 +4,7 @@ show = true
; The source of the hierarchy data -- may be Solr or XMLFile
treeSource = Solr
; When using Solr as a treeSource, this value determines how long tree data is
; cached (in seconds) -- default 12h
; cached (in seconds, or -1 to never expire) -- default 12h
solrCacheTime = 43200
; When using XMLFile as a treeSource, this value specifies where tree data is found
;XMLFileDir = /usr/local/vufind/hierarchy_xml
......
......@@ -4,7 +4,7 @@ show = false
; The source of the hierarchy data -- may be Solr or XMLFile
;treeSource = Solr
; When using Solr as a treeSource, this value determines how long tree data is
; cached (in seconds) -- default 12h
; cached (in seconds, or -1 to never expire) -- default 12h
;solrCacheTime = 0
; When using XMLFile as a treeSource, this value specifies where tree data is found
;XMLFileDir = /usr/local/vufind/hierarchy_xml
......
......@@ -113,10 +113,11 @@ abstract class AbstractBase implements \Zend\Log\LoggerAwareInterface
* Get XML for the specified hierarchy ID.
*
* @param string $id Hierarchy ID.
* @param array $options Additional options for XML generation.
*
* @return string
*/
abstract public function getXML($id);
abstract public function getXML($id, $options = array());
/**
* Does this data source support the specified hierarchy ID?
......
......@@ -71,23 +71,24 @@ class Solr extends AbstractBase
*
* Build the XML file from the Solr fields
*
* TODO: this should return false if it fails.
*
* @param string $id Hierarchy ID.
* @param string $id Hierarchy ID.
* @param array $options Additional options for XML generation. (Currently one
* option is supported: 'refresh' may be set to true to bypass caching).
*
* @return string
*/
public function getXML($id)
public function getXML($id, $options = array())
{
$top = $this->db->getRecord($id);
$cacheFile = (null !== $this->cacheDir)
? $this->cacheDir . '/hierarchyTree_' . urlencode($id) . '.xml'
: false;
$useCache = isset($options['refresh']) ? !$options['refresh'] : true;
$cacheTime = $this->getHierarchyDriver()->getTreeCacheTime();
if ($cacheFile && file_exists($cacheFile)
&& filemtime($cacheFile) > (time() - $cacheTime)
if ($useCache && file_exists($cacheFile)
&& ($cacheTime < 0 || filemtime($cacheFile) > (time() - $cacheTime))
) {
$this->debug("Using cached data from $cacheFile");
$xml = file_get_contents($cacheFile);
......
......@@ -78,10 +78,11 @@ class XMLFile extends AbstractBase
* Get XML for the specified hierarchy ID.
*
* @param string $id Hierarchy ID.
* @param array $options Additional options for XML generation (unused here).
*
* @return string
*/
public function getXML($id)
public function getXML($id, $options = array())
{
return file_get_contents($this->getFilename($id));
}
......
......@@ -117,6 +117,16 @@ class AbstractBase extends AbstractActionController
return $this->getServiceLocator()->get('VuFind\ILSConnection');
}
/**
* Get the search manager.
*
* @return \VuFind\Search\Manager
*/
public function getSearchManager()
{
return $this->getServiceLocator()->get('SearchManager');
}
/**
* Get a database table object.
*
......
......@@ -385,4 +385,25 @@ class UtilController extends AbstractBase
}
return $this->getSuccessResponse();
}
/**
* Tool to auto-fill hierarchy cache.
*
* @return \Zend\Console\Response
*/
public function createhierarchytreesAction()
{
$solr = $this->getSearchManager()->setSearchClassId('Solr')->getResults();
$hierarchies = $solr->getFullFieldFacets(array('hierarchy_top_id'));
foreach ($hierarchies['hierarchy_top_id']['data']['list'] as $hierarchy) {
Console::writeLine("Building tree for {$hierarchy['value']}...");
$driver = $solr->getRecord($hierarchy['value']);
if ($driver->getHierarchyType()) {
// Only do this if the record is actually a hierarchy type record
$driver->getHierarchyDriver()->getTreeSource()
->getXML($hierarchy['value'], array('refresh' => true));
}
}
return $this->getSuccessResponse();
}
}
<?php
/**
* Create all the hierarchy files which are used for looking up hierarchichal trees.
* This script will search the Solr index and create the files needed so they don't
* need to be built at runtime. If this script is run after every index, the caching
* time for hierarchy trees can be set to -1 so that trees are always assumed to be
* up to date.
*
* -!!!!-This script is specifically for trees built for JSTree from Solr.-!!!!-
*
* PHP version 5
*
* Copyright (C) National Library of Ireland 2012.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Utilities
* @author Lutz Biedinger <lutz.biedinger@gmail.com>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki Wiki
*/
// Load the Zend framework -- this will automatically trigger the appropriate
// controller action based on directory and file names
define('CLI_DIR', __DIR__); // save directory name of current script
require_once __DIR__ . '/../public/index.php';
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment