Commit d09e97da authored by Mark Jordan's avatar Mark Jordan
Browse files

Further work.

parent 72a63fc3
......@@ -39,23 +39,57 @@ All configuration for this module is performed either via Drush options and .ini
Sample .ini file:
```
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; File paths, logging options, etc. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths, etc.
[SYSTEM]
; Defaults to /yourdrupalinstalldir/sites/all/libraries/BagItPHP. Use
; only if your BagItPHP library is somewhere else.
; bagit_library_path = /use/only/if/not/sites/all/libraries/BagItPHP
; Log creation of Bags to the Drupal system log.
log_bag_creation = true
; Defaults to /tmp/islandora_bagit_temp. Use only if you want a
; different temporary directory.
; temp_dir = /special/temporary/directory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Options for the BagIt directory or file. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[BAG-OUTPUT]
; bag_output_dir
; Base name for the Bags. The object's PID is appended to this value.
bag_file_name_prefix = 'Bag-'
; serialize_bags
; serialized_compression_type
; delete_unserialized_bag
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Entries in this section are added to the bag-info.txt file. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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.
; These two entries like Bagging-Date and Payload-Oxum take a true/false value
; and are generated automatically.
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."
Contact-Email = someone@example.org
Contact-Phone = 1-555-555-5555
; But you can use Drupal tokens in tag values.
Organization-Address = "Visit us at [site:url] any time you want."
```
Using tokens: https://www.drupal.org/node/390482. Islandora Bagit Light submodules may provide additional tokens.
You can insert Drupal tokens in tag values if the [Token](https://www.drupal.org/project/token) module is enabled. Most standard Drupal tokens won't apply to Islandora content, but some [types of tokens](https://www.drupal.org/node/390482), such as tokens that describe site information or users, might be applicable. The [Islandora Pathauto](https://github.com/Islandora/islandora_pathauto) module provides some Islandora-specific tokens, such as `[fedora:pid]`, `[fedora:namespace]`, and `[fedora:label]`. Any module that implements [hook_tokens](https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_tokens/7.x) can provide additional tokens.
## Submodules
......@@ -73,6 +107,24 @@ Bags can be created for individual Islandora objects or for all objects in a giv
where UID is the user ID or user name of the fedoraAdmin user (or equivalent), 'object' or 'collection' indicates whether you want to create a Bag for a single object or a Bag for every member of a collection, and PID is the PID of the Islandora object or collection.
### Using a PID file
copies from Islandora DOI
This module provides a Drush command to assign DOIs to a list of objects identified in a "PID file." The PID file is a simple list of object PIDS, one PID per line, like this:
```
islandora:23
islandora:29
// Comments can be prefixed by // or #.
islandora:107
islandora:2183
```
The command (using a file at `/tmp/pids.txt` containing the above list) is:
`drush islandora_doi_assign_dois --user=admin --pid_file=/tmp/pids.txt`
## Permissions and security
This module is intended for users who have a fairly high level of permissions on a Drupal site. Because the goal is to package up all or some of the datastreams in an Islandora object, users who can create and download Bags should have access to those datastreams. However, the module does check the current users' access to a datastream before adding it to the Bag.
......
......@@ -18,20 +18,29 @@ function islandora_bagit_light_create_bag($pid, $ini) {
// Sanitize the PID so it is usable in file paths.
$pid = str_replace(array(':', '-'), '_', $islandora_object->id);
if ($ini['SYSTEM']['tmp_dir']) {
$tmp_dir = $ini['SYSTEM']['tmp_dir'];
}
else {
$tmp_dir = '/tmp/islandora_bagit_tmp';
}
// Save all the datastreams to a randomly named temporary directory so
// they can be added to the Bag. We delete these files after creating the Bag.
$random_string = substr(md5(rand()), 0, 7);
$tmp_ds_directory = variable_get('islandora_bagit_bag_tmp_dir', file_directory_temp()) .
'/islandora_bagit_tmp/' . $random_string;
$tmp_ds_directory = $tmp_dir . '/islandora_bagit_tmp/' . $random_string;
if (!file_exists($tmp_ds_directory)) {
mkdir($tmp_ds_directory, 0777, TRUE);
}
// Load the BagItPHP library.
$bagit_library_dir = variable_get('islandora_bagit_library_dir', 'BagItPHP');
if ($bagit_library_path = libraries_get_path($bagit_library_dir)) {
require_once $bagit_library_path . '/lib/bagit.php';
if ($ini['SYSTEM']['bagit_library_path']) {
$bagit_library_path = $ini['SYSTEM']['bagit_library_path'];
}
else {
$bagit_library_path = libraries_get_path($bagit_library_dir) . 'BagItPHP/lib/bagit.php';
}
require_once $bagit_library_path;
$bag_file_name = variable_get('islandora_bagit_bag_name', 'Bag-') . $pid;
$bag_output_path = variable_get('islandora_bagit_bag_output_dir', '/tmp') .
......@@ -76,7 +85,6 @@ function islandora_bagit_light_create_bag($pid, $ini) {
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);
......@@ -94,8 +102,7 @@ function islandora_bagit_light_create_bag($pid, $ini) {
}
// Delete the temp directory created by file create plugins, if it exists.
$bag_tmp_dir = variable_get('islandora_bagit_bag_tmp_dir', file_directory_temp()) .
DIRECTORY_SEPARATOR . $pid;
$bag_tmp_dir = $tmp_dir . DIRECTORY_SEPARATOR . $pid;
if (file_exists($bag_tmp_dir)) {
rrmdir($bag_tmp_dir);
}
......@@ -107,7 +114,7 @@ function islandora_bagit_light_create_bag($pid, $ini) {
$all_added_files = array_unique($all_added_files);
$serialized_all_added_files = implode(', ', $all_added_files);
if (variable_get('islandora_bagit_log_bag_creation', 1)) {
if ($ini['SYSTEM']['log_bag_creation']) {
watchdog('islandora_bagit', 'Bag created for PID !pid (!files).',
array('!pid' => $islandora_object->id, '!files' => $serialized_all_added_files));
}
......@@ -317,3 +324,40 @@ function islandora_bagit_light_create_baginfo($ini) {
return $bag_info;
}
/**
* Returns a list of PIDs from a PID file.
*
* @param string $pid_file_path
* The absolute path to the PID file.
*
* @return array
* A list of PIDs.
*/
function islandora_bagit_light_read_pid_file($pid_file_path) {
if (!file_exists($pid_file_path)) {
drush_set_error('PID_FILE_NOT_FOUND',
dt('The specified PID file (!pid_file) does not exist.',
array('!pid_file' => $pid_file_path)));
drupal_exit();
}
$pids = array();
$lines = file($pid_file_path);
foreach ($lines as $pid) {
$pid = trim($pid);
// Skip commented out rows.
if (!preg_match('!(#|//)!', $pid)) {
$pids[] = $pid;
}
}
if (!count($pids) || !$pids) {
drush_set_error('PID_FILE_IS_EMPTY',
dt('The specified PID file (!pid_file) is empty.',
array('!pid_file' => $pid_file_path)));
drupal_exit();
}
return $pids;
}
......@@ -61,6 +61,9 @@ function hook_islandora_bagit_post_create($pid, $bag_path) {
function mymodule_islandora_bagit_alter($bag, $islandora_object) {
// Add some custom metadata to bag-info.txt.
$bag->setBagInfoData('Some-Arbitrary-Field', 'Foo bar baz');
if ($islandora_object['DC']) {
$bag->setBagInfoData('External-Description', $islandora_object['DC']->content);
}
// Add a file that is not managed by a plugin.
$bag->addFile('/path/to/file.txt', 'myfile.txt');
......
......@@ -23,8 +23,9 @@ function islandora_bagit_light_drush_command() {
$items['create-islandora-bag'] = array(
'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.'),
'pid' => dt('The PID for the object you want to create a Bag for.'),
'pid_file' => dt('Absolute path to a file containing PIDs you want to create a Bag for.'),
'ini_file' => dt('Absolute path to an .ini file containing configuration options.'),
),
'examples' => array(
),
......@@ -41,11 +42,12 @@ function islandora_bagit_light_drush_command() {
* The PID of the Islandora object to create a Bag for.
*/
function drush_islandora_bagit_light_create_islandora_bag() {
module_load_include('inc', 'islandora_bagit_light', 'includes/utilities');
if (!file_exists('sites/all/libraries/BagItPHP')) {
return drush_set_error(DRUSH_FRAMEWORK_ERROR, dt('BagIt library not found.'));
}
if ($path_to_ini = drush_get_option('ini')) {
if ($path_to_ini = drush_get_option('ini_file')) {
$ini = parse_ini_file($path_to_ini, TRUE);
}
else {
......@@ -53,6 +55,11 @@ function drush_islandora_bagit_light_create_islandora_bag() {
}
// List of objects to create Bags for.
if ($pids = drush_get_option('pid_file')) {
$path_to_pid_file = drush_get_option('pid_file');
$objects_to_bag = islandora_bagit_light_read_pid_file($path_to_pid_file);
}
// If both options are provided, use the single PID option.
if ($pid = drush_get_option('pid')) {
$objects_to_bag = array($pid);
}
......@@ -62,7 +69,6 @@ function drush_islandora_bagit_light_create_islandora_bag() {
return FALSE;
}
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, $ini);
......
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