diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 2c725564370ac7eb63b93455bf27ec9492e21725..c7bfedd357248604a8d71a5eaba6448368174ee9 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -260,6 +260,11 @@ $config = array( ), 'service_manager' => array( 'factories' => array( + 'worldcatconnection' => function ($sm) { + $wc = new \VuFind\Connection\WorldCat(); + $wc->setLogger(\VuFind\Log\Logger::getInstance()); + return $wc; + }, 'worldcatutils' => function ($sm) { $wcu = new \VuFind\Connection\WorldCatUtils(); $wcu->setLogger(\VuFind\Log\Logger::getInstance()); diff --git a/module/VuFind/src/VuFind/Connection/SRU.php b/module/VuFind/src/VuFind/Connection/SRU.php index 4b7ba95e964bcb3733b7617efab40e857f477dee..e1ceca2e420ab8646fcad18d2ca2af62f77f6797 100644 --- a/module/VuFind/src/VuFind/Connection/SRU.php +++ b/module/VuFind/src/VuFind/Connection/SRU.php @@ -26,8 +26,8 @@ * @link http://vufind.org/wiki/system_classes#searching Wiki */ namespace VuFind\Connection; -use VuFind\Http\Client as HttpClient, VuFind\Log\Logger, - VuFind\XSLT\Processor as XSLTProcessor; +use VuFind\Http\Client as HttpClient, VuFind\XSLT\Processor as XSLTProcessor, + Zend\Log\Logger; /** * SRU Search Interface @@ -42,29 +42,35 @@ class SRU { /** * Logger object for debug info (or false for no debugging). + * + * @var Logger|bool */ - protected $logger; + protected $logger = false; /** * Whether to Serialize to a PHP Array or not. + * * @var bool */ protected $raw = false; /** * The HTTP_Request object used for REST transactions - * @var object HTTP_Request + * + * @var HttpClient */ protected $client; /** * The host to connect to + * * @var string */ protected $host; /** * The version to specify in the URL + * * @var string */ protected $sruVersion = '1.1'; @@ -81,10 +87,50 @@ class SRU // Initialize properties needed for HTTP connection: $this->host = $host; $this->client = new HttpClient(); + } + + /** + * Set the logger + * + * @param Logger $logger Logger to use. + * + * @return void + */ + public function setLogger(Logger $logger) + { + $this->logger = $logger; + } + + /** + * Does the current logger require debug messages? + * + * @return bool + */ + protected function debugNeeded() + { + // There are three situations we need to worry about: + // - Logger not set -- no debug needed + // - Logger set but does not support debugNeeded() method -- assume debug + // - Logger has debugNeeded() method -- defer to that + if (!$this->logger) { + return false; + } + return !method_exists($this->logger, 'debugNeeded') + || $this->logger->debugNeeded(); + } - // Don't waste time generating debug messages if nobody is listening: - $logger = Logger::getInstance(); - $this->logger = $logger->debugNeeded() ? $logger : false; + /** + * Log a debug message. + * + * @param string $msg Message to log. + * + * @return void + */ + protected function debug($msg) + { + if ($this->logger) { + $this->logger->debug($msg); + } } /** @@ -190,8 +236,8 @@ class SRU 'startRecord' => 1, 'recordSchema' => 'marcxml'); - if ($this->logger) { - $this->logger->debug('More Like This Query: ' . print_r($query, true)); + if ($this->debugNeeded()) { + $this->debug('More Like This Query: ' . print_r($query, true)); } return $this->call('GET', $options); @@ -236,8 +282,8 @@ class SRU public function search($query, $start = 1, $limit = null, $sortBy = null, $schema = 'marcxml', $process = true ) { - if ($this->logger) { - $this->logger->debug('Query: ' . print_r($query, true)); + if ($this->debugNeeded()) { + $this->debug('Query: ' . print_r($query, true)); } // Query String Parameters @@ -297,8 +343,8 @@ class SRU $queryString = implode('&', $query); } - if ($this->logger) { - $this->logger->debug( + if ($this->debugNeeded()) { + $this->debug( 'Connect: ' . print_r($this->host . '?' . $queryString, true) ); } diff --git a/module/VuFind/src/VuFind/Connection/WorldCat.php b/module/VuFind/src/VuFind/Connection/WorldCat.php index f978b7c4bee4804ea1b2f3c2c3d1cee09000cc33..662741263434088ae82d93c1a9f57c0abd8d9cf2 100644 --- a/module/VuFind/src/VuFind/Connection/WorldCat.php +++ b/module/VuFind/src/VuFind/Connection/WorldCat.php @@ -40,7 +40,18 @@ use VuFind\Config\Reader as ConfigReader; */ class WorldCat extends SRU { + /** + * OCLC API key + * + * @var string + */ protected $wskey; + + /** + * OCLC codes for limiting search results + * + * @var string + */ protected $limitCodes; /** @@ -72,8 +83,8 @@ class WorldCat extends SRU $uri = "http://www.worldcat.org/webservices/catalog/content/libraries/{$id}"; $uri .= "?wskey={$this->wskey}&servicelevel=full"; $this->client->setUri($uri); - if ($this->logger) { - $this->logger->debug('Connect: ' . $uri); + if ($this->debugNeeded()) { + $this->debug('Connect: ' . $uri); } $result = $this->client->setMethod('POST')->send(); $this->checkForHttpError($result); @@ -95,8 +106,8 @@ class WorldCat extends SRU $uri = 'http://www.worldcat.org/webservices/catalog/content/' . $id; $uri .= "?wskey={$this->wskey}&servicelevel=full"; $this->client->setUri($uri); - if ($this->logger) { - $this->logger->debug('Connect: ' . $uri); + if ($this->debugNeeded()) { + $this->debug('Connect: ' . $uri); } $result = $this->client->setMethod('POST')->send(); $this->checkForHttpError($result); diff --git a/module/VuFind/src/VuFind/RecordDriver/WorldCat.php b/module/VuFind/src/VuFind/RecordDriver/WorldCat.php index 08a28521b39d860e074831633365cf2300a7cab5..1add839bc3538f6a9a5dc12a65b491fc0c3a1650 100644 --- a/module/VuFind/src/VuFind/RecordDriver/WorldCat.php +++ b/module/VuFind/src/VuFind/RecordDriver/WorldCat.php @@ -26,7 +26,6 @@ * @link http://vufind.org/wiki/other_than_marc Wiki */ namespace VuFind\RecordDriver; -use VuFind\Connection\WorldCat as WorldCatConnection; /** * Model for MARC records in WorldCat. @@ -312,7 +311,8 @@ class WorldCat extends SolrMarc */ public function getWorldCatHoldings() { - $wc = new WorldCatConnection(); + $wc = $this->getServiceLocator()->getServiceLocator() + ->get('WorldCatConnection'); return $wc->getHoldings($this->getUniqueId()); } } diff --git a/module/VuFind/src/VuFind/Search/WorldCat/Results.php b/module/VuFind/src/VuFind/Search/WorldCat/Results.php index 9f3bc4e9937be92be2a356f32b00c565062a38c2..5e08e1b665c0a1bebe5c4380a73000e7221b9cf8 100644 --- a/module/VuFind/src/VuFind/Search/WorldCat/Results.php +++ b/module/VuFind/src/VuFind/Search/WorldCat/Results.php @@ -27,7 +27,6 @@ */ namespace VuFind\Search\WorldCat; use VuFind\Config\Reader as ConfigReader, - VuFind\Connection\WorldCat as WorldCatConnection, VuFind\Exception\RecordMissing as RecordMissingException, VuFind\Search\Base\Results as BaseResults; @@ -48,15 +47,11 @@ class Results extends BaseResults /** * Get a connection to the WorldCat API. * - * @return WorldCatConnection + * @return \VuFind\Connection\WorldCat */ public function getWorldCatConnection() { - static $wc = false; - if (!$wc) { - $wc = new WorldCatConnection(); - } - return $wc; + return $this->getServiceLocator()->get('WorldCatConnection'); } /**