diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 7b6bd58ca79f15e6c74c6f81eb174fb4cb46b451..5455e0ddf042495423cfa4991d627dab931dd8e5 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -76,6 +76,28 @@ $config = array( 'sip' => 'Sip2', ), ), + 'autocomplete_handler_manager' => array( + 'abstract_factories' => array('VuFind\Autocomplete\PluginFactory'), + 'invokables' => array( + 'none' => 'VuFind\Autocomplete\None', + 'oclcidentities' => 'VuFind\Autocomplete\OCLCIdentities', + 'solr' => 'VuFind\Autocomplete\Solr', + 'solrauth' => 'VuFind\Autocomplete\SolrAuth', + 'solrcn' => 'VuFind\Autocomplete\SolrCN', + 'solrreserves' => 'VuFind\Autocomplete\SolrReserves', + 'tag' => 'VuFind\Autocomplete\Tag', + ), + 'aliases' => array( + // for legacy 1.x compatibility + 'noautocomplete' => 'None', + 'oclcidentitiesautocomplete' => 'OCLCIdentities', + 'solrautocomplete' => 'Solr', + 'solrauthautocomplete' => 'SolrAuth', + 'solrcnautocomplete' => 'SolrCN', + 'solrreservesautocomplete' => 'SolrReserves', + 'tagautocomplete' => 'Tag', + ), + ), 'controllers' => array( 'invokables' => array( 'admin' => 'VuFind\Controller\AdminController', diff --git a/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php b/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php index d5069abfd94b503f6bceca54dd64814eefd11d80..96b18a39464439a1483ee84bee6b7964616e1ea3 100644 --- a/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php +++ b/module/VuFind/src/VuFind/Autocomplete/AutocompleteInterface.php @@ -53,4 +53,16 @@ interface AutocompleteInterface * @return array The suggestions for the provided query */ public function getSuggestions($query); + + /** + * setConfig + * + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. + * + * @param string $params Parameters to set + * + * @return void + */ + public function setConfig($params); } diff --git a/module/VuFind/src/VuFind/Autocomplete/None.php b/module/VuFind/src/VuFind/Autocomplete/None.php index 485df18eb887b2a6b8c458f6e371f78edbb48253..dbee88328fd0438c5f965d0e010055b1b69f8342 100644 --- a/module/VuFind/src/VuFind/Autocomplete/None.php +++ b/module/VuFind/src/VuFind/Autocomplete/None.php @@ -41,18 +41,6 @@ namespace VuFind\Autocomplete; */ class None implements AutocompleteInterface { - /** - * Constructor - * - * Establishes base settings for making autocomplete suggestions. - * - * @param string $params Additional settings from searches.ini. - */ - public function __construct($params) - { - // No parameters - } - /** * getSuggestions * @@ -68,4 +56,19 @@ class None implements AutocompleteInterface // No suggestions return array(); } + + /** + * setConfig + * + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. + * + * @param string $params Parameters to set + * + * @return void + */ + public function setConfig($params) + { + // Ignore all parameters + } } diff --git a/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php b/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php index 13c5e8ea634d7c2186d1a63a45cc8a04860e4897..74eff8e736d61413db7de0ad548ec7a4c922c81a 100644 --- a/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php +++ b/module/VuFind/src/VuFind/Autocomplete/OCLCIdentities.php @@ -41,20 +41,7 @@ namespace VuFind\Autocomplete; */ class OCLCIdentities implements AutocompleteInterface { - protected $url; - - /** - * Constructor - * - * Establishes base settings for making autocomplete suggestions. - * - * @param string $params Additional settings from searches.ini. - */ - public function __construct($params) - { - // For now, incoming parameters are ignored and a hard-coded URL is used: - $this->url = 'http://worldcat.org/identities/AutoSuggest'; - } + protected $url = 'http://worldcat.org/identities/AutoSuggest'; /** * getSuggestions @@ -89,4 +76,19 @@ class OCLCIdentities implements AutocompleteInterface // Send back results: return array_unique($results); } + + /** + * setConfig + * + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. + * + * @param string $params Parameters to set + * + * @return void + */ + public function setConfig($params) + { + // For now, incoming parameters are ignored. + } } diff --git a/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php b/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..cb26dcfea9505f08594910fd6a39e679c34e2c10 --- /dev/null +++ b/module/VuFind/src/VuFind/Autocomplete/PluginFactory.php @@ -0,0 +1,48 @@ +<?php +/** + * Autocomplete handler plugin factory + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * 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 Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +namespace VuFind\Autocomplete; + +/** + * Autocomplete handler plugin factory + * + * @category VuFind2 + * @package Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory +{ + /** + * Constructor + */ + public function __construct() + { + $this->defaultNamespace = 'VuFind\Autocomplete'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Autocomplete/Factory.php b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php similarity index 65% rename from module/VuFind/src/VuFind/Autocomplete/Factory.php rename to module/VuFind/src/VuFind/Autocomplete/PluginManager.php index 13c8034e29d236733e79b2d9b4d18dd36db85115..dff684fc82fe81dbbb75070f1ee5605a6d591441 100644 --- a/module/VuFind/src/VuFind/Autocomplete/Factory.php +++ b/module/VuFind/src/VuFind/Autocomplete/PluginManager.php @@ -1,6 +1,6 @@ <?php /** - * Code for generating Autocomplete objects + * Autocomplete handler plugin manager * * PHP version 5 * @@ -20,60 +20,34 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * @category VuFind2 - * @package Autocomplete + * @package Session_Handlers * @author Demian Katz <demian.katz@villanova.edu> - * @author Chris Hallberg <challber@villanova.edu> * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link http://vufind.org/wiki/autocomplete Wiki + * @link http://vufind.org/wiki/creating_a_session_handler Wiki */ namespace VuFind\Autocomplete; use VuFind\Config\Reader as ConfigReader, VuFind\Search\Options as SearchOptions; /** - * Code for generating Autocomplete objects - * - * This is a factory class to build autocomplete modules for use in searches. + * Autocomplete handler plugin manager * * @category VuFind2 - * @package Autocomplete + * @package Session_Handlers * @author Demian Katz <demian.katz@villanova.edu> * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link http://vufind.org/wiki/autocomplete Wiki + * @link http://vufind.org/wiki/creating_a_session_handler Wiki */ -class Factory +class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager { /** - * initRecommendation - * - * This constructs an autocomplete plug-in object. - * - * @param string $module The name of the autocomplete module to build - * @param string $params Configuration string to send to the constructor + * Return the name of the base class or interface that plug-ins must conform + * to. * - * @return mixed The $module object on success, false otherwise + * @return string */ - public static function initAutocomplete($module, $params) + protected function getExpectedInterface() { - // backward compatibility with VuFind 1.x names: - switch ($module) { - case 'NoAutocomplete': - $module = 'None'; - break; - default: - $module = str_replace('Autocomplete', '', $module); - break; - } - - // Try to load the appropriate class, if any: - if (!empty($module)) { - $module = 'VuFind\Autocomplete\\' . $module; - if (class_exists($module)) { - $auto = new $module($params); - return $auto; - } - } - - return false; + return 'VuFind\Autocomplete\AutocompleteInterface'; } /** @@ -91,9 +65,8 @@ class Factory * * @return array */ - public static function getSuggestions($request, $typeParam = 'type', - $queryParam = 'q' - ) { + public function getSuggestions($request, $typeParam = 'type', $queryParam = 'q') + { // Process incoming parameters: $type = $request->get($typeParam, ''); $query = $request->get($queryParam, ''); @@ -120,10 +93,11 @@ class Factory $module .= ':'; // force colon to avoid warning in explode below } list($name, $params) = explode(':', $module, 2); - $handler = self::initAutocomplete($name, $params); + $handler = $this->get($name); + $handler->setConfig($params); } return (isset($handler) && is_object($handler)) ? array_values($handler->getSuggestions($query)) : array(); } -} +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Autocomplete/Solr.php b/module/VuFind/src/VuFind/Autocomplete/Solr.php index 8464fc4ed4ce3e0410d867f332611cf7d23be620..cc989178fbf8e83ce6a3d81f5ae7b6290345f5eb 100644 --- a/module/VuFind/src/VuFind/Autocomplete/Solr.php +++ b/module/VuFind/src/VuFind/Autocomplete/Solr.php @@ -48,14 +48,18 @@ class Solr implements AutocompleteInterface protected $filters; protected $searchObject; + /** - * Constructor + * setConfig + * + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. * - * Establishes base settings for making autocomplete suggestions. + * @param string $params Parameters to set * - * @param string $params Additional settings from searches.ini. + * @return void */ - public function __construct($params) + public function setConfig($params) { // Save the basic parameters: $params = explode(':', $params); @@ -125,6 +129,10 @@ class Solr implements AutocompleteInterface */ public function getSuggestions($query) { + if (!is_object($this->searchObject)) { + throw new \Exception('Please set configuration first.'); + } + try { $this->searchObject->getParams()->setBasicSearch( $this->mungeQuery($query), $this->handler diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php b/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php index afb6f75b00c8643e255c39a6a96928462b561225..daa42eb352b79cb1818aa790d53afe73463a110f 100644 --- a/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php +++ b/module/VuFind/src/VuFind/Autocomplete/SolrAuth.php @@ -43,16 +43,10 @@ class SolrAuth extends Solr { /** * Constructor - * - * Establishes base settings for making autocomplete suggestions. - * - * @param string $params Additional settings from searches.ini. */ - public function __construct($params) + public function __construct() { - // Use a different default field; otherwise, behave the same as the parent: $this->defaultDisplayField = 'heading'; - parent::__construct($params); } /** diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrCN.php b/module/VuFind/src/VuFind/Autocomplete/SolrCN.php index 0ec54cf6869a6e5fe301291c0048151af72daa40..aa8c3de625a88df51b389f5c3f40d24e5d24e740 100644 --- a/module/VuFind/src/VuFind/Autocomplete/SolrCN.php +++ b/module/VuFind/src/VuFind/Autocomplete/SolrCN.php @@ -42,15 +42,19 @@ namespace VuFind\Autocomplete; class SolrCN extends Solr { /** - * Constructor + * setConfig * - * Establishes base settings for making autocomplete suggestions. + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. * - * @param string $params Additional settings from searches.ini. + * @param string $params Parameters to set + * + * @return void */ - public function __construct($params) + public function setConfig($params) { - parent::__construct('CallNumber'); + // Ignore incoming configuration and force CallNumber settings. + parent::setConfig('CallNumber'); } /** diff --git a/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php b/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php index caf15fdff8c9f1d61e39e7964918c15b9a7b5833..e6a91ec5a2cc6d715b353a3f29bc7ef01bf6c920 100644 --- a/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php +++ b/module/VuFind/src/VuFind/Autocomplete/SolrReserves.php @@ -43,17 +43,10 @@ class SolrReserves extends Solr { /** * Constructor - * - * Establishes base settings for making autocomplete suggestions. - * - * @param string $params Additional settings from searches.ini. */ - public function __construct($params) + public function __construct() { - // Use a different default field; otherwise, behave the same as the parent: $this->defaultDisplayField = 'course'; - - parent::__construct($params); } /** diff --git a/module/VuFind/src/VuFind/Autocomplete/Tag.php b/module/VuFind/src/VuFind/Autocomplete/Tag.php index 9fcac8f7094edb0ebff206ddd85252adb12b13d9..5debdda29302ba8afbd7939341d462391d822d61 100644 --- a/module/VuFind/src/VuFind/Autocomplete/Tag.php +++ b/module/VuFind/src/VuFind/Autocomplete/Tag.php @@ -41,18 +41,6 @@ use VuFind\Db\Table\Tags as TagsTable; */ class Tag implements AutocompleteInterface { - /** - * Constructor - * - * Establishes base settings for making autocomplete suggestions. - * - * @param string $params Additional settings from searches.ini. - */ - public function __construct($params) - { - // No parameters - } - /** * getSuggestions * @@ -75,4 +63,19 @@ class Tag implements AutocompleteInterface } return $tagList; } + + /** + * setConfig + * + * Set parameters that affect the behavior of the autocomplete handler. + * These values normally come from the search configuration file. + * + * @param string $params Parameters to set + * + * @return void + */ + public function setConfig($params) + { + // Ignore all parameters + } } diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index 1119988764905a7891ce79fde3e2abdf0cb8d1aa..030688273f5a9c76b3f303f8fc7b979abb80b51d 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -93,6 +93,11 @@ class Bootstrap new ServiceManagerConfig($config['auth_handler_manager']) ) ); + $serviceManager->setService( + 'AutocompleteHandlerManager', new \VuFind\Autocomplete\PluginManager( + new ServiceManagerConfig($config['autocomplete_handler_manager']) + ) + ); $serviceManager->setService( 'SessionHandlerManager', new \VuFind\Session\PluginManager( new ServiceManagerConfig($config['session_handler_manager']) diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php index 47bd55461934a7a9c3ce96eb8608ace4d986ac3a..7fe6223f5f885d317e0274732abbc4c21fcdb3a5 100644 --- a/module/VuFind/src/VuFind/Controller/AjaxController.php +++ b/module/VuFind/src/VuFind/Controller/AjaxController.php @@ -917,8 +917,10 @@ class AjaxController extends AbstractBase public function getACSuggestions() { $query = $this->getRequest()->getQuery(); + $autocompleteManager = $this->getServiceLocator() + ->get('AutocompleteHandlerManager'); return $this->output( - \VuFind\Autocomplete\Factory::getSuggestions($query), self::STATUS_OK + $autocompleteManager->getSuggestions($query), self::STATUS_OK ); } diff --git a/module/VuFind/src/VuFind/Controller/SearchController.php b/module/VuFind/src/VuFind/Controller/SearchController.php index 7139ae03b7c32b4e743c48e154f8eb9cc4650181..2b64abfbfe34502b70a86f5e4babbe7f885bdcd2 100644 --- a/module/VuFind/src/VuFind/Controller/SearchController.php +++ b/module/VuFind/src/VuFind/Controller/SearchController.php @@ -559,7 +559,9 @@ class SearchController extends AbstractSearch // Get suggestions and make sure they are an array (we don't want to JSON // encode them into an object): - $suggestions = \VuFind\Autocomplete\Factory::getSuggestions( + $autocompleteManager = $this->getServiceLocator() + ->get('AutocompleteHandlerManager'); + $suggestions = $autocompleteManager->getSuggestions( $query, 'type', 'lookfor' );