Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.04% covered (danger)
37.04%
20 / 54
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connector
37.04% covered (danger)
37.04%
20 / 54
28.57% covered (danger)
28.57%
2 / 7
119.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMoreLikeThis
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 scan
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 sruSearch
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 checkForHttpError
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 call
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
6.05
 process
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * SRU Search Interface
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  SRU
25 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindSearch\Backend\SRU;
31
32use VuFind\XSLT\Processor as XSLTProcessor;
33use VuFindSearch\Backend\Exception\BackendException;
34use VuFindSearch\Backend\Exception\HttpErrorException;
35
36use function is_array;
37
38/**
39 * SRU Search Interface
40 *
41 * @category VuFind
42 * @package  SRU
43 * @author   Andrew S. Nagy <vufind-tech@lists.sourceforge.net>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development Wiki
46 */
47class Connector implements \Laminas\Log\LoggerAwareInterface
48{
49    use \VuFind\Log\LoggerAwareTrait;
50
51    /**
52     * Whether to Serialize to a PHP Array or not.
53     *
54     * @var bool
55     */
56    protected $raw = false;
57
58    /**
59     * The HTTP_Request object used for REST transactions
60     *
61     * @var \Laminas\Http\Client
62     */
63    protected $client;
64
65    /**
66     * The host to connect to
67     *
68     * @var string
69     */
70    protected $host;
71
72    /**
73     * The version to specify in the URL
74     *
75     * @var string
76     */
77    protected $sruVersion = '1.1';
78
79    /**
80     * Constructor
81     *
82     * Sets up the SOAP Client
83     *
84     * @param string               $host   The URL of the SRU Server
85     * @param \Laminas\Http\Client $client An HTTP client object
86     */
87    public function __construct($host, \Laminas\Http\Client $client)
88    {
89        // Initialize properties needed for HTTP connection:
90        $this->host = $host;
91        $this->client = $client;
92    }
93
94    /**
95     * Get records similar to one record
96     *
97     * @param array  $record An associative array of the record data
98     * @param string $id     The record id
99     * @param int    $max    The maximum records to return; Default is 5
100     *
101     * @return array         An array of query results
102     */
103    public function getMoreLikeThis($record, $id, $max = 5)
104    {
105        // More Like This Query
106        $query = 'title="' . $record['245']['a'] . '" ' .
107                 "NOT rec.id=$id";
108
109        // Query String Parameters
110        $options = ['operation' => 'searchRetrieve',
111                         'query' => $query,
112                         'maximumRecords' => $max,
113                         'startRecord' => 1,
114                         'recordSchema' => 'marcxml'];
115
116        $this->debug('More Like This Query: ' . $query);
117
118        return $this->call('GET', $options);
119    }
120
121    /**
122     * Scan
123     *
124     * @param string $clause   The CQL clause specifying the start point
125     * @param int    $pos      The position of the start point in the response
126     * @param int    $maxTerms The maximum number of terms to return
127     *
128     * @return string          XML response
129     */
130    public function scan($clause, $pos = null, $maxTerms = null)
131    {
132        $options = ['operation' => 'scan',
133                         'scanClause' => $clause];
134        if (null !== $pos) {
135            $options['responsePosition'] = $pos;
136        }
137        if (null !== $maxTerms) {
138            $options['maximumTerms'] = $maxTerms;
139        }
140
141        return $this->call('GET', $options, false);
142    }
143
144    /**
145     * Search
146     *
147     * @param string $query   The search query
148     * @param string $start   The record to start with
149     * @param string $limit   The amount of records to return
150     * @param string $sortBy  The value to be used by for sorting
151     * @param string $schema  Record schema to use in results list
152     * @param bool   $process Process into array (true) or return raw (false)
153     *
154     * @return array          An array of query results
155     */
156    public function sruSearch(
157        $query,
158        $start = 1,
159        $limit = null,
160        $sortBy = null,
161        $schema = 'marcxml',
162        $process = true
163    ) {
164        $this->debug('Query: ' . $query);
165
166        // Query String Parameters
167        $options = ['operation' => 'searchRetrieve',
168                         'query' => $query,
169                         'startRecord' => ($start) ? $start : 1,
170                         'recordSchema' => $schema];
171        if (null !== $limit) {
172            $options['maximumRecords'] = $limit;
173        }
174        if (null !== $sortBy) {
175            $options['sortKeys'] = $sortBy;
176        }
177
178        return $this->call('GET', $options, $process);
179    }
180
181    /**
182     * Check for HTTP errors in a response.
183     *
184     * @param \Laminas\Http\Response $result The response to check.
185     *
186     * @throws BackendException
187     * @return void
188     */
189    public function checkForHttpError($result)
190    {
191        if (!$result->isSuccess()) {
192            throw HttpErrorException::createFromResponse($result);
193        }
194    }
195
196    /**
197     * Submit REST Request
198     *
199     * @param string $method  HTTP Method to use: GET or POST
200     * @param array  $params  An array of parameters for the request
201     * @param bool   $process Should we convert the MARCXML?
202     *
203     * @return string|SimpleXMLElement The response from the XServer
204     */
205    protected function call($method = 'GET', $params = null, $process = true)
206    {
207        $queryString = '';
208        if ($params) {
209            $query = ['version=' . $this->sruVersion];
210            foreach ($params as $function => $value) {
211                if (is_array($value)) {
212                    foreach ($value as $additional) {
213                        $additional = urlencode($additional);
214                        $query[] = "$function=$additional";
215                    }
216                } else {
217                    $value = urlencode($value);
218                    $query[] = "$function=$value";
219                }
220            }
221            $queryString = implode('&', $query);
222        }
223
224        $url = $this->host . '?' . $queryString;
225        $this->debug('Connect: ' . $url);
226
227        // Send Request
228        $this->client->resetParameters();
229        $this->client->setUri($url);
230        $result = $this->client->setMethod($method)->send();
231        $this->checkForHttpError($result);
232
233        // Return processed or unprocessed response, as appropriate:
234        return $process ? $this->process($result->getBody()) : $result->getBody();
235    }
236
237    /**
238     * Process an SRU response. Returns either the raw XML string or a
239     * SimpleXMLElement based on the contents of the class' raw property.
240     *
241     * @param string $response SRU response
242     *
243     * @return string|SimpleXMLElement
244     */
245    protected function process($response)
246    {
247        // Send back either the raw XML or a SimpleXML object, as requested:
248        $result = XSLTProcessor::process('sru-convert.xsl', $response);
249        if (!$result) {
250            throw new BackendException(
251                sprintf('Error processing SRU response: %20s', $response)
252            );
253        }
254        return $this->raw ? $result : simplexml_load_string($result);
255    }
256}