-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_fpr.module
230 lines (218 loc) · 8.2 KB
/
islandora_fpr.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
<?php
/**
* @file
* Defines all the hooks this module implements, and some other functions.
*/
/**
* Implements hook_menu().
*/
function islandora_fpr_menu() {
$items = array();
$items['admin/islandora/tools/fpr'] = array(
'title' => 'Islandora FPR',
'description' => 'Configure Islandora FPR.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_fpr_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items['islandora/object/%islandora_object/manage/fpr'] = array(
'title' => 'FPR',
'weight' => '20',
'page callback' => 'islandora_fpr_get_fpr_data',
'page arguments' => array(2),
'access callback' => 'islandora_fits_metadata_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Admin settings form builder.
*/
function islandora_fpr_admin_settings() {
$form['islandora_fpr_fpr_endpoint'] = array(
'#title' => t('FPR endpoint URL'),
'#type' => 'textfield',
'#size' => 60,
'#default_value' => variable_get('islandora_fpr_fpr_endpoint', 'https://fpr.example.com/fpr/api/v2/'),
'#description' => t("URL of the Archivematica Format Policy Registry endpoint."),
'#maxlength' => 255,
);
$form['islandora_fpr_http_timeout'] = array(
'#title' => t('Timeout'),
'#type' => 'textfield',
'#size' => 5,
'#default_value' => variable_get('islandora_fpr_http_timeout', '2'),
'#description' => t("Number of seconds to use as the timeout for HTTP
requests to the FPR endpoint."),
'#maxlength' => 5,
);
return system_settings_form($form);
}
/**
* Fetches data from the remote instance of the FPR.
*
* @param AbstractObject $islandora_object
* The Islandora object.
*
* return string
* A string containing the data to display.
*/
function islandora_fpr_get_fpr_data(AbstractObject $islandora_object) {
$dsid = variable_get('islandora_fits_techmd_dsid', 'TECHMD');
if (!$islandora_object[$dsid]) {
return t("FITS data not available.");
}
else {
$header = array();
$header[] = array("data" => t("PUID"));
$header[] = array("data" => t("Description"));
$header[] = array("data" => t("Access command"));
$header[] = array("data" => t("Preservation command"));
$row = array();
$puid = islandora_fpr_get_puid($islandora_object[$dsid]->content);
if (!$puid) {
return t("No PUID is available in the FITS output, skipping the FPR.");
}
$row[] = l($puid, 'http://apps.nationalarchives.gov.uk/pronom/' . $puid,
array('attributes' => array('target' => '_blank')));
$endpoint = variable_get('islandora_fpr_fpr_endpoint', 'https://fpr.example.com/fpr/api/v2/');
$timeout = (int) variable_get('islandora_fpr_http_timeout', '2');
// Format info request.
$format_request = $endpoint . 'format-version/?pronom_id=' . $puid . '&format=json';
$format_response = drupal_http_request($format_request, array('timeout' => $timeout));
// Check for timeout. We only check here, not on every request.
if ($format_response->code === HTTP_REQUEST_TIMEOUT) {
return t("Can't reach FPR server at @endpoint", array('@endpoint' => $endpoint));
}
$format_response_data = json_decode($format_response->data);
if ($format_response_data->meta->total_count === 0) {
return t("Sorry, the format @format isn't in the FPR.", array('@format' => $puid));
}
$format_uuid = $format_response_data->objects[0]->uuid;
$format_description = $format_response_data->objects[0]->description;
$row[] = $format_description;
// Access rule request.
$access_rule_request = $endpoint .
'fp-rule/?purpose=access&format=json&fmt=' . $format_uuid;
$access_rule_response = drupal_http_request($access_rule_request);
$access_rule_response_data = json_decode($access_rule_response->data);
if ($access_rule_response_data->meta->total_count === 0) {
$row[] = t("n/a (no access rule for format @puid in the FPR)",
array('@puid' => $puid));
}
else {
// Access command request.
$command_parts = explode('/', $access_rule_response_data->objects[0]->command);
$access_command_uuid = $command_parts[5];
$access_command_request = $endpoint . 'fp-command/?format=json&uuid=' .
$access_command_uuid;
$access_command_response = drupal_http_request($access_command_request);
$access_command_response_data = json_decode($access_command_response->data);
if ($access_command_response_data->meta->total_count === 0) {
$row[] = t("n/a");
}
else {
$row[] = $access_command_response_data->objects[0]->command;
islandora_fpr_add_datastream($islandora_object, 'FPR_ACCESS_CMD', $access_command_response_data->objects[0]->command);
}
}
// Preservation rule request.
$preservation_rule_request = $endpoint .
'fp-rule/?purpose=preservation&format=json&fmt=' . $format_uuid;
$preservation_rule_response = drupal_http_request($preservation_rule_request);
$preservation_rule_response_data = json_decode($preservation_rule_response->data);
if ($preservation_rule_response_data->meta->total_count === 0) {
$row[] = t("n/a (no preservation rule for format @puid in the FPR)",
array('@puid' => $puid));
}
else {
// Preservation command request.
$command_parts = explode('/', $preservation_rule_response_data->objects[0]->command);
$preservation_rule_uuid = $command_parts[5];
$preservation_command_request = $endpoint .
'fp-command/?format=json&uuid=' . $preservation_rule_uuid;
$preservation_command_response = drupal_http_request($preservation_command_request);
$preservation_command_response_data = json_decode($preservation_command_response->data);
if ($preservation_command_response_data->meta->total_count == '0') {
$row[] = t("n/a");
}
else {
$row[] = $preservation_command_response_data->objects[0]->command;
islandora_fpr_add_datastream($islandora_object, 'FPR_PRESERVATION_CMD', $preservation_command_response_data->objects[0]->command);
}
}
$rows[] = $row;
return theme('table', array('header' => $header, 'rows' => $rows));;
}
}
/**
* Adds a datastream to an object.
*
* @param AbstractObject $object
* The object to add a datastream to.
* @param string $dsid
* The datastream ID to be added.
* @param string $command
* A string containing the command
*
* @return bool|string
* TRUE if the datastream was added/modified successfully, the error message
* otherwise.
*/
function islandora_fpr_add_datastream(AbstractObject $object, $dsid, $command) {
try {
$command = preg_replace("/\r|\n/", "", $command);
$ingest = !isset($object[$dsid]);
if ($ingest) {
$ds = $object->constructDatastream($dsid, 'M');
$ds->label = $dsid;
$ds->mimeType = 'text/plain';
}
else {
$ds = $object[$dsid];
}
$ds->setContentFromString($command);
if ($ingest) {
$object->ingestDatastream($ds);
}
return TRUE;
}
catch (exception $e) {
$variables = array(
'@dsid' => $dsid,
'@ret' => $e->getTraceAsString(),
);
watchdog('islandora_fpr', 'Failed to add @dsid datastream.<br/>Error: @ret', $variables, WATCHDOG_ERROR);
$message = $e->getMessage();
return $message;
}
}
/**
* Parses an object's FITS datastream to get the PUID.
*
* @param string $fits_xml
* The content of the FITS datastream.
*
* return bool|string
* The PRONOM ID extracted from the FITS XML. Returns
* FALSE if no 'identification' element was found or
* if a conflict was explicitly found.
*/
function islandora_fpr_get_puid($fits_xml) {
$xml = simplexml_load_string($fits_xml);
$xml->registerXPathNamespace('f', 'http://hul.harvard.edu/ois/xml/ns/fits/fits_output');
$expression = "/f:fits/f:identification/f:identity/f:externalIdentifier[@type='puid']";
$puids = $xml->xpath($expression);
if (count($puids)) {
// There should be only one.
return (string) $puids[0];
}
else {
// We probably have a format identification conflict, or FITS has
// not been able to deterime the PUID and has not added an 'identification'
// element.
return FALSE;
}
}