-
Notifications
You must be signed in to change notification settings - Fork 0
/
idu.module
413 lines (355 loc) · 13.5 KB
/
idu.module
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
<?php
/**
* Implements hook_init.
*
* @return type
*/
function idu_init( ) {
// module_load_include('inc', 'idu', 'idu_constants');
// module_load_include('inc', 'repository_control');
// module_load_include('inc', 'repository_control', 'log_record');
// module_load_include('inc', 'repository_control', 'csv_cells');
// module_load_include('inc', 'repository_control', 'object_record');
// module_load_include('inc', 'phpexcel');
if (module_exists('islandora_paged_content')) {
module_load_include('inc', 'islandora_paged_content', 'includes/utilities');
module_load_include('inc', 'islandora_paged_content', 'includes/manage_pages');
}
if (module_exists('islandora_audio')) { module_load_include('inc', 'islandora_audio', 'includes/derivatives'); }
if (module_exists('islandora_fits')) { module_load_include('inc', 'islandora_fits', 'includes/derivatives'); }
if (module_exists('islandora_basic_image')) { module_load_include('inc', 'islandora_basic_image', 'includes/derivatives'); }
if (module_exists('islandora_large_image')) { module_load_include('inc', 'islandora_large_image', 'includes/derivatives'); }
if (module_exists('islandora_pdf')) { module_load_include('inc', 'islandora_pdf', 'includes/derivatives'); }
return;
}
/**
* Implements hook_help.
*
* @param type $path
* @param type $arg
* @return type
*/
function idu_help($path, $arg) {
if ($path == 'admin/help#idu') {
return t('Islandora Drush Utilities provides a suite of commands and options to help admins manage Fedora objects and their datastreams.');
}
}
/**
* Implements hook form.
*
* @param type $form
* @param type $form_state
* @return type
*/
function idu_main_form($form, &$form_state) {
if (!isset($form_state['storage']['confirm'])) {
$form['operation'] = array(
'#type' => 'select',
'#title' => t('Select the operation to be performed.'),
'#default_value' => variable_get('icg_operation', 'HEALTH_CHECK'),
'#options' => array(
'OPEN_REPORT' => t(' OPEN_REPORT - Present a link to download / open the specified XLSX health check report. '),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} else { // The confirmation (secondary) form.
$op = $form_state['values']['operation'];
$default_file = variable_get('icg_xlsxFile', "public://HEALTH_CHECK.xlsx");
if ($op === 'OPEN_REPORT') {
$form['xlsxFile'] = array(
'#type' => 'textfield',
'#title' => t('Enter the name of the existing XLSX file to open. '),
'#default_value' => $default_file,
'#size' => 80,
'#maxlength' => 80,
);
} else {
$form['first'] = array(
'#type' => 'textfield',
'#title' => t('Enter the first PID number to process.'),
'#default_value' => variable_get('icg_first', '0'),
'#size' => 10,
'#maxlength' => 10,
);
$form['last'] = array(
'#type' => 'textfield',
'#title' => t('Enter the last PID number to process.'),
'#default_value' => variable_get('icg_last', '10000'),
'#size' => 10,
'#maxlength' => 10,
);
$form['inactives'] = array(
'#type' => 'checkbox',
'#title' => t('Check here if you want to include inactive and empty (deleted) objects.'),
'#default_value' => variable_get('icg_inactives', FALSE),
);
}
}
$alias = variable_get('icg_repoAlias');
$namespace = variable_get('icg_namespace');
return confirm_form($form, " Operation '$op' - Proceed with the specified inputs and the '$namespace' namespace of '$alias'? ", 'idu', ' This action cannot be undone. ', $op, 'Cancel');
}
/**
* Implements hook_form_validate.
*
* @param type $form
* @param type $form_state
* @return type
*/
function idu_main_form_validate($form, &$form_state) {
return;
}
/**
* Implements hook_form_submit.
*
* @param type $form
* @param type $form_state
* @return type
*/
function idu_main_form_submit($form, &$form_state) {
// If confirm is NOT set, this will cause the form to be rebuilt, entering the confirm part.
if (!isset($form_state['storage']['confirm'])) {
$form_state['storage']['confirm'] = TRUE;
$form_state['storage']['original_form'] = $form_state['values'];
$form_state['rebuild'] = TRUE;
} else { // Do real form submittal.
drupal_set_message(t('The Islandora Health Check main form has been submitted.'));
$main = $form_state['storage']['original_form']; // get controls from the main form
$secondary = $form_state['values']; // get controls from the secondary form
icg_set_variable('operation', $main);
icg_set_variable('first', $secondary);
icg_set_variable('last', $secondary);
icg_set_variable('inactives', $secondary);
icg_set_variable('onlyCols', $secondary);
icg_set_variable('xlsxFile', $secondary);
idu_op( ); // Distribute control to the specified $op.
}
return;
}
/**
* Implements hook_menu().
*
* @return type
*/
function idu_menu( ) {
$items = array();
$items['idu'] = array(
'title' => 'Islandora Drush Utilities',
'page callback' => 'drupal_get_form',
'page arguments' => array('idu_main_form'),
'description' => 'Islandora Drush Utilities',
'access arguments' => array('administer users'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Returns an associative array of PIDS found in the specified range.
*
* @param array $context
* Drupal batch $context array. Returns with an associative array of
* PIDnum => Title values in $context['sandbox']['objPIDs'] for objects
* found in the specified range.
* @return integer
* The PID number of the last/highest numbered PID found in the range. The
* variable icg_last is automatically set to this limiting value.
*/
function idu_getPIDs(&$context) {
$obj_pids = array();
$first = intval(variable_get('icg_first'));
$last = intval(variable_get('icg_last'));
$range = $last - $first + 1;
$largest = 0;
$ns = variable_get('icg_namespace');
// Loop through the repo looking for objects in the specified range.
for ($i=$first, $notFound=0; $i<=$last; $i++) {
$pid = $ns.$i;
try {
$object = idu_repo( )->getObject($pid);
} catch (Exception $e) {
if ($e->getCode() === 404) {
$obj_pids[$i] = '-*-Not Found-*-';
$notFound++;
} else {
drupal_set_message("PID '$pid' forced a FedoraRepository getObject( ) exception: '$e' ", 'error');
}
continue; // for any exception...skip this PID
}
$obj_pids[$i] = $object->label;
$largest = $i;
}
// If any PIDs were not found, report them.
if ($notFound > 0) {
if ($range > 10) {
drupal_set_message("A total of $notFound PIDs in the specified range were NOT FOUND.", 'warning');
}
foreach ($obj_pids as $pid => $status) {
if ($status === '-*-Not Found-*-') {
if ($range<=10 && variable_get('icg_verbose')) {
drupal_set_message("Object $pid was NOT FOUND.", 'warning');
}
unset($obj_pids[$pid]); // remove it from our list
}
}
}
// Save the found object PIDs in the batch results.
$context['results']['objectPIDs'] = $obj_pids;
idu_post($context);
variable_set('icg_last', $largest);
return $largest;
}
/**
* Open the repository for the rest of IDU to use.
*
* @global FedoraRepository $repository
* @return FedoraRepository
*/
function idu_repo( ) {
global $repository;
if (!is_null($repository)) {
return $repository;
} else if (!$repository = repository_control_connect()) {
die('Error in idu_repo( )...no connection to Fedora!');
}
return $repository;
}
/**
* Create or Modify an object handle and assign it.
*
* This function relies on constants defined in idu_constants.inc. It attempts to open a new or existing Handle system
* batch command file and appends necessary commands to this file to create or modify and assign a new handle to the target
* object. The batch file is defined in temporary://H_BATCH. This function does NOT initiate processing of the batch file,
* that must be done elsewhere.
*
* @param FedoraObject $object
* The Fedora object for which the handle is created or modified.
* @param string $op
* Set to either 'CREATE' or 'MODIFY'. If --force is specified this value may be overridden.
* @param bool $force
* If TRUE, this function will continue even if the state of the mods:identifier[@type='hdl'] element is not as expected for the specified $op.
* @return int
* Returns 0 or 1 indicating the number of handles processed.
*/
function idu_handle_create_or_modify($object, $op='CREATE', $force=FALSE) {
$pid = $object->id;
// Specify the target batch and log files in temporary://.
$fileName = 'temporary://'.H_BATCH;
$logFile = 'temporary://'.H_LOG;
// Open (or create) the batch file and log file.
$old = file_exists($fileName);
if (!$file = fopen($fileName, 'a+')) die("Could not open batch file $fileName! ");
if (!$log = fopen($logFile, 'a+')) die("Could not open log file $logFile! ");
// Open the MODS datastream, if none... done.
if (!$mods = $object['MODS']) {
fprintf($log, "Warning: Object '$pid' has NO MODS record.\n");
fclose($file);
fclose($log);
return 0;
}
// Make the MODS content searchable as a SimpleXMLElement object.
$content = $mods->content;
$xml = new SimpleXMLElement($content);
$xpath = "/mods:mods/mods:identifier[@type='hdl']";
// Does this object already have a handle?
if ($nodes = $xml->xpath($xpath)) { // Yes...
$oldHandle = (string)$nodes[0];
if ($op === 'CREATE' && !$force) {
fclose($file);
fclose($log);
return 0;
}
} else if ($op === 'MODIFY') { // No existing handle...if Modify we need to adjust or give up.
if ($force) {
$op = 'CREATE';
} else {
fprintf($log, "Warning: Object '$pid' has no assigned Handle. Use --force or 'drush iduH $pid Create' if you wish to create one.\n\n");
fclose($file);
fclose($log);
return 0;
}
}
// Appropriate handle status found. Create or modify one as needed.
list($ns, $pidn) = explode(':', $pid);
// If this is a new batch command file, print an AUTHENTICATE command block.
if (!$old) {
$cmd = "AUTHENTICATE PUBKEY:300:0.NA/".H_PREFIX." \n".H_SVRDIR."/admpriv.bin";
if (H_ADMKEY == ' ') {
$key = $keyLog = '';
} else {
$key = '|'.H_ADMKEY;
$keyLog = '|***obfuscated*keyphrase***';
}
fprintf($file, "%s%s\n\n", $cmd, $key);
fprintf($log, "%s%s\n\n", $cmd, $keyLog);
}
// Set the batch file permissions so that it can be deleted later.
$path = drupal_realpath($fileName);
if (!$ok = chmod($path, 0777)) {
die("Permissions could not be set for batch file $fileName. ");
}
// Build the object handle and handle.net URI for this object. If the namespace of the object matches the specified
// H_NUMERIC_ONLY defined parameter, then use only the numeric part of the $pid; otherwise use the entire $pid.
//
$handle = ("$ns:" === H_NUMERIC_ONLY ? $pidn : $pid);
$hdl = 'http://hdl.handle.net/'.H_PREFIX.'/'.$handle;
// Sanitize the object ID (title) for Handle! No double quotes and length no more than 120 characters.
$handle_title = str_replace('"', "", $object->label);
if (strlen($handle_title) > 117) {
$lines = explode("\n", wordwrap($handle_title, 117));
$handle_title = $lines[0] . "...";
}
// Remove all old handle identifiers and add one new HDL identifier element to the MODS.
$mods = new DOMDocument( );
$mods->loadXML($content);
$xpath = new DOMXpath($mods);
foreach ($xpath->query("/mods:mods/mods:identifier[@type='hdl']") as $node) {
$parent = $node->parentNode;
$parent->removeChild($node); // remove the old handles
}
// Add a new type='hdl' identifier.
$id = $mods->createElement('identifier', $hdl);
$mods->documentElement->appendChild($id);
$id->setAttribute('type', 'hdl');
// Remove all URI and DOI identifier elements found at the first level.
$types = array("doi", "uri");
foreach ($types as $type) {
foreach ($xpath->query("/mods:mods/mods:identifier[@type='$type']") as $node) {
$parent = $node->parentNode;
$parent->removeChild($node); // remove the old element
}
}
// If the object has a 'local' identifier, preserve it. If not, add the handle as a new local ID.
$nLocal = 0;
foreach ($xpath->query("/mods:mods/mods:identifier[@type='local']") as $node) {
$nLocal++;
}
if ($nLocal === 0) {
$id = $mods->createElement('identifier', $pid);
$mods->documentElement->appendChild($id);
$id->setAttribute('type', 'local');
}
/* Do NOT add a new type='uri' identifier to match the handle ID.
$id = $mods->createElement('identifier', $hdl);
$mods->documentElement->appendChild($id);
$id->setAttribute('type', 'uri');
*/
// Save the MODS changes.
$object['MODS']->setContentFromString($mods->saveXML());
// Add the appropriate command to the batch file.
$cmd = "$op ".H_PREFIX."/$handle";
fprintf($file, "%s\n", $cmd);
fprintf($log, "%s\n", $cmd);
$cmd = "100 HS_ADMIN 86400 1110 ADMIN 300:111111111111:0.NA/".H_PREFIX."\n";
$cmd .= "8 URL 86400 1110 UTF8 ".H_OBJECT_PATH."$pid\n";
$cmd .= "9 DESC 86400 1110 UTF8 $handle_title\n";
fprintf($file, "%s\n", $cmd);
fprintf($log, "%s\n", $cmd);
// Close the batch and log files and return 1, the number of objects processed.
fclose($file);
fclose($log);
return 1;
}