diff --git a/config/vufind/config.ini b/config/vufind/config.ini index a1f08ce04aae0f037cddc1d659bb76e0b06bb786..4c71ea9f61d828b9d87255a4e25687762a919857 100644 --- a/config/vufind/config.ini +++ b/config/vufind/config.ini @@ -633,9 +633,22 @@ pw = "Password" ; WorldCat is Optional. Worldcat offers extra features such as "Other Editions" ; and the WorldCat searching. ;[WorldCat] -;id = myAccount +;Your WorldCat search API key ;apiKey = ApiKey +;Your holdings symbol (usually a three-letter code) - used for excluding your +; institution's holdings from the search results. ;OCLCCode = MYCODE +; Your worldcat xID Affiliate ID - used for Other Editions feature +;id = myAccount +; If you have increased your OCLC xID daily limits using the token method, +; fill in those credentials here. +; note: xOCLCNUM uses the xISBN token/secret +;xISBN_token = myToken +;xISBN_secret = mySecret +; As of 2014-09, although not specified by OCLC's documentation, the xISSN call +; does not work if you have not also specified your affiliate ID. +;xISSN_token = myToken +;xISSN_secret = mySecret ; DPLA key -- uncomment and fill in to use DPLATerms recommendations (see also ; searches.ini). diff --git a/module/VuFind/src/VuFind/Connection/WorldCatUtils.php b/module/VuFind/src/VuFind/Connection/WorldCatUtils.php index 5864dfae7eff268a2660de7982c996774844f5db..460707b5648c4c3573bbbf3cbfe52edf3087ff29 100644 --- a/module/VuFind/src/VuFind/Connection/WorldCatUtils.php +++ b/module/VuFind/src/VuFind/Connection/WorldCatUtils.php @@ -26,7 +26,8 @@ * @link http://vufind.org/wiki/vufind2:developer_manual Wiki */ namespace VuFind\Connection; -use File_MARCXML, VuFind\XSLT\Processor as XSLTProcessor, Zend\Log\LoggerInterface; +use File_MARCXML, VuFind\XSLT\Processor as XSLTProcessor, Zend\Config\Config, + Zend\Log\LoggerInterface; /** * World Cat Utilities @@ -49,11 +50,11 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface protected $logger = false; /** - * WorldCat ID + * WorldCat configuration * - * @var string + * @var \Zend\Config\Config */ - protected $worldCatId; + protected $config; /** * HTTP client @@ -69,19 +70,34 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface */ protected $silent; + /** + * Current server IP address + * + * @var string + */ + protected $ip; + /** * Constructor * - * @param string $worldCatId WorldCat ID - * @param \Zend\Http\Client $client HTTP client - * @param bool $silent Should we silently ignore HTTP failures? + * @param Config|string $config WorldCat configuration (either a full Config + * object, or a string containing the id setting). + * @param \Zend\Http\Client $client HTTP client + * @param bool $silent Should we silently ignore HTTP failures? + * @param string $ip Current server IP address (optional, but + * needed for xID token hashing */ - public function __construct($worldCatId, \Zend\Http\Client $client, - $silent = true + public function __construct($config, \Zend\Http\Client $client, $silent = true, + $ip = null ) { - $this->worldCatId = $worldCatId; + // Legacy compatibility -- prior to VuFind 2.4, this parameter was a string. + if (!($config instanceof Config)) { + $config = new Config(array('id' => $config)); + } + $this->config = $config; $this->client = $client; $this->silent = $silent; + $this->ip = $ip; } /** @@ -141,7 +157,33 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface */ protected function getWorldCatId() { - return $this->worldCatId; + return isset($this->config->id) ? $this->config->id : false; + } + + /** + * Build a url to use in querying OCLC's xID service. + * + * @param string $base base url with no querystring + * @param string $tokenVar config file variable holding the token + * @param string $secretVar config file variable holding the secret + * @param string $format data format for api response + * + * @return string + */ + protected function buildXIdUrl($base, $tokenVar, $secretVar, $format) { + $token = isset($this->config->$tokenVar) + ? $this->config->$tokenVar : false; + $secret = isset($this->config->$secretVar) + ? $this->config->$secretVar : false; + $querystr = '?method=getEditions&format=' . $format; + if ($token && $secret) { + $hash = md5($base . '|' . $this->ip . '|' . $secret); + $querystr .= '&token=' . $token . '&hash=' . $hash; + } if ($wcId = $this->getWorldCatId()) { + $querystr .= '&ai=' . urlencode($wcId); + } + $base .= $querystr; + return $base; } /** @@ -154,12 +196,9 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface public function getXISBN($isbn) { // Build URL - $url = 'http://xisbn.worldcat.org/webservices/xid/isbn/' . - urlencode(is_array($isbn) ? $isbn[0] : $isbn) . - '?method=getEditions&format=json'; - if ($wcId = $this->getWorldCatId()) { - $url .= '&ai=' . urlencode($wcId); - } + $base = 'http://xisbn.worldcat.org/webservices/xid/isbn/' . + urlencode(is_array($isbn) ? $isbn[0] : $isbn); + $url = $this->buildXIdUrl($base, 'xISBN_token', 'xISBN_secret', 'json'); // Print Debug code $this->debug("XISBN: $url"); @@ -193,12 +232,9 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface public function getXOCLCNUM($oclc) { // Build URL - $url = 'http://xisbn.worldcat.org/webservices/xid/oclcnum/' . - urlencode(is_array($oclc) ? $oclc[0] : $oclc) . - '?method=getEditions&format=json'; - if ($wcId = $this->getWorldCatId()) { - $url .= '&ai=' . urlencode($wcId); - } + $base = 'http://xisbn.worldcat.org/webservices/xid/oclcnum/' . + urlencode(is_array($oclc) ? $oclc[0] : $oclc); + $url = $this->buildXIdUrl($base, 'xISBN_token', 'xISBN_secret', 'json'); // Print Debug code $this->debug("XOCLCNUM: $url"); @@ -234,13 +270,9 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface public function getXISSN($issn) { // Build URL - $url = 'http://xissn.worldcat.org/webservices/xid/issn/' . - urlencode(is_array($issn) ? $issn[0] : $issn) . - //'?method=getEditions&format=csv'; - '?method=getEditions&format=xml'; - if ($wcId = $this->getWorldCatId()) { - $url .= '&ai=' . urlencode($wcId); - } + $base = 'http://xissn.worldcat.org/webservices/xid/issn/' . + urlencode(is_array($issn) ? $issn[0] : $issn); + $url = $this->buildXIdUrl($base, 'xISSN_token', 'xISSN_secret', 'xml'); // Print Debug code $this->debug("XISSN: $url"); @@ -565,4 +597,4 @@ class WorldCatUtils implements \Zend\Log\LoggerAwareInterface } return $main; } -} \ No newline at end of file +} diff --git a/module/VuFind/src/VuFind/Service/Factory.php b/module/VuFind/src/VuFind/Service/Factory.php index 20c895770876523c9d7996233a21b47505d0ec9f..b0a6473523248b3adbc84fdbf6e923234be9e83d 100644 --- a/module/VuFind/src/VuFind/Service/Factory.php +++ b/module/VuFind/src/VuFind/Service/Factory.php @@ -772,9 +772,11 @@ class Factory public static function getWorldCatUtils(ServiceManager $sm) { $config = $sm->get('VuFind\Config')->get('config'); - $wcId = isset($config->WorldCat->id) - ? $config->WorldCat->id : false; $client = $sm->get('VuFind\Http')->createClient(); - return new \VuFind\Connection\WorldCatUtils($wcId, $client); + $ip = $sm->get('Request')->getServer()->get('SERVER_ADDR'); + return new \VuFind\Connection\WorldCatUtils( + isset($config->WorldCat) ? $config->WorldCat : null, + $client, true, $ip + ); } -} \ No newline at end of file +}