-
Notifications
You must be signed in to change notification settings - Fork 17
/
islandora_pathauto.module
450 lines (416 loc) · 13.4 KB
/
islandora_pathauto.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
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
<?php
/**
* @file
* Exposes Islandora object urls to pathauto,
* so that if a pattern is configured, then
* aliases are updated automatically on object
* ingest/modify/purge.
*
* Also enables batch creation/updates of aliases.
*/
/**
* Implements hook_menu().
*/
function islandora_pathauto_menu() {
$items['admin/islandora/tools/islandora-pathauto'] = array(
'title' => 'Pathauto',
'description' => 'Configure settings for the Islandora Pathauto module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_pathauto_admin_settings'),
'access arguments' => array('administer islandora_pathauto'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_pathauto_permission() {
return array(
'administer islandora_pathauto' => array(
'title' => t('Administer Islandora Pathauto'),
'description' => t('Select content models available for custom Islandora Pathauto patterns.'),
),
);
}
/**
* Implements hook_theme().
*/
function islandora_pathauto_theme() {
return array(
'islandora_pathauto_admin_table' => array(
'render element' => 'form',
),
);
}
/**
* Implements hook_pathauto().
*/
function islandora_pathauto_pathauto($op) {
switch ($op) {
case 'settings':
module_load_include('inc', 'islandora', 'includes/utilities');
$settings = new stdClass();
$settings->module = 'islandora';
$settings->groupheader = t('Islandora object paths');
$settings->patterndescr = t('All Islandora objects (except those with custom paths specified below)');
$settings->patterndefault = '';
$settings->token_type = 'fedora';
$settings->patternitems = array();
$settings->batch_update_callback = 'islandora_pathauto_pathauto_bulkupdate';
$all_cmodels = islandora_get_content_models();
$pathauto_cmodels = variable_get('islandora_pathauto_selected_cmodels', array());
$cmodels = array();
foreach ($all_cmodels as $key => $value) {
if (in_array($key, $pathauto_cmodels)) {
$cmodels[$key] = $value;
}
}
foreach ($cmodels as $cmodel) {
$settings->patternitems[$cmodel['pid']] = t('Custom pattern for @cmodel objects', array("@cmodel" => $cmodel['label']));
}
return $settings;
default:
break;
}
}
/**
* Wrapper to pathauto_create_alias().
*
* @param FedoraObject|IslandoraFedoraObjectReadOnly $object
* an Islandora object
*
* @param string $op
* The operation to perform: 'insert' or 'update'.
*
* @return int
* Returns the number of aliases generated from this object;
* may be >1 if the object has multiple content models.
*/
function islandora_pathauto_create_alias($object, $op) {
module_load_include('inc', 'pathauto');
$count = 0;
if (islandora_namespace_accessible($object->id)) {
$path = 'islandora/object/' . $object->id;
// Give priority to any cmodels that are enabled for pathauto.
$all_enabled = variable_get('islandora_pathauto_selected_cmodels', array());
$models = array_intersect($object->models, $all_enabled);
if (!$models) {
$models = $object->models;
}
foreach ($models as $cmodel) {
$result = pathauto_create_alias('islandora', $op, $path, array('fedora' => $object), $cmodel);
if ($result != '') {
$count++;
}
}
}
return $count;
}
/**
* Implements hook_islandora_object_ingested().
*/
function islandora_pathauto_islandora_object_ingested($object) {
islandora_pathauto_create_alias($object, 'insert');
}
/**
* Implements hook_islandora_object_alter().
*/
function islandora_pathauto_islandora_object_alter($original, $context) {
// Skip for new objects. hook_islandora_object_ingested will create the alias.
if (isset($context['action']) and $context['action'] == 'ingest') {
return;
}
// If the label is what was modified, then
// the $original does not have the new label yet.
// This is a nasty fix to get it from $context.
// See ISLANDORA-1072.
$object = new IslandoraFedoraObjectReadOnly($original);
if (isset($context['action']) and $context['action'] == 'modify') {
if (isset($context['params']['label'])) {
$object->label = $context['params']['label'];
}
}
islandora_pathauto_create_alias($object, 'update');
}
/**
* Implements hook_islandora_object_purged().
*/
function islandora_pathauto_islandora_object_purged($pid) {
pathauto_path_delete_all('islandora/object/' . $pid);
}
/**
* Batch operation for generating aliases for Islandora objects.
*
* @param bool $use_cron_last
* Flag for using cron last time.
* @param array $context
* Batch context.
*/
function islandora_pathauto_bulk_batch_process($use_cron_last = FALSE, &$context = array()) {
module_load_include('inc', 'islandora', 'includes/utilities');
$sandbox = &$context['sandbox'];
$args = array(
'!filters' => '',
'!limit' => '',
'!md_name' => '',
'!md_field' => '',
);
if (empty($sandbox)) {
$sandbox['total'] = 0;
$sandbox['total_alias'] = 0;
$sandbox['total_processed'] = 0;
$sandbox['filters'] = array();
$sandbox['filters']['model'] = '';
$sandbox['models'] = array();
$settings = islandora_pathauto_pathauto('settings');
$pattern_all_objects = variable_get('pathauto_' . $settings->module . '_pattern', $settings->patterndefault);
// Determine what content models have a pattern and
// only query objects with those particular models.
if (!empty($pattern_all_objects)) {
$sandbox['models'][] = 'fedora-system:FedoraObject-3.0';
}
elseif (isset($settings->patternitems)) {
foreach ($settings->patternitems as $model => $label) {
$pattern = variable_get('pathauto_' . $settings->module . '_' . $model . '_pattern', '');
if (!empty($pattern)) {
$sandbox['models'][] = $model;
}
}
}
if (empty($sandbox['models'])) {
drupal_set_message(t('No patterns exist for Islandora objects.'), 'warning');
$context['finished'] = 1;
return;
}
$model_filters = $sandbox['models'];
foreach ($model_filters as &$filter) {
$filter = "sameTerm(?model, <info:fedora/$filter>)";
}
$sandbox['filters']['model'] = format_string('FILTER(!models)', array('!models' => implode(' || ', $model_filters)));
if (variable_get('islandora_namespace_restriction_enforced', FALSE)) {
$namespace_array = islandora_get_allowed_namespaces();
$namespace_sparql = implode('|', $namespace_array);
$sandbox['filters']['ns'] = format_string('FILTER(regex(str(?object), "info:fedora/(!namespaces):"))', array('!namespaces' => $namespace_sparql));
}
}
if ($use_cron_last) {
$args['!md_name'] = '?md';
$args['!md_field'] = '<info:fedora/fedora-system:def/view#lastModifiedDate> ?md ;';
if (!isset($sandbox['filters']['cron_last'])) {
$modified_date_time = gmdate("Y-m-d\TH:i:s\Z", variable_get('cron_last'));
$sandbox['filters']['cron_last'] = "FILTER(?md > '{$modified_date_time}'^^xsd:dateTime)";
}
}
$query = <<<EOQ
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?object ?cd !md_name
FROM <#ri>
WHERE {
?object <info:fedora/fedora-system:def/model#createdDate> ?cd ;
!md_field
<fedora-model:hasModel> ?model ;
<fedora-model:state> <fedora-model:Active> .
!filters
}
ORDER BY ?cd
!limit
EOQ;
$tuque = islandora_get_tuque_connection();
if ($tuque) {
$context['finished'] = 0;
if ($sandbox['total'] == 0) {
$args['!filters'] = implode(' ', $sandbox['filters']);
$query_string = format_string($query, $args);
$sandbox['total'] = $tuque->repository->ri->countQuery($query_string, 'sparql');
if ($sandbox['total'] == 0) {
$context['finished'] = 1;
drupal_set_message(t('No objects found for selected models (@models).', array('@models' => implode(', ', $sandbox['models']))));
return t('No objects found.');
}
}
if (isset($sandbox['cd'])) {
$sandbox['filters']['cd'] = format_string('FILTER(?cd > "!created"^^xsd:dateTime || (?cd = "!created"^^xsd:dateTime && xsd:string(?pid) > "info:fedora/!pid"^^xsd:string))', array(
'!created' => $sandbox['cd'],
'!pid' => $sandbox['pid'],
));
}
$args['!filters'] = implode(' ', $sandbox['filters']);
$args['!limit'] = "LIMIT 50";
$query_string = format_string($query, $args);
$results = $tuque->repository->ri->sparqlQuery($query_string);
if (empty($results)) {
$context['finished'] = 1;
}
else {
$sandbox['total_processed'] += count($results);
foreach ($results as $result) {
$pid = $result['object']['value'];
$sandbox['cd'] = $result['cd']['value'];
$sandbox['pid'] = $pid;
$object = islandora_object_load($pid);
if ($object) {
$alias = islandora_pathauto_create_alias($object, 'bulkupdate');
if ($alias != "") {
$sandbox['total_alias']++;
}
}
}
}
}
$context['message'] = t('Processed @total_processed of @total. Added @total_alias aliases.', array(
'@total_processed' => $sandbox['total_processed'],
'@total_alias' => $sandbox['total_alias'],
'@total' => $sandbox['total']));
if ($context['finished'] == 1) {
drupal_set_message(t('Added @total_alias aliases on @total_processed PIDs.', array('@total_alias' => $sandbox['total_alias'], '@total_processed' => $sandbox['total_processed'])));
}
}
/**
* Implements hook_path_alias_types().
*/
function islandora_pathauto_path_alias_types() {
$objects['islandora/object/'] = t('Islandora');
return $objects;
}
/**
* Implements hook_token_info().
*/
function islandora_pathauto_token_info() {
$info['types']['fedora'] = array(
'name' => 'Fedora object tokens',
'description' => t('Tokens for islandora relating to fedora objects'),
'needs-data' => 'fedora',
);
$info['tokens']['fedora']['pid'] = array(
'name' => t('Object PID'),
'description' => t('Full PID of object in Fedora repository'),
);
$info['tokens']['fedora']['label'] = array(
'name' => t('Object label'),
'description' => t('Fedora object label'),
);
$info['tokens']['fedora']['shortpid'] = array(
'name' => t('Short PID'),
'description' => t('Fedora object pid without namespace'),
);
$info['tokens']['fedora']['namespace'] = array(
'name' => t('Namespace'),
'description' => t('Fedora object namespace'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function islandora_pathauto_tokens($type, $tokens, array $data = array(), array $options = array()) {
if ($type == 'fedora' & !empty($data['fedora'])) {
$object = $data['fedora'];
$replacements = array();
foreach ($tokens as $name => $original) {
if ($name == 'pid') {
$replacements[$original] = $object->id;
}
if ($name == 'shortpid') {
$temp = explode(':', $object->id, 2);
$replacements[$original] = $temp[1];
}
if ($name == 'namespace') {
$temp = explode(':', $object->id, 2);
$replacements[$original] = $temp[0];
}
if ($name == 'label') {
$replacements[$original] = $object->label;
}
}
return $replacements;
}
return array();
}
class IslandoraFedoraObjectReadOnly implements ArrayAccess {
private $fake;
private $savedLabel;
/**
* IslandoraFedoraObjectReadOnly constructor.
*
* @param FedoraObject $original
* The FedoraObject for which this class is a wrapper.
*/
public function __construct($original) {
$this->fake = $original;
$this->savedLabel = $original->label;
}
/**
* Overload assignment to set label and nothing else.
*
* @param string $name
* The name of the property being set. Only 'label' will have any effect.
* @param mixed $value
* The value to which the property should be set.
*/
public function __set($name, $value) {
if ($name == 'label') {
$this->savedLabel = $value;
}
}
/**
* Overload get to pull values from original object (except for label).
*
* @param string $name
* The name of the property to get.
*
* @return string|void
* Returns the value of the property.
*/
public function __get($name) {
if ($name == 'label') {
return $this->savedLabel;
}
return $this->fake->__get($name);
}
/**
* Implements ArrayAccess.
*/
public function offsetExists($offset) {
return isset($this->fake[$offset]);
}
/**
* Implements ArrayAccess.
*/
public function offsetGet($offset) {
return $this->fake[$offset];
}
/**
* Implements ArrayAccess.
*/
public function offsetSet($offset, $value) {}
/**
* Implements ArrayAccess.
*/
public function offsetUnset($offset) {}
}
/**
* Implements hook_cron().
*/
function islandora_pathauto_cron() {
if (variable_get('islandora_pathauto_use_cron', FALSE)) {
batch_set(array(
'title' => t('Islandora Pathauto cron bulk updating URL aliases'),
'operations' => array(
array('islandora_pathauto_bulk_batch_process', array(TRUE)),
),
));
// This is a hack around the broken batch API.
// https://drupal.org/node/638712#comment-2289138
$batch =& batch_get();
$batch['progressive'] = FALSE;
batch_process();
}
}
/**
* Pathauto bulk update callback.
*/
function islandora_pathauto_pathauto_bulkupdate(&$context) {
return islandora_pathauto_bulk_batch_process(FALSE, $context);
}