-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagile_lcp.drush.inc
executable file
·639 lines (588 loc) · 17.1 KB
/
agile_lcp.drush.inc
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
/**
* @file
* Implementation of Drush hooks.
*/
/**
* Implements hook_drush_help().
*/
function agile_lcp_drush_help($command) {
switch ($command) {
case 'drush:agile_OAI_harvest_batch':
return dt('Creates objects');
}
}
/**
* Implements hook_drush_command().
*/
function agile_lcp_drush_command() {
$items = [];
$items['agile_OAI_harvest_batch'] = [
'description' => dt('Harvests records from OAI stream.'),
'options' => [
'limit' => [
'description' => dt('The number of batches to process. Defaults to 1'),
'required' => FALSE,
],
],
'aliases' => ['OAI'],
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 OAI --limit=3',
],
];
$items['agile_OAI_harvest_one'] = [
'description' => dt('Harvests one record from the OAI source.'),
'options' => [
'identifier' => [
'description' => dt('The Koha Biblio Number of the MARC record to harvest from OIA.'),
'required' => TRUE,
],
],
'aliases' => ['OAI_1'],
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 OAI_1 --identifier=197559'
]
];
$items['agile_OAI_prep_batch'] = [
'description' => dt('Preps batch process to harvest records from OAI stream.'),
'options' => [
'url' => [
'value' => 'optional',
'description' => dt("The url to initiate harvesting. Defaults to 'https://librarycompany.kohacatalog.com/cgi-bin/koha/oai.pl?verb=ListIdentifiers&metadataPrefix=marc21'"),
],
'reset' => [
'description' => dt("Set to 1 if you want to restart the harvest using the parameters; otherwise the harvest will continue where it last left off."),
'required' => FALSE,
],
'from' => [
'description' => dt("Datestamp of first harvested object - optional (YYYY-MM-DD)"),
'required' => FALSE,
],
'to' => [
'description' => dt("Datestamp of first harvested object - optional (YYYY-MM-DD)"),
'required' => FALSE,
],
],
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 OAI_Prep',
],
'aliases' => ['OAI_Prep'],
];
$items['agile_get_collection_pids'] = [
'description' => dt('Gets and records OAI collection PIDS.'),
'options' => [
'limit' => [
'description' => dt("Number of records to retrieve at once"),
'required' => FALSE,
],
'iterations' => [
'description' => dt("number of times to iterate"),
'required' => FALSE,
],
],
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 get_collection_pids --limit=10',
],
'aliases' => ['GCP'],
];
$items['agile_remove_indexed_collection_pids'] = [
'description' => dt('Removes already indexed pids from database.'),
'examples' => [
'drush -u 1 UCPT',
],
'aliases' => ['UCPT'],
];
$items['agile_activate'] = [
'description' => dt('Activates objects from file'),
'examples' => [
'drush -u 1 AA',
],
'aliases' => ['AA'],
];
$items['agile_lcp_solr_reindex'] = [
'description' => dt('Reindexes PIDS from RI to solr'),
'options' => [
'iterations' => [
'description' => dt("number of times to iterate"),
'required' => FALSE,
],
],
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 SOLR_INDEX --limit=10',
],
'aliases' => ['SOLR_INDEX'],
];
$items['mark_solr_pids'] = [
'description' => dt('Takes all pids form solr and updates cp table'),
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 mark_solr',
],
'aliases' => ['mark_solr'],
];
$items['remove_duplicates'] = [
'description' => dt('Removes duplicated OAI objects'),
'drupal dependencies' => [
'islandora',
],
'examples' => [
'drush -u 1 remove_dupes',
],
'aliases' => ['remove_dupes'],
];
return $items;
}
function drush_agile_lcp_agile_OAI_harvest_one() {
$identifier = drush_get_option('identifier', '');
if ($identifier != '') {
agile_lcp_drush_build_object($identifier);
}
}
/**
* Kicks off series of sub-batches to harvest all OAI records from token.
*/
function drush_agile_lcp_agile_OAI_harvest_batch() {
module_load_include('inc', 'agile_lcp', 'includes/db');
$batch = [];
$limit = drush_get_option('limit', 1);
$tokens = agile_lcp_get_unproccessed_tokens($limit);
$count = count($tokens);
if ($count < $limit) {
$limit = count($tokens);
drush_print(dt("There are only @count tokens left to process", ['@count' => $count]));
}
foreach ($tokens as $token_id => $token) {
$batch['operations'][] = [
'agile_lcp_add_OAI_batch_worker',
[
'token_id' => $token_id,
'token' => $token,
],
];
}
drush_print("Start batch. Processing $limit tokens");
batch_set($batch);
drush_backend_batch_process();
}
/**
* Kicks off sub_batch.
*
* @param string $token_id
* token id
* @param string $token
* resumption_token
* @param context $context
* Batch context.
*/
function agile_lcp_add_OAI_batch_worker($token_id = NULL, $token = NULL, &$context) {
drush_print('.', 0, NULL, FALSE);
if ($token) {
$url = "https://librarycompany.kohacatalog.com/cgi-bin/koha/oai.pl?verb=ListIdentifiers&$token";
}
else {
drush_print(dt("First run."));
$url = 'https://librarycompany.kohacatalog.com/cgi-bin/koha/oai.pl?verb=ListIdentifiers&metadataPrefix=marc21';
}
$input = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadXML($input);
$new_identifiers = $dom->getElementsByTagName('identifier');
// previously processed each update in the token as a batch operation
// super slow, so doing it directly
agile_lcp_batch_notifier($token_id, $token);
foreach ($new_identifiers as $new_identifier) {
agile_lcp_drush_build_object($new_identifier->nodeValue);
}
if ($token) {
agile_lcp_drush_mark_token($token_id, $token);
}
// here's the code for the batch operations if needed
/*
$batch = [];
$batch['operations'][] = [
'agile_lcp_batch_notifier',
[
'token_id' => $token_id,
'token' => $token,
],
];
foreach ($new_identifiers as $new_identifier) {
$batch['operations'][] = [
'agile_lcp_drush_build_object',
[
'identifier' => $new_identifier->nodeValue,
],
];
}
if ($token) {
$batch['operations'][] = [
'agile_lcp_drush_mark_token',
[
'token_id' => $token_id,
'token' => $token,
],
];
}
batch_set($batch);
*/
}
/**
* Builds and ingests islandora object for OAI record.
*
* @param string $identifier
* Unique OAI identifier
*/
function agile_lcp_drush_build_object(string $identifier) {
drush_print('Loading ' . $identifier);
module_load_include('inc', 'agile_lcp', 'includes/utilities');
$streams = agile_get_streams_from_entry($identifier);
$existing = agile_lcp_get_pids_from_identifier($identifier);
// Streams are not returned if OAI record has been deleted.
if (!$streams) {
if(!$existing) {
drush_print(" Record $identifier has deleted status. Not added.");
}
else {
// Remove current OAI records, if record has been deleted since original ingest.
$oai_records = agile_lcp_get_pids_from_identifier($identifier, TRUE);
$tuque = new IslandoraTuque();
foreach ($oai_records as $pid) {
$tuque->repository->purgeObject($pid);
drush_print(" Object $pid with identifier $identifier has been deleted.");
}
}
return;
}
if ($existing) {
foreach ($existing as $pid) {
$object = islandora_object_load($pid);
foreach ($streams as $dsid => $content) {
if ($object[$dsid]) {
$object[$dsid]->content = $content;
}
}
drush_print("Updated $pid. Identifier = $identifier");
}
return;
}
/* Discover layer is disabled, so don't add any new records */
/* ---------------------------------------------
$tuque = new IslandoraTuque();
$repository = $tuque->repository;
$object = $repository->constructObject('islandora');
$object->relationships->add(FEDORA_MODEL_URI, 'hasModel', 'islandora:oai_importCModel');
$object->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:imported_oai');
$dom = new DOMDocument();
$dom->loadXML($streams['MODS']);
$non_sort = '';
$nonSort_nodes = $dom->getElementsByTagName('nonSort');
if ($nonSort_nodes->length > 0) {
$non_sort = $nonSort_nodes->item(0)->nodeValue . ' ';
}
$title = $dom->getElementsByTagName('title')->item(0)->nodeValue;
$label = $non_sort . $title;
$object->label = $label;
foreach ($streams as $dsid => $content) {
$type = ($dsid == 'DC') ? 'X' : 'M';
$mime = ($dsid == 'DC') ? 'text/xml' : 'application/xml';
$ds = $object->constructDatastream($dsid, $type);
$ds->setContentFromString($content, FALSE);
$ds->label = $dsid;
$ds->mimetype = $mime;
$object->ingestDatastream($ds);
}
try {
islandora_add_object($object);
drush_print("Added {$object->id}. Identifier = $identifier");
} catch (Exception $ex) {
drush_print("Failed to ingest $identifier");
}
--------------------------------------------------- */
}
/**
* Output for feedback for screen.
*
* @param string $token_id
* OAI token
*/
function agile_lcp_batch_notifier($token_id, $token) {
drush_print("\n Start sub_batch - Processing token #$token_id - $token");
}
/**
* Marks token as processed.
*
* @param int $token_id
* Unique identifier for token
* @param string $token
* Token being updated
*/
function agile_lcp_drush_mark_token($token_id, $token) {
drush_print("$token_id complete.");
$query = db_update('resumption_token')
->condition('token_id', $token_id)
->fields([
'processed' => 1,
])->execute();
}
/**
* Adds all tokens from supplied URL to prepare for harvesting.
*/
function drush_agile_lcp_agile_OAI_prep_batch() {
drush_print(dt("Adding to database -"));
module_load_include('inc', 'agile_lcp', 'includes/db');
$url = drush_get_option('url', 'https://librarycompany.kohacatalog.com/cgi-bin/koha/oai.pl?verb=ListIdentifiers&');
$reset = drush_get_option('reset', FALSE);
set_time_limit(0);
if ($reset == '1')
{
// set the initial token to load the first set of records
$token = 'metadataPrefix=marc21';
$to = drush_get_option('to', FALSE);
$from = drush_get_option('from', FALSE);
if ($to) {
$token .= "&to=$to";
}
if ($from) {
$token .= "&from=$from";
}
agile_lcp_add_token($token);
}
else
{
// make a new initial token based on last run date
$token = 'metadataPrefix=marc21';
$from = agile_lcp_get_last_rundate();
if ($from) {
$token .= "&from=$from";
}
agile_lcp_add_token($token);
drush_print("Resuming from last run date in database: $token.");
}
$count = 1;
while ($token) {
$new_url = $url . $token;
$token = NULL;
$data = file_get_contents($new_url);
$sub_dom = new DOMDocument();
$sub_dom->loadXML($data);
$resumption_tokens = $sub_dom->getElementsByTagName('resumptionToken');
if ($resumption_tokens->length > 0) {
$count++;
$resumption_node = $sub_dom->getElementsByTagName('resumptionToken')->item(0);
$token = 'resumptionToken=' . $resumption_node->nodeValue;
agile_lcp_add_token($token);
drush_print('.', 0, NULL, FALSE);
}
}
drush_print(dt("\n Complete."));
}
/**
* Gets all pids from resource index and puts them in database.
*/
function drush_agile_lcp_agile_get_collection_pids() {
$result = db_select('collection_pids', 'cp')
->fields('cp')
->execute();
$count = $result->rowCount();
drush_print("$count in database table - start");
$limit = drush_get_option('limit', 1000);
$iterations = drush_get_option('iterations', 1);
$tuque = new IslandoraTuque();
for ($i = 0; $i < $iterations; $i++) {
$offset = ($i * $limit);
$query = <<<EOQ
PREFIX fm: <info:fedora/fedora-system:def/model#>
SELECT distinct ?s
FROM <RISEARCH>
WHERE
{
?s fm:state fm:Active .
?s fm:hasModel ?o
}
limit $limit
offset $offset
EOQ;
$results = $tuque->repository->ri->sparqlQuery($query);
if (count($results) == 0) {
drush_print(dt("No more repository results"));
break;
}
foreach ($results as $result) {
$pid = $result['s']['value'];
drush_print($pid);
$nid = db_merge('collection_pids')
->key(['pid' => $pid])
->fields([
'pid' => $pid,
'indexed' => 0,
])
->execute();
}
}
$result = db_select('collection_pids', 'cp')
->fields('cp')
->execute();
$count = $result->rowCount();
drush_print("$count in database table - end");
}
/**
* One time function to remove processed PIDS from database table.
*/
function drush_agile_lcp_agile_remove_indexed_collection_pids() {
$result = db_select('collection_pids', 'cp')
->fields('cp')
->condition('indexed', 0)
->execute();
$count = $result->rowCount();
drush_print("$count unindexed in database table - start");
$file = "/tmp/collectionPIDS.txt";
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== FALSE) {
$pid = trim($line);
$query = db_update('collection_pids')
->condition('pid', $pid)
->fields([
'indexed' => 1,
])->execute();
}
fclose($handle);
}
else {
drush_print("Could not open file");
}
$result = db_select('collection_pids', 'cp')
->fields('cp')
->condition('indexed', 0)
->execute();
$count = $result->rowCount();
drush_print("$count unindexed in database table - end");
}
function drush_agile_lcp_solr_reindex() {
$iterations = drush_get_option('iterations', 1);
drush_print($iterations);
for ($i = 1; $i <= $iterations; $i++) {
$start = microtime(TRUE);
$results = db_select('collection_pids', 'cp')
->fields('cp')
->condition('indexed', 0)
->range(0, 1000)
->execute()
->fetchAll();
if (!$results) {
drush_print("All pids have been indexed");
break;
}
foreach ($results as $result) {
$pid = $result->PID;
$curl = curl_init();
$url = "http://localhost:8080/fedoragsearch/rest?operation=updateIndex&action=fromPid&value=$pid";
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERPWD => 'fedoraAdmin:DXhn8eg4zU6Z6NgA',
//CURLOPT_USERPWD => 'fedoraAdmin:fedoraAdmin',
]);
$success = curl_exec($curl);
curl_close($curl);
if ($success != FALSE) {
$num_updated = db_update('collection_pids')
->fields([
'indexed' => 1,
])
->condition('pid', $pid)
->execute();
}
else {
drush_print("Failure on $pid");
}
}
drush_print(microtime(TRUE) - $start);
}
}
function drush_agile_lcp_mark_solr_pids() {
$query = '*:*';
$limit = 4000;
$start = 0;
for ($i = 0; $i < 86; $i++) {
$qp = new IslandoraSolrQueryProcessor();
$qp->buildQuery('*:*');
$qp->solrParams['fq'] = $query;
$qp->solrStart = $start;
$qp->solrParams['fl'] = 'PID';
$qp->solrLimit = $limit;
$qp->executeQuery(FALSE);
$objects = $qp->islandoraSolrResult['response']['objects'];
if ($objects) {
foreach ($objects as $object) {
$pid = $object['PID'];
$num_updated = db_update('collection_pids')
->fields([
'indexed' => 1,
])
->condition('pid', $pid)
->execute();
}
drush_print("Batch $start completed.");
$start = $start + $limit;
}
}
}
/**
* One time function to remove OAI duplicated functions
*/
function drush_agile_lcp_remove_duplicates() {
drush_print("Removing duplicates...");
module_load_include('inc', 'agile_lcp', 'includes/utilities');
$duplicate_identifiers = agile_lcp_get_duplicates();
$total = count($duplicate_identifiers);
drush_print("$total found");
foreach ($duplicate_identifiers as $identifier) {
$pids_to_delete = [];
$oai_only = TRUE;
$query_processor = new IslandoraSolrQueryProcessor();
$query_processor->solrQuery = "mods_recordInfo_recordIdentifier_s:$identifier";
$query_processor->solrStart = 0;
$query_processor->solrLimit = 100;
$query_processor->solrParams = [
'fl' => "PID RELS_EXT_hasModel_uri_s",
];
$query_processor->executeQuery(FALSE);
$solr_results = $query_processor->islandoraSolrResult;
if ($solr_results['response']['objects']) {
foreach ($solr_results['response']['objects'] as $object) {
if ($object['solr_doc']['RELS_EXT_hasModel_uri_s'] == 'info:fedora/islandora:oai_importCModel') {
$pids_to_delete[] = $object['PID'];
}
else {
$oai_only = FALSE;
}
}
if ($oai_only) {
array_shift($pids_to_delete);
}
foreach ($pids_to_delete as $pid) {
$obj = islandora_object_load($pid);
drush_print("Deleting $obj->id");
$obj->delete();
}
}
}
}