From c4adcf7b24371e74bee6e73b3009e175aa21b036 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 5 Sep 2012 10:39:17 -0400
Subject: [PATCH] Incorporated Search Manager into deminification process.

---
 .../src/VuFind/Controller/AbstractSearch.php  |  7 +-
 .../Controller/Plugin/ResultScroller.php      |  4 +-
 .../VuFind/Controller/SearchController.php    |  4 +-
 module/VuFind/src/VuFind/Db/Table/Search.php  |  8 +-
 module/VuFind/src/VuFind/Search/Minified.php  | 97 ++++++++-----------
 5 files changed, 55 insertions(+), 65 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
index 0b743cee05a..96b7aef1f13 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php
@@ -124,7 +124,7 @@ class AbstractSearch extends AbstractBase
         if ($search->session_id == $sessId || $search->user_id == $userId) {
             // They do, deminify it to a new object.
             $minSO = unserialize($search->search_object);
-            $savedSearch = $minSO->deminify();
+            $savedSearch = $minSO->deminify($this->getSearchManager());
 
             // Now redirect to the URL associated with the saved search; this
             // simplifies problems caused by mixing different classes of search
@@ -201,7 +201,8 @@ class AbstractSearch extends AbstractBase
                 $sessId = $this->getServiceLocator()->get('SessionManager')->getId();
                 $history = new SearchTable();
                 $history->saveSearch(
-                    $results, $sessId, $history->getSearches(
+                    $this->getSearchManager(), $results, $sessId,
+                    $history->getSearches(
                         $sessId, isset($user->id) ? $user->id : null
                     )
                 );
@@ -304,7 +305,7 @@ class AbstractSearch extends AbstractBase
 
         // Restore the full search object:
         $minSO = unserialize($search->search_object);
-        $savedSearch = $minSO->deminify();
+        $savedSearch = $minSO->deminify($this->getSearchManager());
 
         // Fail if this is not the right type of search:
         if ($savedSearch->getParams()->getSearchType() != 'advanced') {
diff --git a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php
index 67ac9759e87..c1f8df0d409 100644
--- a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php
+++ b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php
@@ -333,7 +333,9 @@ class ResultScroller extends AbstractPlugin
             $row = $searchTable->getRowById($this->data->searchId, false);
             if (!empty($row)) {
                 $minSO = unserialize($row->search_object);
-                $search = $minSO->deminify();
+                $search = $minSO->deminify(
+                    $this->getController()->getServiceLocator()->get('SearchManager')
+                );
                 // The saved search does not remember its original limit;
                 // we should reapply it from the session data:
                 $search->getParams()->setLimit($this->data->limit);
diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php
index 2b64abfbfe3..3f231303bdf 100644
--- a/module/VuFind/src/VuFind/Controller/SearchController.php
+++ b/module/VuFind/src/VuFind/Controller/SearchController.php
@@ -251,7 +251,7 @@ class SearchController extends AbstractSearch
 
             // Saved searches
             if ($current->saved == 1) {
-                $saved[] = $minSO->deminify();
+                $saved[] = $minSO->deminify($this->getSearchManager());
             } else {
                 // All the others...
 
@@ -263,7 +263,7 @@ class SearchController extends AbstractSearch
                     Memory::forgetSearch();
                 } else {
                     // Otherwise add to the list
-                    $unsaved[] = $minSO->deminify();
+                    $unsaved[] = $minSO->deminify($this->getSearchManager());
                 }
             }
         }
diff --git a/module/VuFind/src/VuFind/Db/Table/Search.php b/module/VuFind/src/VuFind/Db/Table/Search.php
index 63a11b27b82..c32959d91ea 100644
--- a/module/VuFind/src/VuFind/Db/Table/Search.php
+++ b/module/VuFind/src/VuFind/Db/Table/Search.php
@@ -140,6 +140,7 @@ class Search extends Gateway
     /**
      * Add a search into the search table (history)
      *
+     * @param \VuFind\Search\Manager      $manager       Search manager
      * @param \VuFind\Search\Base\Results $newSearch     Search to save
      * @param string                      $sessionId     Current session ID
      * @param array                       $searchHistory Existing saved searches (for
@@ -147,8 +148,9 @@ class Search extends Gateway
      *
      * @return void
      */
-    public function saveSearch($newSearch, $sessionId, $searchHistory = array())
-    {
+    public function saveSearch($manager, $newSearch, $sessionId,
+        $searchHistory = array()
+    ) {
         // Duplicate elimination
         $dupSaved  = false;
         foreach ($searchHistory as $oldSearch) {
@@ -160,7 +162,7 @@ class Search extends Gateway
                 ? stream_get_contents($oldSearch->search_object)
                 : $oldSearch->search_object
             );
-            $dupSearch = $minSO->deminify();
+            $dupSearch = $minSO->deminify($manager);
             // See if the classes and urls match
             $oldUrl = $dupSearch->getUrlQuery()->getParams();
             $newUrl = $newSearch->getUrlQuery()->getParams();
diff --git a/module/VuFind/src/VuFind/Search/Minified.php b/module/VuFind/src/VuFind/Search/Minified.php
index 54feccf4505..a7743cc230e 100644
--- a/module/VuFind/src/VuFind/Search/Minified.php
+++ b/module/VuFind/src/VuFind/Search/Minified.php
@@ -28,27 +28,21 @@
 namespace VuFind\Search;
 
 /**
- * A minified search object used exclusively for trimming
- *  a search object down to it's barest minimum size
- *  before storage in a cookie or database.
+ * A minified search object used exclusively for trimming a search object down to its
+ * barest minimum size before storage in a cookie or database.
  *
- * It's still contains enough data granularity to
- *  programmatically recreate search urls.
+ * It still contains enough data granularity to programmatically recreate search
+ * URLs.
  *
- * This class isn't intended for general use, but simply
- *  a way of storing/retrieving data from a search object:
+ * This class isn't intended for general use, but simply a way of storing/retrieving
+ * data from a search object:
  *
  * eg. Store
  * $searchHistory[] = serialize($this->minify());
  *
  * eg. Retrieve
- * $searchObject  = SearchObjectFactory::initSearchObject();
- * $searchObject->deminify(unserialize($search));
- *
- * Note: codingStandardsIgnore settings within this class are used to suppress
- *       warnings related to the name not meeting PEAR standards; since there
- *       are serialized versions of this class stored in databases in the wild,
- *       it is too late to easily rename it for standards compliance.
+ * $searchObject = unserialize($search);
+ * $searchObject->deminify($manager);
  *
  * @category VuFind2
  * @package  Search
@@ -128,17 +122,20 @@ class Minified
     /**
      * Turn the current object into search results.
      *
+     * @param \VuFind\Search\Manager $manager Search manager
+     *
      * @return \VuFind\Search\Base\Results
      */
-    public function deminify()
+    public function deminify(\VuFind\Search\Manager $manager)
     {
         // Figure out the parameter and result classes based on the search class ID:
         $this->populateClassNames();
 
         // Deminify everything:
-        $params = new $this->pc();
+        $manager->setSearchClassId($this->cl);
+        $params = $manager->getParams();
         $params->deminify($this);
-        $results = new $this->rc($params);
+        $results = $manager->getResults($params);
         $results->deminify($this);
 
         return $results;
@@ -152,46 +149,34 @@ class Minified
      */
     protected function populateClassNames()
     {
-        // Simple case -- this is a recently-built object which has a search class ID
-        // populated:
-        if (isset($this->cl)) {
-            $this->pc = 'VuFind\Search\\' . $this->cl . '\Params';
-            $this->rc = 'VuFind\Search\\' . $this->cl . '\Results';
-            return;
-        }
-
-        // If we got this far, it's a legacy entry from VuFind 1.x.  We need to
-        // figure out the engine type for the object we're about to construct:
-        switch($this->ty) {
-        case 'Summon':
-        case 'SummonAdvanced':
-            $this->pc = 'VuFind\Search\Summon\Params';
-            $this->rc = 'VuFind\Search\Summon\Results';
-            $fixType = true;
-            break;
-        case 'WorldCat':
-        case 'WorldCatAdvanced':
-            $this->pc = 'VuFind\Search\WorldCat\Params';
-            $this->rc = 'VuFind\Search\WorldCat\Results';
-            $fixType = true;
-            break;
-        case 'Authority':
-        case 'AuthorityAdvanced':
-            $this->pc = 'VuFind\Search\SolrAuth\Params';
-            $this->rc = 'VuFind\Search\SolrAuth\Results';
-            $fixType = true;
-            break;
-        default:
-            $this->pc = 'VuFind\Search\Solr\Params';
-            $this->rc = 'VuFind\Search\Solr\Results';
-            $fixType = false;
-            break;
-        }
+        // If this is a legacy entry from VuFind 1.x, we need to figure out the
+        // search class ID for the object we're about to construct:
+        if (!isset($this->cl)) {
+            $fixType = true;    // by default, assume we need to fix type
+            switch($this->ty) {
+            case 'Summon':
+            case 'SummonAdvanced':
+                $this->cl = 'Summon';
+                break;
+            case 'WorldCat':
+            case 'WorldCatAdvanced':
+                $this->cl = 'WorldCat';
+                break;
+            case 'Authority':
+            case 'AuthorityAdvanced':
+                $this->cl = 'SolrAuth';
+                break;
+            default:
+                $this->cl = 'Solr';
+                $fixType = false;
+                break;
+            }
 
-        // Now rewrite the type if necessary (only needed for legacy objects):
-        if ($fixType) {
-            $this->ty = (substr($this->ty, -8) == 'Advanced')
-                ? 'advanced' : 'basic';
+            // Now rewrite the type if necessary (only needed for legacy objects):
+            if ($fixType) {
+                $this->ty = (substr($this->ty, -8) == 'Advanced')
+                    ? 'advanced' : 'basic';
+            }
         }
     }
 }
-- 
GitLab