-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHarvester.php
470 lines (431 loc) · 14.6 KB
/
Harvester.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/**
* OAI-PMH Harvest Tool
*
* PHP version 7
*
* Copyright (c) Demian Katz 2010.
*
* 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 <[email protected]>
* @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 VuFindHarvest\ConsoleOutput\WriterAwareTrait;
use VuFindHarvest\Exception\OaiException;
use function count;
/**
* OAI-PMH Harvest Tool
*
* @category VuFind
* @package Harvest_Tools
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/indexing:oai-pmh Wiki
*/
class Harvester
{
use WriterAwareTrait;
/**
* Record writer
*
* @var RecordWriter
*/
protected $writer;
/**
* Low-level OAI-PMH communicator
*
* @var Communicator
*/
protected $communicator;
/**
* State manager
*
* @var StateManager
*/
protected $stateManager;
/**
* Target set(s) to harvest (null for all records)
*
* @var string|array
*/
protected $set = null;
/**
* Metadata type to harvest
*
* @var string
*/
protected $metadataPrefix = 'oai_dc';
/**
* Harvest end date (null for no specific end)
*
* @var string
*/
protected $harvestEndDate;
/**
* Harvest start date (null for no specific start)
*
* @var string
*/
protected $startDate = null;
/**
* Date granularity ('auto' to autodetect)
*
* @var string
*/
protected $granularity = 'auto';
/**
* Identify information from OAI host
*
* @var stdClass
*/
protected $identifyResponse = null;
/**
* Flag to limit number of harvested records (null = no limit).
* Used only for testing.
*
* @var ?int
*/
protected $stopAfter = null;
/**
* Count harvested records.
*/
protected $recordsCount = 0;
/**
* Constructor.
*
* @param Communicator $communicator Low-level API client
* @param RecordWriter $writer Record writer
* @param StateManager $stateManager State manager
* @param array $settings OAI-PMH settings
*/
public function __construct(
Communicator $communicator,
RecordWriter $writer,
StateManager $stateManager,
$settings = []
) {
// Don't time out during harvest!!
set_time_limit(0);
// Store dependencies
$this->communicator = $communicator;
$this->writer = $writer;
$this->stateManager = $stateManager;
// Store other settings
$this->storeDateSettings($settings);
$this->storeMiscSettings($settings);
}
/**
* Set an end date for the harvest (only harvest records BEFORE this date).
*
* @param string $date End date (YYYY-MM-DD format).
*
* @return void
*/
public function setEndDate($date)
{
$this->harvestEndDate = $date;
}
/**
* Set a start date for the harvest (only harvest records AFTER this date).
*
* @param string $date Start date (YYYY-MM-DD format).
*
* @return void
*/
public function setStartDate($date)
{
$this->startDate = $date;
}
/**
* Harvest all available documents.
*
* @return void
*
* @throws \Exception
*/
public function launch()
{
// Normalize sets setting to an array:
$sets = (array)$this->set;
if (empty($sets)) {
$sets = [null];
}
// The harvestEndDate may be null. Some OAI-PMH hosts may depend on a
// null value for backwards compatibility and reliability for various
// edge cases, so we allow a null value to be used during the initial
// records request. However, we still need to track an explicit end
// date, based on the current OAI server time, as the basis for future
// harvest start ranges. Note that this value can also be declared via
// state data as it should always track the time the harvest was
// first started.
// @see https://github.com/vufind-org/vufindharvest/issues/7
if (empty($this->harvestEndDate)) {
$explicitHarvestEndDate = $this->getIdentifyResponse()->responseDate;
// Add support for OAI-PMH hosts that require day granularity by
// converting the date format if necessary.
$granularity = $this->granularity == 'auto' ?
$this->getIdentifyResponse()->granularity : $this->granularity;
if ($granularity == 'YYYY-MM-DD') {
$explicitHarvestEndDate = substr($explicitHarvestEndDate, 0, 10);
}
} else {
$explicitHarvestEndDate = $this->harvestEndDate;
}
// Load last state, if applicable (used to recover from server failure).
if ($state = $this->stateManager->loadState()) {
$this->write("Found saved state; attempting to resume.\n");
// State data must contain 4 values for reliable resumption.
if (count($state) !== 4) {
$this->stateManager->clearState();
throw new \Exception(
'Corrupt or incomplete state data detected; '
. 'removing last_state.txt. Please restart harvest.'
);
}
[
$resumeSet,
$resumeToken,
$this->startDate,
$explicitHarvestEndDate
] = $state;
}
// Loop through all of the selected sets:
foreach ($sets as $set) {
// If we're resuming and there are multiple sets, find the right one.
if (isset($resumeToken) && $resumeSet != $set) {
continue;
}
// If we have a token to resume from, pick up there now...
if (isset($resumeToken)) {
$token = $resumeToken;
unset($resumeToken);
} else {
// ...otherwise, start harvesting at the requested date:
$token = $this->getRecordsByDate(
$this->startDate,
$set,
$this->harvestEndDate
);
}
// Keep harvesting as long as a resumption token is provided:
while ($token !== false) {
// If stopAfter is set, stop harvesting after given limit
if (
!empty($this->stopAfter)
&& $this->recordsCount >= $this->stopAfter
) {
$this->writeLine(
'reached limit of records to harvest: ' . $this->stopAfter
);
$this->writeLine('stop harvesting.');
$token = false;
break;
}
// Save current state in case we need to resume later:
$this->stateManager->saveState(
$set,
$token,
$this->startDate,
$explicitHarvestEndDate
);
$token = $this->getRecordsByToken($token);
}
}
// If we made it this far, all was successful. Save last harvest info and
// clean up the stored state (unless we have a limit imposed by stopAfter)
if (empty($this->stopAfter)) {
$this->stateManager->saveDate($explicitHarvestEndDate);
}
$this->stateManager->clearState();
}
/**
* Make an OAI-PMH request. Die if there is an error; return a SimpleXML object
* on success.
*
* @param string $verb OAI-PMH verb to execute.
* @param array $params GET parameters for ListRecords method.
*
* @return object SimpleXML-formatted response.
*/
protected function sendRequest($verb, $params = [])
{
$response = $this->communicator->request($verb, $params);
$this->checkResponseForErrors($response);
return $response;
}
/**
* Check an OAI-PMH response for errors that need to be handled.
*
* @param object $result OAI-PMH response (SimpleXML object)
*
* @return void
*
* @throws \Exception
* @throws OaiException
*/
protected function checkResponseForErrors($result)
{
// Detect errors and die if one is found:
if ($result->error) {
$attribs = $result->error->attributes();
// If this is a bad resumption token error and we're trying to
// restore a prior state, we should clean up.
if (
$attribs['code'] == 'badResumptionToken'
&& $this->stateManager->loadState()
) {
$this->stateManager->clearState();
throw new \Exception(
'Token expired; removing last_state.txt. Please restart harvest.'
);
}
throw new OaiException($attribs['code'], $result->error);
}
}
/**
* Harvest records using OAI-PMH.
*
* @param array $params GET parameters for ListRecords method.
*
* @return mixed Resumption token if provided, false if finished
*/
protected function getRecords($params)
{
// Make the OAI-PMH request:
$response = $this->sendRequest('ListRecords', $params);
// Save the records from the response:
if ($response->ListRecords->record) {
$newRecords = count($response->ListRecords->record);
$this->writeLine(
'[' . $this->recordsCount . ' records harvested] Processing '
. $newRecords . ' records...'
);
// count numRecords
$this->recordsCount += $newRecords;
$this->writer->write($response->ListRecords->record);
}
// If we have a resumption token, keep going; otherwise, we're done.
if (
isset($response->ListRecords->resumptionToken)
&& !empty($response->ListRecords->resumptionToken)
) {
return $response->ListRecords->resumptionToken;
}
return false;
}
/**
* Harvest records via OAI-PMH using date and set.
*
* @param string $from Harvest start date (null for no specific start).
* @param string $set Set to harvest (null for all records).
* @param string $until Harvest end date (null for no specific end).
*
* @return mixed Resumption token if provided, false if finished
*/
protected function getRecordsByDate($from = null, $set = null, $until = null)
{
$params = ['metadataPrefix' => $this->metadataPrefix];
if (!empty($from)) {
$params['from'] = $from;
}
if (!empty($set)) {
$params['set'] = $set;
}
if (!empty($until)) {
$params['until'] = $until;
}
return $this->getRecords($params);
}
/**
* Harvest records via OAI-PMH using resumption token.
*
* @param string $token Resumption token.
*
* @return mixed Resumption token if provided, false if finished
*/
protected function getRecordsByToken($token)
{
return $this->getRecords(['resumptionToken' => (string)$token]);
}
/**
* Get identify information from OAI-PMH host. Unless $reset = TRUE, this
* method will only invoke an OAI-PMH call upon its first usage and will
* return cached data after that.
*
* @param boolean $reset Whether-or-not to reset identity information
* already fetched during this request.
*
* @return stdClass An object of response properties as defined by
* http://www.openarchives.org/OAI/openarchivesprotocol.html#Identify
* plus a 'responseDate' property representing the
* datestamp of the identify response in the form
* YYYY-MM-DDThh:mm:ssZ
*
* @see http://www.openarchives.org/OAI/openarchivesprotocol.html#Identify
*/
protected function getIdentifyResponse($reset = false)
{
if (empty($this->identifyResponse) || $reset) {
$response = $this->sendRequest('Identify');
// Save callers the burden of casting XML elements by preparing a
// flat list of string properties.
$this->identifyResponse = (object)(array)$response->Identify;
$this->identifyResponse->responseDate = (string)$response->responseDate;
}
return $this->identifyResponse;
}
/**
* Set date range configuration (support method for constructor).
*
* @param array $settings Configuration
*
* @return void
*/
protected function storeDateSettings($settings)
{
// Set up start/end dates:
$from = empty($settings['from'])
? $this->stateManager->loadDate() : $settings['from'];
$until = empty($settings['until']) ? null : $settings['until'];
$this->setStartDate($from);
$this->setEndDate($until);
}
/**
* Set miscellaneous configuration (support method for constructor).
*
* @param array $settings Configuration
*
* @return void
*/
protected function storeMiscSettings($settings)
{
if (isset($settings['set'])) {
$this->set = $settings['set'];
}
if (isset($settings['metadataPrefix'])) {
$this->metadataPrefix = $settings['metadataPrefix'];
}
if (isset($settings['dateGranularity'])) {
$this->granularity = $settings['dateGranularity'];
}
if (isset($settings['stopAfter'])) {
$this->stopAfter = $settings['stopAfter'];
}
}
}