forked from mjordan/islandora_premis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_premis.module
86 lines (71 loc) · 3.14 KB
/
islandora_premis.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
<?php
// phpcs:disable
if (class_exists('\EasyRdf_Graph') && !class_exists('\EasyRdf\Graph')) {
class_alias('\EasyRdf_Graph', '\EasyRdf\Graph');
}
if (class_exists('\EasyRdf_Namespace') && !class_exists('\EasyRdf\RdfNamespace')) {
class_alias('\EasyRdf_Namespace', '\EasyRdf\RdfNamespace');
}
// phpcs:enable
use EasyRdf\Graph;
use EasyRdf\RdfNamespace;
/**
* Implements hook_islandora_premis_turtle_alter().
*/
function islandora_premis_islandora_premis_turtle_alter($nid, &$turtle) {
$config = \Drupal::config('islandora_premis.settings');
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$utils = \Drupal::service('islandora.utils');
$media_source = \Drupal::service('islandora.media_source_service');
$file_uri_lookup = \Drupal::service('islandora_premis.utils');
$current_path = \Drupal::service('path.current')->getPath();
if (\Drupal::moduleHandler()->moduleExists('islandora_riprap')) {
$islandora_riprap_utils = \Drupal::service('islandora_riprap.riprap');
}
if (!$node) {
return array();
}
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $nid], ['absolute' => TRUE])->toString();
/**
* @var Symfony\Component\Serializer\Serializer $serializer
*/
$serializer = \Drupal::service('serializer');
$jsonld = $serializer->serialize($node, 'jsonld');
$graph = new Graph($url, $jsonld, 'jsonld');
$graph->addType($url, "http://www.loc.gov/premis/rdf/v3/IntellectualEntity");
$media = $utils->getMedia($node);
if (count($media) > 0) {
foreach ($media as $medium) {
$file = $media_source->getSourceFile($medium);
$file_url = $file_uri_lookup->getUrl($medium->id());
if (is_null($file_url)) {
return;
}
$binary_resource_url = $file_url;
if (\Drupal::moduleHandler()->moduleExists('islandora_riprap')) {
$riprap_config = \Drupal::config('islandora_riprap.settings');
if ($riprap_config->get('use_drupal_urls')) {
$binary_resource_url = $islandora_riprap_utils->getLocalUrl($medium->id());
}
}
// Register some namespaces.
$ns = new RdfNamespace();
$ns->set('premisObject', 'http://www.loc.gov/premis/rdf/v3/Object');
$ns->set('premis', 'http://id.loc.gov/vocabulary/preservation/eventOutcome');
$ns->set('crypHashFunc', 'http://id.loc.gov/vocabulary/preservation/cryptographicHashFunctions/');
$ns->set('ebucore', 'https://www.ebu.ch/metadata/ontologies/ebucore#');
$graph->resource($binary_resource_url, 'premisObject:File');
$graph->add($binary_resource_url, 'premis:size', $file->getSize());
$graph->add($binary_resource_url, 'premis:compositionLevel', 0);
$graph->add($binary_resource_url, 'ebucore:hasMimeType', $file->getMimeType());
$original_name_field = $config->get('islandora_premis_file_original_name_field');
if ($medium->hasField($original_name_field)) {
$original_name = trim($medium->get($original_name_field)->getString());
if (strlen($original_name)) {
$graph->add($binary_resource_url, 'premis:originalName', $original_name);
}
}
}
}
$turtle = $graph->serialise('turtle');
}