-
Notifications
You must be signed in to change notification settings - Fork 8
/
Communicator.php
164 lines (151 loc) · 4.92 KB
/
Communicator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* OAI-PMH Communicator (handles low-level request/response processing).
*
* PHP version 7
*
* Copyright (c) Demian Katz 2016.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Harvest_Tools
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/indexing:oai-pmh Wiki
*/
namespace VuFindHarvest\OaiPmh;
use Laminas\Http\Client;
use Laminas\Uri\Http;
use VuFindHarvest\ConsoleOutput\WriterAwareTrait;
use VuFindHarvest\ResponseProcessor\ResponseProcessorInterface;
/**
* OAI-PMH Communicator (handles low-level request/response processing).
*
* @category VuFind
* @package Harvest_Tools
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/indexing:oai-pmh Wiki
*/
class Communicator
{
use WriterAwareTrait;
/**
* HTTP client
*
* @var Client
*/
protected $client;
/**
* URL to harvest from
*
* @var string
*/
protected $baseUrl;
/**
* Response processor
*
* @var ResponseProcessorInterface
*/
protected $responseProcessor;
/**
* Constructor
*
* @param string $uri Base URI for OAI-PMH server
* @param Client $client HTTP client
* @param ResponseProcessorInterface $processor Response processor (optional)
*/
public function __construct(
$uri,
Client $client,
ResponseProcessorInterface $processor = null
) {
$this->baseUrl = $uri;
$this->client = $client;
$this->responseProcessor = $processor;
}
/**
* Perform a single OAI-PMH request.
*
* @param string $verb OAI-PMH verb to execute.
* @param array $params GET parameters for ListRecords method.
*
* @return string
*/
protected function sendRequest($verb, $params)
{
// Set up the request:
$this->client->resetParameters(false, false); // keep cookies/auth
$this->client->setUri($this->baseUrl);
// Load request parameters:
$query = $this->client->getRequest()->getQuery();
$query->set('verb', $verb);
foreach ($params as $key => $value) {
$query->set($key, $value);
}
// Perform request:
return $this->client->setMethod('GET')->send();
}
/**
* Make an OAI-PMH request. Throw an exception if there is an error; return
* an XML string on success.
*
* @param string $verb OAI-PMH verb to execute.
* @param array $params GET parameters for ListRecords method.
*
* @return string
*/
protected function getOaiResponse($verb, $params)
{
// Debug:
$this->write(
"Sending request: verb = {$verb}, params = " . print_r($params, true)
);
// Set up retry loop:
do {
$result = $this->sendRequest($verb, $params);
if ($result->getStatusCode() == 503) {
$delayHeader = $result->getHeaders()->get('Retry-After');
$delay = is_object($delayHeader)
? $delayHeader->getDeltaSeconds() : 0;
if ($delay > 0) {
$this->writeLine(
"Received 503 response; waiting {$delay} seconds..."
);
sleep($delay);
}
} elseif (!$result->isSuccess()) {
throw new \Exception('HTTP Error ' . $result->getStatusCode());
}
} while ($result->getStatusCode() == 503);
// If we got this far, there was no error -- send back response.
return $result->getBody();
}
/**
* Make an OAI-PMH request. Throw an exception if there is an error; return
* the processed response on success.
*
* @param string $verb OAI-PMH verb to execute.
* @param array $params GET parameters for ListRecords method.
*
* @return mixed
*/
public function request($verb, $params = [])
{
$xml = $this->getOaiResponse($verb, $params);
return $this->responseProcessor
? $this->responseProcessor->process($xml) : $xml;
}
}