Browse Source

Solution pack required objects now takes NewFedoraObjects rather than arrays.

pull/220/head
Nigel Banks 12 years ago
parent
commit
a330997c63
  1. 715
      includes/solution_packs.inc
  2. 14
      includes/utilities.inc

715
includes/solution_packs.inc

@ -9,157 +9,140 @@
/** /**
* Solution pack admin page callback. * Solution pack admin page callback.
*
* @return string
* The html repersentation of all solution pack forms for required objects.
*/ */
function islandora_solution_packs_admin() { function islandora_solution_packs_admin() {
module_load_include('inc', 'islandora', 'includes/utilities'); module_load_include('inc', 'islandora', 'includes/utilities');
drupal_add_css(drupal_get_path('module', 'islandora') . '/css/islandora.admin.css'); if (($connection = islandora_get_tuque_connection()) === NULL) {
if (!islandora_describe_repository()) { islandora_display_repository_inaccessible_message();
$message = t('Could not connect to the repository. Please check the settings on the ' . return;
'<a href="@config_url" title="Islandora configuration page">Islandora configuration</a> page.',
array('@config_url' => url('admin/islandora/configure')));
drupal_set_message($message, 'error');
} }
$enabled_solution_packs = module_invoke_all('islandora_required_objects'); drupal_add_css(drupal_get_path('module', 'islandora') . '/css/islandora.admin.css');
$output = ''; $output = '';
$enabled_solution_packs = module_invoke_all('islandora_required_objects', $connection);
foreach ($enabled_solution_packs as $solution_pack_module => $solution_pack_info) { foreach ($enabled_solution_packs as $solution_pack_module => $solution_pack_info) {
$objects = array(); // @todo We should probably get the title of the solution pack from the
foreach ($solution_pack_info as $field => $value) { // systems table for consistency in the interface.
switch ($field) { $solution_pack_name = $solution_pack_info['title'];
case 'title': $objects = array_filter($solution_pack_info['objects']);
$solution_pack_name = $value; $form = drupal_get_form('islandora_solution_pack_form_' . $solution_pack_module, $solution_pack_module, $solution_pack_name, $objects);
break; $output .= drupal_render($form);
case 'objects':
$objects = $value;
break;
}
}
$form_array = drupal_get_form('islandora_solution_pack_form_' . $solution_pack_module, $solution_pack_module, $solution_pack_name, $objects);
$output .= drupal_render($form_array);
} }
return $output; return $output;
} }
/** /**
* Solution pack admin page * A solution pack form for the given module, it lists all the given objects and
* their status, allowing the user to re-ingest them.
*
* @param array $form
* The Drupal form definition.
* @param array $form_state
* The Drupal form state.
* @param string $solution_pack_module
* The module which requires the given objects.
* @param string $solution_pack_name
* The name of the solution pack to display to the users.
* @param array $objects
* An array of NewFedoraObjects which describe the state in which objects
* should exist.
*
* @return array
* The Drupal form definition.
*/ */
function islandora_solution_pack_form($form, &$form_state, $solution_pack_module, $solution_pack_name, $objects = array()) { function islandora_solution_pack_form(array $form, array &$form_state, $solution_pack_module, $solution_pack_name, $objects = array()) {
global $base_url; // The order is important in terms of severity of the status, where higher
$needs_update = FALSE; // index indicates the status is more serious, this will be used to determine
$needs_install = FALSE; // what messages get displayed to the user.
$could_not_connect = FALSE; $ok_image = theme_image(array('path' => 'misc/watchdog-ok.png', 'attributes' => array()));
$form = array(); $warning_image = theme_image(array('path' => 'misc/watchdog-warning.png', 'attributes' => array()));
$status_info = array(
$form['solution_pack'] = array( 'up_to_date' => array(
'#type' => 'fieldset', 'object' => t('Up-to-date'),
'#collapsible' => FALSE, 'solution_pack' => t('All required objects are installed and up-to-date.'),
'#collapsed' => FALSE, 'image' => $ok_image,
'#attributes' => array('class' => array('islandora-solution-pack-fieldset')), 'button' => t("Force reinstall objects"),
),
'out_of_date' => array(
'object' => t('Out-of-date'),
'solution_pack' => t('Some objects must be reinstalled. See objects list for details.'),
'image' => $warning_image,
'button' => t("Reinstall objects")
),
'missing_datastream' => array(
'object' => t('Missing Datastream'),
'solution_pack' => t('Some objects must be reinstalled. See objects list for details.'),
'image' => $warning_image,
'button' => t("Reinstall objects")
),
'missing' => array(
'object' => t('Missing'),
'solution_pack' => t( 'Some objects are missing and must be installed. See objects list for details.'),
'image' => $warning_image,
'button' => t("Install objects")
),
); );
$status_severities = array_keys($status_info);
// adding values $solution_pack_status_severity = array_search('up_to_date', $status_severities);
$form['solution_pack']['solution_pack_module'] = array(
'#type' => 'value',
'#value' => $solution_pack_module,
);
$form['solution_pack']['solution_pack_name'] = array(
'#type' => 'value',
'#value' => $solution_pack_name,
);
$form['solution_pack']['objects'] = array(
'#type' => 'value',
'#value' => $objects,
);
$table_header = array(t('Label'), t('PID'), t('Status'));
$table_rows = array(); $table_rows = array();
foreach ($objects as $object) { foreach ($objects as $object) {
$datastreams = NULL; $object_status = islandora_check_object_status($object);
if (isset($object['pid'])) { $object_status_info = $status_info[$object_status];
$pid = $object['pid']; $object_status_severity = array_search($object_status, $status_severities);
$table_row = array(); // The solution pack status severity will be the highest severity of the objects.
$object_status = islandora_check_object_status($object); $solution_pack_status_severity = max($solution_pack_status_severity, $object_status_severity);
switch ($object_status) { $exists = $object_status != 'missing';
case 'up_to_date': $label = $exists ? l($object->label, "islandora/object/{$object->id}") : $object->label;
$object_status = t('Up-to-date'); $status_msg = "{$object_status_info['image']}&nbsp{$object_status_info['object']}";
break; $table_rows[] = array($label, $object->id, $status_msg);
case 'missing':
$object_status = t('Missing');
$needs_install = TRUE;
break;
case 'missing_datastream':
$object_status = t('Missing datastream');
$needs_update = TRUE;
break;
case 'out_of_date':
$object_status = t('Out-of-date');
$needs_update = TRUE;
break;
case 'could_not_connect':
$object_status = t('Could not connect');
$could_not_connect = TRUE;
break;
}
if ($needs_install OR $could_not_connect) {
$label = $object['label'] ? $object['label'] : '';
}
else {
$label = $object['label'] ? l($object['label'], $base_url . '/islandora/object/' . $pid) : '';
}
$table_row[] = $label;
$table_row[] = $pid;
$table_row[] = $object_status;
$table_rows[] = $table_row;
}
} }
$solution_pack_status = $status_severities[$solution_pack_status_severity];
// title $solution_pack_status_info = $status_info[$solution_pack_status];
if (!$form_state['submitted']) { return array(
$form['solution_pack']['solution_pack_label'] = array( 'solution_pack' => array(
'#markup' => filter_xss($solution_pack_name), '#type' => 'fieldset',
'#prefix' => '<h3>', '#collapsible' => FALSE,
'#suffix' => '</h3>', '#collapsed' => FALSE,
); '#attributes' => array('class' => array('islandora-solution-pack-fieldset')),
'solution_pack_module' => array(
$form['solution_pack']['install_status'] = array( '#type' => 'value',
'#markup' => '<strong>' . t('Object status:') . '&nbsp;</strong>', '#value' => $solution_pack_module,
'#prefix' => '<div class="islandora-solution-pack-install-status">', ),
'#suffix' => '</div>', 'solution_pack_name' => array(
); '#type' => 'value',
if (!$needs_install AND !$needs_update AND !$could_not_connect) { '#value' => $solution_pack_name,
$form['solution_pack']['install_status']['#markup'] .= ' ' . theme('image', array('path' => 'misc/watchdog-ok.png')) . ' ' . t('All required objects are installed and up-to-date.'); ),
$submit_button_text = t("Force reinstall objects"); 'objects' => array(
} '#type' => 'value',
elseif ($needs_install) { '#value' => $objects,
$form['solution_pack']['install_status']['#markup'] .= ' ' . theme('image', array('path' => 'misc/watchdog-warning.png')) . ' ' . t('Some objects are missing and must be installed. See objects list for details.'); ),
$submit_button_text = t("Install objects"); 'solution_pack_label' => array(
} '#markup' => $solution_pack_name,
elseif ($needs_update) { '#prefix' => '<h3>',
$form['solution_pack']['install_status']['#markup'] .= ' ' . theme('image', array('path' => 'misc/watchdog-warning.png')) . ' ' . t('Some objects must be reinstalled. See objects list for details.'); '#suffix' => '</h3>',
$submit_button_text = t("Reinstall objects"); ),
} 'install_status' => array(
elseif ($could_not_connect) { '#markup' => t('<strong>Object status:</strong> !image !status', array(
$form['solution_pack']['install_status']['#markup'] .= ' ' . theme('image', array('path' => 'misc/watchdog-error.png')) . ' ' . t('Could not connect to the repository.'); '!image' => $solution_pack_status_info['image'],
$submit_button_text = ''; '!status' => $solution_pack_status_info['solution_pack']
} )),
'#prefix' => '<div class="islandora-solution-pack-install-status">',
$form['solution_pack']['table'] = array( '#suffix' => '</div>',
'#type' => 'item', ),
'#markup' => theme('table', array('header' => $table_header, 'rows' => $table_rows)), 'table' => array(
); '#type' => 'item',
} '#markup' => theme('table', array('header' => array(t('Label'), t('PID'), t('Status')), 'rows' => $table_rows))
),
if (!$could_not_connect) { 'submit' => array(
$form['solution_pack']['submit'] = array( '#type' => 'submit',
'#value' => $submit_button_text, '#name' => $solution_pack_module,
'#type' => 'submit', '#value' => $solution_pack_status_info['button'],
'#name' => $solution_pack_module, '#attributes' => array('class' => array('islandora-solution-pack-submit')),
'#attributes' => array('class' => array('islandora-solution-pack-submit')), )
'#weight' => 40, )
); );
$form['solution_pack']['#submit'] = array('islandora_solution_pack_form_submit');
}
return $form;
} }
/** /**
@ -167,349 +150,235 @@ function islandora_solution_pack_form($form, &$form_state, $solution_pack_modul
* *
* @param array $form * @param array $form
* The form submitted. * The form submitted.
* @param array_reference $form_state * @param array $form_state
* The state of the form submited. * The state of the form submited.
*/ */
function islandora_solution_pack_form_submit($form, &$form_state) { function islandora_solution_pack_form_submit(array $form, array &$form_state) {
$solution_pack_module = $form_state['values']['solution_pack_module']; $solution_pack_module = $form_state['values']['solution_pack_module'];
$solution_pack_name = $form_state['values']['solution_pack_name']; $solution_pack_name = $form_state['values']['solution_pack_name'];
$objects = $form_state['values']['objects']; $objects = $form_state['values']['objects'];
$batch = array( $batch = array(
'title' => t('Installing / updating solution pack objects'), 'title' => t('Installing / Updating solution pack objects'),
'file' => drupal_get_path('module', 'islandora') . '/includes/solution_packs.inc', 'file' => drupal_get_path('module', 'islandora') . '/includes/solution_packs.inc',
'operations' => array(), 'operations' => array(),
); );
foreach ($objects as $object) { foreach ($objects as $object) {
// Add this object to the batch job queue. $batch['operations'][] = array('islandora_solution_pack_batch_operation_reingest_object', array($object));
$batch['operations'][] = array('islandora_batch_reingest_object', array($object));
} }
batch_set($batch); batch_set($batch);
// Hook to let solution pack objects be modified. // Hook to let solution pack objects be modified.
// Not using module_invoke so solution packs can be expanded by other modules. // Not using module_invoke so solution packs can be expanded by other modules.
// @todo shouldn't we send the object list along as well?
module_invoke_all('islandora_postprocess_solution_pack', $solution_pack_module); module_invoke_all('islandora_postprocess_solution_pack', $solution_pack_module);
}
/**
* Batch operation used by the solution pack forms to ingest/reingest required
* object(s)
*
* @param NewFedoraObject $object
* The object to ingest/reingest.
* @param array $context
* The context of this batch operation.
*/
function islandora_solution_pack_batch_operation_reingest_object(NewFedoraObject $object, array &$context) {
$deleted = FALSE;
$existing_object = islandora_object_load($object->id);
if ($existing_object) {
$deleted = islandora_delete_object($existing_object);
$purged = $deleted && $exsiting_object == NULL;
if (!$purged) {
$object_link = l($existing_object->label, "islandora/object/{$existing_object->id}");
drupal_set_message(t('Failed to purge existing object !object_link.', array(
'!object_link' => $object_link,
)), 'error');
// Failed to purge don't attempt to ingest.
return;
}
}
// Object was deleted or did not exist.
$pid = $object->id;
$label = $object->label;
$action = $deleted ? 'reinstalled' : 'installed';
$object_link = l($label, "islandora/object/{$pid}");
$object = islandora_add_object($object);
$msg = $object ? "Successfully $action !object_link." : "Failed to $action @label identified by @pid.";
$status = $object ? 'status' : 'error';
drupal_set_message(t($msg, array(
'@pid' => $pid,
'@label' => $label,
'!object_link' => $object_link
)), $status);
} }
/** /**
* Batch reingest object(s) * This is to be called from the solution pack's hook_install()
* and hook_uninstall() functions. It provides a convient way to have a
* solution pack's required objects ingested at install time.
* *
* @param array $object * @param string $module_name
* @param type $context * The name of the module that is calling this function in its
* @return type * install/unistall hooks.
* @param string $op
* The operation to perform, either install or uninstall.
*
* @todo Implement hook_modules_installed/hook_modules_uninstalled instead of
* calling this function directly.
* @todo Remove the second parameter and have two seperate functions.
*/ */
function islandora_batch_reingest_object($object_model, &$context) { function islandora_install_solution_pack($module, $op = 'install') {
if ($op == 'uninstall') {
islandora_uninstall_solution_pack($module);
return;
}
module_load_include('module', 'islandora', 'islandora');
module_load_include('inc', 'islandora', 'includes/utilities'); module_load_include('inc', 'islandora', 'includes/utilities');
global $base_url; module_load_include('module', $module, $module);
$connection = islandora_get_tuque_connection(); $info_file = drupal_get_path('module', $module) . "/{$module}.info";
if (!$connection) { $info_array = drupal_parse_info_file($info_file);
$module_name = $info_array['name'];
$admin_link = l(t('Solution Pack admin'), 'admin/islandora/solution_packs');
$config_link = l(t('Islandora configuration'), 'admin/islandora/configure');
if (!islandora_describe_repository()) {
$msg = '@module: Did not install any objects. Could not connect to the ';
$msg .= 'repository. Please check the settings on the !config_link page ';
$msg .= 'and install the required objects manually on the !admin_link page.';
drupal_set_message(st($msg, array(
'@module' => $module_name,
'!config_link' => $config_link,
'@admin_link' => $admin_link
)), 'error');
return; return;
} }
if (!empty($object_model) && is_array($object_model)) { $connection = islandora_get_tuque_connection();
$pid = $object_model['pid']; $islandora_required_objects = $module . '_islandora_required_objects';
if (!islandora_is_valid_pid($pid)) { $required_objects = $islandora_required_objects($connection);
return NULL; $objects = $required_objects[$module]['objects'];
} $status_messages = array(
'up_to_date' => 'The object already exists and is up-to-date',
// purge object 'missing_datastream' => 'The object already exists but is missing a datastream. Please reinstall the object on the !admin_link page',
// check if object already exits 'out_of_date' => 'The object already exists but is out-of-date. Please update the object on the !admin_link page'
$object_query = $connection->api->a->findObjects('query', 'pid=' . $pid); );
$reinstall = FALSE; foreach ($objects as $object) {
if (!empty($object_query['results'])) { $query = $connection->api->a->findObjects('query', 'pid=' . $object->id);
$object = islandora_object_load($pid); $already_exists = !empty($query['results']);
if (isset($object)) { $label = $object->label;
islandora_delete_object($object); $object_link = l($label, "islandora/object/{$object->id}");
} if ($already_exists) {
$reinstall = TRUE; $object_status = islandora_check_object_status($object);
$status_msg = $status_messages[$object_status];
drupal_set_message(st("@module: Did not install !object_link. $status_msg.", array(
'@module' => $module_name,
'!object_link' => $object_link,
'!admin_link' => $admin_link,
)), 'warning');
} }
else {
// build and ingest new object $object = islandora_add_object($object);
try { if ($object) {
$object = islandora_solution_pack_add_object($object_model); drupal_set_message(t('@module: Successfully installed. !object_link.', array(
$object_name = $object->label; '@module' => $module_name,
if ($reinstall) { '!object_link' => $object_link,
drupal_set_message(t('Successfully reinstalled <em>@object_name</em>.', array('@object_name' => $object_name, '@pid' => $pid))); )), 'status');
} }
else { else {
drupal_set_message(t('Successfully installed <em>@object_name</em>.', array('@object_name' => $object_name, '@pid' => $pid))); drupal_set_message(t('@module: Failed to install. @label.', array(
'@module' => $module_name,
'@label' => $label,
)), 'warning');
} }
} }
catch (Exception $e) {
drupal_set_message(t('Installation of object @pid failed', array('@pid' => $pid)), 'error');
}
} }
} }
/** /**
* Callback function that can be called from the solution pack's hook_install() and hook_uninstall() functions. * Uninstalls the given solution pack.
* *
* @TODO: add documentation * @param string $module
* The solution pack to uninstall.
*
* @todo Implement hook_modules_uninstalled instead of calling this function
* directly for each solution pack.
*/ */
function islandora_install_solution_pack($module_name = NULL, $op = 'install') { function islandora_uninstall_solution_pack($module) {
// check if a module name is given. // @TODO: check module name for existance module_load_include('module', 'islandora', 'islandora');
if (!empty($module_name)) { module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('module', 'islandora', 'islandora'); module_load_include('module', $module, $module);
module_load_include('inc', 'islandora', 'includes/utilities'); $admin_link = l(t('Solution Pack admin'), 'admin/islandora/solution_packs');
module_load_include('module', $module_name, $module_name); $config_link = l(t('Islandora configuration'), 'admin/islandora/configure');
$info_file = drupal_get_path('module', $module) . "/{$module}.info";
// set globals $info_array = drupal_parse_info_file($info_file);
global $base_url; $module_name = $info_array['name'];
global $user; if (!islandora_describe_repository()) {
$msg = '@module: Did not uninstall any objects. Could not connect to the ';
// set variables $msg .= 'repository. Please check the settings on the !config_link page ';
$sp_admin = url($base_url . '/admin/islandora/solution_packs'); $msg .= 'and uninstall the required objects manually if necessary.';
$config_url = url('admin/islandora/configure'); drupal_set_message(st($msg, array(
'@module' => $module_name,
// get module info '!config_link' => $config_link
$info_file = drupal_get_path('module', $module_name) . '/' . $module_name . '.info'; )), 'error');
$info_array = drupal_parse_info_file($info_file); return;
$module_label = $info_array['name']; }
$connection = islandora_get_tuque_connection();
// check connection $islandora_required_objects = $module . '_islandora_required_objects';
$url = variable_get('islandora_base_url', 'http://localhost:8080/fedora'); $required_objects = $islandora_required_objects($connection);
$info = islandora_describe_repository($url); $objects = $required_objects[$module]['objects'];
if (!$info) { $existing_objects = array_filter($objects, function($o) use($connection) {
// operation $param = "pid={$o->id}";
switch ($op) { $query = $connection->api->a->findObjects('query', $param);
case 'install': return !empty($query['results']);
drupal_set_message(st('@module_label: Did not install any objects. Could not connect to the repository. Please check the settings on the <a href="@config_url" title="Islandora configuration page">Islandora configuration</a> page and install the required objects manually on the <a href="@sp_url" title="Solution pack admin">solution pack admin</a> page.', array('@module_label' => $module_label, '@config_url' => $config_url, '@sp_url' => $sp_admin)), 'error'); }
break; );
foreach ($existing_objects as $object) {
case 'uninstall': $msg = '@module: Did not remove !object_link. It may be used by other sites.';
drupal_set_message(st('@module_label: Did not uninstall any objects. Could not connect to the repository. Please check the settings on the <a href="@config_url" title="Islandora configuration page">Islandora configuration</a> page and uninstall the required objects manually if necessary.', array('@module_label' => $module_label, '@config_url' => $config_url)), 'error'); $object_link = l($object->label, "islandora/object/{$object->id}");
break; drupal_set_message(st($msg, array(
} '!object_link' => $object_link,
return; '@module' => $module_name
} )), 'warning');
$connection = islandora_get_tuque_connection();
// get object models
$enabled_solution_packs = module_invoke_all('islandora_required_objects');
$islandora_required_objects = $module_name . '_islandora_required_objects';
$required_objects = $islandora_required_objects();
$objects = $required_objects[$module_name]['objects'];
// loop over object models
foreach ($objects as $object) {
// set variables
$pid = $object['pid'];
$label = isset($object['label']) ? $object['label'] : st('Object');
// check if object already exists
$query = $connection->api->a->findObjects('query', 'pid=' . $pid);
// object url
$object_url = url($base_url . '/islandora/object/' . $pid);
// operation: install or uninstall
switch ($op) {
case 'install':
// if object exists, don't re-ingest
if (!empty($query['results'])) {
// check object status
$object_status = islandora_check_object_status($object);
// set messages
switch ($object_status) {
case 'up_to_date':
drupal_set_message(st('@module_label: did not install <a href="@object_url" title="@pid">@label</a>. The object already exists and is up-to-date.', array('@module_label' => $module_label, '@label' => $label, '@pid' => $pid, '@object_url' => $object_url)));
break;
case 'missing_datastream':
drupal_set_message(st('@module_label: did not install <a href="@object_url" title="@pid">@label</a>. The object already exists but is missing a datastream. Please reinstall the object on the <a href="@sp_admin" title="Solution pack admin page">solution pack admin page</a>.', array('@module_label' => $module_label, '@label' => $label, '@pid' => $pid, '@objecturl' => $object_url, '@sp_admin' => $sp_admin)), 'warning');
break;
case 'out_of_date':
drupal_set_message(st('@module_label: did not install <a href="@object_url" title="@pid">@label</a>. The object already exists but is out-of-date. Please update the object on the <a href="@sp_admin" title="Solution pack admin page">solution pack admin page</a>.', array('@module_label' => $module_label, '@label' => $label, '@pid' => $pid, '@object_url' => $object_url, '@sp_admin' => $sp_admin)), 'warning');
break;
}
}
else {
islandora_solution_pack_add_object($object);
drupal_set_message(st('@module_label: installed <a href="@object_url" title="@pid">@label</a> object.', array('@module_label' => $module_label, '@label' => $label, '@pid' => $pid, '@object_url' => $object_url)));
}
break;
case 'uninstall':
// if object exists, set message
if (!empty($query['results'])) {
$object_url = url($base_url . '/islandora/object/' . $pid);
drupal_set_message(st('@module_label: did not remove <a href="@object_url" title="@pid">@label</a>. It may be used by other sites.', array('@pid' => $pid, '@object_url' => $object_url, '@label' => $label, '@module_label' => $module_label)), 'warning');
}
break;
}
}
} }
} }
/** /**
* Function to check the status of an object against an object model array. * Function to check the status of an object against an object model array.
* *
* @param array $object_model * @param NewFedoraObject $object_definition
* an array describing an object * A new fedora object that defines what the object should contain.
*
* @return string * @return string
* Returns one of the following values: * Returns one of the following values:
* up_to_date, missing, missing_datastream or out_of_date * up_to_date, missing, missing_datastream or out_of_date
* You can perform an appropriate action based on this value. * You can perform an appropriate action based on this value.
* Returns FALSE if the array is empty
* *
* @see islandora_solution_pack_form() * @see islandora_solution_pack_form()
* @see islandora_install_solution_pack() * @see islandora_install_solution_pack()
* @todo: Should this function live in islandora.module so it can be called * @todo Should this function live in islandora.module so it can be called
* easier without having to include the solution_packs.inc file? * easier without having to include the solution_packs.inc file?
*/
function islandora_check_object_status($object_model = array()) {
if (!empty($object_model)) {
// set variables
$pid = $object_model['pid'];
$object_status = 'up_to_date';
// table row
$table_row = array();
// check connection
module_load_include('inc', 'islandora', 'includes/utilities');
$url = variable_get('islandora_base_url', 'http://localhost:8080/fedora');
$info = islandora_describe_repository($url);
if (!$info) {
$object_status = 'could_not_connect';
}
else {
// load object
$object = islandora_object_load($pid);
// check if object exists
if (!$object) {
$object_status = 'missing';
}
else {
// object defined with single datastream file
// @TODO: should dsversion be mandatory for the check to valid?
if (isset($object_model['dsid']) && isset($object_model['datastream_file']) && isset($object_model['dsversion'])) {
$datastreams = array(
array(
'dsid' => $object_model['dsid'],
'datastream_file' => $object_model['datastream_file'],
'dsversion' => $object_model['dsversion'],
),
);
}
// object defined with multiple datastreams (using an array)
elseif (!empty($object_model['datastreams'])) {
$datastreams = $object_model['datastreams'];
}
if (!empty($datastreams) && is_array($datastreams)) {
// loop over defined datastreams
foreach ($datastreams as $ds) {
$ds_id = $ds['dsid'];
// check if defined datastream exists in the object
if (!$object[$ds_id]) {
$object_status = 'missing_datastream';
break;
}
elseif (isset($ds['dsversion'])) {
// Check if the datastream is versioned and needs updating.
$installed_version = islandora_get_islandora_datastream_version($object, $ds['dsid']);
$available_version = islandora_get_islandora_datastream_version(NULL, NULL, $ds['datastream_file']);
if ($available_version > $installed_version) {
$object_status = 'out_of_date';
break;
}
}
}
}
}
}
return $object_status;
}
else {
return FALSE;
}
}
/**
* Converts the given definition into an object and add's it to the repository.
*
* @param array $object_definition
* An associative array containing the necessary parameters to create the
* desired object:
* - pid: The PID with which the object will be created.
* - label: An optional label to apply to the object.
* - datastreams: Same as the "datastreams" array accepted by
* islandora_prepare_new_object().
* - cmodel: Either an array of content models as accepted by
* islandora_preprare_new_object(), or a single content model PID to add
* to the object.
* - parent: Either an array of parents, or a single parent PID to which to
* relate to; uses isMemberOfCollection by default.
* - relationships: An array of relationships as accepted by
* islandora_prepare_new_object().
*
* @return FedoraObject
* The newly created object.
*/
function islandora_solution_pack_add_object(array $object_definition) {
$object = islandora_solution_pack_prepare_new_object($object_definition);
return islandora_add_object($object);
}
/**
* Prepares a new object based on the solution pack style of declaring them as arrays.
*
* @param array $object_definition
* An associative array containing the necessary parameters to create the
* desired object:
* - pid: The PID with which the object will be created.
* - label: An optional label to apply to the object.
* - datastreams: Same as the "datastreams" array accepted by
* islandora_prepare_new_object().
* - cmodel: Either an array of content models as accepted by
* islandora_prepare_new_object(), or a single content model PID to add
* to the object.
* - parent: Either an array of parents, or a single parent PID to which to
* relate to; uses isMemberOfCollection by default.
* - relationships: An array of relationships as accepted by
* islandora_prepare_new_object().
*
* @return NewFedoraObject
* An NewFedoraObject which has been initalized with the given properties.
*/ */
function islandora_solution_pack_prepare_new_object(array $object_definition) { function islandora_check_object_status(NewFedoraObject $object_definition) {
module_load_include('inc', 'islandora', 'includes/utilities'); $existing_object = islandora_object_load($object_definition->id);
$namespace = $object_definition['pid']; if (!$existing_object) {
$label = !empty($object_definition['label']) ? $object_definition['label'] : NULL; return 'missing';
$datastreams = array();
if (!empty($object_definition['datastreams']) AND is_array($object_definition['datastreams'])) {
$datastreams = $object_definition['datastreams'];
}
$content_models = array();
if (!empty($object_definition['cmodel'])) {
if (is_array($object_definition['cmodel'])) {
$content_models = $object_definition['cmodel'];
}
else {
$content_models[] = $object_definition['cmodel'];
}
}
$relationships = array();
if (!empty($object_definition['parent']) AND !is_array($object_definition['parent'])) {
$relationships[] = array('relationship' => 'isMemberOfCollection', 'pid' => $object_definition['parent']);
} }
if (!empty($object_definition['parents']) AND is_array($object_definition['parents'])) { $existing_datastreams = array_keys(iterator_to_array($existing_object));
foreach ($object_definition['parents'] as $parent) { $expected_datastreams = array_keys(iterator_to_array($object_definition));
$relationships[] = array('relationship' => 'isMemberOfCollection', 'pid' => $parent); $datastream_diff = array_diff($expected_datastreams, $existing_datastreams);
} if (!empty($datastream_diff)) {
return 'missing_datastream';
} }
if (!empty($object_definition['relationships']) AND is_array($object_definition['relationships'])) { $is_xml_datastream = function($ds) { return $ds->mimetype == 'text/xml'; };
foreach ($object_definition['relationships'] as $relationship) { $xml_datastreams = array_filter(iterator_to_array($object_definition), $is_xml_datastream);
$relationships[] = array('relationship' => $relationship['relationship'], 'pid' => $relationship['pid']); foreach ($xml_datastreams as $ds) {
$installed_version = islandora_get_islandora_datastream_version($existing_object, $ds->id);
$available_version = islandora_get_islandora_datastream_version($object_definition, $ds->id);
if ($available_version > $installed_version) {
return 'out_of_date';
} }
} }
return islandora_prepare_new_object($namespace, $label, $datastreams, $content_models, $relationships); // If not anything else we can assume its up to date.
return 'up_to_date';
} }
/** /**
* @defgroup viewer-functions * @defgroup viewer-functions
* @{ * @{

14
includes/utilities.inc

@ -413,3 +413,17 @@ function islandora_prepare_new_object($namespace = NULL, $label = NULL, $datastr
} }
return $object; return $object;
} }
/**
* Displays the repository is inaccessible message
*
* Use anywhere we want to ensure a consitent error message when the repository
* is not accessible.
*/
function islandora_display_repository_inaccessible_message() {
$text = t('Islandora configuration');
$link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text)));
$message = t('Could not connect to the repository. Please check the settings on the @link page.',
array('@link' => $link));
drupal_set_message($message, 'error');
}

Loading…
Cancel
Save