-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathislandora_riprap.module
267 lines (239 loc) · 9.34 KB
/
islandora_riprap.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
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
<?php
use EasyRdf\Graph;
/**
* @file
* Contains the islandora_riprap.module.
*/
/**
* Implements hook_theme().
*/
function islandora_riprap_theme($existing, $type, $theme, $path) {
return [
'islandora_riprap_summary' => [
'variables' => ['content' => NULL, 'outcome' => NULL, 'mid' => NULL],
],
'islandora_riprap_media_events' => [
'variables' => [
'report' => NULL,
'mid' => NULL,
'binary_resource_url' => NULL,
],
'variables' => [
'report' => NULL,
'mid' => NULL,
'binary_resource_url' => NULL,
],
],
'islandora_riprap_events_report' => [
'variables' => [],
],
];
}
/**
* Default preprocessor function for the islandora_riprap_theme hook.
*/
function template_preprocess_islandora_riprap_summary(&$variables) {
$variables['attributes'] = [
'class' => [$variables['outcome']],
];
}
/**
* Default preprocessor function for the islandora_riprap_theme hook.
*/
function template_preprocess_islandora_riprap_media_events(&$variables) {
$variables['attributes'] = [
'id' => ['islandora_riprap_media_events'],
];
}
/**
* Default preprocessor function for the islandora_riprap_theme hook.
*/
function template_preprocess_islandora_riprap_events_report(&$variables) {
$variables['attributes'] = [
'id' => ['islandora_riprap_events_report'],
];
}
/**
* Implements hook_page_attachments().
*/
function islandora_riprap_page_attachments(array &$attachments) {
$config = \Drupal::config('islandora_riprap.settings');
$current_path = \Drupal::service('path.current')->getPath();
$path_args = explode('/', ltrim($current_path, '/'));
// E.g., the Manage Media View for each Islandora object.
if (count($path_args) >= 3 && $path_args[0] == 'node' && $path_args[2] == 'media') {
$attachments['#attached']['library'][] = 'islandora_riprap/islandora_riprap_media';
}
if ($current_path == '/admin/reports/islandora_riprap') {
$riprap_utils = \Drupal::service('islandora_riprap.riprap');
if ($config->get('use_sample_failed_fixity_events')) {
$riprap_data = $riprap_utils->getSampleFailedFixityEventsReportData();
$dataset = new StdClass();
$dataset->data = array_values($riprap_data);
$total_failed_events = array_sum($dataset->data);
$dataset->label = t("@total (sample) failed fixity check events, broken down by month", ['@total' => $total_failed_events]);
$dataset->borderColor = 'red';
$dataset->pointBackgroundColor = 'red';
$dataset->borderWidth = 1;
$dataset->pointRadius = 5;
$dataset->fill = FALSE;
$dataset->lineTension = 0;
$line_chart_data = ['labels' => array_keys($riprap_data), 'datasets' => [$dataset]];
$attachments['#attached']['library'][] = 'islandora_riprap/islandora_riprap_chart';
$attachments['#attached']['drupalSettings']['islandora_riprap']['chart_data'] = $line_chart_data;
\Drupal::messenger()->addMessage(t('(Sample) failed fixity check events found. See monthly summary below.'), 'warning');
return;
}
else {
$riprap_data = $riprap_utils->getFailedFixityEventsReportData();
$total_failed_events = array_sum(array_values($riprap_data));
}
if ($total_failed_events == 0) {
$attachments['#attached']['library'][] = 'islandora_riprap/islandora_riprap_no_failed_fixity_events';
\Drupal::logger('islandora_riprap')->info(t('Failed fixity check report has found no events.'));
\Drupal::messenger()->addMessage(t('No failed fixity events found.'), 'status');
}
else {
$attachments['#attached']['library'][] = 'islandora_riprap/islandora_riprap_chart';
$dataset = new StdClass();
$dataset->data = array_values($riprap_data);
$dataset->label = t("@total failed fixity check events, broken down by month", ['@total' => $total_failed_events]);
$dataset->borderColor = 'red';
$dataset->pointBackgroundColor = 'red';
$dataset->borderWidth = 1;
$dataset->pointRadius = 5;
$dataset->fill = FALSE;
$dataset->lineTension = 0;
$line_chart_data = [
'labels' => array_keys($riprap_data),
'datasets' => [$dataset],
];
$attachments['#attached']['drupalSettings']['islandora_riprap']['chart_data'] = $line_chart_data;
\Drupal::messenger()->addMessage(t('Failed fixity check events found. See monthly summary below.'), 'warning');
\Drupal::logger('islandora_riprap')->warning(t('Failed fixity check report has found @total events.', ['@total' => $total_failed_events]));
}
}
}
/**
* Implements hook_cron().
*/
function islandora_riprap_cron() {
$config = \Drupal::config('islandora_riprap.settings');
$riprap = \Drupal::service('islandora_riprap.riprap');
if (!$config->get('execute_riprap_in_cron')) {
return;
}
if ($config->get('riprap_mode') == 'local') {
$success = $riprap->executeRiprapCheckFixity();
}
if ($success) {
\Drupal::logger('islandora_riprap')->info(t('Riprap successfully ran from Drupal cron.'));
}
else {
\Drupal::logger('islandora_riprap')->error(t('Riprap failed to run from Drupal cron.'));
}
}
/**
* Alter the serialized Turtle.
*
* @param string $nid
* The current node's ID.
* @param string $turtle
* The serialized Turtle.
*/
function islandora_riprap_islandora_premis_turtle_alter($nid, &$turtle) {
$config = \Drupal::config('islandora_riprap.settings');
$riprap = \Drupal::service('islandora_riprap.riprap');
$media_source = \Drupal::service('islandora.media_source_service');
$islandora_utils = \Drupal::service('islandora.utils');
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$media = $islandora_utils->getMedia($node);
if (count($media) > 0) {
foreach ($media as $medium) {
$file = $media_source->getSourceFile($medium);
if ($config->get('use_drupal_urls')) {
$binary_resource_url = $riprap->getLocalUrl($medium->id());
}
else {
$binary_resource_url = $riprap->getFedoraUrl($medium->id());
}
// Create the graph from existing Turtle.
$graph = new Graph();
$graph->parse($turtle);
// Get fixity events and express them as PREMIS RDF.
$riprap_events = $riprap->getEvents(['output_format' => 'json', 'resource_id' => $binary_resource_url]);
$riprap_events = json_decode($riprap_events, TRUE);
foreach ($riprap_events as $event) {
$graph->add($binary_resource_url, 'premis:fixity', $event['event_uuid']);
}
foreach ($riprap_events as $event) {
$fixity_event = $graph->resource($event['event_uuid'], ['http://id.loc.gov/vocabulary/preservation/eventType/fix', 'crypHashFunc:' . $event['digest_algorithm']]);
$fixity_event->set('rdf:value', $event['digest_value']);
$fixity_event->set('dc:created', $event['timestamp']);
$fixity_event->set('premis:outcome', $event['event_outcome']);
}
$turtle = $graph->serialise('turtle');
}
}
}
/**
* Implements hook_requirements().
*/
function islandora_riprap_requirements($phase) {
$requirements = [];
$riprap_utils = \Drupal::service('islandora_riprap.riprap');
if ($phase == 'runtime') {
$config = \Drupal::config('islandora_riprap.settings');
$mode = $config->get('riprap_mode');
// If using Riprap as a remote microserive,
// check whether Riprap's endpoint is available.
if ($mode == 'remote') {
$riprap_endpoint = $config->get('riprap_rest_endpoint');
$riprap_response_code = 0;
try {
$client = \Drupal::httpClient();
// Assumes Riprap requires no authentication
// (e.g., it's behind the Symfony or other firewall).
$response = $client->request('HEAD', $riprap_endpoint);
$riprap_response_code = $response->getStatusCode();
}
catch (Exception $e) {
\Drupal::logger('islandora_riprap')->error($e->getMessage());
}
finally {
if ($riprap_response_code == 200) {
$requirements['islandora_riprap_microservice_available'] = [
'title' => t("Islandora Riprap"),
'description' => t("Riprap is running at @endpoint. @chart_link.", ['@endpoint' => $riprap_endpoint, '@chart_link' => $riprap_utils->getLinkToFailedFixityEventsReport()]),
'severity' => REQUIREMENT_INFO,
];
}
else {
$requirements['islandora_riprap_microservice_available'] = [
'title' => t("Islandora Riprap"),
'description' => t("Riprap not found or is not running at @endpoint", ['@endpoint' => $riprap_endpoint]),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
if ($mode == 'local') {
$riprap_directory = $config->get('riprap_local_directory');
if (file_exists($riprap_directory)) {
$requirements['riprap_directory_exists'] = [
'title' => t("Islandora Riprap"),
'description' => t("Riprap is installed at @directory. @chart_link.", ['@directory' => $riprap_directory, '@chart_link' => $riprap_utils->getLinkToFailedFixityEventsReport()]),
'severity' => REQUIREMENT_INFO,
];
}
else {
$requirements['riprap_directory_exists'] = [
'title' => t("Islandora Riprap"),
'description' => t("Riprap is not installed at @directory", ['@directory' => $riprap_directory]),
'severity' => REQUIREMENT_ERROR,
];
}
}
}
return $requirements;
}