From abe750adbe7a2a68bccd443ab571c7a86ad43856 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Wed, 27 Feb 2013 07:55:49 +0100 Subject: [PATCH] Define and use factory for SolrReservers backend --- module/VuFind/src/VuFind/Bootstrapper.php | 3 +- .../Factory/AbstractSolrBackendFactory.php | 234 ++++++++++++++++++ .../Factory/SolrDefaultBackendFactory.php | 143 +---------- .../Factory/SolrReservesBackendFactory.php | 53 ++++ .../VuFind/src/VuFind/Search/Solr/Results.php | 14 +- .../VuFind/Search/SolrReserves/Results.php | 12 + 6 files changed, 322 insertions(+), 137 deletions(-) create mode 100644 module/VuFind/src/VuFind/Search/Factory/AbstractSolrBackendFactory.php create mode 100644 module/VuFind/src/VuFind/Search/Factory/SolrReservesBackendFactory.php diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php index afa19cdc192..21c082a10a8 100644 --- a/module/VuFind/src/VuFind/Bootstrapper.php +++ b/module/VuFind/src/VuFind/Bootstrapper.php @@ -371,7 +371,8 @@ class Bootstrapper /// Hardcoded for now $config = array( 'factories' => array( - 'Solr' => 'VuFind\Search\Factory\SolrDefaultBackendFactory' + 'Solr' => 'VuFind\Search\Factory\SolrDefaultBackendFactory', + 'SolrReserves' => 'VuFind\Search\Factory\SolrReservesBackendFactory', ) ); diff --git a/module/VuFind/src/VuFind/Search/Factory/AbstractSolrBackendFactory.php b/module/VuFind/src/VuFind/Search/Factory/AbstractSolrBackendFactory.php new file mode 100644 index 00000000000..f9a25e1190d --- /dev/null +++ b/module/VuFind/src/VuFind/Search/Factory/AbstractSolrBackendFactory.php @@ -0,0 +1,234 @@ +<?php + +/** + * Abstract factory for SOLR backends. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2013. + * + * 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 Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ + +namespace VuFind\Search\Factory; + +use VuFind\Config\Reader; + +use VuFindSearch\Backend\BackendInterface; +use VuFindSearch\Backend\Solr\QueryBuilder; +use VuFindSearch\Backend\Solr\Connector; +use VuFindSearch\Backend\Solr\Backend; + +use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\ServiceManager\FactoryInterface; + +use Zend\Config\Reader\Yaml as YamlReader; +use VuFind\Config\Reader\CacheDecorator as YamlCacheReader; + +/** + * Abstract factory for SOLR backends. + * + * @category VuFind2 + * @package Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +abstract class AbstractSolrBackendFactory implements FactoryInterface +{ + /** + * Logger. + * + * @var Zend\Log\LoggerInterface + */ + protected $logger; + + /** + * Superior service manager. + * + * @var ServiceLocatorInterface + */ + protected $serviceLocator; + + /** + * Search configuration file identifier. + * + * @var string + */ + protected $searchConfig; + + /** + * YAML searchspecs filename. + * + * @var string + */ + protected $searchYaml; + + /** + * The name of the service created by this factory. + * + * @var string + */ + protected $serviceName; + + /** + * Main VuFind configuration + * + * @var \Zend\Config\Config + */ + protected $config; + + /** + * Solr core name + * + * @var string + */ + protected $solrCore = ''; + + /** + * Constructor + */ + public function __construct() + { + $this->config = Reader::getConfig(); + } + + /** + * Create the backend. + * + * @param ServiceLocatorInterface $serviceLocator Superior service manager + * + * @return BackendInterface + */ + public function createService (ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + if ($this->serviceLocator->has('VuFind\Logger')) { + $this->logger = $this->serviceLocator->get('VuFind\Logger'); + } + $connector = $this->createConnector(); + $backend = $this->createBackend($this->serviceName, $connector); + $this->createListeners($backend); + return $backend; + } + + /** + * Create the SOLR backend. + * + * @param string $identifier Backend identifier + * @param Connector $connector Connector + * + * @return Backend + */ + protected function createBackend ($identifier, Connector $connector) + { + $backend = new Backend($identifier, $connector); + $specs = $this->loadSpecs(); + $builder = new QueryBuilder($specs); + $backend->setQueryBuilder($builder); + + if ($this->logger) { + $backend->setLogger($this->logger); + } + return $backend; + } + + /** + * Create listeners. + * + * @param Backend $backend Backend + * + * @return void + */ + protected function createListeners (Backend $backend) + { + } + + /** + * Create the SOLR connector. + * + * @return Connector + */ + protected function createConnector () + { + $searchSettings = Reader::getConfig($this->searchConfig); + + $url = $this->config->Index->url . '/' . $this->solrCore; + + $connector = new Connector($url); + $connector->setTimeout( + isset($this->config->Index->timeout) ? $this->config->Index->timeout : 30 + ); + + $hl = !isset($searchSettings->General->highlighting) ? false : $searchSettings->General->highlighting; + $sn = !isset($searchSettings->General->snippets) ? false : $searchSettings->General->snippets; + if ($hl || $sn) { + $connector->addQueryInvariant('hl', 'true'); + $connector->addQueryInvariant('hl.fl', '*'); + $connector->addQueryInvariant('hl.simple.pre', '{{{{START_HILITE}}}}'); + $connector->addQueryInvariant('hl.simple.post', '{{{{END_HILITE}}}}'); + } + + // Hidden filters + if (isset($searchSettings->HiddenFilters)) { + foreach ($searchSettings->HiddenFilters as $field => $value) { + $connector->addQueryInvariant('fq', sprintf('%s:"%s"', $field, $value)); + } + } + // Raw hidden filters + if (isset($searchSettings->RawHiddenFilters)) { + foreach ($searchSettings->RawHiddenFilters as $filter) { + $connector->addQueryInvariant('fq', $filter); + } + } + + if ($this->logger) { + $connector->setLogger($this->logger); + } + if ($this->serviceLocator->has('VuFind\Http')) { + $connector->setProxy($this->serviceLocator->get('VuFind\Http')); + } + return $connector; + } + + /** + * Load the search specs. + * + * @return array + */ + protected function loadSpecs () + { + $global = Reader::getBaseConfigPath($this->searchYaml); + $local = Reader::getLocalConfigPath($this->searchYaml); + if ($this->serviceLocator->has('VuFind\CacheManager')) { + $cache = $this->serviceLocator->get('VuFind\CacheManager')->getCache('searchspecs'); + $reader = new YamlCacheReader(new YamlReader('Symfony\Component\Yaml\Yaml::parse'), $cache); + } else { + $reader = new YamlReader(new YamlReader('Symfony\Component\Yaml\Yaml::parse')); + } + $specs = $reader->fromFile($global); + if ($local) { + foreach ($reader->fromFile($local) as $key => $value) { + $specs[$key] = $value; + } + } + return $specs; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Search/Factory/SolrDefaultBackendFactory.php b/module/VuFind/src/VuFind/Search/Factory/SolrDefaultBackendFactory.php index 3a6d318d106..aab61725ff7 100644 --- a/module/VuFind/src/VuFind/Search/Factory/SolrDefaultBackendFactory.php +++ b/module/VuFind/src/VuFind/Search/Factory/SolrDefaultBackendFactory.php @@ -26,24 +26,12 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org Main Site */ - namespace VuFind\Search\Factory; -use VuFind\Config\Reader; - -use VuFindSearch\Backend\BackendInterface; -use VuFindSearch\Backend\Solr\QueryBuilder; -use VuFindSearch\Backend\Solr\Connector; use VuFindSearch\Backend\Solr\Backend; use VuFind\Search\Listener\NormalizeSolrSort; -use Zend\ServiceManager\ServiceLocatorInterface; -use Zend\ServiceManager\FactoryInterface; - -use Zend\Config\Reader\Yaml as YamlReader; -use VuFind\Config\Reader\CacheDecorator as YamlCacheReader; - /** * Factory for the default SOLR backend. * @@ -53,60 +41,19 @@ use VuFind\Config\Reader\CacheDecorator as YamlCacheReader; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org Main Site */ -class SolrDefaultBackendFactory implements FactoryInterface +class SolrDefaultBackendFactory extends AbstractSolrBackendFactory { /** - * Logger. - * - * @var Zend\Log\LoggerInterface - */ - protected $logger; - - /** - * Superior service manager. - * - * @var ServiceLocatorInterface - */ - protected $serviceLocator; - - /** - * Create the backend. - * - * @param ServiceLocatorInterface $serviceLocator Superior service manager - * - * @return BackendInterface + * Constructor */ - public function createService (ServiceLocatorInterface $serviceLocator) + public function __construct() { - $this->serviceLocator = $serviceLocator; - if ($this->serviceLocator->has('VuFind\Logger')) { - $this->logger = $this->serviceLocator->get('VuFind\Logger'); - } - $connector = $this->createConnector(); - $backend = $this->createBackend('Solr', $connector); - $this->createListeners($backend); - return $backend; - } - - /** - * Create the SOLR backend. - * - * @param string $identifier Backend identifier - * @param Connector $connector Connector - * - * @return Backend - */ - protected function createBackend ($identifier, Connector $connector) - { - $backend = new Backend($identifier, $connector); - $specs = $this->loadSpecs(); - $builder = new QueryBuilder($specs); - $backend->setQueryBuilder($builder); - - if ($this->logger) { - $backend->setLogger($this->logger); - } - return $backend; + parent::__construct(); + $this->solrCore = isset($this->config->Index->default_core) + ? $this->config->Index->default_core : 'biblio'; + $this->searchConfig = 'searches'; + $this->searchYaml = 'searchspecs.yaml'; + $this->serviceName = 'Solr'; } /** @@ -118,80 +65,10 @@ class SolrDefaultBackendFactory implements FactoryInterface */ protected function createListeners (Backend $backend) { + parent::createListeners($backend); $events = $this->serviceLocator->get('SharedEventManager'); // Normalize sort directive $listener = new NormalizeSolrSort($backend); $listener->attach($events); } - - /** - * Create the SOLR connector. - * - * @return Connector - */ - protected function createConnector () - { - $config = Reader::getConfig(); - $searchSettings = Reader::getConfig('searches'); - - $url = $config->Index->url . '/'; - $url .= isset($config->Index->default_core) ? $config->Index->default_core : 'biblio'; - - $connector = new Connector($url); - $connector->setTimeout($config->Index->timeout); - - $hl = !isset($searchSettings->General->highlighting) ? false : $searchSettings->General->highlighting; - $sn = !isset($searchSettings->General->snippets) ? false : $searchSettings->General->snippets; - if ($hl || $sn) { - $connector->addQueryInvariant('hl', 'true'); - $connector->addQueryInvariant('hl.fl', '*'); - $connector->addQueryInvariant('hl.simple.pre', '{{{{START_HILITE}}}}'); - $connector->addQueryInvariant('hl.simple.post', '{{{{END_HILITE}}}}'); - } - - // Hidden filters - if (isset($searchSettings->HiddenFilters)) { - foreach ($searchSettings->HiddenFilters as $field => $value) { - $connector->addQueryInvariant('fq', sprintf('%s:"%s"', $field, $value)); - } - } - // Raw hidden filters - if (isset($searchSettings->RawHiddenFilters)) { - foreach ($searchSettings->RawHiddenFilters as $filter) { - $connector->addQueryInvariant('fq', $filter); - } - } - - if ($this->logger) { - $connector->setLogger($this->logger); - } - if ($this->serviceLocator->has('VuFind\Http')) { - $connector->setProxy($this->serviceLocator->get('VuFind\Http')); - } - return $connector; - } - - /** - * Load the search specs. - * - * @return array - */ - protected function loadSpecs () - { - $global = Reader::getBaseConfigPath('searchspecs.yaml'); - $local = Reader::getLocalConfigPath('searchspecs.yaml'); - if ($this->serviceLocator->has('VuFind\CacheManager')) { - $cache = $this->serviceLocator->get('VuFind\CacheManager')->getCache('searchspecs'); - $reader = new YamlCacheReader(new YamlReader('Symfony\Component\Yaml\Yaml::parse'), $cache); - } else { - $reader = new YamlReader(new YamlReader('Symfony\Component\Yaml\Yaml::parse')); - } - $specs = $reader->fromFile($global); - if ($local) { - foreach ($reader->fromFile($local) as $key => $value) { - $specs[$key] = $value; - } - } - return $specs; - } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Search/Factory/SolrReservesBackendFactory.php b/module/VuFind/src/VuFind/Search/Factory/SolrReservesBackendFactory.php new file mode 100644 index 00000000000..b183e6a7c3a --- /dev/null +++ b/module/VuFind/src/VuFind/Search/Factory/SolrReservesBackendFactory.php @@ -0,0 +1,53 @@ +<?php + +/** + * Factory for the reserves SOLR backend. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2013. + * + * 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 Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +namespace VuFind\Search\Factory; + +/** + * Factory for the reserves SOLR backend. + * + * @category VuFind2 + * @package Search + * @author David Maus <maus@hab.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org Main Site + */ +class SolrReservesBackendFactory extends AbstractSolrBackendFactory +{ + /** + * Constructor + */ + public function __construct() + { + parent::__construct(); + $this->solrCore = 'reserves'; + $this->searchConfig = 'reserves'; + $this->searchYaml = 'reservessearchspecs.yaml'; + $this->serviceName = 'SolrReserves'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Search/Solr/Results.php b/module/VuFind/src/VuFind/Search/Solr/Results.php index 4c4aef4367a..88eb7a0dd16 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Results.php +++ b/module/VuFind/src/VuFind/Search/Solr/Results.php @@ -59,6 +59,14 @@ class Results extends BaseResults */ protected $searchService; + /** + * Search backend identifiers. + * + * @var string + * @tag NEW SEARCH + */ + protected $backendId = 'Solr'; + /** * Return search service. * @@ -125,7 +133,7 @@ class Results extends BaseResults $limit = $this->getParams()->getLimit(); $offset = $this->getStartRecord() - 1; $params = $this->createBackendParameters($this->getParams()); - $collection = $this->getSearchService()->search('Solr', $query, $offset, $limit, $params); + $collection = $this->getSearchService()->search($this->backendId, $query, $offset, $limit, $params); $this->rawResponse = $collection->getRawResponse(); $this->resultTotal = $collection->getTotal(); @@ -500,7 +508,7 @@ class Results extends BaseResults */ public function getRecord($id) { - $collection = $this->getSearchService()->retrieve('Solr', $id); + $collection = $this->getSearchService()->retrieve($this->backendId, $id); if (count($collection) == 0) { throw new RecordMissingException( @@ -554,7 +562,7 @@ class Results extends BaseResults */ public function getSimilarRecords($id) { - $collection = $this->getSearchService()->similar('Solr', $id); + $collection = $this->getSearchService()->similar($this->backendId, $id); $rawResponse = $collection->getRawResponse(); $results = array(); diff --git a/module/VuFind/src/VuFind/Search/SolrReserves/Results.php b/module/VuFind/src/VuFind/Search/SolrReserves/Results.php index 1a11649d02a..83595740730 100644 --- a/module/VuFind/src/VuFind/Search/SolrReserves/Results.php +++ b/module/VuFind/src/VuFind/Search/SolrReserves/Results.php @@ -40,6 +40,18 @@ namespace VuFind\Search\SolrReserves; */ class Results extends \VuFind\Search\Solr\Results { + /** + * Constructor + * + * @param \VuFind\Search\Base\Params $params Object representing user search + * parameters. + */ + public function __construct(\VuFind\Search\Base\Params $params) + { + parent::__construct($params); + $this->backendId = 'SolrReserves'; + } + /** * Get a connection to the Solr index. * -- GitLab