Skip to content

Commit

Permalink
Work on #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Nov 22, 2018
1 parent e8b0b2f commit 683cab0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 76 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Beginnings of a Drupal 8 module to provide node-level reports using data from th

## Overview

Currently doesn't do anything useful other than add a "Fixity auditing" menu item to each node of `islandora_object` content type:
Currently under development. So far, adds a "Fixity auditing" menu item to each node of `islandora_object` content type:

![node menu item](docs/islandora_riprap_node_view.png)

and display a very brief report indicating how many successful and failed events are in some sample data:
and displays an list of raw Media URLs for the node:

![details](docs/islandora_riprap_details.png)

The report will eventually look better and more informative than this.
The report will eventually display a report indicating how many successful and failed events for each Media file.

## Requirements

Expand Down
Binary file modified docs/islandora_riprap_details.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion islandora_riprap.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
function islandora_riprap_theme($existing, $type, $theme, $path) {
return [
'islandora_riprap_report' => [
'variables' => ['hello_world' => null],
'variables' => ['report' => null],
],
];
}
Expand Down
193 changes: 122 additions & 71 deletions src/Controller/IslandoraRiprapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,86 +26,137 @@ public function main(NodeInterface $node = NULL) {
$output = $this->getRiprapEvents($nid);
return [
'#theme' => 'islandora_riprap_report',
'#hello_world' => $output,
'#report' => $output,
];
}


/**
* Get various representations of the object.
*/
private function getRiprapEvents($nid) {
try {
$container = \Drupal::getContainer();
$jwt = $container->get('jwt.authentication.jwt');
$auth = 'Bearer ' . $jwt->generateToken();
$client = \Drupal::httpClient();
$options = [
'auth' => [],
'headers' => ['Authorization' => $auth],
'form_params' => []
];
$response = $client->request('GET', 'http://localhost:8000/node/' . $nid . '/media?_format=json', $options);
$code = $response->getStatusCode();
if ($code == 200) {
$body = $response->getBody()->getContents();
}
else {
\Drupal::logger('islandora_riprap')->error('HTTP response code: @code', array('@code' => $code));
}
}
catch (RequestException $e) {
\Drupal::logger('islandora_riprap')->error($e->getMessage());
}

/*
-Query curl -v -u admin:islandora http://localhost:8000/node/266/media?_format=json to get list of media.
@todo: How do we authenticate?
-For each media entry, perform the code at https://github.com/mjordan/islandora_riprap/issues/2.
This will get URLs for each media file.
-If necessary, convert each media file URL into its Fedora equivalent using Gemini. This will only be
necessary if Islandora is using Fedora (perhaps an admin option, or an autodetect if one is available).
The Riprap output will look like the same data below.
-For each media use (Preservation master, etc.), provide a summary of the total number of fixity checks,
plus number of success and failures.
*/

$sample_riprap_output = '[{"event_uuid":"cdecb9ac-5938-4992-b5b7-8ef9e4e94c62","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"fix","timestamp":"2018-10-20T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"suc","event_outcome_detail_note":"Fedora says hi."},{"event_uuid":"f26ed6cc-e6e2-4ebe-9607-5ad7a2e4b857","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"fix","timestamp":"2018-10-29T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"suc","event_outcome_detail_note":"Fedora says hi."},{"event_uuid":"af60c8d5-7504-4be0-a355-06177855b8b8","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"ing","timestamp":"2018-11-20T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"fail","event_outcome_detail_note":""}]';
$riprap_output_as_array = json_decode($sample_riprap_output, true);
$successful_events = 0;
$failed_events = 0;
foreach ($riprap_output_as_array as $event) {
if ($event['event_outcome'] == 'suc') {
$successful_events++;
}
if ($event['event_outcome'] == 'fail') {
$failed_events++;
}
}

$output = "Report from sample Riprap data (3 events): $successful_events successful events, $failed_events failed events.";
return $output;
}
private function getRiprapEvents($nid) {
// Query http://localhost:8000/node/[nid]/media?_format=json to get list of media.
try {
$container = \Drupal::getContainer();
$jwt = $container->get('jwt.authentication.jwt');
$auth = 'Bearer ' . $jwt->generateToken();
$client = \Drupal::httpClient();
$options = [
'auth' => [],
'headers' => ['Authorization' => $auth],
'form_params' => []
];
$response = $client->request('GET', 'http://localhost:8000/node/' . $nid . '/media?_format=json', $options);
$code = $response->getStatusCode();
if ($code == 200) {
$body = $response->getBody()->getContents();
}
else {
\Drupal::logger('islandora_riprap')->error('HTTP response code: @code', array('@code' => $code));
}
}
catch (RequestException $e) {
\Drupal::logger('islandora_riprap')->error($e->getMessage());
return "Sorry, there has been an error, please refer to the system log";
}

/**
* Only show tab on nodes with the 'islandora_object' content type.
*/
public function islandoraContentTypeOnly(NodeInterface $node = NULL) {
return ($node->getType() == 'islandora_object') ? AccessResult::allowed() : AccessResult::forbidden();
}
// Get the URLs for each media associated with the current node.
$media_urls = $this->getMediaUrls($body);
if (count($media_urls) == 0) {
return "This node has no media associated with it.";
}

/**
* Convert from term ID to term name.
*
* @param int $tid
* The term ID.
*
* @return string
* The term name.
$output = $media_urls;

/*
-If necessary, convert each media file URL into its Fedora equivalent using Gemini. This will only be
necessary if Islandora is using Fedora (perhaps an admin option, or an autodetect if one is available).
The Riprap output will look like the same data below.
-For each media use (Preservation master, etc.), provide a summary of the total number of fixity checks,
plus number of success and failures.
*/
public function tidToName($tid) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
return $term->getName();
}

/*
$sample_riprap_output = '[{"event_uuid":"cdecb9ac-5938-4992-b5b7-8ef9e4e94c62","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"fix","timestamp":"2018-10-20T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"suc","event_outcome_detail_note":"Fedora says hi."},{"event_uuid":"f26ed6cc-e6e2-4ebe-9607-5ad7a2e4b857","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"fix","timestamp":"2018-10-29T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"suc","event_outcome_detail_note":"Fedora says hi."},{"event_uuid":"af60c8d5-7504-4be0-a355-06177855b8b8","resource_id":"http:\/\/localhost:8000\/mockrepository\/rest\/11","event_type":"ing","timestamp":"2018-11-20T07:35:04-0800","digest_algorithm":"SHA-1","digest_value":"339e2ebc99d2a81e7786a466b5cbb9f8b3b81377","event_detail":"","event_outcome":"fail","event_outcome_detail_note":""}]';
$riprap_output_as_array = json_decode($sample_riprap_output, true);
$successful_events = 0;
$failed_events = 0;
foreach ($riprap_output_as_array as $event) {
if ($event['event_outcome'] == 'suc') {
$successful_events++;
}
if ($event['event_outcome'] == 'fail') {
$failed_events++;
}
}
$output = "Report from sample Riprap data (3 events): $successful_events successful events, $failed_events failed events.";
*/
return $output;
}

/**
* Only show tab on nodes with the 'islandora_object' content type.
*/
public function islandoraContentTypeOnly(NodeInterface $node = NULL) {
// @todo: 'islandora_object' should be settable as an admin option.
return ($node->getType() == 'islandora_object') ? AccessResult::allowed() : AccessResult::forbidden();
}

/**
* Get a list of all media associated with a node, grouped by Media Use tags.
*
* @param string $results
* The JSON results of the call to the http://localhost:8000/node/[nid]/media?_format=json.
*
* @return array
* An associative array with taxonomy term names as keys and media URLs as values.
*/
public function getMediaUrls($results) {
$array = json_decode($results, true);
// @todo: These fields should probably be settable as admin options.
$media_fields = array(
'field_media_file',
'field_media_image',
'field_media_audio_file',
'field_media_video_file',
);

$media_use_groups = array();
foreach ($array as $media) {
foreach ($media_fields as $media_field) {
if (array_key_exists($media_field, $media)) {
if (!in_array($media['field_media_use'][0]['url'], $media_use_groups)) {
$tag_url = $media['field_media_use'][0]['url'];
}
$media_use_groups[$tag_url][] = $media[$media_field][0]['url'];
}
}
}

foreach ($media_use_groups as $tid_url => $value) {
$parts = explode('/', $tid_url);
$tid = end($parts);
$name = $this->tidToName($tid);
$media_use_groups[$name] = $media_use_groups[$tid_url];
unset($media_use_groups[$tid_url]);
}

return $media_use_groups;
}

/**
* Convert from term ID to term name.
*
* @param int $tid
* The term ID.
*
* @return string
* The term name.
*/
public function tidToName($tid) {
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
return $term->getName();
}

}
9 changes: 8 additions & 1 deletion templates/islandora-riprap-report.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div {{ attributes }}>
<div class="islandora_riprap_report">
{{ hello_world }}
{% for media_name, urls in report %}
<h4>{{ media_name }}</h4>
<ul>
{% for media_url in urls %}
<li>{{ media_url }}</li>
{% endfor %}
</ul>
{% endfor %}
</div>

0 comments on commit 683cab0

Please sign in to comment.