From 2b2252551f404453b336ad7df9e59d5154087720 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 8 Nov 2012 11:44:08 -0500
Subject: [PATCH] Added console tool to regenerate hierarchy tree cache.  Added
 more flexible cache lifetime settings to support new tool.

---
 config/vufind/HierarchyDefault.ini            |  2 +-
 config/vufind/HierarchyFlat.ini               |  2 +-
 .../Hierarchy/TreeDataSource/AbstractBase.php |  3 +-
 .../VuFind/Hierarchy/TreeDataSource/Solr.php  | 13 ++++---
 .../Hierarchy/TreeDataSource/XMLFile.php      |  3 +-
 .../VuFindConsole/Controller/AbstractBase.php | 10 +++++
 .../Controller/UtilController.php             | 21 ++++++++++
 util/createHierarchyTrees.php                 | 38 +++++++++++++++++++
 8 files changed, 82 insertions(+), 10 deletions(-)
 create mode 100644 util/createHierarchyTrees.php

diff --git a/config/vufind/HierarchyDefault.ini b/config/vufind/HierarchyDefault.ini
index 833131f7c55..8969aa60f3c 100644
--- a/config/vufind/HierarchyDefault.ini
+++ b/config/vufind/HierarchyDefault.ini
@@ -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
diff --git a/config/vufind/HierarchyFlat.ini b/config/vufind/HierarchyFlat.ini
index e7696bcc02a..1d1ece1d1dc 100644
--- a/config/vufind/HierarchyFlat.ini
+++ b/config/vufind/HierarchyFlat.ini
@@ -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
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/AbstractBase.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/AbstractBase.php
index f8861c76ae9..d1161d76573 100644
--- a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/AbstractBase.php
+++ b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/AbstractBase.php
@@ -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?
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php
index ce9bccc7bac..62552371d95 100644
--- a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php
+++ b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php
@@ -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);
diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/XMLFile.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/XMLFile.php
index 1d883899f47..9879919cc87 100644
--- a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/XMLFile.php
+++ b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/XMLFile.php
@@ -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));
     }
diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/AbstractBase.php b/module/VuFindConsole/src/VuFindConsole/Controller/AbstractBase.php
index 85d7b7a2d1f..875cb9366cf 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/AbstractBase.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/AbstractBase.php
@@ -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.
      *
diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
index ddee7c9adf4..2454c1fc3a2 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
@@ -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();
+    }
 }
diff --git a/util/createHierarchyTrees.php b/util/createHierarchyTrees.php
new file mode 100644
index 00000000000..01486fe67da
--- /dev/null
+++ b/util/createHierarchyTrees.php
@@ -0,0 +1,38 @@
+<?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';
-- 
GitLab