Skip to content
Permalink
Browse files

Refactored recommendation handling to an event system.

  • Loading branch information...
demiankatz committed Jun 12, 2015
1 parent 9046787 commit d8e0011efb6e165131974fb9293db62bd4b00667
Showing with 749 additions and 463 deletions.
  1. +1 −0 module/VuFind/config/module.config.php
  2. +0 −1 module/VuFind/src/VuFind/Autocomplete/Solr.php
  3. +48 −39 module/VuFind/src/VuFind/Controller/AbstractSearch.php
  4. +0 −1 module/VuFind/src/VuFind/Controller/BrowseController.php
  5. +5 −9 module/VuFind/src/VuFind/Controller/CombinedController.php
  6. +18 −13 module/VuFind/src/VuFind/Controller/MyResearchController.php
  7. +0 −1 module/VuFind/src/VuFind/OAI/Server.php
  8. +3 −5 module/VuFind/src/VuFind/Recommend/Factory.php
  9. +17 −15 module/VuFind/src/VuFind/Recommend/SearchObject.php
  10. +34 −18 module/VuFind/src/VuFind/RecordTab/CollectionList.php
  11. +2 −2 module/VuFind/src/VuFind/RecordTab/Factory.php
  12. +66 −2 module/VuFind/src/VuFind/Search/Base/Options.php
  13. +0 −196 module/VuFind/src/VuFind/Search/Base/Params.php
  14. +24 −15 module/VuFind/src/VuFind/Search/Base/Results.php
  15. +23 −0 module/VuFind/src/VuFind/Search/Combined/Options.php
  16. +0 −24 module/VuFind/src/VuFind/Search/Combined/Params.php
  17. +15 −0 module/VuFind/src/VuFind/Search/Favorites/Options.php
  18. +0 −14 module/VuFind/src/VuFind/Search/Favorites/Params.php
  19. +16 −0 module/VuFind/src/VuFind/Search/MixedList/Options.php
  20. +0 −14 module/VuFind/src/VuFind/Search/MixedList/Params.php
  21. +194 −0 module/VuFind/src/VuFind/Search/RecommendListener.php
  22. +183 −0 module/VuFind/src/VuFind/Search/SearchRunner.php
  23. +33 −0 module/VuFind/src/VuFind/Search/SolrAuthor/Options.php
  24. +0 −32 module/VuFind/src/VuFind/Search/SolrAuthor/Params.php
  25. +16 −0 module/VuFind/src/VuFind/Search/SolrAuthorFacets/Options.php
  26. +0 −14 module/VuFind/src/VuFind/Search/SolrAuthorFacets/Params.php
  27. +19 −0 module/VuFind/src/VuFind/Search/SolrCollection/Options.php
  28. +2 −35 module/VuFind/src/VuFind/Search/SolrCollection/Params.php
  29. +16 −0 module/VuFind/src/VuFind/Search/Tags/Options.php
  30. +0 −13 module/VuFind/src/VuFind/Search/Tags/Params.php
  31. +14 −0 module/VuFind/src/VuFind/Service/Factory.php
@@ -172,6 +172,7 @@
'VuFind\SearchOptionsPluginManager' => 'VuFind\Service\Factory::getSearchOptionsPluginManager',
'VuFind\SearchParamsPluginManager' => 'VuFind\Service\Factory::getSearchParamsPluginManager',
'VuFind\SearchResultsPluginManager' => 'VuFind\Service\Factory::getSearchResultsPluginManager',
'VuFind\SearchRunner' => 'VuFind\Service\Factory::getSearchRunner',
'VuFind\SearchSpecsReader' => 'VuFind\Service\Factory::getSearchSpecsReader',
'VuFind\SearchStats' => 'VuFind\Service\Factory::getSearchStats',
'VuFind\SessionManager' => 'VuFind\Service\Factory::getSessionManager',
@@ -148,7 +148,6 @@ protected function initSearchObject()
// Build a new search object:
$this->searchObject = $this->resultsManager->get($this->searchClassId);
$this->searchObject->getOptions()->spellcheckEnabled(false);
$this->searchObject->getParams()->recommendationsEnabled(false);
}
/**
@@ -26,7 +26,7 @@
* @link http://www.vufind.org Main Page
*/
namespace VuFind\Controller;
use VuFind\Solr\Utils as SolrUtils;
use VuFind\Search\RecommendListener, VuFind\Solr\Utils as SolrUtils;
use Zend\Stdlib\Parameters;
/**
@@ -201,7 +201,7 @@ protected function rememberSearch($results)
protected function getActiveRecommendationSettings()
{
// Enable recommendations unless explicitly told to disable them:
$all = ['top', 'side', 'noresults'];
$all = ['top', 'side', 'noresults', 'bottom'];
$noRecommend = $this->params()->fromQuery('noRecommend', false);
if ($noRecommend === 1 || $noRecommend === '1'
|| $noRecommend === 'true' || $noRecommend === true
@@ -217,6 +217,35 @@ protected function getActiveRecommendationSettings()
);
}
/**
* Get a callback for setting up a search (or null if callback is unnecessary).
*
* @return mixed
*/
protected function getSearchSetupCallback()
{
// Setup callback to attach listener if appropriate:
$activeRecs = $this->getActiveRecommendationSettings();
if (empty($activeRecs)) {
return null;
}
$rManager = $this->getServiceLocator()->get('VuFind\RecommendPluginManager');
return function ($runner, $params, $searchId) use ($rManager, $activeRecs) {
$listener = new RecommendListener($rManager, $searchId);
$config = [];
$rawConfig = $params->getOptions()
->getRecommendationSettings($params->getSearchHandler());
foreach ($rawConfig as $key => $value) {
if (in_array($key, $activeRecs)) {
$config[$key] = $value;
}
}
$listener->setConfig($config);
$listener->attach($runner->getEventManager()->getSharedManager());
};
}
/**
* Send search results to results view
*
@@ -232,36 +261,30 @@ public function resultsAction()
return $this->redirectToSavedSearch($savedId);
}
$results = $this->getResultsManager()->get($this->searchClassId);
$params = $results->getParams();
$params->recommendationsEnabled($this->getActiveRecommendationSettings());
$runner = $this->getServiceLocator()->get('VuFind\SearchRunner');
// Send both GET and POST variables to search class:
$params->initFromRequest(
new Parameters(
$this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray()
)
);
// Make parameters available to the view:
$view->params = $params;
// Attempt to perform the search; if there is a problem, inspect any Solr
// exceptions to see if we should communicate to the user about them.
try {
// Explicitly execute search within controller -- this allows us to
// catch exceptions more reliably:
$results->performAndProcessSearch();
$request = $this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray();
$view->results = $results = $runner->run(
$request, $this->searchClassId, $this->getSearchSetupCallback()
);
$view->params = $results->getParams();
// If we received an EmptySet back, that indicates that the real search
// failed due to some kind of syntax error, and we should display a
// warning to the user; otherwise, we should proceed with normal post-search
// processing.
if ($results instanceof \VuFind\Search\EmptySet\Results) {
$view->parseError = true;
} else {
// If a "jumpto" parameter is set, deal with that now:
if ($jump = $this->processJumpTo($results)) {
return $jump;
}
// Send results to the view and remember the current URL as the last
// search.
$view->results = $results;
// Remember the current URL as the last search.
$this->rememberSearch($results);
// Add to search history:
@@ -282,22 +305,8 @@ public function resultsAction()
if ($this->resultScrollerActive()) {
$this->resultScroller()->init($results);
}
} catch (\VuFindSearch\Backend\Exception\BackendException $e) {
if ($e->hasTag('VuFind\Search\ParserError')) {
// If it's a parse error or the user specified an invalid field, we
// should display an appropriate message:
$view->parseError = true;
// We need to create and process an "empty results" object to
// ensure that recommendation modules and templates behave
// properly when displaying the error message.
$view->results = $this->getResultsManager()->get('EmptySet');
$view->results->setParams($params);
$view->results->performAndProcessSearch();
} else {
throw $e;
}
}
// Save statistics:
if ($this->logStatistics) {
$this->getServiceLocator()->get('VuFind\SearchStats')
@@ -598,7 +598,6 @@ protected function getFacetList($facet, $category = null,
$params->setOverrideQuery($query);
$params->getOptions()->disableHighlighting();
$params->getOptions()->spellcheckEnabled(false);
$params->recommendationsEnabled(false);
// Get limit from config
$params->setFacetLimit($this->config->Browse->result_limit);
$params->setLimit(0);
@@ -131,14 +131,10 @@ public function resultAction()
public function resultsAction()
{
// Set up current request context:
$results = $this->getResultsManager()->get('Combined');
$params = $results->getParams();
$params->recommendationsEnabled(true);
$params->initFromRequest(
new Parameters(
$this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray()
)
$request = $this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray();
$results = $this->getServiceLocator()->get('VuFind\SearchRunner')->run(
$request, 'Combined', $this->getSearchSetupCallback()
);
// Remember the current URL, then disable memory so multi-search results
@@ -203,7 +199,7 @@ public function resultsAction()
'columns' => $columns,
'combinedResults' => $combinedResults,
'config' => $config,
'params' => $params,
'params' => $results->getParams(),
'placement' => $placement,
'results' => $results,
'supportsCart' => $supportsCart,
@@ -31,7 +31,7 @@
VuFind\Exception\Mail as MailException,
VuFind\Exception\ListPermission as ListPermissionException,
VuFind\Exception\RecordMissing as RecordMissingException,
Zend\Stdlib\Parameters;
VuFind\Search\RecommendListener, Zend\Stdlib\Parameters;
/**
* Controller for the user account area.
@@ -641,23 +641,28 @@ public function mylistAction()
// If we got this far, we just need to display the favorites:
try {
$results = $this->getServiceLocator()
->get('VuFind\SearchResultsPluginManager')->get('Favorites');
$params = $results->getParams();
$runner = $this->getServiceLocator()->get('VuFind\SearchRunner');
// We want to merge together GET, POST and route parameters to
// initialize our search object:
$params->initFromRequest(
new Parameters(
$this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray()
+ ['id' => $this->params()->fromRoute('id')]
)
);
$request = $this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray()
+ ['id' => $this->params()->fromRoute('id')];
// Set up listener for recommendations:
$rManager = $this->getServiceLocator()
->get('VuFind\RecommendPluginManager');
$setupCallback = function ($runner, $params, $searchId) use ($rManager) {
$listener = new RecommendListener($rManager, $searchId);
$listener->setConfig(
$params->getOptions()->getRecommendationSettings()
);
$listener->attach($runner->getEventManager()->getSharedManager());
};
$results->performAndProcessSearch();
$results = $runner->run($request, 'Favorites', $setupCallback);
return $this->createViewModel(
['params' => $params, 'results' => $results]
['params' => $results->getParams(), 'results' => $results]
);
} catch (ListPermissionException $e) {
if (!$this->getUser()) {
@@ -717,7 +717,6 @@ protected function listRecordsGetNonDeleted($from, $until, $offset, $limit,
$params->setLimit($limit);
$params->getOptions()->disableHighlighting();
$params->getOptions()->spellcheckEnabled(false);
$params->recommendationsEnabled(false);
$params->setSort('last_indexed asc', true);
// Construct a range query based on last indexed time:
@@ -96,7 +96,7 @@ public static function getAuthorityRecommend(ServiceManager $sm)
public static function getCatalogResults(ServiceManager $sm)
{
return new CatalogResults(
$sm->getServiceLocator()->get('VuFind\SearchResultsPluginManager')
$sm->getServiceLocator()->get('VuFind\SearchRunner')
);
}
@@ -247,7 +247,7 @@ public static function getSummonDatabases(ServiceManager $sm)
public static function getSummonResults(ServiceManager $sm)
{
return new SummonResults(
$sm->getServiceLocator()->get('VuFind\SearchResultsPluginManager')
$sm->getServiceLocator()->get('VuFind\SearchRunner')
);
}
@@ -316,9 +316,7 @@ public static function getVisualFacets(ServiceManager $sm)
*/
public static function getWebResults(ServiceManager $sm)
{
return new WebResults(
$sm->getServiceLocator()->get('VuFind\SearchResultsPluginManager')
);
return new WebResults($sm->getServiceLocator()->get('VuFind\SearchRunner'));
}
/**
@@ -27,6 +27,7 @@
* @link http://vufind.org/wiki/vufind2:recommendation_modules Wiki
*/
namespace VuFind\Recommend;
use VuFind\Search\SearchRunner;
/**
* Abstract SearchObject Recommendations Module (needs to be extended to use
@@ -62,20 +63,20 @@ abstract class SearchObject implements RecommendInterface
protected $requestParam;
/**
* Results plugin manager
* Search runner
*
* @var \VuFind\Search\Results\PluginManager
* @var SearchRunner
*/
protected $resultsManager;
protected $runner;
/**
* Constructor
*
* @param \VuFind\Search\Results\PluginManager $results Results plugin manager
* @param SearchRunner $runner Search runner
*/
public function __construct(\VuFind\Search\Results\PluginManager $results)
public function __construct(SearchRunner $runner)
{
$this->resultsManager = $results;
$this->runner = $runner;
}
/**
@@ -123,17 +124,18 @@ public function init($params, $request)
$lookfor = $params->getQuery()->getAllTerms();
}
// Set up the parameters:
$this->results = $this->resultsManager->get($this->getSearchClassId());
$params = $this->results->getParams();
$params->setLimit($this->limit);
$params->setBasicSearch(
$lookfor,
$params->getOptions()->getHandlerForLabel($typeLabel)
);
// Set up the callback to initialize the parameters:
$limit = $this->limit;
$callback = function ($runner, $params) use ($lookfor, $limit, $typeLabel) {
$params->setLimit($limit);
$params->setBasicSearch(
$lookfor, $params->getOptions()->getHandlerForLabel($typeLabel)
);
};
// Perform the search:
$this->results->performAndProcessSearch();
$this->results
= $this->runner->run([], $this->getSearchClassId(), $callback);
}
/**

0 comments on commit d8e0011

Please sign in to comment.
You can’t perform that action at this time.