Skip to content

Commit

Permalink
WIP on #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed May 5, 2021
1 parent 9e06628 commit 1aaaca1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Introduction

A Drupal 8 module that serializes PREMIS metadata generated by various Islandora modules and microsrevices. This module produces PREMIS v3 in Turtle RDF for a node by appending `/premis` to the end of the node's URL, e.g., `https://localhost/node/250/premis`.
A Drupal 8/9 module that serializes PREMIS metadata generated by various Islandora modules and microsrevices. This module produces PREMIS v3 in Turtle RDF for a node by appending `/premis` to the end of the node's URL, e.g., `https://localhost/node/250/premis`.

This module is not ready for prodcution use. But could be soon with your feedback and testing!

Expand Down
1 change: 1 addition & 0 deletions islandora_premis.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ description: "Serializes PREMIS data."
type: module
package: Islandora
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
- islandora
21 changes: 8 additions & 13 deletions islandora_premis.module
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use EasyRdf\Graph;
use EasyRdf\RdfNamespace;

/**
* Implements hook_islandora_premis_turtle_alter().
*/
Expand All @@ -8,7 +11,7 @@ function islandora_premis_islandora_premis_turtle_alter($nid, &$turtle) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
$utils = \Drupal::service('islandora.utils');
$media_source = \Drupal::service('islandora.media_source_service');
$gemini = \Drupal::service('islandora.gemini.lookup');
$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.utils');
Expand All @@ -26,29 +29,21 @@ function islandora_premis_islandora_premis_turtle_alter($nid, &$turtle) {
$base_url = \Drupal::request()->getSchemeAndHttpHost();
$resource = $base_url . '/node/' . $nid;
$url = $base_url . '/node/' . $nid . "?_format=jsonld";
$graph = EasyRdf_Graph::newAndLoad($url);
$graph = Graph::newAndLoad($url);
$graph->addType($resource, "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 = $gemini->lookup($file);
if (is_null($file_url)) {
$binary_resource_url = $file_uri_lookup->getUrl($medium->id());
if (is_null($binary_resource_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 EasyRdf_Namespace();
$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/');
Expand Down
3 changes: 3 additions & 0 deletions islandora_premis.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
islandora_premis.utils:
class: Drupal\islandora_premis\Utils
4 changes: 3 additions & 1 deletion src/Controller/IslandoraPremisPremisController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use EasyRdf\Graph;

namespace Drupal\islandora_premis\Controller;

use Drupal\Core\Controller\ControllerBase;
Expand All @@ -24,7 +26,7 @@ public function main() {
\Drupal::moduleHandler()->invokeAll('islandora_premis_turtle_alter', [$nid, &$turtle]);

// Create and serialize the graph, then send it to the client.
$graph = new \EasyRdf_Graph();
$graph = new \EasyRdf\Graph();
$graph->parse($turtle);
$output = $graph->serialise('turtle');

Expand Down
51 changes: 51 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\islandora_premis;

use Drupal\Core\Site\Settings;
use Drupal\media\Entity\Media;

/**
* Islandora-PREMIS specific utilities.
*/
class Utils {

/**
* Get a Fedora URL for a File entity from Gemini.
*
* @param string $mid
* The Meida entity's ID.
*
* @return string
* The Fedora URL to the file corresponding to the Media's ID, or False.
*/
public function getUrl($mid) {
$media = Media::load($mid);
$media_source_service = \Drupal::service('islandora.media_source_service');
$source_file = $media_source_service->getSourceFile($media);

$uri = $source_file->getFileUri();
$scheme = \Drupal::service('stream_wrapper_manager')->getScheme($uri);

if ($scheme == 'fedora') {
$flysystem_config = Settings::get('flysystem');
if (isset($flysystem_config[$scheme]) && $flysystem_config[$scheme]['driver'] == 'fedora') {
$fedora_root = $flysystem_config['fedora']['config']['root'];
$fedora_root = rtrim($fedora_root, '/');
$parts = parse_url($uri);
$path = $parts['host'] . $parts['path'];
}
else {
return false;
}
$path = ltrim($path, '/');
$fedora_uri = "$fedora_root/$path";
return($fedora_uri);
}
else {
// 'public' or 'private' filesystem scheme.
return(file_create_url($uri));
}
}

}

0 comments on commit 1aaaca1

Please sign in to comment.