Skip to content
Snippets Groups Projects
Commit 3e6d1b75 authored by Demian Katz's avatar Demian Katz
Browse files

Access WorldCat connection via service manager; added missing comments;...

Access WorldCat connection via service manager; added missing comments; switched logging to use dependency injection.
parent b865200c
No related merge requests found
......@@ -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());
......
......@@ -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)
);
}
......
......@@ -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);
......
......@@ -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());
}
}
......@@ -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');
}
/**
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment