-
Notifications
You must be signed in to change notification settings - Fork 3
/
uofm_maintenance.module
387 lines (370 loc) · 12.4 KB
/
uofm_maintenance.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
<?php
/**
* @file
* Contains hooks and functions
*/
/**
* Fix tiffs queue
*/
define('UOFM_MAINTENANCE_FIX_TIFFS_QUEUE', 'uofm_maintenance_fix_tiff_mimetype');
define('UOFM_MAINTENANCE_FIX_TIFFS_TIMER', UOFM_MAINTENANCE_FIX_TIFFS_QUEUE);
/**
* Regenerate JP2 datastream queue and timer
*/
define('UOFM_MAINTENANCE_REGEN_JP2_QUEUE', 'uofm_maintenance_regenerate_jp2');
define('UOFM_MAINTENANCE_REGEN_JP2_TIMER', UOFM_MAINTENANCE_REGEN_JP2_QUEUE);
/**
* Batch index queue
*/
define('UOFM_MAINTENANCE_BATCH_INDEX_QUEUE', 'uofm_maintenance_batch_reindex');
/**
* Derivative Fixer queue and timer.
*/
define('UOFM_MAINTENANCE_DERIVATIVE_FIXER_QUEUE', 'uofm_maintenance_derivative_fixer');
define('UOFM_MAINTENANCE_DERIVATIVE_FIXER_TIMER', UOFM_MAINTENANCE_DERIVATIVE_FIXER_QUEUE);
/**
* Update parent queue.
*/
define('UOFM_MAINTENANCE_UPDATE_PARENT_QUEUE', 'uofm_maintenance_update_parent');
/**
* Propogate XACML.
*/
define('UOFM_MAINTENANCE_COPY_XACML_QUEUE', 'uofm_maintenance_copy_xacml');
/**
* Correct any incorrect image/tiff mimetypes.
*
* @param string|AbstractObject $object
* the string PID or a Fedora Object
*/
function uofm_maintenance_fix_tiff_worker($object) {
$obj = FALSE;
if (is_string($object)) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($object)) {
$obj = islandora_object_load($object);
}
}
if (is_a($object, 'AbstractObject')) {
$obj = $object;
}
if (isset($obj['OBJ']) && $obj['OBJ']->mimetype == "image/tif") {
$obj['OBJ']->mimetype = "image/tiff";
}
if (isset($obj['TIFF']) && $obj['TIFF']->mimetype == "image/tif") {
$obj['TIFF']->mimetype = "image/tiff";
}
return TRUE;
}
/**
* Regenerate JP2 derivative from OBJ/TIFF.
*
* @param string|AbstractObject $object
* the string PID or a Fedora Object
*/
function uofm_maintenance_regen_jp2_worker($object) {
$obj = FALSE;
if (is_string($object)) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($object)) {
$obj = islandora_object_load($object);
}
}
if (is_a($object, 'AbstractObject')) {
$obj = $object;
}
if (isset($obj['OBJ']) || isset($obj['TIFF'])) {
if ((isset($obj['OBJ']) && isset($obj['JP2']) && ($obj['OBJ']->size * 0.001) > $obj['JP2']->size) ||
(isset($obj['TIFF']) && isset($obj['JP2']) && ($obj['TIFF']->size * 0.001) > $obj['JP2']->size)) {
module_load_include('inc', 'islandora_large_image', 'includes/derivatives');
drush_log("Regenerate JP2 for " . $obj->id, "ok");
islandora_large_image_create_jp2_derivative($obj);
}
}
return TRUE;
}
/**
* Custom queue generation for derivative fixer.
*
* @var $pid string
* An object pid to process
* @return array
* An array for the derivative fixer worker.
*/
function uofm_maintenance_derivative_fixer_queue($pid) {
$force = drush_get_option('force', NULL);
$item = array(
'pid' => $pid,
'force' => (bool) (!is_null($force)),
);
if (!is_null(drush_get_option('source_dsid', NULL))) {
$item['source_dsid'] = drush_get_option('source_dsid', '');
}
if (!is_null(drush_get_option('destination_dsid', NULL))) {
$item['destination_dsid'] = drush_get_option('destination_dsid', '');
}
return $item;
}
/**
* Generate any missing derivatives.
*
* @param array $data
* array with keys 'pid' and 'force'
*/
function uofm_maintenance_derivative_fixer_worker($data) {
$obj = FALSE;
if (array_key_exists('pid', $data) && is_string($data['pid'])) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($data['pid'])) {
$obj = islandora_object_load($data['pid']);
}
}
if ($obj) {
module_load_include('inc', 'islandora', 'includes/derivatives');
$options = array_diff_key($data, array('pid' => 1));
if (!isset($options['force']) || (!is_bool($options['force']))) {
$options['force'] = FALSE;
}
$results = islandora_do_derivatives($obj, $options);
islandora_derivative_logging($results);
return TRUE;
}
return FALSE;
}
/**
* Custome queue item builder.
*/
function uofm_maintenance_update_parent_custom_queue($pid) {
$relation_uri = drush_get_option('parent_relationship_uri', 'info:fedora/fedora-system:def/relations-external#');
$relation_pred = drush_get_option('parent_relationship_pred', 'isMemberOf');
$old_parent = drush_get_option('old_parent', NULL);
$new_parent = drush_get_option('new_parent', NULL);
$update_xacml = (bool) (!is_null(drush_get_option('update_xacml', NULL)));
return array(
'relation_uri' => $relation_uri,
'relation_pred' => $relation_pred,
'old_parent' => $old_parent,
'new_parent' => $new_parent,
'update_xacml' => $update_xacml,
'pid' => $pid,
);
}
/**
* Replace/add a parent to the object.
*
* @param array $data
* array with keys:
*
* 'pid' => pid to act on,
* 'relation_uri' => relation URI to use,
* 'relation_pred' => relation predicate to use,
* 'old_parent' => parent to replace (if exists),
* 'new_parent' => parent to add,
* 'update_xacml' => copy POLICY from the new parent,
*/
function uofm_maintenance_update_parent_worker($data) {
$key_template = array(
'pid' => 'test',
'relation_uri' => 'test',
'relation_pred' => 'test',
'new_parent' => 'test',
'update_xacml' => TRUE,
);
if (count(array_intersect_key($data, $key_template)) != 5) {
drush_log("Invalid array passed to the update_parent_worker", "error");
return FALSE;
}
$pid = $data['pid'];
if (is_string($pid)) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($pid)) {
$object = islandora_object_load($pid);
}
}
if (!is_a($object, 'AbstractObject')) {
drush_log("Did not load object properly in update_parent_worker", "error");
return FALSE;
}
$new_parent = $data['new_parent'];
if (!(strpos($new_parent, "info:fedora/") == 0)) {
$new_parent = "info:fedora/$new_parent";
}
if (isset($data['old_parent'])) {
$old_parent = $data['old_parent'];
if (!(strpos($old_parent, "info:fedora/") == 0)) {
$old_parent = "info:fedora/$old_parent";
}
if (count($object->relationships->get($data['relation_uri'], $data['relation_pred'])) > 0) {
$object->relationships->remove($data['relation_uri'], $data['relation_pred'], $old_parent);
}
}
$object->relationships->add($data['relation_uri'], $data['relation_pred'], $new_parent);
$result = TRUE;
if (module_exists('islandora_xacml_editor') && $data['update_xacml']) {
$result = uofm_maintenance_update_xacml_worker(array('from' => $data['new_parent'], 'to' => $pid));
}
return $result;
}
/**
* XACML queue custom builder.
*
* @param string $pid
* the PID for this action.
*/
function uofm_maintenance_copy_xacml_custom_queue($pid) {
$from = drush_get_option('from_pid', NULL);
if (is_null($from)) {
drush_set_error("--from_pid must be set.");
}
return array('to' => $pid, 'from' => $from);
}
/**
* Copies XACML policy from one object to another.
*
* @param array $data
* array with keys ('from' and 'to') and values as the objects or PIDs.
* @return bool
* Was it successful.
*/
function uofm_maintenance_update_xacml_worker($data) {
if (!module_exists('islandora_xacml_editor')) {
return FALSE;
}
if (!is_array($data) || !array_key_exists('from', $data) || !array_key_exists('to', $data)) {
return FALSE;
}
$from = $data['from'];
$to = $data['to'];
$loader = function($object) {
if (is_string($object)) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($object)) {
return islandora_object_load($object);
}
}
if (is_a($object, 'AbstractObject')) {
return $object;
}
};
$from_obj = $loader($from);
if (!isset($from_obj['POLICY'])) {
return FALSE;
}
module_load_include('inc', 'islandora_xacml_editor', 'includes/batch');
$to_obj = $loader($to);
$xml = $from_obj['POLICY']->content;
$policy_update = new IslandoraUpdatePolicy($to_obj->id, $xml);
$success = $policy_update->updatePolicy();
return $success;
}
/**
*
* Fix dates for faceting.
*
* Copy full mods:subject/mods:temporal dates, if YYY? make YYY5
*
* @param $pid string
* The pid of the object to process
*
* return boolean
* Did it succeed?
*/
function uofm_maintenance_fix_dates_worker($pid) {
if (is_string($pid)) {
module_load_include('inc', 'islandora', 'includes/utilities');
if (islandora_is_valid_pid($pid)) {
$object = islandora_object_load($pid);
if (isset($object['MODS'])) {
$mods = new DOMDocument();
try {
$mods->loadXML($object['MODS']->content);
}
catch (Exception $e) {
drush_log(t("Error loading MODS for object @p: @m", array('@p' => $pid, '@m' => $e->getMessage())), 'warning');
return FALSE;
}
$xpath = new DOMXpath($mods);
$xpath->registerNamespace('mods', 'http://www.loc.gov/mods/v3');
$mods_top = $xpath->query('/mods:mods');
$mods_top = $mods_top->item(0);
$dates = $xpath->query('//mods:subject/mods:temporal');
$adds = array();
$today = new DateTime();
foreach ($dates as $date) {
$text = $date->textContent;
$date_string = FALSE;
// Match the date, if it is only to the decade make it a 5.
// If the full date is there, use it otherwise append -01-01.
if (preg_match('/^([0-9]{2,3}\?{1,2}|[0-9]{4})([\-\/][0-9]{1,2})?([\-\/][0-9]{1,2})?/', $text, $matches)) {
if (strpos($matches[1], '??') === FALSE) {
$date_string = str_replace('?', 5, $matches[1]) . (isset($matches[2]) ? $matches[2] : '-01') . (isset($matches[3]) ? $matches[3] : '-01');
}
}
// Match MM/DD/YYYY
elseif (preg_match('/^([0-9\?]{1,2})\/([0-9\?]{1,2})\/([0-9\?]{4})$/', $text, $matches)) {
if (strpos($matches[3], '??') === FALSE) {
$date_string = str_replace('?', 5, $matches[3]) . "-" . (strpos($matches[1], '?') === FALSE ? $matches[1] : '01') . "-" . (strpos($matches[2], '?') === FALSE ? $matches[2] : '01');
}
}
// Match ca.YYYY
elseif (preg_match('/^ca\.?\s*([0-9]{2,3}\?{1,2}|[0-9]{4})/i', $text, $matches)) {
if (strpos($matches[1], '??') === FALSE) {
$date_string = str_replace('?', 5, $matches[1]) . "-01-01";
}
}
// Must have at least one digit or skip it.
else if (preg_match('/\d/', $text)){
// Try to parse it to a DateTime.
// Remove commas because of 11 January, 1929 format.
$date_string = str_replace(',', '', $text);
}
if ($date_string) {
try {
$dt = new DateTime($date_string);
if ($dt->format('Y-m-d') != $today->format('Y-m-d')) {
$adds[] = '<dateCreated>' . $dt->format('Y-m-d') . '</dateCreated>';
}
}
catch (Exception $e) {
drush_log(t('Could not parse (@t) for object (@p): @e', array(
'@p' => $pid,
'@t' => $text,
'@e' => $e->getMessage(),
)), 'error');
}
}
}
// There is a change so update MODS and save back.
if (count($adds) > 0) {
$ext = $xpath->query('//mods:extension');
$extension = '<extension xmlns="http://www.loc.gov/mods/v3">' . implode($adds, ' ') . '</extension>';
$new_node = dom_import_simplexml(simplexml_load_string($extension));
$new_node = $mods->importNode($new_node, TRUE);
if ($ext->length > 0) {
$mods_top->replaceChild($new_node, $ext->item(0));
}
else {
$mods_top->appendChild($new_node);
}
$new_xml = $mods->saveXML();
$object['MODS']->content = $new_xml;
}
else {
drush_log(t("No date information to add for object @p", array('@p' => $pid)), "notice");
}
return TRUE;
}
else {
drush_log(t("No MODS datastream for object @p", array('@p' => $pid)), "notice");
return TRUE;
}
}
else {
drush_log(t("@p is not a valid pid", array('@p' => $pid)), "error");
return FALSE;
}
}
else {
drush_log("Did not receive a string pid", "error");
return FALSE;
}
}