Browse Source

Batch each derivative operation individually.

pull/439/head
Jordan Dukart 11 years ago
parent
commit
6405a453ce
  1. 57
      includes/derivatives.inc
  2. 59
      includes/regenerate_derivatives.form.inc

57
includes/derivatives.inc

@ -121,3 +121,60 @@ function islandora_derivative_logging(array $logging_results) {
} }
} }
} }
/**
* Kicks off derivative functions based upon hooks and conditions.
*
* @param AbstractObject $object
* An AbstractObject representing a FedoraObject.
* @param array $options
* An array of parameters containing:
* - force: Bool denoting whether we are forcing the generation of
* derivatives.
* - source_dsid: (Optional) String of the datastream id we are generating
* from or NULL if it's the object itself.
* - destination_dsid: (Optional) String of the datastream id that is being
* created. To be used in the UI.
*
* @return array
* An array of operations to be called from within a batch.
*/
function islandora_do_batch_derivatives(AbstractObject $object, array $options) {
module_load_include('inc', 'islandora', 'includes/utilities');
$options += array(
'force' => FALSE,
);
$hooks = islandora_invoke_hook_list(ISLANDORA_DERVIATIVE_CREATION_HOOK, $object->models, array($object));
uasort($hooks, 'drupal_sort_weight');
$operations = array();
if (array_key_exists('source_dsid', $options)) {
$hooks = array_filter($hooks, function($filter_hook) use($options) {
return array_key_exists('source_dsid', $filter_hook) &&
$filter_hook['source_dsid'] == $options['source_dsid'];
});
}
if (array_key_exists('destination_dsid', $options)) {
$hooks = array_filter($hooks, function($filter_hook) use($options) {
return array_key_exists('destination_dsid', $filter_hook) &&
$filter_hook['destination_dsid'] == $options['destination_dsid'];
});
}
foreach ($hooks as $hook) {
$file = FALSE;
if (isset($hook['file'])) {
$file = $hook['file'];
}
foreach ($hook['function'] as $function) {
$operations[] = array('islandora_derivative_perform_batch_operation', array(
$function,
$file,
$object->id,
$options['force']),
);
}
}
return $operations;
}

59
includes/regenerate_derivatives.form.inc

@ -97,13 +97,12 @@ function islandora_regenerate_object_derivatives_form_submit(array $form, array
function islandora_regenerate_object_derivatives_batch(AbstractObject $object) { function islandora_regenerate_object_derivatives_batch(AbstractObject $object) {
return array( return array(
'title' => t('Regenerating all derivatives for @label', array('@label' => $object->label)), 'title' => t('Regenerating all derivatives for @label', array('@label' => $object->label)),
'operations' => array( 'operations' => islandora_do_batch_derivatives($object, array('force' => TRUE)),
array('islandora_regenerate_object_derivatives_batch_operation', array($object)),
),
'init_message' => t('Preparing to regenerate derivatives...'), 'init_message' => t('Preparing to regenerate derivatives...'),
'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaning @estimate.'), 'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaning @estimate.'),
'error_message' => t('An error has occurred.'), 'error_message' => t('An error has occurred.'),
'file' => drupal_get_path('module', 'islandora') . '/includes/regenerate_derivatives.form.inc', 'file' => drupal_get_path('module', 'islandora') . '/includes/regenerate_derivatives.form.inc',
'finished' => 'islandora_regenerate_derivative_batch_finished',
); );
} }
@ -135,13 +134,15 @@ function islandora_regenerate_object_derivatives_batch_operation(AbstractObject
function islandora_regenerate_datastream_derivative_batch(AbstractDatastream $datastream) { function islandora_regenerate_datastream_derivative_batch(AbstractDatastream $datastream) {
return array( return array(
'title' => t('Regenerating derivatives for the @dsid datastream', array('@dsid' => $datastream->id)), 'title' => t('Regenerating derivatives for the @dsid datastream', array('@dsid' => $datastream->id)),
'operations' => array( 'operations' => islandora_do_batch_derivatives($datastream->parent, array(
array('islandora_regenerate_datastream_derivative_batch_operation', array($datastream)), 'force' => TRUE,
), 'destination_dsid' => $datastream->id,
)),
'init_message' => t('Preparing to regenerate derivatives...'), 'init_message' => t('Preparing to regenerate derivatives...'),
'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaning @estimate.'), 'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaning @estimate.'),
'error_message' => t('An error has occurred.'), 'error_message' => t('An error has occurred.'),
'file' => drupal_get_path('module', 'islandora') . '/includes/regenerate_derivatives.form.inc', 'file' => drupal_get_path('module', 'islandora') . '/includes/regenerate_derivatives.form.inc',
'finished' => 'islandora_regenerate_derivative_batch_finished',
); );
} }
@ -161,3 +162,49 @@ function islandora_regenerate_datastream_derivative_batch_operation(AbstractData
)); ));
islandora_derivative_logging($logging_results); islandora_derivative_logging($logging_results);
} }
/**
* Wrapper to call out to batch operations.
*
* @param string $function
* The name of the function we are calling for derivatives.
* @param bool|string $file
* FALSE if there is no file to load, the path to require otherwise
* @param string $pid
* The pid of the object we are performing.
* @param bool $force
* Whether we are forcing derivative regeneration or not.
* @param array $context
* The context of the current batch operation.
*/
function islandora_derivative_perform_batch_operation($function, $file, $pid, $force, &$context) {
if ($file) {
require_once $file;
}
if (function_exists($function)) {
$logging = call_user_func($function, islandora_object_load($pid), $force);
if (!empty($logging)) {
$context['results']['logging'][] = $logging;
}
}
else {
watchdog('islandora', 'Unable to call derivative function @function as it was not found!', array('@function' => $function), WATCHDOG_ERROR);
}
}
/**
* Finished function for derivative batch regeneration.
*
* @param array $success
* An array of success passed from the batch.
* @param array $results
* An array of results passed from the batch.
* @param array $operations
* An array of operations passed from the batch.
*/
function islandora_regenerate_derivative_batch_finished($success, $results, $operations) {
module_load_include('inc', 'islandora', 'includes/derivatives');
if (!empty($results['logging'])) {
islandora_derivative_logging($results['logging']);
}
}

Loading…
Cancel
Save