Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move search history from the SearchController to an helper Service #1018

Merged
merged 11 commits into from Oct 2, 2017
1 change: 1 addition & 0 deletions module/VuFind/config/module.config.php
Expand Up @@ -220,6 +220,7 @@
'VuFind\ResolverDriverPluginManager' => 'VuFind\Service\Factory::getResolverDriverPluginManager',
'VuFind\Search' => 'VuFind\Service\Factory::getSearchService',
'VuFind\Search\BackendManager' => 'VuFind\Service\Factory::getSearchBackendManager',
'VuFind\Search\History' => 'VuFind\Service\Factory::getSearchHistory',
'VuFind\Search\Memory' => 'VuFind\Service\Factory::getSearchMemory',
'VuFind\SearchOptionsPluginManager' => 'VuFind\Service\Factory::getSearchOptionsPluginManager',
'VuFind\SearchParamsPluginManager' => 'VuFind\Service\Factory::getSearchParamsPluginManager',
Expand Down
39 changes: 5 additions & 34 deletions module/VuFind/src/VuFind/Controller/SearchController.php
Expand Up @@ -40,6 +40,7 @@
*/
class SearchController extends AbstractSearch
{

/**
* Handle an advanced search
*
Expand Down Expand Up @@ -237,41 +238,11 @@ public function historyAction()
return $this->forceLogin();
}

// Retrieve search history
$search = $this->getTable('Search');
$searchHistory = $search->getSearches(
$this->serviceLocator->get('VuFind\SessionManager')->getId(),
is_object($user) ? $user->id : null
);

// Build arrays of history entries
$saved = $unsaved = [];

// Loop through the history
foreach ($searchHistory as $current) {
$minSO = $current->getSearchObject();

// Saved searches
if ($current->saved == 1) {
$saved[] = $minSO->deminify($this->getResultsManager());
} else {
// All the others...

// If this was a purge request we don't need this
if ($this->params()->fromQuery('purge') == 'true') {
$current->delete();

// We don't want to remember the last search after a purge:
$this->getSearchMemory()->forgetSearch();
} else {
// Otherwise add to the list
$unsaved[] = $minSO->deminify($this->getResultsManager());
}
}
}

/** @var \VuFind\Search\History $searchHistoryHelper */
$searchHistoryHelper = $this->serviceLocator->get('VuFind\Search\History');
$lastSearches = $searchHistoryHelper->getSearchHistory(is_object($user) ? $user->id : null);
return $this->createViewModel(
['saved' => $saved, 'unsaved' => $unsaved]
$lastSearches
);
}

Expand Down
88 changes: 88 additions & 0 deletions module/VuFind/src/VuFind/Search/History.php
@@ -0,0 +1,88 @@
<?php

namespace VuFind\Search;

use minSO;

class History
{

/**
* @var \VuFind\Db\Table\Search
*/
protected $searchTable;

/**
* @var \Zend\Session\SessionManager
*/
protected $sessionManager;

/**
* @var \VuFind\Search\Results\PluginManager
*/
protected $resultsManager;

/**
* @var \VuFind\Search\Memory
*/
protected $searchMemory;

/**
* History constructor
* @param \VuFind\Db\Table\Search $searchTable
* @param \Zend\Session\SessionManager $sessionManager
* @param \VuFind\Search\Results\PluginManager $resultsManager
* @param \VuFind\Search\Memory $searchMemory
*/
public function __construct($searchTable, $sessionManager, $resultsManager, $searchMemory)
{
$this->searchTable = $searchTable;
$this->sessionManager = $sessionManager;
$this->resultsManager = $resultsManager;
$this->searchMemory = $searchMemory;
}

/**
* @param int $userId
* @return array
*/
public function getSearchHistory($userId = null, $purged = false)
{
// Retrieve search history
$searchHistory = $this->searchTable->getSearches(
$this->sessionManager->getId(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it may be overkill to inject the session manager in order to extract the session ID here. I would suggest either injecting the session ID through the constructor (since it's essentially a constant value in the context of a single script execution) or else make it a parameter to getSearchHistory (since it's part of the input required to figure out which history to retrieve).

$userId
);

// Build arrays of history entries
$saved = $unsaved = [];

// Loop through the history
/** @var \VuFind\Db\Row\Search $current */
foreach ($searchHistory as $current) {

/** @var minSO $minSO */
$minSO = $current->getSearchObject();

// Saved searches
if ($current->saved == 1) {
$saved[] = $minSO->deminify($this->resultsManager);
} else {
// All the others...

// If this was a purge request we don't need this
if ($purged) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purge search history functionality is broken by this refactoring. The $this->params()->fromQuery('purge') == 'true' used in the controller to check for the purge parameter is removed. I think this should instead be used to pass a value to this function, and perhaps the $purged variable should be renamed to something more clear.

It might also be more clean to separate the display and purge actions to separate functions -- that is, rather than purging as part of the retrieval of search history in the helper, instead have logic in the controller like:

if (we need to purge) {
    purge the unsaved history
    forget the search in the search memory
}
return the history

That would require a little bit of refactoring, but would probably also be more efficient -- causing a single DELETE query followed by a single SELECT query, rather than one SELECT followed by potentially many DELETEs.

It also has the benefit of decoupling the search memory and search history helper.

(Note that if you want to test this, you just need to click the "Purge unsaved searches" link on the search history page).

$current->delete();

// We don't want to remember the last search after a purge:
$this->searchMemory->forgetSearch();
} else {
// Otherwise add to the list
$unsaved[] = $minSO->deminify($this->resultsManager);
}
}
}

return ['saved' => $saved, 'unsaved' => $unsaved];
}
}
25 changes: 25 additions & 0 deletions module/VuFind/src/VuFind/Service/Factory.php
Expand Up @@ -689,6 +689,31 @@ public static function getSearchBackendManager(ServiceManager $sm)
return $manager;
}

/**
* Construct the search history helper.
*
* @param ServiceManager $sm Service manager.
*
* @return \VuFind\Search\History
*/
public static function getSearchHistory(ServiceManager $sm)
{
/** @var \VuFind\Db\Table\Search $searchTable */
$searchTable = $sm->get('VuFind\DbTablePluginManager')
->get("Search");

/** @var \VuFind\Search\Results\PluginManager $resultsManager */
$resultsManager = $sm->get('VuFind\SearchResultsPluginManager');

/** @var \VuFind\Search\Memory $searchMemory */
$searchMemory = $sm->get('VuFind\Search\Memory');

/** @var \Zend\Session\SessionManager $sessionManager */
$sessionManager = $sm->get('VuFind\SessionManager');

return new \VuFind\Search\History($searchTable, $sessionManager, $resultsManager, $searchMemory);
}

/**
* Construct the search memory helper.
*
Expand Down