This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
FOXML.inc
448 lines (413 loc) · 17.5 KB
/
FOXML.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
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
<?php
/**
* @file
*
*/
module_load_include('inc', 'islandora_form_builder', 'Utilities');
class FOXML extends DOMDocument {
const foxml = 'info:fedora/fedora-system:def/foxml#';
const xlink = 'http://www.w3.org/1999/xlink';
const xsi = 'http://www.w3.org/2001/XMLSchema-instance';
const xmlns = 'http://www.w3.org/2000/xmlns/';
const rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const rdfs = 'http://www.w3.org/2000/01/rdf-schema#';
const fedora = 'info:fedora/fedora-system:def/relations-external#';
const dc = 'http://purl.org/dc/elements/1.1/';
const oai_dc = 'http://www.openarchives.org/OAI/2.0/oai_dc/';
const fedora_model = 'info:fedora/fedora-system:def/model#';
/**
*
* @var <type>
*/
protected $root;
protected $pid;
protected $dsid;
protected $collectionPid;
protected $contentModelPid;
protected $ingestFileLocations;
protected $label;
protected $user;
protected $document;
protected $transform;
protected $state;
/**
*
* @global <type> $user
* @param <type> $label
* @param <type> $pid
* @param <type> $dsid
* @param <type> $content_model_pid
* @param <type> $collection_pid
* @param <type> $relationship
* @param <type> $ingest_file_location
* @param <type> $document
*/
public function __construct($label, $pid, $dsid, $content_model_pid, $collection_pid, $relationship, $ingest_file_locations, $document, $transform, $state = 'A') {
global $user;
parent::__construct("1.0", "UTF-8"); // DomDocument
$this->formatOutput = TRUE;
$this->preserveWhiteSpace = FALSE;
$this->label = substr($label, 0, 256);
$this->user = $user->name;
$this->pid = $pid;
$this->dsid = $dsid;
$this->state = $state;
$this->contentModelPid = $content_model_pid;
$this->collectionPid = $collection_pid;
$this->relationship = isset($relationship) ? $relationship : 'isMemberOfCollection';
$this->ingestFileLocations = $ingest_file_locations;
$this->document = $document;
$this->transform = $transform;
$this->root = $this->createRootElement();
$this->createDocument();
}
private function createRootElement() {
$root = $this->createElementNS(self::foxml, 'foxml:digitalObject');
$root->setAttribute('VERSION', '1.1');
$root->setAttribute('PID', "{$this->pid}");
$root->setAttribute('xmlns', self::foxml);
$root->setAttributeNS(self::xmlns, 'xmlns:foxml', self::foxml);
$root->setAttributeNS(self::xmlns, 'xmlns:xsi', self::xsi);
$root->setAttributeNS(self::xsi, 'xsi:schemaLocation', self::foxml . " http://www.fedora.info/definitions/1/0/foxml1-1.xsd");
$this->appendChild($root);
return $root;
}
private function createDocument() {
/**
* If DOMNodes are not appended in the corrected order root -> leaf, namespaces may break...
* So be be cautious, add DOMNode's to thier parent element before adding child elements to them.
*/
$this->createObjectProperties();
$this->createRelationships();
$this->createIngestFileDatastreams();
$this->createPolicy();
$this->createDocumentDatastream();
if($this->dsid != 'DC') {
$this->createDublinCoreDatastream();
}
$this->createCollectionPolicy();
$this->createWorkflowStream();
}
/**
*
* @param DOMElement $root
* @return DOMElement
*/
private function createObjectProperties() {
$object_properties = $this->createElementNS(self::foxml, 'foxml:objectProperties');
$this->root->appendChild($object_properties);
$property = $this->createElementNS(self::foxml, 'foxml:property');
$property->setAttribute('NAME', 'info:fedora/fedora-system:def/model#state');
$property->setAttribute('VALUE', $this->state);
$object_properties->appendChild($property);
// Because of FCREPO-1059 we need to make sure that any label we create doesn't
// contain any newlines. This is valid for at least version 3.5 of Fedora.
$label = str_replace(array("\r", "\r\n", "\n"), '', $this->label);
$property = $this->createElementNS(self::foxml, 'foxml:property');
$property->setAttribute('NAME', 'info:fedora/fedora-system:def/model#label');
$property->setAttribute('VALUE', $label);
$object_properties->appendChild($property);
$property = $this->createElementNS(self::foxml, 'foxml:property');
$property->setAttribute('NAME', 'info:fedora/fedora-system:def/model#ownerId');
// Set the ownerId to the username or anonymous as in core.
if (user_is_logged_in()) {
global $user;
$owner = $user->name;
}
else {
$owner = 'anonymous';
}
$property->setAttribute('VALUE', $owner);
$object_properties->appendChild($property);
return $object_properties;
}
/**
*
* @return DOMElement
*/
private function createRelationships() {
$datastream = $this->createDatastreamElement('RELS-EXT', NULL, 'X');
$version = $this->createDatastreamVersionElement('RELS-EXT.0', 'RDF Statements about this Object', 'application/rdf+xml', 'info:fedora/fedora-system:FedoraRELSExt-1.0');
$content = $this->createDatastreamContentElement();
$rdf = $this->createElementNS(self::rdf, 'rdf:RDF');
$rdf->setAttributeNS(self::xmlns, 'xmlns:rdf', self::rdf);
$rdf->setAttributeNS(self::xmlns, 'xmlns:rdfs', self::rdfs);
$rdf->setAttributeNS(self::xmlns, 'xmlns:fedora', self::fedora);
$rdf->setAttributeNS(self::xmlns, 'xmlns:dc', self::dc);
$rdf->setAttributeNS(self::xmlns, 'xmlns:oai_dc', self::oai_dc);
$rdf->setAttributeNS(self::xmlns, 'xmlns:fedora-model', self::fedora_model);
$rdf_description = $this->createElementNS(self::rdf, 'rdf:Description');
$rdf_description->setAttributeNS(self::rdf, 'rdf:about', "info:fedora/{$this->pid}");
$member = $this->createElementNS(self::fedora, "fedora:{$this->relationship}");
$member->setAttributeNS(self::rdf, 'rdf:resource', "info:fedora/{$this->collectionPid}");
$has_rdf_model = $this->createElementNS(self::fedora_model, 'fedora-model:hasModel');
$has_rdf_model->setAttributeNS(self::rdf, "rdf:resource", "info:fedora/{$this->contentModelPid}");
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content);
$content->appendChild($rdf)->appendChild($rdf_description);
$rdf_description->appendChild($member);
$rdf_description->appendChild($has_rdf_model);
}
private function createIngestFileDatastreams() {
module_load_include('inc', 'fedora_repository', 'api/fedora_util');
if (empty($this->ingestFileLocations) || count($this->ingestFileLocations) === 0)
return;
foreach ($this->ingestFileLocations as $dsid => $file) {
$dsid = fix_dsid($dsid);
list($label, $mime_type, $file_url) = $this->getFileAttributes($file);
$datastream = $this->createDatastreamElement($dsid, 'A', 'M');
$version = $this->createDatastreamVersionElement($dsid . '.0', $label, $mime_type);
$content = $this->createDatastreamContentLocationElement('URL', $file_url);
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content);
}
}
private function getFileAttributes($file) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'api/fedora_util');
$mime = new MimeClass();
$mime_type = $mime->getType($file);
$parts = explode('/', $file);
foreach ($parts as $n => $part) {
$parts[$n] = rawurlencode($part);
}
$path = implode('/', $parts);
$file_url = $base_url . '/' . $path;
$beginIndex = strrpos($file_url, '/');
$dtitle = substr($file_url, $beginIndex + 1);
$dtitle = urldecode($dtitle);
$dtitle = fix_dsid($dtitle);
$file_url = str_ireplace('https://', 'http://', $file_url);
return array($dtitle, $mime_type, $file_url);
}
/**
* This function is for adding a POLICY (XACML)
* datastream to the FOXML. It adds the collection's
* CHILD_POLICY but fires a drupal_alter to let other modules change it.
*/
private function createPolicy() {
$policy_element = $this->getPolicyStreamElement();
// See if anything wants to change the policy before it is set.
drupal_alter('islandora_content_model_forms_policy',
$policy_element,
$this->pid,
$this->collectionPid,
$this->contentModelPid);
if ($policy_element) {
$datastream = $this->createDatastreamElement('POLICY', 'A', 'X');
$version = $this->createDatastreamVersionElement('POLICY.0', 'POLICY', 'text/xml');
$content = $this->createDatastreamContentElement();
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content)->appendChild($policy_element);
}
}
/**
*
* @return DOMElement
*/
private function getPolicyStreamElement() {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$object_helper = new ObjectHelper();
$policy_stream = $object_helper->getStream($this->collectionPid, 'CHILD_SECURITY', FALSE);
if (!isset($policy_stream)) {
return NULL; //there is no policy stream so object will not have a policy stream
}
try {
$xml = new SimpleXMLElement($policy_stream);
} catch (Exception $exception) {
watchdog(t("Fedora_Repository"), t("Problem getting security policy."), NULL, WATCHDOG_ERROR);
drupal_set_message(t('Problem getting security policy: @e', array('@e' => $exception->getMessage())), 'error');
return FALSE;
}
$policy_element = $this->createDocumentFragment();
if (!$policy_element) {
drupal_set_message(t('Error parsing security policy stream.'));
watchdog(t("Fedora_Repository"), t("Error parsing security policy stream, could not parse policy stream."), NULL, WATCHDOG_NOTICE);
return FALSE;
}
$this->importNode($policy_element, TRUE);
$value = $policy_element->appendXML($policy_stream);
if (!$value) {
drupal_set_message(t('Error creating security policy stream.'));
watchdog(t("Fedora_Repository"), t("Error creating security policy stream, could not parse collection policy template file."), NULL, WATCHDOG_NOTICE);
return FALSE;
}
return $policy_element;
}
/**
* Creates Collection policy data stream from a template stored within the
* Content Model.
*
* @return DOMElement
*/
private function createCollectionPolicy() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$fedora_item = new fedora_item($this->contentModelPid);
$datastreams = $fedora_item->get_datastreams_list_as_array();
if (isset($datastreams['COLLECTION_POLICY_TMPL'])) {
$collection_policy_template = $fedora_item->get_datastream_dissemination('COLLECTION_POLICY_TMPL');
$collection_policy_template_dom = DOMDocument::loadXML($collection_policy_template);
$collection_policy_template_root = $collection_policy_template_dom->getElementsByTagName('collection_policy');
if ($collection_policy_template_root->length > 0) {
$collection_policy_template_root = $collection_policy_template_root->item(0);
$node = $this->importNode($collection_policy_template_root, TRUE);
$datastream = $this->createDatastreamElement('COLLECTION_POLICY', 'A', 'X');
$version = $this->createDatastreamVersionElement('COLLECTION_POLICY.0', 'Collection Policy', 'text/xml');
$content = $this->createDatastreamContentElement();
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content)->appendChild($node);
}
}
}
private function createDatastreamElement($id = NULL, $state = NULL, $control_group = NULL) {
$datastream = $this->createElementNS(self::foxml, 'foxml:datastream');
if (isset($id)) {
$datastream->setAttribute('ID', $id);
}
if (isset($state)) {
$datastream->setAttribute('STATE', $state);
}
if (isset($control_group)) {
$datastream->setAttribute('CONTROL_GROUP', $control_group);
}
return $datastream;
}
private function createDatastreamVersionElement($id = NULL, $label = NULL, $mime_type = NULL, $format_uri = NULL) {
$version = $this->createElementNS(self::foxml, 'foxml:datastreamVersion');
if (isset($id)) {
$version->setAttribute('ID', $id);
}
if (isset($label)) {
$version->setAttribute('LABEL', $label);
}
if (isset($mime_type)) {
$version->setAttribute('MIMETYPE', $mime_type);
}
if (isset($format_uri)) {
$version->setAttribute('FORMAT_URI', $format_uri);
}
return $version;
}
private function createDatastreamContentElement() {
$content = $this->createElementNS(self::foxml, 'foxml:xmlContent');
return $content;
}
private function createDatastreamContentLocationElement($type = NULL, $ref = NULL) {
$location = $this->createElementNS(self::foxml, 'foxml:contentLocation');
if (isset($type)) {
$location->setAttribute('TYPE', $type);
}
if (isset($ref)) {
$location->setAttribute('REF', $ref);
}
return $location;
}
/**
* Creates WorkFlow datastream from a template stored within the Content Model.
*
* @return DOMElement
*/
private function createWorkflowStream() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$fedora_item = new fedora_item($this->contentModelPid);
$datastreams = $fedora_item->get_datastreams_list_as_array();
if (isset($datastreams['WORKFLOW_TMPL'])) {
$work_flow_template = $fedora_item->get_datastream_dissemination('WORKFLOW_TMPL');
$work_flow_template_dom = DOMDocument::loadXML($work_flow_template);
$work_flow_template_root = $work_flow_template_dom->getElementsByTagName('workflow');
if ($work_flow_template_root->length > 0) {
$work_flow_template_root = $work_flow_template_root->item(0);
$node = $this->importNode($work_flow_template_root, TRUE);
$datastream = $this->createDatastreamElement('WORKFLOW', 'A', 'X');
$version = $this->createDatastreamVersionElement("{$this->dsid}.0", "{$this->dsid} Record", 'text/xml');
$content = $this->createDatastreamContentElement();
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content)->appendChild($node);
}
}
}
/**
* Creates an xml based datastream by importing the root node from $this->document.
*/
public function createDocumentDatastream() {
if($this->document !== NULL) {
$datastream = $this->createDatastreamElement($this->dsid, 'A', 'X');
$version = $this->createDatastreamVersionElement("{$this->dsid}.0", "{$this->dsid} Record", 'text/xml');
$content = $this->createDatastreamContentElement();
$node = $this->importNode($this->document->documentElement, TRUE);
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content)->appendChild($node);
// Once again god damn you libxml...
$class = get_class($this->document);
// FIX THIS!
/*
$namespaces = call_user_func(array($class, 'getRequiredNamespaces'));
foreach ($namespaces as $prefix => $uri) {
$node->setAttributeNS(self::xmlns, "xmlns:$prefix", $uri);
} */
}
}
/**
* Creates a Dublin Core document by transforming $this->document.
*/
private function createDublinCoreDatastream() {
$datastream = $this->createDatastreamElement('DC', 'A', 'X');
$version = $this->createDatastreamVersionElement('DC.0', 'DC Record', 'text/xml');
$content = $this->createDatastreamContentElement();
$dublin_core = $this->applyTransformation();
if (isset($dublin_core)) {
$this->root->appendChild($datastream)->appendChild($version)->appendChild($content)->appendChild($dublin_core);
$dublin_core->setAttributeNS(self::xmlns, 'xmlns:xsi', self::xsi); // GOD Damn you libxml!
}
}
/**
* Transforms a document via an XSL.
*
* @return DOMElement
* A DOMDocumentFragment that contains the transformed Dublin Core
* Document.
*/
private function applyTransformation() {
if($this->transform !== NULL) {
$xsl = new DOMDocument();
$xsl->load($this->transform);
$xslt = new XSLTProcessor();
$xslt->importStyleSheet($xsl);
$document = $xslt->transformToDoc($this->document);
if ($document) {
$xpath = new DOMXPath($document);
// Set the Label
$results = $xpath->query("*[local-name()='title']");
$results->item(0)->nodeValue = $this->label;
// Set the Pid
$results = $xpath->query("*[local-name()='identifier']");
$length = $results->length;
$results->item(0)->nodeValue = $this->pid;
if (isset($document->documentElement)) {
return $this->importNode($document->documentElement, TRUE);
}
}
}
return NULL;
}
/**
* Ingests this Foxml document into fedora.
*/
public function ingest() {
$ret = FALSE;
try {
$object = Fedora_Item::ingest_from_FOXML($this);
if (!empty($object->pid)) {
drupal_set_message(t("Item !pid created successfully.", array('!pid' => l($object->pid, 'fedora/repository/' . $object->pid))), "status");
$ret = TRUE;
}
if (is_array($this->ingestFileLocations)) {
foreach ($this->ingestFileLocations as $file) {
file_delete($file);
}
}
} catch (exception $exception) {
drupal_set_message(t("Error ingesting object: @e", array('@e' => $exception->getMessage())), 'error');
watchdog(t("Fedora_Repository"), t("Error ingesting object: @e", array('@e' => $exception->getMessage())), NULL, WATCHDOG_ERROR);
// TODO is this necessary?
foreach ($this->ingestFileLocations as $file)
file_delete($file);
}
return $ret;
}
}