Skip to content

Commit

Permalink
Resolves #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Dec 28, 2018
1 parent 3f9f2be commit f0d6464
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Islandora Bagger

Tool to generate [Bags](https://en.wikipedia.org/wiki/BagIt) for objects using Islandora's REST interface. Specific content is added to the Bag's `data` directory and `bag-info.txt` file using plugins. Bags are compliant with version 0.96 of the BagIt specification.
Command-line tool to generate [Bags](https://en.wikipedia.org/wiki/BagIt) for objects using Islandora's REST interface. Specific content is added to the Bag's `data` directory and `bag-info.txt` file using plugins. Bags are compliant with version 0.96 of the BagIt specification.

This utility is for Islandora 8.x-1.x (CLAW). For creating Bags for Islandora 7.x, use [Islandora Fetch Bags](https://github.com/mjordan/islandora_fetch_bags).

Expand Down Expand Up @@ -79,6 +79,11 @@ fedora_base_url: 'http://localhost:8080/fcrepo/rest/'
# from the "Islandora Media Use" vocabulary. Use an emply list (e.g., [])
# to include all media.
drupal_media_tags: ['/taxonomy/term/15']

# Used by the 'AddMedia' plugin. Indicates whether the Bag should contain a file
# named 'media_use_summary.tsv' that lists all the media files plus the taxonomy
# name corresponding to the 'drupal_media_tags' list. Default is false.
include_media_use_list: true.
```
The command to generate a Bag takes two required parameters, `--settings` and `--node`. Assuming the above configuration file is named `sample_config.yml`, and the Drupal node ID you want to generate a Bag from is 112, the command would look like this:
Expand Down Expand Up @@ -120,7 +125,7 @@ Apart from the static tags mentioned in the previous section, all file content a

#### Included plugins

The following plugins are bunlded with Islandora Bagger:
The following plugins are bundled with Islandora Bagger:

* AddBasicTags: Adds the `Internal-Sender-Identifier` bag-info.txt tag using the Drupal URL for the node as its value, and the `Bagging-Date` tag using the current date as its value.
* AddNodeJson: Adds the Drupal JSON representation of the node, specifically, the response to a request to `/node/1234?_format=json`.
Expand Down
5 changes: 5 additions & 0 deletions sample_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ fedora_base_url: 'http://localhost:8080/fcrepo/rest/'
# from the "Islandora Media Use" vocabulary. Use an emply list (e.g., [])
# to include all media.
drupal_media_tags: ['/taxonomy/term/15']

# Used by the 'AddMedia' plugin. Indicates whether the Bag should contain a file
# named 'media_use_summary.tsv' that lists all the media files plus the taxonomy
# name corresponding to the 'drupal_media_tags' list. Default is false.
include_media_use_list: true.
7 changes: 3 additions & 4 deletions src/Command/CreateBagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$nid = $input->getOption('node');
$settings_path = $input->getOption('settings');
$this->settings = Yaml::parseFile($settings_path);
$this->settings['drupal_base_url'] .= '/node/';

// Set some configuration defaults.
$this->settings['http_timeout'] = (!isset($this->settings['http_timeout'])) ?
Expand All @@ -63,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$client = new \GuzzleHttp\Client();

// Get the node's UUID from Drupal.
$drupal_url = $this->settings['drupal_base_url'] . $nid . '?_format=json';
$drupal_url = $this->settings['drupal_base_url'] . '/node/' . $nid . '?_format=json';
$response = $client->get($drupal_url);
$response_body = (string) $response->getBody();
$node_json = $response_body;
Expand Down Expand Up @@ -113,12 +112,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bag_name = $bag_name . '.' . $package;
}

$io->success("Bag created for " . $this->settings['drupal_base_url'] . $nid . " at " . $bag_dir);
$io->success("Bag created for " . $this->settings['drupal_base_url'] . '/node/' . $nid . " at " . $bag_dir);
if ($this->settings['log_bag_creation']) {
$this->logger->info(
"Bag created.",
array(
'node URL' => $this->settings['drupal_base_url'] . $nid,
'node URL' => $this->settings['drupal_base_url'] . '/node/' . $nid,
'node UUID' => $uuid,
'Bag location' => $this->settings['output_dir'],
'Bag name' => $bag_name
Expand Down
34 changes: 33 additions & 1 deletion src/Plugin/AddMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public function __construct($settings, $logger)
*/
public function execute($bag, $bag_temp_dir, $nid, $node_json)
{
$this->settings['include_media_use_list'] = (!isset($this->settings['include_media_use_list'])) ?
false: $this->settings['include_media_use_list'];

// Get the media associated with this node using the Islandora-supplied Manage Media View.
$media_client = new \GuzzleHttp\Client();
$media_url = $this->settings['drupal_base_url'] . $nid . '/media';
$media_url = $this->settings['drupal_base_url'] . '/node/' . $nid . '/media';
$media_response = $media_client->request('GET', $media_url, [
'http_errors' => false,
'auth' => $this->settings['drupal_media_auth'],
Expand All @@ -45,6 +48,9 @@ public function execute($bag, $bag_temp_dir, $nid, $node_json)

// Loop through all the media and pick the ones that are tagged with terms in $this->settings['drupal_media_tags'].
// If that list is empty, add all media to the Bag.
if ($this->settings['include_media_use_list']) {
$file_use_list = '';
}
foreach ($media_list as $media) {
if (count($media['field_media_use'])) {
foreach ($media['field_media_use'] as $term) {
Expand All @@ -56,6 +62,13 @@ public function execute($bag, $bag_temp_dir, $nid, $node_json)
$file_url = $media['field_media_file'][0]['url'];
}
$filename = $this->getFilenameFromUrl($file_url);

if ($this->settings['include_media_use_list']) {
$term_info = $this->fetchTermInfo($term['url']);
$term_external_uri = $term_info['field_external_uri'][0]['uri'];
$file_use_list .= $filename . "\t" . $term_external_uri . PHP_EOL;
}

$temp_file_path = $bag_temp_dir . DIRECTORY_SEPARATOR . $filename;
// Fetch file and save it to $bag_temp_dir with its original filename.
// @todo: Determine what to do if the file already exists.
Expand All @@ -74,6 +87,11 @@ public function execute($bag, $bag_temp_dir, $nid, $node_json)
}
}
}
if ($this->settings['include_media_use_list']) {
$temp_file_path = $bag_temp_dir . DIRECTORY_SEPARATOR . 'media_use_summary.tsv';
file_put_contents($temp_file_path, $file_use_list);
$bag->addFile($temp_file_path, 'media_use_summary.tsv');
}
return $bag;
}

Expand All @@ -83,4 +101,18 @@ protected function getFilenameFromUrl($url)
$filename = pathinfo($path, PATHINFO_BASENAME);
return $filename;
}

protected function fetchTermInfo($term)
{
$client = new \GuzzleHttp\Client();
$url = $this->settings['drupal_base_url'] . $term;
$response = $client->request('GET', $url, [
'http_errors' => false,
'auth' => $this->settings['drupal_media_auth'],
'query' => ['_format' => 'json']
]);
$body = (string) $response->getBody();
$tag_info = json_decode($body, true);
return $tag_info;
}
}
2 changes: 1 addition & 1 deletion src/Plugin/AddMediaJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($settings, $logger)
public function execute($bag, $bag_temp_dir, $nid, $node_json)
{
$client = new \GuzzleHttp\Client();
$media_url = $this->settings['drupal_base_url'] . $nid . '/media';
$media_url = $this->settings['drupal_base_url'] . '/node/' . $nid . '/media';
$response = $client->request('GET', $media_url, [
'http_errors' => false,
'auth' => $this->settings['drupal_media_auth'],
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/AddMediaJsonld.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($settings, $logger)
public function execute($bag, $bag_temp_dir, $nid, $node_json)
{
$client = new \GuzzleHttp\Client();
$media_url = $this->settings['drupal_base_url'] . $nid . '/media';
$media_url = $this->settings['drupal_base_url'] . '/node/' . $nid . '/media';
$response = $client->request('GET', $media_url, [
'http_errors' => false,
'auth' => $this->settings['drupal_media_auth'],
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/AddNodeJsonld.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($settings, $logger)
public function execute($bag, $bag_temp_dir, $nid, $node_json)
{
$client = new \GuzzleHttp\Client();
$url = $this->settings['drupal_base_url'] . $nid;
$url = $this->settings['drupal_base_url'] . '/node/' . $nid;
$response = $client->request('GET', $url, [
'http_errors' => false,
'query' => ['_format' => 'jsonld']
Expand Down

0 comments on commit f0d6464

Please sign in to comment.