-
Notifications
You must be signed in to change notification settings - Fork 8
/
islandora_harvester.inc
343 lines (313 loc) · 11.5 KB
/
islandora_harvester.inc
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
<?php
/**
* Function: get_collections_as_array
*
* Description: Returns an associative array of all collection objects in Fedora instance
*
* @return array
*/
function get_collections_as_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$restricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:');
$namespaces = explode(':', $allowed_string);
$query = 'select $object $title from <#ri>
where ($object <dc:title> $title
and $object <info:fedora/fedora-system:def/model#hasModel> <info:fedora/islandora:collectionCModel>
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
foreach ($namespaces as $namespace) {
$trimmed_names[] = trim($namespace);
}
$list = preg_replace('/info:fedora\//', '', $list);
$options = array();
foreach ($list as $item) { //removes blanks
if ($item) {
$parts = explode(',', $item);
$namespace = explode(':', $parts[0]);
$namespace = trim($namespace[0]);
$bool = in_array($namespace, $trimmed_names);
if (!$restricted || in_array($namespace, $trimmed_names)) {
$options[$parts[0]] = $parts[1];
}
}
}
unset($options['islandora:ContentModelCollection']);
return $options;
}
/**
* Function: oai_get_content_models_as_array
*
* Description: Returns an associative array of all collection objects in Fedora instance
*
* @return array
*/
function oai_get_content_models_as_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$query = 'select $object $title from <#ri>
where ($object <dc:title> $title
and ($object <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
or $object <fedora-rels-ext:isMemberOfCollection> <info:fedora/islandora:ContentModelsCollection>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
foreach ($list as $item) { //removes blanks
if ($item) {
$parts = explode(',', $item);
$item = new fedora_item($parts[0]);
if ($item->get_datastream_dissemination('ISLANDORACM')) {
$options[$parts[0]] = $parts[1];
}
}
}
return $options;
}
/**
* Function: build_oai_objects
*
* Description: Builds and ingests Fedora objects from OAI inputs
*
* @param array $record
* @param string $namespace
* @param string $collection_pid
*/
function build_oai_objects($record, $namespace, $collection_pid, $content_model) {
set_time_limit(0);
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$thumbnail_file = drupal_get_path('module', 'islandora_harvester') . '/images/Crystal_Clear_action_filenew.png';
$pid = fedora_item::get_next_PID_in_namespace($namespace);
$dcStream = $record['dc'];
$modsStream = oai_create_mods($dcStream);
preg_match('/<dc:title>.*<\/dc:title>/', $dcStream, $matches);
$title = preg_replace('/<\/?dc:title>/', "", $matches[0]);
$item = fedora_item::ingest_new_item($pid, 'A', $title, 'Owner');
$item->modify_datastream_by_value($dcStream, 'DC', "Dublin Core", 'text/xml');
$item->add_datastream_from_string($modsStream, 'MODS', "MODS", 'text/xml');
$item->add_relationship('isMemberOfCollection', $collection_pid, 'info:fedora/fedora-system:def/relations-external#');
$item->add_relationship('hasModel', $content_model, 'info:fedora/fedora-system:def/model#');
$item->add_datastream_from_file($thumbnail_file, 'TN');
}
/**
* Function: build_csv_objects from CSV inputs
*
* Description: Builds and ingests Fedora objects
*
* @param array $headers
* @param array $data
* @param string $namespace
* @param string $collection_pid
*/
function build_csv_objects($headers, $data, $namespace, $collection_pid, $content_model) {
set_time_limit(0);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/dublin_core');
$thumbnail_file = drupal_get_path('module', 'islandora_harvester') . '/images/Crystal_Clear_action_filenew.png';
$dc = new Dublin_Core();
$limit = count($headers);
for ($count = 0; $count < $limit; $count++) {
$header = strtolower($headers[$count]);
if (!preg_match('/^dc/', $header)) {
$header = 'dc:' . $header;
}
if (preg_match('/dc\./', $header) && $data[$count]) {
$header = str_replace('dc.', 'dc:', $header);
}
$dc->add_element($header, $data[$count]);
if (!$title && $header == "dc:title") {
$title = $data[$count];
}
}
$pid = fedora_item::get_next_PID_in_namespace($namespace);
$item = fedora_item::ingest_new_item($pid, 'A', $title, 'Owner');
$item->add_relationship('isMemberOfCollection', $collection_pid, 'info:fedora/fedora-system:def/relations-external#');
$item->add_relationship('hasModel', $content_model, 'info:fedora/fedora-system:def/model#');
$item->modify_datastream_by_value($dc->as_xml(), 'DC', 'Dublin Core Metadata', 'text/xml', 'X');
$modsStream = oai_create_mods($dc->as_xml());
$item->add_datastream_from_string($modsStream, 'MODS', "MODS", 'text/xml');
$item->add_datastream_from_file($thumbnail_file, 'TN');
}
/**
* Function create_oai_batch
*
* Description: builds and kicks off oai batch
*
* @param string $namespace
* @param string $collection_pid
* @param array $urls
*/
function create_oai_batch($fullDom, $namespace, $collection_pid, $content_model) {
$batch = array(
'title' => "Creating objects in Fedora repository",
'progress_message' => t('Processed @current objects out of @total.'),
'operations' => array(),
'file' => drupal_get_path('module', 'islandora_harvester') . '/islandora_harvester.inc',
);
$records_to_add = oai_get_input_data($fullDom);
$count = count($records_to_add);
foreach ($records_to_add as $record) {
$batch['operations'][] = array('build_oai_objects', array($record, $namespace, $collection_pid, $content_model));
}
batch_set($batch);
}
/**
* Function create_csv_batch
*
* Description: builds and kicks off csv batch
*
* @param File $file
* @param string $namespace
* @param string $collection_pid
*/
function create_csv_batch($file, $namespace, $collection_pid, $type, $content_model) {
set_time_limit(0);
if ($type == 'TSV') {
$delimiter = "\t";
}
else {
$delimiter = ",";
}
$csv = $file->filepath;
$handle = fopen($csv, "r");
$count = 1;
$batch = array(
'title' => t('Creating and ingesting Fedora objects'),
'operations' => array(),
'file' => drupal_get_path('module', 'islandora_harvester') . '/islandora_harvester.inc',
);
while (($data = fgetcsv($handle, 5000, $delimiter)) !== FALSE) {
if ($count == 1) {
foreach ($data as $header) {
$header = preg_replace('/\[.*\]/', '', $header);
if ($pos = strpos($header, '.', 4)) {
$header = substr($header, 0, $pos);
}
$headers[] = $header;
}
}
else {
$batch['operations'][] = array('build_csv_objects', array($headers, $data, $namespace, $collection_pid, $content_model));
}
$count++;
}
batch_set($batch);
return;
}
/**
* Function build_full_dom
*
* Description: returns dom object with all objects to be imported
* @param URL $url
* @return DomDocument
*/
function build_full_dom($url) {
set_time_limit(0);
$urlParts = explode('?', $url);
$base_url = $urlParts[0] . "?verb=ListRecords&resumptionToken=";
$data = file_get_contents($url);
if (!$data) {
drupal_set_message("No results returned - please check url and login credentials", 'warning');
return;
}
$dom = new DOMDocument();
$dom->loadXML($data);
$domBase = $dom->getElementsByTagName('ListRecords')->item(0);
$resumption_node = $dom->getElementsByTagName('resumptionToken')->item(0);
$token = $resumption_node->nodeValue;
while ($token) {
$new_url = $base_url . $token;
$data = file_get_contents($new_url);
$sub_dom = new DOMDocument();
$sub_dom->loadXML($data);
$records = $sub_dom->getElementsByTagName("record");
foreach ($records as $record) {
$newNode = $dom->importNode($record, TRUE);
$domBase->appendChild($newNode);
}
$resumption_node = $sub_dom->getElementsByTagName('resumptionToken')->item(0);
$token = $resumption_node->nodeValue;
}
$records = $dom->getElementsByTagName("record");
return $dom;
}
/**
* Function oai_create_mods
*
* Description: Creates MODS stream DC stream
* @param string $xmlstr
* @return string
*/
function oai_create_mods($xmlstr) {
$path = drupal_get_path('module', 'islandora_harvester');
module_load_include('inc', 'fedora_repository', 'api/ObjectHelper');
module_load_include('inc', 'fedora_repository', 'core/CollectionClass');
if ($xmlstr == NULL || strlen($xmlstr) < 5) {
return " ";
}
try {
$proc = new XsltProcessor();
} catch (Exception $e) {
drupal_set_message(t('@e', array('@e' => check_plain($e->getMessage()))), 'error');
return " ";
}
$xsl = new DomDocument();
$xsl->load($path . '/xslt/dc_to_mods.xsl');
$input = new DomDocument();
$input->loadXML(trim($xmlstr));
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$content = $newdom->saveXML();
return $content;
}
function get_allowed_content_models($collection_pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$collection_item = new fedora_item($collection_pid);
$collection_policy = $collection_item->get_datastream_dissemination('COLLECTION_POLICY');
$xml = simplexml_load_string($collection_policy);
$json = json_encode($xml);
$results = json_decode($json, true);
if ($results) {
$results_array = array();
foreach ($results['content_models']['content_model'] as $cm) {
$key = $cm['@attributes']['pid'];
if (!$key) {
$key = $cm['pid'];
}
$value = $cm['@attributes']['name'];
if (!$value) {
$value = $cm['name'];
}
$results_array[$key] = $value;
}
unset($results_array['islandora:collectionCModel']);
}
return $results_array;
}
function oai_get_input_data($dom){
set_time_limit(0);
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$allData = array();
$records = $dom->getElementsByTagName("record");
foreach ($records as $record) {
$recordData = array();
$metadata = $record->getElementsByTagName('metadata')->item(0);
$oai_dc = $metadata->getElementsByTagNameNS('http://www.openarchives.org/OAI/2.0/oai_dc/', '*');
$oai_dc = $oai_dc->item(0);
$recordData['dc'] = $dom->saveXML($oai_dc);
$header = $record->getElementsByTagName('header')->item(0);
$recordData['header'] = $dom->saveXML($header);
$allData[] = $recordData;
}
return $allData;
}