-
Notifications
You must be signed in to change notification settings - Fork 15
/
islandora_datastream_crud.drush.inc
719 lines (680 loc) · 29.2 KB
/
islandora_datastream_crud.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
* @file
* Drush file for the Islandora Datastream CRUD module.
*/
/**
* Implements hook_drush_command().
*/
function islandora_datastream_crud_drush_command() {
$items = array();
$items['islandora_datastream_crud_fetch_pids'] = array(
'aliases' => array('idcrudfp'),
'description' => 'Fetch PIDs from Islandora for the objects whose datastreams you want to update. The `--collection`, `--content_model`, `--namespace`, and `--solr_query` options, if present, are ANDed together.',
'examples' => array(
'drush islandora_datastream_crud_fetch_pids --user=admin --collection=islandora:sp_basic_image_collection --pid_file=/tmp/imagepids.txt',
'drush islandora_datastream_crud_fetch_pids --user=admin --content_model=islandora:sp_basic_image --collection=islandora:sp_basic_image_collection --pid_file=/tmp/imagepids.txt',
),
'options' => array(
'content_model' => array(
'description' => 'The content model of the objects you want to update.',
),
'without_cmodel' => array(
'description' => 'The content model you want to exclude from your list of objects.',
),
'namespace' => array(
'description' => 'The namespace of the objects you want to update.',
),
'collection' => array(
'description' => 'The PID of the collection from which to select the objects.',
),
'is_member_of' => array(
'description' => 'The PID of the parent with the isMemberOf relationship.',
),
'with_dsid' => array(
'description' => 'Limit to objects that have a datastream with the specified datastream ID.',
),
'without_dsid' => array(
'description' => 'Limit to objects that do not have a datastream with the specified datastream ID.',
),
'solr_query' => array(
'description' => 'A raw Solr query. If the --solr_query option is present, it overrides --content_model, --namespace, and --collection.',
),
'pid_file' => array(
'description' => 'Absolute path to the file where you want the PID list to be saved. If absent, PIDs will be written to STDOUT.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_datastream_crud_fetch_datastreams'] = array(
'aliases' => array('idcrudfd'),
'description' => 'Fetch files from Islandora corresponding to the datastreams you want to update.',
'examples' => array(
'drush islandora_datastream_crud_fetch_datastreams --user=admin --pid_file=/tmp/imagepids.txt --dsid=MODS --datastreams_directory=/tmp/imagemods',
'drush islandora_datastream_crud_fetch_datastreams --user=admin --pid_file=/tmp/imagepids.txt --dsid=OBJ --datastreams_directory=/tmp/imagecollection_objs',
),
'options' => array(
'dsid' => array(
'description' => 'Datastream ID.',
'required' => TRUE,
),
'pid_file' => array(
'description' => 'Absolute path to the file that lists PIDs for objects you want to fetch datastreams from.',
'required' => TRUE,
),
'datastreams_directory' => array(
'description' => 'Absolute path to the directory where you want the datastream files to be saved.',
'required' => TRUE,
),
'datastreams_extension' => array(
'description' => "Optional. The file extension, without the period, to give the datastream files. If absent, Islandora will assign an extension based on the datastream's MIME type.",
),
'datastreams_version' => array(
'description' => 'Optional. The version number of the datastream. 0 is the current version (the default, so you wouldn\'t normally specify it), 1 is the previous version, 2 is the version before that, etc.',
),
'pause' => array(
'description' => 'Optional. Number of seconds to pause before fetching the next datastream.',
),
'filename_separator' => array(
'description' => 'Optional. Single character used to separate the PID namespace, PID number, and datastream ID in datastream filenames. Defaults to _.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_datastream_crud_push_datastreams'] = array(
'aliases' => array('idcrudpd'),
'description' => 'Add or replace datastreams. This operation writes datastreams to objects in your repository.',
'examples' => array(
'drush islandora_datastream_crud_push_datastreams --user=admin --datastreams_source_directory=/tmp/imagecollection_objs',
'drush islandora_datastream_crud_push_datastreams --user=admin --no_derivs --datastreams_source_directory=/tmp/imagecollection_objs',
'drush islandora_datastream_crud_push_datastreams --user=admin --datastreams_source_directory=/tmp/imagecollection_objs --pid_file=/tmp/wanted_pids.txt',
'drush islandora_datastream_crud_push_datastreams --user=admin --datastreams_directory=/tmp/my_new_datastreams --pid_file=/tmp/pids_for_target_objects.txt --datastream_label="My new datastream" --datastream_mimetype=application/foo',
),
'options' => array(
'datastreams_source_directory' => array(
'description' => 'Absolute path to the directory where updated datastream files are stored.',
'required' => TRUE,
),
'pid_file' => array(
'description' => 'Optional. Absolute path to the file that lists PIDs for objects you want to push datastreams to. If absent, all of the datastreams in the source directory will be pushed',
),
'datastreams_mimetype' => array(
'description' => 'Optional. The MIME type to apply to the new datastreams. If absent, the existing MiME type is used.',
),
'datastreams_label' => array(
'description' => 'Optional. The label to apply to the new datastreams. Enclose in quotation marks. If absent and the datastream exists, the original label is retained. If absent and the datastream is being created, no label is applied.',
),
'datastreams_crud_log' => array(
'description' => 'Optional. Absolute path to the CRUD log. If present, the PID, DSID, and a message will be written to the specified file.' ,
),
'pause' => array(
'description' => 'Optional. Number of seconds to pause before pushing the next datastream.',
),
'datastreams_revert' => array(
'description' => 'Optional. Include if the push is to revert datastream versions.',
'value' => 'optional',
),
'update_dc' => array(
'description' => 'Optional. Set to 0 (zero) if you do not want to update your DC datastreams from the MODS or other XML datastreams ou are pushing. Default is 1, but you will be prompted to confirm the DC update.',
),
'dc_transform' => array(
'description' => 'Optional. Absolute path to the XSLT stylesheet to use for regenerating DC from MODS or other XML datastream.',
),
'no_derivs' => array(
'description' => 'Optional. Turns off all derivative generation that would otherwise result from pushing datastreams.',
'value' => 'optional',
),
'filename_separator' => array(
'description' => 'Optional. Single character used to separate the PID namespace, PID number, and datastream ID in datastream filenames. Defaults to _.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_datastream_crud_delete_datastreams'] = array(
'aliases' => array('idcruddd'),
'description' => 'Delete datastreams.',
'examples' => array(
'drush islandora_datastream_crud_delete_datastreams --user=admin --dsid=FOO --pid_file=/tmp/delete_foo_from_these_objects.txt',
'drush islandora_datastream_crud_delete_datastreams --user=admin --dsid=FOO --versions="1,2" --pid_file=/tmp/delete_foo_from_these_objects.txt',
'drush islandora_datastream_crud_delete_datastreams --user=admin --dsid=FOO --versions="1.." --pid_file=/tmp/delete_foo_from_these_objects.txt',
),
'options' => array(
'pid_file' => array(
'description' => 'Absolute path to the file that lists PIDs for objects you want to delete datastreams from.',
'required' => TRUE,
),
'dsid' => array(
'description' => 'Datastream ID.',
'required' => TRUE,
),
'versions' => array(
'description' => 'If the versions option is present, then only datastream versions so specified list will be deleted. It may be expressed as a comma-separated list of datastream version numbers that you wish to delete, or as a range of version numbers, expressed as N..N. A range can also be specified as just one number, with the ".." after, which means delete this version and higher, or with the ".." before, which means delete version zero up to and including this version.'
),
'datastreams_crud_log' => array(
'description' => 'Optional. Absolute path to the CRUD log. If present, the PID, DSID, and a message will be written to the specified file.',
),
'pause' => array(
'description' => 'Optional. Number of seconds to pause before deleting the next datastream.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_datastream_crud_generate_derivatives'] = array(
'aliases' => array('idcrudgd'),
'description' => 'Generate or regenerate derivative datastreams from a source DSID.',
'examples' => array(
'drush islandora_datastream_crud_generate_derivatives --user=admin --source_dsid=OBJ --pid_file=/tmp/regenerate_derivatives_for_these_objects.txt',
),
'options' => array(
'pid_file' => array(
'description' => 'Absolute path to the file that lists PIDs for objects you want to generate derivatives for.',
'required' => TRUE,
),
'source_dsid' => array(
'description' => 'The source datastream ID.',
'required' => TRUE,
),
'dest_dsids' => array(
'description' => 'Optional. A comma-separated list of datastream IDs to generate. If absent, all derivatives will be generated.',
),
'skip_dsids' => array(
'description' => 'Optional. A comma-separated list of datastream IDs to skip. If absent, all derivatives will be generated.',
),
'datastreams_crud_log' => array(
'description' => 'Optional. Absolute path to the CRUD log. If present, the PID, DSID, and a message will be written to the specified file.',
),
'pause' => array(
'description' => 'Optional. Number of seconds to pause between objects.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_datastream_crud_update_object_properties'] = array(
'aliases' => array('idcrudop'),
'description' => 'Update object properties.',
'examples' => array(
'drush islandora_datastream_crud_update_object_properties --user=admin --pid_file=/tmp/update_these_objects.txt',
),
'options' => array(
'pid_file' => array(
'description' => 'Absolute path to the file that lists PIDs for objects whose properties you want to update.',
'required' => TRUE,
),
'owner' => array(
'description' => "The object's owner.",
),
'state' => array(
'description' => "The object's state (A/I/D).",
),
'update_object_label' => array(
'description' => "Include if you want to update the object's label from either a MODS or DC element.",
'value' => 'optional' ,
),
'source_dsid' => array(
'description' => "Tells the 'update_object_label' option which datastream to use as the source of the label.",
),
'source_xpath' => array(
'description' => "Xpath expression that tells the 'update_object_label' option where to find the label value. Must be wrapped in double quotation marks.",
),
'add_namespace' => array(
'description' => "Additional namespaceprefix:namespaceuri pair to register in the XPath used to get the object label.",
),
'concat_string' => array(
'description' => "String used to join repeated values retrieved by --source_xpath. Default is a single space.",
),
'datastreams_crud_log' => array(
'description' => 'Optional. Absolute path to the CRUD log. If present, the PID, old value of the property, and new value of the property will be written to the specified file.',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
return $items;
}
/**
* Fetches a list of PIDs from Solr.
*/
function drush_islandora_datastream_crud_fetch_pids() {
if (!module_exists('islandora_solr')) {
drush_set_error('SOLR_SEARCH_NOT_AVAILABLE',
dt('Sorry, Islandora Solr Search not enabled.'));
}
// If applicable, check that the specified parent object exists and
// is accessible; if it's not, exit with an error.
if (drush_get_option('is_member_of')) {
if (!islandora_object_load(drush_get_option('is_member_of'))) {
drush_set_error('PARENT_NOT_FOUND',
dt('The specified parent object (!is_member_of) is not found or is not accessible.',
array('!is_member_of' => drush_get_option('is_member_of'))));
}
}
// If applicable, check that the specified collection object exists and
// is accessible; if it's not, exit with an error. Also confirm that the
// object has the expected content model.
if (drush_get_option('collection')) {
if (!islandora_object_load(drush_get_option('collection'))) {
drush_set_error('COLLECTION_NOT_FOUND',
dt('The specified collection object (!collection) is not found or is not accessible.',
array('!collection' => drush_get_option('collection'))));
}
else {
$collection_object = islandora_object_load(drush_get_option('collection'));
$collection_cmodels = $collection_object->models;
if ($collection_cmodels[0] != 'islandora:collectionCModel') {
drush_set_error('OBJECT_IS_NOT_COLLECTION',
dt('The specified collection object (!collection) does not have a content model of islandora:collectionCModel.',
array('!collection' => drush_get_option('collection'))));
}
}
}
// Build the Solr query.
$query = '';
$query_parts = array();
if (drush_get_option('is_member_of')) {
$query_parts[] = 'RELS_EXT_isMemberOf_uri_t' . ':"' . drush_get_option('is_member_of') . '"';
}
if (drush_get_option('collection')) {
$query_parts[] = 'RELS_EXT_isMemberOfCollection_uri_t' . ':"' . drush_get_option('collection') . '"';
}
if (drush_get_option('content_model')) {
$query_parts[] = 'RELS_EXT_hasModel_uri_t' . ':' . '"info:fedora/' . drush_get_option('content_model') . '"';
}
if (drush_get_option('without_cmodel')) {
$query_parts[] = '-RELS_EXT_hasModel_uri_t' . ':' . '"info:fedora/' . drush_get_option('without_cmodel') . '"';
}
if (count($query_parts) > 1) {
$query = implode(' AND ', $query_parts);
}
else {
$query = $query_parts[0];
}
if (drush_get_option('namespace')) {
// Other options are present.
if (strlen($query)) {
$query .= ' AND PID:' . drush_get_option('namespace') . '\:*';
}
else {
// Only option is --namespace.
$query = 'PID:' . drush_get_option('namespace') . '\:*';
}
}
if (drush_get_option('with_dsid')) {
// Other options are present.
if (strlen($query)) {
$query .= ' AND fedora_datastreams_mt:' . drush_get_option('with_dsid');
}
else {
// Only option is --with_dsid.
$query = 'fedora_datastreams_mt:' . drush_get_option('with_dsid');
}
}
if (drush_get_option('without_dsid')) {
// Other options are present.
if (strlen($query)) {
$query .= ' AND -fedora_datastreams_mt:' . drush_get_option('without_dsid');
}
else {
// Only option is --without_dsid.
$query = '-fedora_datastreams_mt:' . drush_get_option('without_dsid');
}
}
// --query overrides all other options.
if (drush_get_option('solr_query')) {
$query = drush_get_option('solr_query');
}
// Get the PIDs from the Solr query.
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
$pids = islandora_datastream_crud_query_solr($query);
$filter_counter = 0;
if (!count($pids) || !$pids) {
drush_set_error('NO_PIDS_FOUND',
dt('Sorry, no PIDS were found.'));
}
else {
module_load_include('inc', 'islandora', 'includes/utilities');
foreach ($pids as $pid) {
if (islandora_namespace_accessible($pid)) {
$filter_counter++;
if (drush_get_option('pid_file')) {
file_put_contents(drush_get_option('pid_file'), $pid . PHP_EOL, FILE_APPEND);
}
else {
drush_print($pid);
}
}
}
if (drush_get_option('pid_file')) {
drush_log(dt("PIDS written to file !pid_file", array('!pid_file' => drush_get_option('pid_file'))), 'ok');
}
$restrictions = variable_get('islandora_namespace_restriction_enforced', FALSE);
if ($restrictions == TRUE) {
print($filter_counter . t(" namespace-accessible objects found.\n"));
}
}
}
/**
* Iterates through the list of PIDs and fetches the content of a datastream.
*
* This is the 'Read' function in CRUD.
*/
function drush_islandora_datastream_crud_fetch_datastreams() {
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
$dsid = (drush_get_option('dsid'));
$datastreams_directory = drush_get_option('datastreams_directory');
$pid_file = drush_get_option('pid_file');
$filename_separator = drush_get_option('filename_separator', '_');
$prohibited_filename_separators = array('/', '*', ':');
if (in_array($filename_separator, $prohibited_filename_separators)) {
drush_user_abort(dt('Exiting, ":", "*", and "/" are not allowed as the filename separator.'));
exit;
}
if (file_exists($datastreams_directory)) {
if (!drush_confirm(dt("Datastreams directory !dir already exists. Any " .
"datastream files from the same object PID/datastream ID combination " .
"that already exist in it will be overwritten. Do you want to want to " .
"continue?",
array('!dir' => $datastreams_directory)))) {
drush_user_abort(dt('Exiting, no datastreams fetched.'));
exit;
}
}
else {
if (drush_confirm(dt("Datastreams directory !dir does not exist. Do you " .
"want to create it and continue?",
array('!dir' => $datastreams_directory)))) {
mkdir($datastreams_directory);
}
else {
drush_user_abort(dt('Exiting, no datastreams fetched.'));
exit;
}
}
$pids = islandora_datastream_crud_read_pid_file(drush_get_option('pid_file'));
if (!count($pids)) {
drush_set_error('NO_PIDS_IN_PID_FILE',
dt('The specified PID file (!pid_file) contains no PIDS.',
array('!pid_file' => drush_get_option('pid_file'))));
}
foreach ($pids as $pid) {
if (drush_get_option('pause')) {
sleep(drush_get_option('pause'));
}
if (islandora_datastream_crud_retrieve_datastream($pid, $dsid, $datastreams_directory)) {
drush_log(dt("!dsid datastream for object !pid retrieved", array('!dsid' => $dsid, '!pid' => $pid)), 'ok');
}
else {
drush_log(dt("Datastream !dsid not retrieved for object !pid",
array('!dsid' => $dsid, '!pid' => $pid)), 'warning');
}
}
}
/**
* Ingests the datastream files into the repository.
*
* This is the 'Update' function in CRUD, and
* is also part of the 'Create' function in CRUD.
*/
function drush_islandora_datastream_crud_push_datastreams() {
if (drush_get_option('datastreams_revert')) {
if (!drush_confirm(dt("You are about to push datastreams to objects " .
"in your repository. This will revert old versions of the datastreams. " .
"Do you want to want to continue?",
array()))) {
drush_user_abort(dt('Exiting, no datastreams pushed.'));
exit;
}
}
else {
if (!drush_confirm(dt("You are about to push datastreams to objects " .
"in your repository. This will create new versions of the datastreams, or " .
"create new datastreams if none exist. Do you want to want to continue?",
array()))) {
drush_user_abort(dt('Exiting, no datastreams pushed.'));
exit;
}
}
$source_directory = drush_get_option('datastreams_source_directory');
if (!file_exists($source_directory)) {
drush_set_error('DATASTREAM_SOURCE_DIRECTORY_NOT_FOUND',
dt('The specified datastream source directory (!dir) does not exist.',
array('!dir' => $source_directory)));
}
$filename_separator = drush_get_option('filename_separator', '_');
$prohibited_filename_separators = array('/', '*', ':');
if (in_array($filename_separator, $prohibited_filename_separators)) {
drush_user_abort(dt('Exiting, ":", "*", and "/" are not allowed as the filename separator.'));
exit;
}
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
// If --pid_file was specified, only push to objects that have a PID there.
if (drush_get_option('pid_file')) {
$pid_file = drush_get_option('pid_file');
$pids = islandora_datastream_crud_read_pid_file($pid_file);
if (!count($pids)) {
drush_set_error('NO_PIDS_IN_PID_FILE',
dt('The specified PID file (!pid_file) contains no PIDS.',
array('!pid_file' => $pid_file)));
}
}
// We only provide the option to generate DC from datastream files that
// end in .xml. We need to get one such fileaname so we can determine
// which DSID we are generating the DC from.
$regenerate_dc = FALSE;
$xml_files = array_slice(array_filter(glob($source_directory .
DIRECTORY_SEPARATOR . '*.xml'), 'is_file'), 0, 1);
if (count($xml_files) === 1) {
list($pid, $dsid) = islandora_datastream_crud_parse_dsfilename($xml_files[0]);
// list($namespace, $restofthepid, $dsid) = explode($filename_separator, pathinfo($xml_files[0], PATHINFO_FILENAME));
if (drush_get_option('update_dc', 1)) {
if (drush_confirm(dt("Do you want to update each object's DC datastream using the new !dsid?",
array('!dsid' => $dsid)))) {
$regenerate_dc = TRUE;
}
}
}
foreach (glob("$source_directory/*") as $filepath) {
list($pid, $dsid) = islandora_datastream_crud_parse_dsfilename($filepath);
if (drush_get_option('pause')) {
sleep(drush_get_option('pause'));
}
if (drush_get_option('pid_file')) {
if ($pid) {
if (in_array($pid, $pids)) {
islandora_datastream_crud_push_datastream($filepath, $regenerate_dc);
}
}
else {
continue;
}
}
else {
islandora_datastream_crud_push_datastream($filepath, $regenerate_dc);
}
}
}
/**
* Deletes datastreams from objects.
*
* This is the 'Delete' function in CRUD.
*/
function drush_islandora_datastream_crud_delete_datastreams() {
$dsid = drush_get_option('dsid');
$versions = drush_get_option('versions');
if ($dsid == 'DC') {
drush_user_abort(dt('DC is a required datastream and cannot be deleted.'));
exit;
}
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
if(!is_null($versions)) {
$confirm = dt("You are about to delete (purge) versions !versions from the !dsid " .
"datastream from a set of objects in your repository. This action " .
"cannot be undone. Do you want to continue?",
array('!dsid' => drush_get_option('dsid'), '!versions' => $versions));
$versions = islandora_datastream_crud_parse_versions_option($versions);
}
else {
$confirm = dt("You are about to delete (purge) the !dsid " .
"datastream from a set of objects in your repository. This action " .
"cannot be undone. Do you want to continue?",
array('!dsid' => drush_get_option('dsid')));
}
if (!drush_confirm($confirm)) {
drush_user_abort(dt('Exiting, no datastreams deleted.'));
exit;
}
$pid_file = drush_get_option('pid_file');
$pids = islandora_datastream_crud_read_pid_file($pid_file);
if (!count($pids)) {
drush_set_error('NO_PIDS_IN_PID_FILE',
dt('The specified PID file (!pid_file) contains no PIDS.',
array('!pid_file' => $pid_file)));
}
foreach ($pids as $pid) {
if (drush_get_option('pause')) {
sleep(drush_get_option('pause'));
}
islandora_datastream_crud_delete_datastream($pid, $dsid, $versions);
}
}
/**
* Regenerates derivative datastreams.
*/
function drush_islandora_datastream_crud_generate_derivatives() {
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
$pid_file = drush_get_option('pid_file');
$pids = islandora_datastream_crud_read_pid_file($pid_file);
$dsid = drush_get_option('source_dsid');
if (!drush_confirm(dt("You are about to regenerate derivatives from the " .
"!dsid datastream for !num object(s) in your repository. This action " .
"cannot be undone. Do you want to want to continue?",
array('!num' => count($pids), '!dsid' => $dsid)))) {
drush_user_abort(dt('Exiting, no derivatives generated.'));
exit;
}
module_load_include('inc', 'islandora', 'includes/derivatives');
if (!count($pids)) {
drush_set_error('NO_PIDS_IN_PID_FILE',
dt('The specified PID file (!pid_file) contains no PIDS.',
array('!pid_file' => $pid_file)));
}
foreach ($pids as $pid) {
if (drush_get_option('pause')) {
sleep(drush_get_option('pause'));
}
// Load the object.
if (!$object = islandora_object_load($pid)) {
drush_set_error('IBD_OBJECT_NOT_FOUND',
dt('The specified object (!pid) does not exist or is not accessible.',
array('!pid' => $pid)));
continue;
}
// Regenerate derivatives.
drush_log(dt('Please be patient, generating derivatives from the !dsid datastream for !pid',
array('!dsid' => $dsid, '!pid' => $pid)), 'ok');
$options = array('force' => TRUE, 'source_dsid' => $dsid);
$messages = islandora_do_derivatives($object, $options);
// Print the output from the derivative generation functions.
foreach ($messages as $message) {
if ($message['success']) {
foreach ($message['messages'] as $ds_message) {
drush_log(strip_tags(dt($ds_message['message'], (array) $ds_message['message_sub'])), 'ok');
}
}
else {
foreach ($message['messages'] as $ds_message) {
drush_set_error('DERIVATIVE_NOT_GENERATED', strip_tags(dt($ds_message['message'], (array) $ds_message['message_sub'])));
}
}
}
}
}
/**
* Updates object properties.
*/
function drush_islandora_datastream_crud_update_object_properties() {
module_load_include('inc', 'islandora_datastream_crud', 'includes/utilities');
$pid_file = drush_get_option('pid_file');
$pids = islandora_datastream_crud_read_pid_file($pid_file);
$confirmation_message = '';
$props = array();
if (drush_get_option('owner')) {
$props['owner'] = drush_get_option('owner');
$confirmation_message = 'owner, ';
}
if (drush_get_option('state')) {
$props['state'] = drush_get_option('state');
$confirmation_message .= 'state, ';
}
if (drush_get_option('update_object_label')) {
$confirmation_message .= 'label';
}
$confirmation_message = rtrim($confirmation_message, " ,");
if (!drush_confirm(dt("You are about to change the following properties" .
" of !num objects in your repository: !properties. This action" .
" cannot be undone. Do you want to want to continue?",
array('!num' => count($pids), '!properties' => $confirmation_message)))) {
drush_user_abort(dt('Exiting, no objects updated.'));
exit;
}
if (!count($pids)) {
drush_set_error('NO_PIDS_IN_PID_FILE',
dt('The specified PID file (!pid_file) contains no PIDS.',
array('!pid_file' => $pid_file)));
}
foreach ($pids as $pid) {
if (drush_get_option('pause')) {
sleep(drush_get_option('pause'));
}
// Load the object.
if (!$object = islandora_object_load($pid)) {
drush_set_error('IBD_OBJECT_NOT_FOUND',
dt('The specified object (!pid) does not exist or is not accessible.',
array('!pid' => $pid)));
continue;
}
// Update the object's properties.
islandora_datastream_crud_update_object_properties($object, $props);
}
}
/**
* Validate function for islandora_datastream_crud_update_object_properties().
*/
function drush_islandora_datastream_crud_update_object_properties_validate() {
if (drush_get_option('state') &&
!in_array(drush_get_option('state'), array('A', 'I', 'D'))) {
drush_set_error('INVALID_OBJECT_STATE_PROVIDED', NULL, dt("Value of the --state option must be one of 'A', 'I', or 'D'. "));
}
}
/**
* Implements hook_islandora_derivative_alter().
*/
function islandora_datastream_crud_islandora_derivative_alter(&$derivatives, AbstractObject $object) {
if (drush_get_option('no_derivs', FALSE)) {
$derivatives = array();
}
if (drush_get_option('dest_dsids', FALSE)) {
$dsid_list = drush_get_option('dest_dsids');
$dsid_list = rtrim($dsid_list, ',');
$dsids = explode(',', $dsid_list);
foreach ($dsids as &$dsid) {
$dsid = trim($dsid);
}
foreach ($derivatives as $key => $derivative) {
if (!in_array($derivative['destination_dsid'], $dsids)) {
unset($derivatives[$key]);
}
}
}
if (drush_get_option('skip_dsids', FALSE)) {
$dsid_list = drush_get_option('skip_dsids');
$dsid_list = rtrim($dsid_list, ',');
$dsids = explode(',', $dsid_list);
foreach ($dsids as &$dsid) {
$dsid = trim($dsid);
}
foreach ($derivatives as $key => $derivative) {
if (in_array($derivative['destination_dsid'], $dsids)) {
unset($derivatives[$key]);
}
}
}
}