From fc60d570f136789efd406efe098fcf47d0b5ea23 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 28 May 2013 15:52:02 -0400 Subject: [PATCH] Improved PostgreSQL compatibility for saved searches. Thanks to Michele Meloni. --- .../src/VuFind/Controller/AbstractSearch.php | 4 +-- .../Controller/Plugin/ResultScroller.php | 2 +- .../VuFind/Controller/SearchController.php | 2 +- module/VuFind/src/VuFind/Db/Row/Search.php | 29 +++++++++++++++++++ module/VuFind/src/VuFind/Db/Table/Search.php | 6 +--- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/module/VuFind/src/VuFind/Controller/AbstractSearch.php b/module/VuFind/src/VuFind/Controller/AbstractSearch.php index 6978ccfbf89..cd1fd8f1984 100644 --- a/module/VuFind/src/VuFind/Controller/AbstractSearch.php +++ b/module/VuFind/src/VuFind/Controller/AbstractSearch.php @@ -132,7 +132,7 @@ class AbstractSearch extends AbstractBase $userId = $user ? $user->id : false; if ($search->session_id == $sessId || $search->user_id == $userId) { // They do, deminify it to a new object. - $minSO = unserialize($search->search_object); + $minSO = $search->getSearchObject(); $savedSearch = $minSO->deminify($this->getResultsManager()); // Now redirect to the URL associated with the saved search; this @@ -322,7 +322,7 @@ class AbstractSearch extends AbstractBase } // Restore the full search object: - $minSO = unserialize($search->search_object); + $minSO = $search->getSearchObject(); $savedSearch = $minSO->deminify($this->getResultsManager()); // Fail if this is not the right type of search: diff --git a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php index 11154478237..c676b2c1aa8 100644 --- a/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php +++ b/module/VuFind/src/VuFind/Controller/Plugin/ResultScroller.php @@ -343,7 +343,7 @@ class ResultScroller extends AbstractPlugin $searchTable = $this->getController()->getTable('Search'); $row = $searchTable->getRowById($this->data->searchId, false); if (!empty($row)) { - $minSO = unserialize($row->search_object); + $minSO = $row->getSearchObject(); $manager = $this->getController()->getServiceLocator() ->get('VuFind\SearchResultsPluginManager'); $search = $minSO->deminify($manager); diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index ef7a1826242..5140a82dde7 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -257,7 +257,7 @@ class SearchController extends AbstractSearch // Loop through the history foreach ($searchHistory as $current) { - $minSO = unserialize($current->search_object); + $minSO = $current->getSearchObject(); // Saved searches if ($current->saved == 1) { diff --git a/module/VuFind/src/VuFind/Db/Row/Search.php b/module/VuFind/src/VuFind/Db/Row/Search.php index 7b203a0f318..0f1fcaaeba7 100644 --- a/module/VuFind/src/VuFind/Db/Row/Search.php +++ b/module/VuFind/src/VuFind/Db/Row/Search.php @@ -48,4 +48,33 @@ class Search extends RowGateway { parent::__construct('id', 'search', $adapter); } + + /** + * Get the search object from the row + * + * @return \VuFind\Search\Minified + */ + public function getSearchObject() + { + // Resource check for PostgreSQL compatibility: + $raw = is_resource($this->search_object) + ? stream_get_contents($this->search_object) : $this->search_object; + return unserialize($raw); + } + + /** + * Save + * + * @return int + */ + public function save() + { + // Note that if we have a resource, we need to grab the contents before + // saving -- this is necessary for PostgreSQL compatibility although MySQL + // returns a plain string + $this->search_object = is_resource($this->search_object) + ? stream_get_contents($this->search_object) + : $this->search_object; + parent::save(); + } } diff --git a/module/VuFind/src/VuFind/Db/Table/Search.php b/module/VuFind/src/VuFind/Db/Table/Search.php index 18c28ec3a1f..bf6cb46b4f0 100644 --- a/module/VuFind/src/VuFind/Db/Table/Search.php +++ b/module/VuFind/src/VuFind/Db/Table/Search.php @@ -157,11 +157,7 @@ class Search extends Gateway // Deminify the old search (note that if we have a resource, we need // to grab the contents -- this is necessary for PostgreSQL compatibility // although MySQL returns a plain string). - $minSO = unserialize( - is_resource($oldSearch->search_object) - ? stream_get_contents($oldSearch->search_object) - : $oldSearch->search_object - ); + $minSO = $oldSearch->getSearchObject(); $dupSearch = $minSO->deminify($manager); // See if the classes and urls match $oldUrl = $dupSearch->getUrlQuery()->getParams(); -- GitLab