forked from mitchmac/islandora_sync
-
Notifications
You must be signed in to change notification settings - Fork 5
/
islandora_sync.module
517 lines (470 loc) · 16.5 KB
/
islandora_sync.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
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
<?php
/**
* @file
* Sync data between Drupal and Fedora Commons.
*/
/**
* Implements hook_menu().
*/
function islandora_sync_menu() {
$items['admin/structure/types/manage/%/fedora'] = array(
'title' => 'Fedora Commons',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_sync_bundle_settings_form', 4),
'access arguments' => array('islandora sync settings'),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/bundle_settings.form.inc',
);
$items['admin/structure/types/manage/%/fields/%/fedora'] = array(
'title' => 'Fedora Commons',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_sync_field_settings_form', 4, 6),
'access arguments' => array('islandora sync settings'),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/field_settings.form.inc',
);
$items['admin/islandora/tools/sync/xml-datastreams'] = array(
'title' => 'XML Datastream Settings',
'page callback' => 'islandora_sync_xml_ds_page',
'access arguments' => array('islandora sync settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/xml_ds_settings.form.inc',
);
$items['admin/islandora/tools/sync/xml-datastreams/add'] = array(
'title' => 'Add XML Datastream Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_sync_xml_ds_add'),
'access arguments' => array('islandora sync settings'),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/xml_ds_settings.form.inc',
);
$items['admin/islandora/tools/sync/xml-datastreams/%/edit'] = array(
'title' => 'Edit XML Datastream Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_sync_xml_ds_add', 5),
'access arguments' => array('islandora sync settings'),
'type' => MENU_CALLBACK,
'file' => 'includes/xml_ds_settings.form.inc',
);
$items['admin/islandora/tools/sync/xml-datastreams/%/delete'] = array(
'title' => 'Delete XML Datastream Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_sync_xml_ds_delete', 5),
'access arguments' => array('islandora sync settings'),
'type' => MENU_CALLBACK,
'file' => 'includes/xml_ds_settings.form.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_sync_permission() {
return array(
'islandora sync settings' => array(
'title' => t('Administer Islandora sync settings'),
),
);
}
/**
* Implements hook_islandora_object_ingested().
*/
function islandora_sync_islandora_object_ingested(FedoraObject $object) {
$obj_ids = &drupal_static(__FUNCTION__);
if (!is_array($obj_ids)) {
$obj_ids = array();
}
$bundles_info = db_query("SELECT * FROM {islandora_sync_type_settings} ists
INNER JOIN {islandora_sync_type_cmodel} istc ON istc.bundle = ists.bundle
WHERE istc.cmodel IN (:models)
AND ists.sync_drupal = 1
AND ists.sync_drupal_create = 1", array(':models' => $object->models)
)->fetchAll();
foreach ($bundles_info as $bundle_info) {
if (!isset($obj_ids[$bundle_info->bundle])) {
$obj_ids[$bundle_info->bundle] = array();
}
if (in_array($object->id, $obj_ids[$bundle_info->bundle])) {
continue;
}
$obj_ids[$bundle_info->bundle][] = $object->id;
if ($bundle_info && $bundle_info->sync_drupal_timing == 'real_time') {
module_load_include('inc', 'islandora_sync', 'includes/import_to_drupal');
islandora_sync_import_from_pid($object->id, $bundle_info->bundle);
}
elseif ($bundle_info) {
islandora_sync_queue_operation($object->id, $bundle_info->entity_type, $bundle_info->bundle, 'sync_drupal');
}
}
}
/**
* Implements hook_islandora_object_modified().
*/
function islandora_sync_islandora_object_modified(FedoraObject $object) {
islandora_sync_update_from_fedora($object);
}
/**
* Implements hook_islandora_object_purged().
*/
function islandora_sync_islandora_object_purged($pid) {
$nids = islandora_sync_get_nids_from_pid($pid);
foreach ($nids as $nid) {
if ($nid) {
$node = node_load($nid);
$bundle_info = islandora_sync_get_bundle_settings($node->type);
if ($bundle_info->sync_drupal_delete) {
if ($bundle_info->sync_drupal_timing == 'real_time') {
entity_delete($bundle_info->entity_type, $nid);
db_delete('islandora_sync_map')->condition('pid', $pid)->execute();
}
else {
islandora_sync_queue_operation($pid, $bundle_info->entity_type, $node->type, 'delete_drupal', $node->nid);
}
}
}
}
}
/**
* Implements hook_islandora_datastream_ingested().
*/
function islandora_sync_islandora_datastream_ingested(FedoraObject $object, FedoraDatastream $datastream) {
islandora_sync_update_from_fedora($object, $datastream->id);
}
/**
* Implements hook_islandora_datatastream_modified().
*/
function islandora_sync_islandora_datastream_modified(FedoraObject $object, FedoraDatastream $datastream) {
islandora_sync_update_from_fedora($object, $datastream->id);
}
/**
* Implements hook_islandora_datastream_purged().
*/
function islandora_sync_islandora_datastream_purged(FedoraObject $object, $dsid) {
$nids = islandora_sync_get_nids_from_pid($object->id);
foreach ($nids as $nid) {
if ($nid) {
$node = node_load($nid);
$bundle_info = islandora_sync_get_bundle_settings($node->type);
if ($bundle_info->sync_drupal_update) {
$has_non_managed = db_select('islandora_sync_fields', 'f')
->fields('f', array())
->condition('dsid', $dsid)
->condition('bundle', $node->type)
->condition('export_type', 'managed_datastream', '!=')
->countQuery()
->execute()
->fetchField() > 0;
if ($has_non_managed) {
// Handle like a normal update.
islandora_sync_update_from_fedora($object);
}
elseif ($bundle_info->sync_drupal_timing == 'real_time') {
$managed_datastream_fields = db_select('islandora_sync_fields', 'f')
->fields('f', array())
->condition('dsid', $dsid)
->condition('bundle', $node->type)
->condition('export_type', 'managed_datastream')
->execute();
foreach ($managed_datastream_fields as $managed_file_field) {
islandora_sync_delete_field_by_dsid($node, $dsid, $managed_file_field);
}
}
else {
islandora_sync_queue_operation(
$object->id,
$bundle_info->entity_type,
$node->type,
'delete_datastream_field',
$node->nid,
array('dsid' => $dsid)
);
}
}
}
}
}
/**
* Implements hook_cron().
*/
function islandora_sync_cron() {
module_load_include('inc', 'islandora_sync', 'includes/import_to_drupal');
module_load_include('inc', 'islandora_sync', 'includes/export_to_fedora');
$queue_items = db_query("SELECT id FROM {islandora_sync_queue} WHERE processed_time IS NULL ORDER BY id ASC")->fetchCol();
foreach ($queue_items as $item_id) {
$item = db_query("SELECT * FROM {islandora_sync_queue} WHERE id = :id", array(':id' => $item_id))->fetchObject();
switch ($item->operation) {
case 'sync_drupal':
$nid = islandora_sync_import_from_pid($item->pid, $item->bundle);
$item->entity_id = $nid;
db_update('islandora_sync_queue')
->fields(array('entity_id' => $nid))
->condition('pid', $item->pid)
->execute();
break;
case 'sync_fedora':
$node = node_load($item->entity_id);
$type_info = islandora_sync_get_bundle_settings($item->bundle);
islandora_sync_export_from_node($node, $type_info);
break;
case 'update_drupal':
$data = unserialize($item->data);
islandora_sync_import_from_pid($item->pid, $item->bundle, $item->entity_id, $data['dsid']);
break;
case 'delete_datastream_field':
$node = node_load($item->entity_id);
$data = unserialize($item->data);
islandora_sync_delete_field_by_dsid($node, $data['dsid']);
break;
case 'delete_drupal':
node_delete($item->entity_id);
db_delete('islandora_sync_map')->condition('pid', $item->pid)->execute();
break;
}
// Should check for success.
$item->processed_time = time();
drupal_write_record('islandora_sync_queue', $item, 'id');
}
}
/**
* Callback for a number of object related update events.
*/
function islandora_sync_update_from_fedora(FedoraObject $object, $ds_id = NULL) {
$nids = islandora_sync_get_nids_from_pid($object->id);
foreach ($nids as $nid) {
$node = node_load($nid);
$bundle_info = db_query("SELECT * FROM {islandora_sync_type_settings} ists
INNER JOIN {islandora_sync_type_cmodel} istc ON istc.bundle = ists.bundle
WHERE istc.cmodel IN (:models)
AND ists.sync_drupal = 1
AND ists.bundle = :bundle
AND ists.sync_drupal_update = 1", array(':bundle' => $node->type, ':models' => $object->models)
)->fetchObject();
if ($bundle_info && $bundle_info->sync_drupal_timing == 'real_time') {
module_load_include('inc', 'islandora_sync', 'includes/import_to_drupal');
islandora_sync_import_from_pid($object->id, $bundle_info->bundle, $nid, $ds_id);
}
elseif ($bundle_info) {
islandora_sync_queue_operation(
$object->id,
$bundle_info->entity_type,
$bundle_info->bundle,
'update_drupal',
$nid,
array('dsid' => $ds_id)
);
}
}
}
/**
* Delete node field values based on the field mapping to a datastream ID.
*/
function islandora_sync_delete_field_by_dsid($node, $dsid, $field_in = NULL) {
if (is_null($field_in)) {
$fields = db_query("SELECT * FROM {islandora_sync_fields}
WHERE dsid = :dsid
AND bundle = :bundle",
array(':dsid' => $dsid, ':bundle' => $node->type)
);
}
else {
$fields = array($field_in);
}
foreach ($fields as $field_info) {
$fid = isset($node->{$field_info->field}[$node->language][0]['fid']) ? $node->{$field_info->field}[$node->language][0]['fid'] : FALSE;
if ($fid) {
$file = file_load($fid);
file_delete($file);
}
$node->{$field_info->field} = array();
}
node_save($node);
}
/**
* Implements hook_node_insert().
*/
function islandora_sync_node_insert($node) {
$type_info = islandora_sync_get_bundle_settings($node->type);
if (isset($type_info->id) && $type_info->sync_fedora && $type_info->sync_fedora_create) {
if ($type_info->sync_fedora_timing == 'real_time') {
module_load_include('inc', 'islandora_sync', 'includes/export_to_fedora');
islandora_sync_export_from_node($node, $type_info);
}
else {
$queue['type'] = $type_info->entity_type;
$queue['bundle'] = $type_info->bundle;
$queue['entity_id'] = $node->nid;
$queue['operation'] = 'sync_fedora';
drupal_write_record('islandora_sync_queue', $queue);
}
}
}
/**
* Implements hook_node_type_delete().
*/
function islandora_sync_node_type_delete($info) {
db_delete('islandora_sync_type_settings')
->condition('bundle', $info->type)
->execute();
db_delete('islandora_sync_type_cmodel')
->condition('bundle', $info->type)
->execute();
}
/**
* Implements hook_field_delete_instance().
*/
function islandora_sync_field_delete_instance($instance) {
db_delete('islandora_sync_fields')
->condition('bundle', $instance['bundle'])
->condition('field', $instance['field_name'])
->execute();
}
/**
* Implements hook_module_implements_alter().
*/
function islandora_sync_module_implements_alter(&$implementations, $hook) {
$hooks = array(
'form_field_ui_field_overview_form_alter',
'form_alter',
'islandora_object_ingested',
);
if (in_array($hook, $hooks) && isset($implementations['islandora_sync'])) {
$group = $implementations['islandora_sync'];
unset($implementations['islandora_sync']);
$implementations['islandora_sync'] = $group;
}
}
/**
* Implements hook_form_field_ui_field_overview_form_alter().
*/
function islandora_sync_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
// @TODO: don't hardcode.
$form['fields']['#header'][6]['colspan'] = 3;
foreach ($form['fields'] as $field_name => $values) {
// @TODO: find a better way.
$row_types = array('field', 'extra_field', 'group');
if (isset($values['#row_type']) && in_array($values['#row_type'], $row_types)) {
$form['fields'][$field_name]['fedora'] = array(
'#type' => 'link',
'#title' => t('fedora'),
'#href' => 'admin/structure/types/manage/' . $form['#bundle'] . '/fields/' . $field_name . '/fedora',
'#options' => array('attributes' => array('title' => t('Manage Fedora Commons mappings.'))),
);
}
elseif ($field_name[0] != '#') {
$form['fields'][$field_name]['fedora'] = array('#markup' => '');
}
}
}
/**
* Implements hook_features_api().
*/
function islandora_sync_features_api() {
return array(
'islandora_sync_field_datastreams' => array(
'name' => t('Field to Fedora mappings'),
'file' => drupal_get_path('module', 'islandora_sync') . '/includes/features.inc',
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
'feature_source' => TRUE,
),
'islandora_sync_bundle_settings' => array(
'name' => t('Islandora Sync content type settings'),
'file' => drupal_get_path('module', 'islandora_sync') . 'includes/features.inc',
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
'feature_source' => TRUE,
),
'islandora_sync_xml_ds_settings' => array(
'name' => t('Islandora Sync XML datastream settings'),
'file' => drupal_get_path('module', 'islandora_sync') . 'includes/features.inc',
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
'features_source' => TRUE,
),
);
}
/**
* Implements hook_views_api().
*/
function islandora_sync_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'islandora_sync') . '/includes',
);
}
/**
* Create a queue entry for later processing by cron.
*/
function islandora_sync_queue_operation($pid, $entity_type, $entity_bundle, $operation, $nid = NULL, $data = NULL) {
$queue = array();
$queue['pid'] = $pid;
$queue['type'] = $entity_type;
$queue['bundle'] = $entity_bundle;
$queue['operation'] = $operation;
if (!is_null($nid)) {
$queue['entity_id'] = $nid;
}
if (!is_null($data)) {
$queue['data'] = $data;
}
drupal_write_record('islandora_sync_queue', $queue);
}
/**
* Get a list of content models.
*/
function islandora_sync_get_content_models() {
module_load_include('inc', 'islandora', 'includes/utilities');
$message = islandora_deprecated('7.x-1.7', 'Use islandora_get_content_models() from "islandora".');
trigger_error(filter_xss($message), E_USER_DEPRECATED);
return array_keys(islandora_get_content_models(TRUE));
}
/**
* Get the node IDs based on a Fedora pid from the map table.
*/
function islandora_sync_get_nids_from_pid($pid) {
$nids = db_query("SELECT entity_id FROM {islandora_sync_map} WHERE pid = :pid",
array(':pid' => $pid)
)->fetchCol();
return $nids;
}
/**
* Get the cmodels that have been configured to be mapped to a node bundle.
*/
function islandora_sync_get_bundle_cmodels($bundle) {
$cmodels = db_query("SELECT cmodel FROM {islandora_sync_type_cmodel} WHERE bundle = :bundle",
array(':bundle' => $bundle)
)->fetchCol();
return $cmodels;
}
/**
* Get the islandora_sync bundle related settings for a node type.
*/
function islandora_sync_get_bundle_settings($bundle) {
$settings = db_query("SELECT * FROM {islandora_sync_type_settings} WHERE entity_type = 'node' AND bundle = :bundle",
array(':bundle' => $bundle)
)->fetchObject();
return $settings;
}
/**
* Get the islandora_sync field settings for a node's field.
*/
function islandora_sync_get_field_settings($type, $field) {
$settings = db_query("SELECT * FROM {islandora_sync_fields} WHERE bundle = :bundle AND field = :field",
array(':bundle' => $type, ':field' => $field)
)->fetchAll();
return $settings;
}
/**
* Implements hook_node_delete().
*/
function islandora_sync_node_delete($node) {
$node_path = "public://islandora_sync/$node->nid";
if (file_prepare_directory($node_path)) {
$removal_files = file_scan_directory($node_path, '/(.*)/');
foreach ($removal_files as $file) {
$file = (object) $file;
$file->fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :uri",
array(':uri' => $file->uri)
)->fetchField();
@file_delete($file);
}
@drupal_rmdir($node_path);
}
}