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

Converted all variable_get()s into .ini file options; renamed hooks.

parent d09e97da
......@@ -44,15 +44,18 @@ Sample .ini file:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[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
; Directory where the Scholars' Lab BagIt for PHP library is installed,
; relative to sites/all/libraries. Do not use a leading or trailing slash.
; Defaults to 'BagItPHP'.
; bagit_library_directory = SomeOtherDir
; 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.
; Defaults to 'islandora_bagit_light_temp' within the directory configured
; in Drupal's "Temporary directory" admin setting. Note: this directory
; must exist and be writable by the user running
; the Drush command.
; temp_dir = /special/temporary/directory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
......@@ -60,24 +63,34 @@ log_bag_creation = true
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[BAG-OUTPUT]
; bag_output_dir
; Defaults to 'islandora_bagit_light_output' within the directory
; configured in Drupal's "Temporary directory" admin setting.
; Note: this directory must exist and be writable by the user running
; the Drush command.
; bag_output_dir = /output/dir
; Base name for the Bags. The object's PID is appended to this value.
bag_file_name_prefix = 'Bag-'
; This must be set in every .ini file; there is no default value.
bag_name_prefix = 'islandora_object_bag-'
; serialize_bags
; Whether or not to create a zip or tgz archive of the Bag.
; Defaults to true;
; serialize_bags = false
; serialized_compression_type
; Must be either 'tgz' or 'zip'. Defaults to 'zip'.
; serialized_compression_type = tgz
; delete_unserialized_bag
; Whether or not to delete the uncompressed copy of the Bag after the
; Bag is serialized. Defaults to true. Does not apply if 'serialize_bags'
; is false.
; delete_unserialized_bag = false
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Entries in this section are added to the bag-info.txt file. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[BAG-INFO]
; These two entries like Bagging-Date and Payload-Oxum take a true/false value
; and are generated automatically.
; Dynamically generated entries like Bagging-Date and Payload-Oxum take a true/false value.
Bagging-Date = true
Payload-Oxum = true
......
......@@ -12,6 +12,9 @@
* The PID of the Islandora object to create a Bag for.
* @param array $ini
* The .ini settings.
*
* @return boolean
* TRUE if Bag create, FALSE otherwise.
*/
function islandora_bagit_light_create_bag($pid, $ini) {
$islandora_object = islandora_object_load($pid);
......@@ -22,29 +25,56 @@ function islandora_bagit_light_create_bag($pid, $ini) {
$tmp_dir = $ini['SYSTEM']['tmp_dir'];
}
else {
$tmp_dir = '/tmp/islandora_bagit_tmp';
$tmp_dir = file_directory_temp() . '/islandora_bagit_light_tmp';
}
if ($ini['SYSTEM']['bag_output_dir']) {
$bag_output_dir = $ini['SYSTEM']['bag_output_dir'];
}
else {
$bag_output_dir = file_directory_temp() . '/islandora_bagit_light_output';
}
if ($ini['BAG-OUTPUT']['serialized_compression_type']) {
$bag_compression_type = $ini['BAG-OUTPUT']['serialized_compression_type'];
}
else {
$bag_compression_type = 'zip';
}
if (isset($ini['BAG-OUTPUT']['delete_unserialized_bag'])) {
$delete_unserialized_bag = $ini['BAG-OUTPUT']['delete_unserialized_bag'];
}
else {
$delete_unserialized_bag = TRUE;
}
if (isset($ini['BAG-OUTPUT']['serialize_bags'])) {
$serialize_bags = $ini['BAG-OUTPUT']['serialize_bags'];
}
else {
$serialize_bags = TRUE;
}
// 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 = $tmp_dir . '/islandora_bagit_tmp/' . $random_string;
$tmp_ds_directory = $tmp_dir . '/' . $random_string;
if (!file_exists($tmp_ds_directory)) {
mkdir($tmp_ds_directory, 0777, TRUE);
}
// Load the BagItPHP library.
if ($ini['SYSTEM']['bagit_library_path']) {
$bagit_library_path = $ini['SYSTEM']['bagit_library_path'];
if ($ini['SYSTEM']['bagit_library_directory']) {
$bagit_library_path = $ini['SYSTEM']['bagit_library_directory'];
}
else {
$bagit_library_path = libraries_get_path($bagit_library_dir) . 'BagItPHP/lib/bagit.php';
$bagit_library_path = libraries_get_path('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') .
DIRECTORY_SEPARATOR . $bag_file_name;
$bag_file_name = $ini['BAG-OUTPUT']['bag_name_prefix'] . $pid;
$bag_output_path = $bag_output_dir . DIRECTORY_SEPARATOR . $bag_file_name;
// Because the BagItPHP library does some things by default if the bag output
// directory already exists (like read the fetch.txt file), we always need to
......@@ -61,9 +91,10 @@ function islandora_bagit_light_create_bag($pid, $ini) {
$bag_info = islandora_bagit_light_create_baginfo($ini);
// Create a new bag.
$bag = new BagIt($bag_output_path, TRUE, TRUE, FALSE, $bag_info);
// $bag = new BagIt($bag_output_path, TRUE, TRUE, FALSE, $bag_info);
$bag = new BagIt($bag_output_path, TRUE, FALSE, FALSE, $bag_info);
$files_to_add = module_invoke_all('islandora_bagit_get_files', $islandora_object, $tmp_ds_directory);
$files_to_add = module_invoke_all('islandora_bagit_get_data_files', $islandora_object, $tmp_ds_directory);
if (count($files_to_add)) {
foreach ($files_to_add as $file) {
......@@ -82,7 +113,7 @@ function islandora_bagit_light_create_bag($pid, $ini) {
// Allow other modules to modify the Bag using
// mymodule_islandora_bagit_alter($bag, $islandora_object).
drupal_alter('islandora_bagit', $bag, $islandora_object);
drupal_alter('islandora_bagit_bag', $bag, $islandora_object);
// Generate octetstream sum.
if ($ini['BAG-INFO']['Payload-Oxum']) {
......@@ -91,14 +122,14 @@ function islandora_bagit_light_create_bag($pid, $ini) {
$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;
$compression_type = variable_get('islandora_bagit_compression_type', 'tgz');
$bag->package($serialized_bag_path, $compression_type);
// Serialize (i.e., compress) the Bag.
if ($serialize_bags) {
$serialized_bag_path = $bag_output_dir . DIRECTORY_SEPARATOR . $bag_file_name;
$bag->package($serialized_bag_path, $bag_compression_type);
if (variable_get('islandora_bagit_delete_unserialized_bag', 1)) {
rrmdir($bag_output_path);
if ($delete_unserialized_bag) {
rrmdir($bag_output_path);
}
}
// Delete the temp directory created by file create plugins, if it exists.
......@@ -119,17 +150,15 @@ function islandora_bagit_light_create_bag($pid, $ini) {
array('!pid' => $islandora_object->id, '!files' => $serialized_all_added_files));
}
$serialized_bag_path .= '.' . $compression_type;
if (variable_get('islandora_bagit_show_messages', 1)) {
drupal_set_message(t("Bag created and saved at %path", array(
'%path' => $serialized_bag_path,
)));
drupal_set_message(t("Files added: %files",
array('%files' => $serialized_all_added_files)));
}
// @todo: this hook should be invoked even if the bag is not serialzied.
// Allow other modules to fire the post-Bag creation hook.
module_invoke_all('islandora_bagit_post_create', $pid, $serialized_bag_path);
$serialized_bag_path .= '.' . $bag_compression_type;
module_invoke_all('islandora_bagit_bag_created', $pid, $serialized_bag_path);
// @todo: Return TRUE if Bag created, FALSE otherwise. Maybe we need to
// wrap some actions in try/catch?
return TRUE;
}
/**
......@@ -207,54 +236,6 @@ function islandora_bagit_light_retrieve_datastreams($islandora_object, $tmp_ds_d
return $ds_files;
}
/**
* Serialize and write to a file the Bag object.
*
* Saving the Bag to a $_SESSION variable resulted in unavoidable
* 'incomplete object' errors. Note: This has nothing to do with
* serializing (zipping) a Bag, it's a replacement for using Drupal's
* $_SESSION.
*
* @param object $bag
* The Bag object.
*
* @param string $pid
* The PID of the collection object that the Bag is being created for.
* This PID should already be sanitized so that it can be used in file paths.
*
* @param string $tmp_ds_directory
* The randomly-generated directory path to the temporary files used in
* creation of this Bag.
*/
function islandora_bagit_light_serialize_bag_object($bag, $pid, $tmp_ds_directory) {
// We can't pass any arguments to islandora_bagit_unserialize_bag_object()
// so save the path to the serialized data in the session.
$_SESSION['islandora_bagit_tmp_ds_directory'] = $tmp_ds_directory;
$serialized_bag_blob = serialize($bag);
file_put_contents($tmp_ds_directory . DIRECTORY_SEPARATOR .
'serialized_bag_object.dat', $serialized_bag_blob
);
}
/**
* Placeholder line to meet Coder style checks.
*
* Unserialize a Bag object serialized by
* islandora_bagit_serialize_bag_object(). Note: The BagitPHP library
* will need to be loaded in the scope that this function is called in,
* otherwise we get fatal 'incomplete object' errors.
*
* @return object
* The unserialized Bag object.
*/
function islandora_bagit_light_unserialize_bag_object() {
$tmp_ds_directory = $_SESSION['islandora_bagit_tmp_ds_directory'];
$path = $tmp_ds_directory . DIRECTORY_SEPARATOR . 'serialized_bag_object.dat';
$serialized_bag_string = file_get_contents($path);
$bag = unserialize($serialized_bag_string);
return $bag;
}
/**
* Generates the value for the Payload-Oxum metadata tag for
* an object-level Bag. We generate this value for collection-
......@@ -287,27 +268,6 @@ function islandora_bagit_light_get_octetstream_sum($files) {
*/
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', '');
}
if (strlen(variable_get('islandora_bagit_transferring_organization_address', ''))) {
$bag_info['Organization-Address'] = variable_get('islandora_bagit_transferring_organization_address', '');
}
if (strlen(variable_get('islandora_bagit_contact_name', ''))) {
$bag_info['Contact-Name'] = variable_get('islandora_bagit_contact_name', '');
}
if (strlen(variable_get('islandora_bagit_contact_phone', ''))) {
$bag_info['Contact-Phone'] = variable_get('islandora_bagit_contact_phone', '');
}
if (strlen(variable_get('islandora_bagit_contact_email', ''))) {
$bag_info['Contact-Email'] = variable_get('islandora_bagit_contact_email', '');
}
if (strlen(variable_get('islandora_bagit_profile_uri', ''))) {
$bag_info['BagIt-Profile-Identifier'] = variable_get('islandora_bagit_profile_uri', '');
}
*/
if ($ini['BAG-INFO']['Bagging-Date']) {
$bag_info['Bagging-Date'] = date("Y-m-d");
unset($ini['BAG-INFO']['Bagging-Date']);
......
......@@ -6,7 +6,7 @@
*/
/**
* Returns an array of source and destination file paths.
* Returns an array of files paths to add to the Bag's data directory..
*
* @param object $islandora_object
* The Islandora object to create a Bag for.
......@@ -15,11 +15,11 @@
* The temporary directory where the datastream or other files have been downloaded.
*
* @return array
* An array of source and destination file paths, which should be
* if no files are availale or present. The destination file path
* should be relative to the Bag's /data directory.
* An array of files, each with a source and a destination file path.
* The source path is absolute and the destination path is relative
* to the Bag's /data directory.
*/
function hook_islandora_bagit_get_files($islandora_object, $tmp_ds_directory) {
function hook_islandora_bagit_get_data_files($islandora_object, $tmp_ds_directory) {
$files_to_add = array();
$my_file_path = $tmp_ds_directory . DIRECTORY_SEPARATOR . 'myfile.txt';
......@@ -40,13 +40,13 @@ function hook_islandora_bagit_get_files($islandora_object, $tmp_ds_directory) {
* @param string $bag_path
* The path to the new Bag.
*/
function hook_islandora_bagit_post_create($pid, $bag_path) {
function hook_islandora_bagit_bag_created($pid, $bag_path) {
watchdog('bagit', 'Bag created for !object at !path.',
array('!object' => $pid, '!path' => $bag_path));
}
/**
* Implementation of drupal_alter().
* Allows other modules to perform a drupal_alter() on Bags.
*
* Implementations can modify the current Bag using any of the
* methods provided by the BagItPHP library. Implementations
......@@ -58,7 +58,7 @@ function hook_islandora_bagit_post_create($pid, $bag_path) {
* @param object $islandora_object
* The current $islandora_object.
*/
function mymodule_islandora_bagit_alter($bag, $islandora_object) {
function mymodule_islandora_bagit_bag_alter($bag, $islandora_object) {
// Add some custom metadata to bag-info.txt.
$bag->setBagInfoData('Some-Arbitrary-Field', 'Foo bar baz');
if ($islandora_object['DC']) {
......
......@@ -71,10 +71,14 @@ function drush_islandora_bagit_light_create_islandora_bag() {
foreach ($objects_to_bag as $object_to_bag_pid) {
try {
islandora_bagit_light_create_bag($object_to_bag_pid, $ini);
if (islandora_bagit_light_create_bag($object_to_bag_pid, $ini)) {
drush_log(dt("Bag created for !pid", array('!pid' => $object_to_bag_pid)), 'ok');
}
}
catch (Exception $e) {
drush_print("Sorry, Islandora cannot create the Bag: " . $e->getMessage());
drush_set_error('BAG_NOT_CREATED',
dt('Bag not created for !pid.', array('!pid' => $object_to_bag_pid)));
drush_print("Exception message: " . $e->getMessage());
}
}
}
......@@ -5,12 +5,12 @@
*/
/**
* Implements islandora_bagit_get_files().
* Implements hook_islandora_bagit_get_data_files().
*
* Writes out a file for each datastream, and returns an array of
* their paths.
*/
function islandora_bagit_all_ds_islandora_bagit_get_files($islandora_object, $tmp_ds_directory) {
function islandora_bagit_all_ds_islandora_bagit_get_data_files($islandora_object, $tmp_ds_directory) {
$files_to_add = array();
module_load_include('inc', 'islandora_bagit_light', 'includes/utilities');
$ds_files = islandora_bagit_light_retrieve_datastreams($islandora_object, $tmp_ds_directory);
......
......@@ -5,11 +5,11 @@
*/
/**
* Implements islandora_bagit_get_files().
* Implements hook_islandora_bagit_get_data_files().
*
* Writes out the FOXML for the object, and returns its path.
*/
function islandora_bagit_foxml_islandora_bagit_get_files($islandora_object, $tmp_ds_directory) {
function islandora_bagit_foxml_islandora_bagit_get_data_files($islandora_object, $tmp_ds_directory) {
// We don't call islandora_bagit_light_retrieve_datastreams() here because this
// plugin doesn't add any datastreams.
$files_to_add = array();
......
......@@ -5,11 +5,11 @@
*/
/**
* Implements islandora_bagit_get_files().
* Implements hook_islandora_bagit_get_data_files().
*
* Writes out the PREMIS file for the object and returns its path.
*/
function islandora_bagit_premis_islandora_bagit_get_files($islandora_object, $tmp_ds_directory) {
function islandora_bagit_premis_islandora_bagit_get_data_files($islandora_object, $tmp_ds_directory) {
$files_to_add = array();
if (module_exists('islandora_premis')) {
module_load_include('inc', 'islandora_premis', 'includes/utilities');
......
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