diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 86353a6fc170868ba6ad041b31c71fee588e51b7..a821d5813ce7e5a2cd26a28f2bd26851a8761d92 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -200,6 +200,15 @@ $config = array( 'worldcat' => 'VuFind\RecordDriver\WorldCat', ), ), + 'related_plugin_manager' => array( + 'abstract_factories' => array('VuFind\Related\PluginFactory'), + 'invokables' => array( + 'editions' => 'VuFind\Related\Editions', + 'similar' => 'VuFind\Related\Similar', + 'worldcateditions' => 'VuFind\Related\WorldCatEditions', + 'worldcatsimilar' => 'VuFind\Related\WorldCatSimilar', + ), + ), 'search_manager' => array( 'default_namespace' => 'VuFind\Search', 'namespaces_by_id' => array( diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index fa72423dbe5ceb79e23f99f43b9ec3cb66809a72..e85e547b85bb3aaebbdc54aadefe06601e507144 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -92,7 +92,7 @@ class Bootstrap // Use naming conventions to set up a bunch of services based on namespace: $namespaces = array( 'Auth', 'Autocomplete', 'ILS\Driver', 'Recommend', 'RecordDriver', - 'Session' + 'Related', 'Session' ); foreach ($namespaces as $ns) { $serviceName = str_replace('\\', '', $ns) . 'PluginManager'; diff --git a/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php b/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php index 87e2b9bc34b52dd57f98ee6587d326a913e4cd5c..b9b716a46ce09ca20b96bc8c15b9ccab8a904c5e 100644 --- a/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php +++ b/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php @@ -34,7 +34,9 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Db\Table\UserResource as UserResourceTable, VuFind\Exception\LoginRequired as LoginRequiredException, VuFind\Tags, VuFind\Translator\Translator, - VuFind\XSLT\Import\VuFind as ArticleStripper; + VuFind\XSLT\Import\VuFind as ArticleStripper, + Zend\ServiceManager\ServiceLocatorInterface, + Zend\ServiceManager\ServiceLocatorAwareInterface; /** * Abstract base record model. @@ -47,7 +49,7 @@ use VuFind\Config\Reader as ConfigReader, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://www.vufind.org Main Page */ -abstract class AbstractBase +abstract class AbstractBase implements ServiceLocatorAwareInterface { protected $resourceSource = 'VuFind'; // Used for identifying database records protected $extraDetails = array(); // For storing extra data with record @@ -274,11 +276,14 @@ abstract class AbstractBase $parts = explode(':', $current); $type = $parts[0]; $params = isset($parts[1]) ? $parts[1] : null; - $class = 'VuFind\Related\\' . $type; - if (@class_exists($class)) { - $retVal[] = new $class($params, $this); + $factory = $this->getServiceLocator()->getServiceLocator() + ->get('RelatedPluginManager'); + if ($factory->has($type)) { + $plugin = clone($factory->get($type)); + $plugin->init($params, $this); + $retVal[] = $plugin; } else { - throw new \Exception("Class {$class} does not exist."); + throw new \Exception("Related module {$type} does not exist."); } } return $retVal; @@ -427,4 +432,27 @@ abstract class AbstractBase ? call_user_func_array(array($this, $method), $params) : null; } + + /** + * Set the service locator. + * + * @param ServiceLocatorInterface $serviceLocator Locator to register + * + * @return AbstractBase + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + return $this; + } + + /** + * Get the service locator. + * + * @return \Zend\ServiceManager\ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } } diff --git a/module/VuFind/src/VuFind/Related/AbstractServiceLocator.php b/module/VuFind/src/VuFind/Related/AbstractServiceLocator.php new file mode 100644 index 0000000000000000000000000000000000000000..84894716e4e5bd905da1d4f9483db2006a5d2091 --- /dev/null +++ b/module/VuFind/src/VuFind/Related/AbstractServiceLocator.php @@ -0,0 +1,80 @@ +<?php +/** + * Base class for helpers that pull resources from the service locator. + * + * 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 Related_Records + * @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/building_a_recommendations_module Wiki + */ +namespace VuFind\Related; +use Zend\ServiceManager\ServiceLocatorInterface, + Zend\ServiceManager\ServiceLocatorAwareInterface; + +/** + * Base class for helpers that pull resources from the service locator. + * + * @category VuFind2 + * @package View_Helpers + * @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/building_a_recommendations_module Wiki + */ +abstract class AbstractServiceLocator implements RelatedInterface, + ServiceLocatorAwareInterface +{ + protected $serviceLocator; + + /** + * Get the search manager. + * + * @return \VuFind\Search\Manager + */ + public function getSearchManager() + { + return $this->getServiceLocator()->get('SearchManager'); + } + + /** + * Set the service locator. + * + * @param ServiceLocatorInterface $serviceLocator Locator to register + * + * @return AbstractServiceLocator + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + // The service locator passed in here is a VuFind\Related\PluginManager; + // we want to pull out the main Zend\ServiceManager\ServiceManager. + $this->serviceLocator = $serviceLocator->getServiceLocator(); + return $this; + } + + /** + * Get the service locator. + * + * @return \Zend\ServiceManager\ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Related/Editions.php b/module/VuFind/src/VuFind/Related/Editions.php index 79a3bf4bb0a0e4619cf5b406d12be7b02cf4a657..f0fd16915f6f5b8aeacd7dff05295933bb63c168 100644 --- a/module/VuFind/src/VuFind/Related/Editions.php +++ b/module/VuFind/src/VuFind/Related/Editions.php @@ -26,8 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Related; -use VuFind\Connection\WorldCatUtils, VuFind\Search\Solr\Params, - VuFind\Search\Solr\Results; +use VuFind\Connection\WorldCatUtils; /** * Related Records: WorldCat-based editions list (Solr results) @@ -38,25 +37,28 @@ use VuFind\Connection\WorldCatUtils, VuFind\Search\Solr\Params, * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class Editions implements RelatedInterface +class Editions extends AbstractServiceLocator { protected $results = array(); /** - * Constructor + * init * * Establishes base settings for making recommendations. * * @param string $settings Settings from config.ini * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object + * + * @return void */ - public function __construct($settings, $driver) + public function init($settings, $driver) { // If we have query parts, we should try to find related records: $parts = $this->getQueryParts($driver); if (!empty($parts)) { // Limit the number of parts based on the boolean clause limit: - $params = new Params(); + $sm = $this->getSearchManager(); + $params = $sm->setSearchClassId('Solr')->getParams(); $limit = $params->getQueryIDLimit(); if (count($parts) > $limit) { $parts = array_slice($parts, 0, $limit); @@ -73,7 +75,7 @@ class Editions implements RelatedInterface // Perform the search and return either results or an error: $params->setLimit(5); $params->setOverrideQuery($query); - $result = new Results($params); + $result = $sm->setSearchClassId('Solr')->getResults($params); $this->results = $result->getResults(); } } diff --git a/module/VuFind/src/VuFind/Related/PluginFactory.php b/module/VuFind/src/VuFind/Related/PluginFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..59ae94f344164d6381af4b9fa24179c7b5cb9a85 --- /dev/null +++ b/module/VuFind/src/VuFind/Related/PluginFactory.php @@ -0,0 +1,48 @@ +<?php +/** + * Related record 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 Related_Records + * @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\Related; + +/** + * Related record plugin factory + * + * @category VuFind2 + * @package Related_Records + * @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\Related'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Related/PluginManager.php b/module/VuFind/src/VuFind/Related/PluginManager.php new file mode 100644 index 0000000000000000000000000000000000000000..f1170b2792e0cc6b4c092409a903694b494dde72 --- /dev/null +++ b/module/VuFind/src/VuFind/Related/PluginManager.php @@ -0,0 +1,51 @@ +<?php +/** + * Related record plugin manager + * + * 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 Related_Records + * @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\Related; + +/** + * Related record plugin manager + * + * @category VuFind2 + * @package Related_Records + * @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 PluginManager extends \VuFind\ServiceManager\AbstractPluginManager +{ + /** + * Return the name of the base class or interface that plug-ins must conform + * to. + * + * @return string + */ + protected function getExpectedInterface() + { + return 'VuFind\Related\RelatedInterface'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Related/RelatedInterface.php b/module/VuFind/src/VuFind/Related/RelatedInterface.php index 95a586b9a76d4ab73df2d456b52ef4fbd7834a31..fbea92e06dce7fbf462f95ccb970c6bcbc60aef6 100644 --- a/module/VuFind/src/VuFind/Related/RelatedInterface.php +++ b/module/VuFind/src/VuFind/Related/RelatedInterface.php @@ -48,7 +48,15 @@ namespace VuFind\Related; */ interface RelatedInterface { - // This is just a marker interface for now. VuFind expects constructors for - // Related Record Modules to accept a settings string and a record driver, but - // it is bad form to define a constructor in an interface. + /** + * init + * + * Establishes base settings for making recommendations. + * + * @param string $settings Settings from config.ini + * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object + * + * @return void + */ + public function init($settings, $driver); } diff --git a/module/VuFind/src/VuFind/Related/Similar.php b/module/VuFind/src/VuFind/Related/Similar.php index 98fea61096ab79775912763498d80ff7d1643b69..f0d7d10cb27e6b564a2828dd846b3f0965dc7697 100644 --- a/module/VuFind/src/VuFind/Related/Similar.php +++ b/module/VuFind/src/VuFind/Related/Similar.php @@ -26,7 +26,6 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Related; -use VuFind\Search\Solr\Params, VuFind\Search\Solr\Results; /** * Related Records: Solr-based similarity @@ -37,22 +36,25 @@ use VuFind\Search\Solr\Params, VuFind\Search\Solr\Results; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ -class Similar implements RelatedInterface +class Similar extends AbstractServiceLocator { protected $results; /** - * Constructor + * init * * Establishes base settings for making recommendations. * * @param string $settings Settings from config.ini * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object + * + * @return void */ - public function __construct($settings, $driver) + public function init($settings, $driver) { - $params = new Params(); - $searcher = new Results($params); + $sm = $this->getSearchManager(); + $params = $sm->setSearchClassId('Solr')->getParams(); + $searcher = $sm->setSearchClassId('Solr')->getResults($params); $this->results = $searcher->getSimilarRecords($driver->getUniqueId()); } diff --git a/module/VuFind/src/VuFind/Related/WorldCatEditions.php b/module/VuFind/src/VuFind/Related/WorldCatEditions.php index ce55cfe86de53d82b9019eeacee1d82f5f90b5ca..68d90bf4591b0bcbe2d376a978e3e7c6a3888e1f 100644 --- a/module/VuFind/src/VuFind/Related/WorldCatEditions.php +++ b/module/VuFind/src/VuFind/Related/WorldCatEditions.php @@ -26,8 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Related; -use VuFind\Connection\WorldCatUtils, VuFind\Search\WorldCat\Params, - VuFind\Search\WorldCat\Results; +use VuFind\Connection\WorldCatUtils; /** * Related Records: WorldCat-based editions list (WorldCat results) @@ -41,14 +40,16 @@ use VuFind\Connection\WorldCatUtils, VuFind\Search\WorldCat\Params, class WorldCatEditions extends Editions { /** - * Constructor + * init * * Establishes base settings for making recommendations. * * @param string $settings Settings from config.ini * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object + * + * @return void */ - public function __construct($settings, $driver) + public function init($settings, $driver) { // If we have query parts, we should try to find related records: $parts = $this->getQueryParts($driver); @@ -61,10 +62,11 @@ class WorldCatEditions extends Editions } // Perform the search and save results: - $params = new Params(); + $sm = $this->getSearchManager(); + $params = $sm->setSearchClassId('WorldCat')->getParams(); $params->setLimit(5); $params->setOverrideQuery($query); - $result = new Results($params); + $result = $sm->setSearchClassId('WorldCat')->getResults($params); $this->results = $result->getResults(); } } diff --git a/module/VuFind/src/VuFind/Related/WorldCatSimilar.php b/module/VuFind/src/VuFind/Related/WorldCatSimilar.php index 9e34924477a859b8cba5c5781cc64a8fcbe5b149..c2eb1b1aa6c853f5eba3dbbd1eaa380aafca383f 100644 --- a/module/VuFind/src/VuFind/Related/WorldCatSimilar.php +++ b/module/VuFind/src/VuFind/Related/WorldCatSimilar.php @@ -26,7 +26,6 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Related; -use VuFind\Search\WorldCat\Params, VuFind\Search\WorldCat\Results; /** * Related Records: WorldCat-based similarity @@ -40,14 +39,16 @@ use VuFind\Search\WorldCat\Params, VuFind\Search\WorldCat\Results; class WorldCatSimilar extends Similar { /** - * Constructor + * init * * Establishes base settings for making recommendations. * * @param string $settings Settings from config.ini * @param \VuFind\RecordDriver\AbstractBase $driver Record driver object + * + * @return void */ - public function __construct($settings, $driver) + public function init($settings, $driver) { // Create array of query parts: $parts = array(); @@ -91,10 +92,11 @@ class WorldCatSimilar extends Similar } // Perform the search and save results: - $params = new Params(); + $sm = $this->getSearchManager(); + $params = $sm->setSearchClassId('WorldCat')->getParams(); $params->setLimit(5); $params->setOverrideQuery($query); - $result = new Results($params); + $result = $sm->setSearchClassId('WorldCat')->getResults($params); $this->results = $result->getResults(); } }