Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.34% covered (success)
94.34%
50 / 53
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connector
94.34% covered (success)
94.34%
50 / 53
75.00% covered (warning)
75.00%
3 / 4
11.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getHoldings
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
4.16
 getRecord
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 search
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Class for accessing OCLC WorldCat search API
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Andrew Nagy 2008.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  WorldCat
25 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFindSearch\Backend\WorldCat;
32
33use VuFindSearch\ParamBag;
34
35/**
36 * WorldCat SRU Search Interface
37 *
38 * @category VuFind
39 * @package  WorldCat
40 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class Connector extends \VuFindSearch\Backend\SRU\Connector
45{
46    /**
47     * OCLC API key
48     *
49     * @var string
50     */
51    protected $wskey;
52
53    /**
54     * Additional options
55     *
56     * @var array
57     */
58    protected $options;
59
60    /**
61     * Constructor
62     *
63     * @param string               $wsKey   Web services key
64     * @param \Laminas\Http\Client $client  An HTTP client object
65     * @param array                $options Additional config settings
66     */
67    public function __construct(
68        $wsKey,
69        \Laminas\Http\Client $client,
70        array $options = []
71    ) {
72        parent::__construct(
73            'http://www.worldcat.org/webservices/catalog/search/sru',
74            $client
75        );
76        $this->wskey = $wsKey;
77        $this->options = $options;
78    }
79
80    /**
81     * Get holdings information for the specified record.
82     *
83     * @param string $id Record to obtain holdings for.
84     *
85     * @throws \Exception
86     * @return \SimpleXMLElement
87     */
88    public function getHoldings($id)
89    {
90        $this->client->resetParameters();
91        if (!isset($this->options['useFrbrGroupingForHoldings'])) {
92            $grouping = 'on';   // default to "on" for backward compatibility
93        } else {
94            $grouping = $this->options['useFrbrGroupingForHoldings'] ? 'on' : 'off';
95        }
96        $uri = "http://www.worldcat.org/webservices/catalog/content/libraries/{$id}"
97            . "?wskey={$this->wskey}&servicelevel=full&frbrGrouping=$grouping";
98        if (isset($this->options['latLon'])) {
99            [$lat, $lon] = explode(',', $this->options['latLon']);
100            $uri .= '&lat=' . urlencode($lat) . '&lon=' . urlencode($lon);
101        }
102        $this->client->setUri($uri);
103        $this->debug('Connect: ' . $uri);
104        $result = $this->client->setMethod('POST')->send();
105        $this->checkForHttpError($result);
106
107        return simplexml_load_string($result->getBody());
108    }
109
110    /**
111     * Retrieve a specific record.
112     *
113     * @param string   $id     Record ID to retrieve
114     * @param ParamBag $params Parameters
115     *
116     * @throws \Exception
117     * @return string    MARC XML
118     */
119    public function getRecord($id, ParamBag $params = null)
120    {
121        $params = $params ?: new ParamBag();
122        $params->set('servicelevel', 'full');
123        $params->set('wskey', $this->wskey);
124
125        $this->client->resetParameters();
126        $uri = 'http://www.worldcat.org/webservices/catalog/content/' . $id;
127        $uri .= '?' . implode('&', $params->request());
128        $this->client->setUri($uri);
129        $this->debug('Connect: ' . $uri);
130        $result = $this->client->setMethod('POST')->send();
131        $this->checkForHttpError($result);
132
133        // Check for error message in response:
134        $body = $result->getBody();
135        $xml = simplexml_load_string($body);
136        $error = isset($xml->diagnostic);
137
138        return [
139            'docs' => $error ? [] : [$body],
140            'offset' => 0,
141            'total' => $error ? 0 : 1,
142        ];
143    }
144
145    /**
146     * Execute a search.
147     *
148     * @param ParamBag $params Parameters
149     * @param int      $offset Search offset
150     * @param int      $limit  Search limit
151     *
152     * @return string
153     */
154    public function search(ParamBag $params, $offset, $limit)
155    {
156        $params->set('startRecord', $offset);
157        $params->set('maximumRecords', $limit);
158        $params->set('servicelevel', 'full');
159        $params->set('wskey', $this->wskey);
160
161        $response = $this->call('POST', $params->getArrayCopy(), false);
162
163        $xml = simplexml_load_string($response);
164        $docs = $xml->records->record ?? [];
165        $finalDocs = [];
166        foreach ($docs as $doc) {
167            $finalDocs[] = $doc->recordData->asXML();
168        }
169        return [
170            'docs' => $finalDocs,
171            'offset' => $offset,
172            'total' => (int)($xml->numberOfRecords ?? 0),
173        ];
174    }
175}