Commit 72a63fc3 authored by Mark Jordan's avatar Mark Jordan
Browse files

Started work on #2 and #6; some code cleanup.

parent 54f2161e
......@@ -36,61 +36,34 @@ To install the Islandora BagIt Light module:
All configuration for this module is performed either via Drush options and .ini files.
## Submodules
### Extending and customizing the BagIt module
Islandora BagIt Light ...
## Modifying a Bag from your own modules
This module provides a drupal_alter() hook, which allows other modules to use hook_islandora_bagit_alter($bag, $islandora_object). Your module can modify the current Bag using any of the methods provided by the BagItPHP library. Each implementation of this hook must take $bag and $islandora_object as parameters; $islandora_object is provided so you can access properties of the object in your module easily. A typical implementation looks like this:
Sample .ini file:
```
/**
* Implementation of hook drupal_alter().
*
* @param object $bag
* A BagIt object instantiated in the BagIt module.
*
* @param object $islandora_object
* The current $islandora_object.
*/
function mymodule_islandora_bagit_alter($bag, $islandora_object) {
// Add some custom metadata to bag-info.txt.
$bag->setBagInfoData('Some-Arbitrary-Field', 'Foo bar baz');
// Add a file that is not managed by a plugin. Note: extra files
// should be added by plugins if possible, since files that are
// added in drupal_alter() hooks are not counted in Payload-Oxum
// values generated by the Islandora BagIt module.
$bag->addFile('/path/to/file.txt', 'myfile.txt');
// Update the Bag (this is required).
$bag->update();
}
; Paths, etc.
[SYSTEM]
; Entries in this section are added to the bag-info.txt file.
[BAG-INFO]
; Dynamically generated entries like Bagging-Date and Payload-Oxum take a true/false value.
Bagging-Date = true
Payload-Oxum = true
; Simple tag/value pairs are added as is.
Contact-Email = mjordan@sfu.ca
Contact-Phone = 1-778-230-7146
; You can use Drupal tokens in tag values.
Organization-Address = "Visit us at [site:urlxxxx] any time you want."
```
Note that implementations of hook_islandora_bagit_alter() must call $bag->update() themselves, typically at the very end of the function.
Using tokens: https://www.drupal.org/node/390482. Islandora Bagit Light submodules may provide additional tokens.
## Post-Bag-creation hook
## Submodules
Islandora BagIt provides an additional hook, islandora_bagit_post_create, that allows other modules to get notifications that a Bag has just been created. A basic implementation is:
### Extending and customizing the BagIt module
```
/**
* Implements hook_islandora_bagit_post_create().
*
* @param string $pid
* The PID of the Islandora object that the Bag was just created for.
*
* @param string $bag_path
* The path to the Bag, relative to the Drupal installation directory.
*/
function mymodule_islandora_bagit_post_create($pid, $bag_path) {
// Do something interesting.
}
```
Islandora BagIt Light ...
This hook can be used to send notification emails after a Bag has been created, to add the Bag to a queue for further processing, or to copy the Bag to a different server.
Hooks are documented in islandora_bagit_light.api.php.
## Drush integration
......
......@@ -8,17 +8,12 @@
/**
* Creates the object-level Bag.
*
* Also acts as the 'controller' to pass program flow off to the appropriate
* batch functions.
*
* @param object $islandora_object
* The Islandora object to create a Bag for.
*
* @return string|array
* Either an empty array, a blank string, or a string containing
* a link to 'Download the Bag.'
* @param object $pid
* The PID of the Islandora object to create a Bag for.
* @param array $ini
* The .ini settings.
*/
function islandora_bagit_light_create_bag($pid) {
function islandora_bagit_light_create_bag($pid, $ini) {
$islandora_object = islandora_object_load($pid);
// Sanitize the PID so it is usable in file paths.
$pid = str_replace(array(':', '-'), '_', $islandora_object->id);
......@@ -54,7 +49,7 @@ function islandora_bagit_light_create_bag($pid) {
$all_added_files = array();
// Get bag-info.txt metadata.
$bag_info = islandora_bagit_light_create_baginfo();
$bag_info = islandora_bagit_light_create_baginfo($ini);
// Create a new bag.
$bag = new BagIt($bag_output_path, TRUE, TRUE, FALSE, $bag_info);
......@@ -62,11 +57,6 @@ function islandora_bagit_light_create_bag($pid) {
$files_to_add = module_invoke_all('islandora_bagit_get_files', $islandora_object, $tmp_ds_directory);
if (count($files_to_add)) {
// Generate octetstream sum.
if (variable_get('islandora_bagit_payload_octetstream_sum', 0)) {
$sum = islandora_bagit_light_get_octetstream_sum($files_to_add);
$bag->setBagInfoData('Payload-Oxum', $sum);
}
foreach ($files_to_add as $file) {
$bag->addFile($file['source'], $file['dest']);
$all_added_files[] = $file['dest'];
......@@ -78,13 +68,21 @@ function islandora_bagit_light_create_bag($pid) {
drupal_set_message(t('There are no files to add to the Bag.'), 'warning');
watchdog('bagit', 'BagIt Bag not created for !object: no files found.',
array('!object' => $islandora_object->id));
return '';
return;
}
// Allow other modules to modify the Bag using
// mymodule_islandora_bagit_alter($bag, $islandora_object).
drupal_alter('islandora_bagit', $bag, $islandora_object);
// Generate octetstream sum.
// if (variable_get('islandora_bagit_payload_octetstream_sum', 0)) {
if ($ini['BAG-INFO']['Payload-Oxum']) {
$sum = islandora_bagit_light_get_octetstream_sum($files_to_add);
$bag->setBagInfoData('Payload-Oxum', $sum);
$bag->update();
}
// Write out the serialized (i.e., compressed) Bag.
$serialized_bag_path = variable_get('islandora_bagit_bag_output_dir', '/tmp') .
DIRECTORY_SEPARATOR . $bag_file_name;
......@@ -124,23 +122,7 @@ function islandora_bagit_light_create_bag($pid) {
}
// Allow other modules to fire the post-Bag creation hook.
$post_create_data = module_invoke_all('islandora_bagit_post_create', $pid, $serialized_bag_path);
if (variable_get('islandora_bagit_provide_download_link', 1)) {
// file_build_uri() needs a relative path.
if (variable_get('islandora_file_default_scheme') == 'private') {
$drupal_files_path = variable_get('islandora_file_private_path');
}
else {
$drupal_files_path = variable_get('islandora_file_public_path', conf_path() . '/files');
}
$relative_bag_path = preg_replace("#$drupal_files_path#", '', $serialized_bag_path);
$download_path = file_create_url(file_build_uri($relative_bag_path));
return l(t('Download the Bag'), $download_path);
}
else {
return array();
}
module_invoke_all('islandora_bagit_post_create', $pid, $serialized_bag_path);
}
/**
......@@ -167,8 +149,6 @@ function islandora_bagit_light_get_extension($dsid, $mimetype) {
}
/**
* Placeholder line to meet Coder style checks.
*
* Iterates through the Islandora object, saves each specified datastream
* as a file in a temporary directory using the DSID as the filename,
* and returns a list of all the files saved.
......@@ -269,8 +249,6 @@ function islandora_bagit_light_unserialize_bag_object() {
}
/**
* Placeholder line to meet Coder style checks.
*
* Generates the value for the Payload-Oxum metadata tag for
* an object-level Bag. We generate this value for collection-
* level Bags in islandora_bagit_collection_batch_finish_bag().
......@@ -300,8 +278,10 @@ function islandora_bagit_light_get_octetstream_sum($files) {
* @return array
* An array containing the tags' name => value pairs.
*/
function islandora_bagit_light_create_baginfo() {
function islandora_bagit_light_create_baginfo($ini) {
$bag_info = array();
var_dump($ini);
/*
if (strlen(variable_get('islandora_bagit_transferring_organization', ''))) {
$bag_info['Source-Organization'] = variable_get('islandora_bagit_transferring_organization', '');
}
......@@ -320,8 +300,20 @@ function islandora_bagit_light_create_baginfo() {
if (strlen(variable_get('islandora_bagit_profile_uri', ''))) {
$bag_info['BagIt-Profile-Identifier'] = variable_get('islandora_bagit_profile_uri', '');
}
if (variable_get('islandora_bagit_bagging_date', 1)) {
*/
if ($ini['BAG-INFO']['Bagging-Date']) {
$bag_info['Bagging-Date'] = date("Y-m-d");
unset($ini['BAG-INFO']['Bagging-Date']);
}
foreach ($ini['BAG-INFO'] as $tag => $value) {
if (module_exists('token')) {
$bag_info[$tag] = token_replace($value);
}
else {
$bag_info[$tag] = $value;
}
}
return $bag_info;
}
......@@ -49,7 +49,8 @@ function hook_islandora_bagit_post_create($pid, $bag_path) {
* Implementation of drupal_alter().
*
* Implementations can modify the current Bag using any of the
* methods provided by the BagItPHP library.
* methods provided by the BagItPHP library. Implementations
* must always issue a $bag->update().
*
* @param object $bag
* A BagIt object instantiated in the Islandora BagIt Light module.
......@@ -61,11 +62,7 @@ function mymodule_islandora_bagit_alter($bag, $islandora_object) {
// Add some custom metadata to bag-info.txt.
$bag->setBagInfoData('Some-Arbitrary-Field', 'Foo bar baz');
// Add a file that is not managed by a plugin. Note: extra files
// should be added within implementations of hook_islandora_bagit_get_files()
// whenever possible, since files added in drupal_alter() hooks are not
// counted in Payload-Oxum values generated by the Islandora BagIt
// Light module.
// Add a file that is not managed by a plugin.
$bag->addFile('/path/to/file.txt', 'myfile.txt');
// Update the Bag (this is required in all implementations).
......
......@@ -24,6 +24,7 @@ function islandora_bagit_light_drush_command() {
'description' => dt('Creates a Bag for an Islandora object.'),
'options' => array(
'pid' => dt('The PID for the object or collection you want to create a Bag for.'),
'ini' => dt('Full path to an .ini file containing configuration options.'),
),
'examples' => array(
),
......@@ -36,18 +37,21 @@ function islandora_bagit_light_drush_command() {
/**
* Callback function for drush create-islandora-bag.
*
* @param string $type
* Either 'object' (for a single Islandora object) or 'collection'
* (for all objects in a collection).
*
* @param string $pid
* The PID of the Islandora object to create a Bag for.
*/
function drush_islandora_bagit_light_create_islandora_bag($type = 'object', $pid = NULL) {
function drush_islandora_bagit_light_create_islandora_bag() {
if (!file_exists('sites/all/libraries/BagItPHP')) {
return drush_set_error(DRUSH_FRAMEWORK_ERROR, dt('BagIt library not found.'));
}
// Validate the arguments.
if ($path_to_ini = drush_get_option('ini')) {
$ini = parse_ini_file($path_to_ini, TRUE);
}
else {
$ini = array();
}
// List of objects to create Bags for.
if ($pid = drush_get_option('pid')) {
$objects_to_bag = array($pid);
......@@ -61,32 +65,10 @@ function drush_islandora_bagit_light_create_islandora_bag($type = 'object', $pid
module_load_include('inc', 'islandora_bagit_light', 'includes/utilities');
foreach ($objects_to_bag as $object_to_bag_pid) {
try {
islandora_bagit_light_create_bag($object_to_bag_pid);
islandora_bagit_light_create_bag($object_to_bag_pid, $ini);
}
catch (Exception $e) {
drush_print("Sorry, Islandora cannot create the Bag: " . $e->getMessage());
}
}
}
/**
* Includes Tuque files.
*
* @return bool
* TRUE if the API was included, FALSE otherwise.
*/
function _islandora_bagit_light_drush_include_tuque() {
if (!file_exists('sites/all/libraries/tuque')) {
return drush_set_error(DRUSH_FRAMEWORK_ERROR, dt('Tuque API files not found.'));
}
@include_once 'sites/all/libraries/tuque/Datastream.php';
@include_once 'sites/all/libraries/tuque/FedoraApi.php';
@include_once 'sites/all/libraries/tuque/FedoraApiSerializer.php';
@include_once 'sites/all/libraries/tuque/Object.php';
@include_once 'sites/all/libraries/tuque/RepositoryConnection.php';
@include_once 'sites/all/libraries/tuque/Cache.php';
@include_once 'sites/all/libraries/tuque/RepositoryException.php';
@include_once 'sites/all/libraries/tuque/Repository.php';
@include_once 'sites/all/libraries/tuque/FedoraRelationships.php';
return TRUE;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment