-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathislandora_metadata_synchronization.drush.inc
More file actions
344 lines (327 loc) · 14 KB
/
islandora_metadata_synchronization.drush.inc
File metadata and controls
344 lines (327 loc) · 14 KB
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
<?php
/**
* @file
* Functions for synchronization via drush.
*
*
* Copyright 2017 Leiden University Library
*
* This file is part of islandora_metadata_synchronization.
*
* islandora_metadata_synchronization is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Implements hook_drush_command().
*/
function islandora_metadata_synchronization_drush_command() {
$items['start_metadata_synchronization'] = array(
'description' => 'Retrieve the metadata that was changed after a specified date from the specified source and synchronize it with the found records in Islandora.',
'options' => array(
'source' => 'The source id as configured in islandora/tools/islandora_metadata_synchronization.',
'date' => 'The date after which the metadata has changed. Format YYYY-MM-DD. Enter \'last\' to use the last synchronization date.',
'mapping_pattern' => 'Optional, but use in conjunction with mapping_replacement. Use this to make another mapping for the OAI identifier',
'mapping_replacement' => 'Optional, but use in conjunction with mapping_pattern. Use this to make another mapping for the OAI identifier',
'mapping_file' => 'Optional, a file containing a mapping for the identifiers. Each line contains 2 identifiers separated by a tab. The first identifier is the one that is used in the source, the second is the one that is used in islandora.',
'ids_file' => 'Optional, a file containing a list of identifiers. Only those identifiers will be synchronised. The mapping are tried on those identifiers as well and if they fail, the identifier from the metadata is used with the mapping.',
'batch_set_id' => 'Optional, a batch set id of a set that was previously ingested succesfully. Only those records will be synchronised.',
'print_start' => 'If exists, prints a start message like: "Synchronisation started at 2018-04-01 14:58:34 from date 2018-03-20T"',
),
'aliases' => array('smds'),
'examples' => array(
'drush start_metadata_synchronization --source=1 --date=last',
'drush smds --source=2 --date=2015-12-15',
'drush smds --source=2 --date=2015-12-15 --mapping_pattern="/oai:archimedes.leidenuniv.nl:/" --mapping_replacement="oai:disc.leidenuniv.nl:"',
'drush smds --source=2 --date=2015-12-15 --mapping_file=/tmp/mapping_file.txt',
'drush smds --source=2 --ids_file=/tmp/ids_file.txt',
'drush smds --source=2 --ids_file=/tmp/ids_file.txt --mapping_file=/tmp/mapping_file.txt',
'drush smds --source=2 --batch_set_id=6',
'drush smds --source=2 --batch_set_id=6 --mapping_file=/tmp/mapping_file.txt',
),
);
return $items;
}
/**
* Implements drush_hook_COMMAND_validate().
*/
function drush_islandora_metadata_synchronization_start_metadata_synchronization_validate() {
$sourceid = drush_get_option('source');
if ($sourceid != NULL) {
if (preg_match("/^\d+$/", $sourceid) != 1) {
return drush_set_error("the option source should be a number indicating the source id as configured in islandora/tools/islandora_metadata_synchronization.");
}
}
$date = drush_get_option('date');
$idsfile = drush_get_option('ids_file');
$batchsetid = drush_get_option('batch_set_id');
$checkset = array();
if ($date !== NULL) { $checkset['date'] = $date; }
if ($idsfile !== NULL) { $checkset['idsfile'] = $idsfile; }
if ($batchsetid !== NULL) { $checkset['batchsetid'] = $batchsetid; }
if (count($checkset) != 1) {
return drush_set_error("Exactly one of the options date, ids_file of batch_set_id should exist.");
}
if ($date != NULL && preg_match("/^(?:last|\d\d\d\d-\d\d-\d\d(?:T\d\d:\d\d:\d\dZ)?)$/", $date) != 1) {
return drush_set_error("the option date should be the value 'last' or a date formatted as YYYY-MM-DD");
}
if ($batchsetid != NULL && preg_match("/^\d+$/", $batchsetid) != 1) {
return drush_set_error("the option batch_set_id should be a number");
}
$mappat = drush_get_option('mapping_pattern');
$maprep = drush_get_option('mapping_replacement');
if (($mappat == NULL && $maprep != NULL) || ($mappat != NULL && $maprep == NULL)) {
return drush_set_error("The options mapping_pattern and mapping_replacement should be used together, or not at all.");
}
if ($mappat != NULL && @preg_match($mappat, "check if pattern is valid regex") === FALSE) {
return drush_set_error("The pattern '$mappat' is not a valid regular expression");
}
$mapfile = drush_get_option('mapping_file');
if ($mapfile != NULL) {
if ($mappat != NULL) {
return drush_set_error("mapping_file cannot be used at the same time as mapping_pattern");
}
if (!file_exists($mapfile)) {
return drush_set_error("mapping_file '$mapfile' does not exist");
}
}
if ($idsfile != NULL) {
if (!file_exists($idsfile)) {
return drush_set_error("ids_file '$idsfile' does not exist");
}
}
if ($batchsetid != NULL) {
module_load_include('inc', 'islandora_batch', 'includes/db');
$count = islandora_batch_get_count_of_queued_set_objects($batchsetid);
if ($count == 0) {
return drush_set_error("batch set id '$batchsetid' does not contain any items");
}
}
return TRUE;
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_islandora_metadata_synchronization_start_metadata_synchronization() {
module_load_include('inc', 'islandora_metadata_synchronization', 'includes/synchronize');
module_load_include('inc', 'islandora_metadata_synchronization', 'includes/objects_identifiers');
$sourceid = drush_get_option('source');
$date = drush_get_option('date');
$mappat = drush_get_option('mapping_pattern');
$maprep = drush_get_option('mapping_replacement');
$mapfile = drush_get_option('mapping_file');
$idsfile = drush_get_option('ids_file');
$batchsetid = drush_get_option('batch_set_id');
$printstart = drush_get_option('print_start');
$mapping = NULL;
if ($mappat != NULL && $maprep != NULL) {
$mapping = array("_pattern" => $mappat, "_replacement" => $maprep);
}
elseif ($mapfile != NULL) {
$fieldseparator = "";
$mapping = array();
$content = file_get_contents($mapfile);
$contentarray = str_getcsv($content, "\n");
foreach ($contentarray as $index => $line) {
if (strlen($line) > 0) {
if ($fieldseparator === "") {
$fieldseparator = "\t";
$comps = str_getcsv($line, $fieldseparator);
if (count($comps) != 2) {
$fieldseparator = ",";
$comps = str_getcsv($line, $fieldseparator);
}
if (count($comps) != 2) {
$fieldseparator = ";";
}
}
$comps = str_getcsv($line, $fieldseparator);
if (count($comps) != 2) {
$linenumber = $index + 1;
return drush_set_error("mapping_file '$mapfile' has an error on line $linenumber '$line': not 2 components but ".count($comps)." components");
}
$key = $comps[0];
$value = $comps[1];
if (isset($mapping[$key]) && $mapping[$key] !== $value) {
$linenumber = $index + 1;
return drush_set_error("mapping_file '$mapfile' has an error on line $linenumber: source id '$key' maps to 2 different target ids");
}
if (isset($mapping[$value]) && $mapping[$value] !== $key) {
$linenumber = $index + 1;
return drush_set_error("mapping_file '$mapfile' has an error on line $linenumber: target id '$value' maps to 2 different source ids");
}
$mapping[$key] = $value;
$mapping[$value] = $key;
}
}
}
if ($date === 'last') {
$date = variable_get('islandora_metadata_synchronization_last_synchronization_date');
}
if (isset($printstart)) {
$start = date("Y-m-d H:i:s");
$message = ($printstart === TRUE)?"Synchronisation started at @start, using fromdate=@date":$printstart;
$args = array(
'@start' => $start,
'@date' => $date,
'@sourceid' => $sourceid,
'@mapping_pattern' => $mappat,
'@mapping_replacement' => $maprep,
'@mapping_file' => $mapfile,
'@ids_file' => $idsfile,
'batch_set_id' => $batchsetid,
);
drush_log(t($message, $args), 'ok');
}
if ($date !== NULL) {
drush_log("Synchronizing from date $date");
$params = array('date' => $date);
if ($sourceid == NULL) {
islandora_metadata_synchronization_start_sync($params, TRUE, $mapping);
}
else {
$oaipmh_url = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_oaipmh_url');
$params['set'] = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_set');
$params['mdprefix'] = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_mdprefix');
$params['target_dsid'] = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_target_dsid');
$params['mdxsl'] = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_mdxsl');
$params['addmdxsl'] = variable_get('islandora_metadata_synchronization_source' . $sourceid . '_addmdxsl');
islandora_metadata_synchronization_start_sync_with_url($oaipmh_url, $params, TRUE, $mapping);
}
}
else {
$sources = islandora_metadata_synchronization_sources($sourceid);
$foundids = array();
if ($idsfile !== NULL) {
$foundids = ids_from_file($idsfile);
}
elseif ($batchsetid !== NULL) {
$foundids = islandora_batch_get_queued_items_for_set($batchsetid);
}
drush_log("Found " . count($foundids). " IDs to synchronize");
foreach ($foundids as $id) {
$object = find_fedora_object_for_id($id);
if ($object) {
drush_log("Synchronizing object " . $object->id . " found for $id");
$usedoaiurls = array();
foreach ($sources as $source) {
$oaiurl = $source['oaipmh_url'];
if (isset($usedoaiurls[$oaiurl])) {
break;
}
$usedoaiurls[$oaiurl] = 1;
$idprefix = $source['id_prefix'];
$syncid = $id;
if (isset($idprefix) && strlen($idprefix) > 0) {
if (substr($syncid, 0, strlen($idprefix)) !== $idprefix) {
$syncid = $idprefix . $syncid;
}
}
$mdxsl = $source['mdxsl'];
$addmdxsl = $source['addmdxsl'];
$mdprefix = $source['mdprefix'];
$dsid = $source['target_dsid'];
$t = islandora_metadata_synchronization_sync_metadata_for_object_using($object, $oaiurl, $syncid, $mdprefix, $mdxsl, $addmdxsl, $dsid);
if ($t) {
$an_id = islandora_metadata_synchronization_map_identifier($id, $mapping);
if ($id !== $an_id) {
$syncid = $an_id;
if (isset($idprefix) && strlen($idprefix) > 0) {
if (substr($syncid, 0, strlen($idprefix)) !== $idprefix) {
$syncid = $idprefix . $syncid;
}
}
$t = islandora_metadata_synchronization_sync_metadata_for_object_using($object, $oaiurl, $syncid, $mdprefix, $mdxsl, $addmdxsl, $dsid);
}
}
if ($t) {
$dsid = $source['id_dsid'];
$xpath = $source['path'];
$an_id = islandora_metadata_synchronization_get_identifier($object, $dsid, $xpath);
if ($an_id != NULL) {
if ($id !== $an_id) {
$t = islandora_metadata_synchronization_sync_metadata_for_object_using($object, $oaiurl, $an_id, $mdprefix, $mdxsl, $addmdxsl, $dsid);
if ($t) {
$another_id = islandora_metadata_synchronization_map_identifier($an_id, $mapping);
$syncid = $another_id;
if (isset($idprefix) && strlen($idprefix) > 0) {
if (substr($syncid, 0, strlen($idprefix)) !== $idprefix) {
$syncid = $idprefix . $syncid;
}
}
if ($an_id !== $syncid) {
$t = islandora_metadata_synchronization_sync_metadata_for_object_using($object, $oaiurl, $syncid, $mdprefix, $mdxsl, $addmdxsl, $dsid);
}
}
}
}
}
if ($t) {
drush_log("Cannot synchronize object $id: $t", "warning");
}
else {
drush_log("Successfully synchronized object $id", "ok");
break;
}
}
}
else {
drush_log("Cannot find object $id", "warning");
}
}
}
}
function ids_from_file($idsfile) {
$foundids = array();
$idsstr = file_get_contents($idsfile);
$fieldseparator = "";
$ids = str_getcsv($idsstr, "\n");
foreach ($ids as $index => $id) {
if (strlen($id) == 0) {
continue;
}
if ($fieldseparator === "") {
$fieldseparator = "\t";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a tab.
}
else {
$fieldseparator = ",";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a comma.
}
else {
$fieldseparator = ";";
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
// Fields are separated by a semicolon.
}
else {
// Fields are not separated.
$fieldseparator = FALSE;
}
}
}
}
if ($fieldseparator !== FALSE) {
$comps = str_getcsv($id, $fieldseparator);
if (count($comps) > 1) {
$id = $comps[0];
}
}
$foundids[] = $id;
}
return $foundids;
}