You are here

function node_revision_delete_cron in Node Revision Delete 7.2

Same name and namespace in other branches
  1. 8 node_revision_delete.module \node_revision_delete_cron()
  2. 7.3 node_revision_delete.module \node_revision_delete_cron()
  3. 7 node_revision_delete.module \node_revision_delete_cron()

Implements hook_cron().

3 string references to 'node_revision_delete_cron'
node_revision_delete_form in ./node_revision_delete.admin.inc
Page callback: Form constructor for Node Revision Delete administration form.
node_revision_delete_form_submit in ./node_revision_delete.admin.inc
Form submit handler for the settings form.
node_revision_delete_uninstall in ./node_revision_delete.install
Implements hook_uninstall().

File

./node_revision_delete.module, line 75
Node Revision Delete Module.

Code

function node_revision_delete_cron() {

  // Get node revision limits for all content types.
  // If there are no tracked content types, exit this cron hook.
  $content_types = node_revision_delete_content_types();
  if (empty($content_types)) {
    return;
  }

  // Figure out whether we should delete revisions or not on this run.
  $node_revision_delete_time = variable_get('node_revision_delete_time');
  $node_revision_delete_last_execute = variable_get('node_revision_delete_last_execute');
  $execute_revision_delete = 0;
  $current_time = time();
  $time_difference = $current_time - $node_revision_delete_last_execute;
  switch ($node_revision_delete_time) {
    case 'every_time':
      variable_set('node_revision_delete_last_execute', $current_time);
      $execute_revision_delete = 1;
      break;
    case 'everyday':
      if ($time_difference > 86400) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_week':
      if ($time_difference > 604800) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_10_days':
      if ($time_difference > 864000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_15_days':
      if ($time_difference > 1296000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_month':
      if ($time_difference > 2592000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_3_months':
      if ($time_difference > 7776000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_6_months':
      if ($time_difference > 15552000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_year':
      if ($time_difference > 31536000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
  }
  if ($execute_revision_delete) {
    $max = variable_get('node_revision_delete_cron', NODE_REVISION_DELETE_CRON);
    $tracked_content_types = node_revision_delete_content_types();

    // Flag used to count how many have been deleted in this cron run.
    $total_deleted = 0;
    foreach ($tracked_content_types as $content_type => $revisions_to_keep) {
      $candidate_nids = node_revision_delete_candidates($content_type, $revisions_to_keep);
      if (!empty($candidate_nids)) {
        foreach ($candidate_nids as $nid) {
          $deleted_revisions = _node_revision_delete_do_delete($nid, $revisions_to_keep, $max);
          $total_deleted += $deleted_revisions->count;
          if ($total_deleted >= $max) {
            break 2;
          }
        }
      }
    }
    drupal_set_message(t('Deleted @total revisions.', array(
      '@total' => $total_deleted,
    )));
  }
}