You are here

function revision_scheduler_operation_process in Revision scheduler 7

Process a single scheduled revision operation.

2 calls to revision_scheduler_operation_process()
revision_scheduler_operation_run in ./revision_scheduler.module
Run a specific scheduled operation.
revision_scheduler_queue_process_item in ./revision_scheduler.module
Callback for the revision_scheduler queue.

File

./revision_scheduler.module, line 377

Code

function revision_scheduler_operation_process($operation) {
  $transaction = db_transaction();
  $args = array(
    '@entity-type' => $operation->entity_type,
    '@entity-id' => $operation->entity_id,
    '@operation' => $operation->operation,
    '@revision-id' => $operation->revision_id,
  );
  try {
    $entity = revision_scheduler_entity_revision_load($operation->entity_type, $operation->entity_id, $operation->revision_id);
    $operation_info = revision_scheduler_entity_revision_operation_get_info($operation->entity_type, $operation->operation);
    $entity_info = entity_get_info($operation->entity_type);
    if (empty($entity)) {
      throw new Exception(t('Failed to load entity @entity-type @entity-id.', $args));
    }
    elseif (empty($operation_info)) {
      throw new Exception(t('Failed to load revision_scheduler_entity_revision_operation_get_info(@entity-type, @operation).', $args));
    }
    elseif (empty($entity_info)) {
      throw new Exception(t('Failed to load entity_get_info(@entity-type).', $args));
    }

    // Allow modules to alter or validate the operation about to be processed.
    module_invoke_all('revision_scheduler_operation_preprocess', $entity, $operation);
    $callback = $operation_info['callback'];
    if (isset($operation_info['file'])) {
      include_once $operation_info['file'];
    }
    if (!is_callable($callback)) {
      throw new Exception(t('Revision operation @operation callback @callback does not exist.', $args + array(
        '@callback' => is_array($callback) ? implode('::', $callback) : $callback,
      )));
    }

    // Switch the user to the one that created the operation.
    $original_session_saving = drupal_save_session();
    $original_user = $GLOBALS['user'];
    if ($operation->uid != $GLOBALS['user']->uid) {
      drupal_save_session(FALSE);
      if ($operation->uid && ($account = user_load($operation->uid))) {
        $GLOBALS['user'] = $account;
      }
      else {
        $GLOBALS['user'] = drupal_anonymous_user();
      }
    }
    $operation->time_executed = time();
    revision_scheduler_operation_save($operation);

    // Run the operation callback with the entity and operation.
    call_user_func($callback, $entity, $operation);

    // Restore the user.
    $GLOBALS['user'] = $original_user;
    drupal_save_session($original_session_saving);

    // Allow modules to react to the operation after it has been processed.
    module_invoke_all('revision_scheduler_operation_postprocess', $entity, $operation);
    watchdog('revision_scheduler', 'Performed scheduled revision operation @operation on @entity-type @entity-id revision @revision-id.', $args);
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('revision_scheduler', $e);
  }
}