You are here

function node_revision_delete_cron in Node Revision Delete 7

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.2 node_revision_delete.module \node_revision_delete_cron()

Implements hook_cron().

1 call to node_revision_delete_cron()
node_revision_delete_form_submit in ./node_revision_delete.module
FormAPI submission to Save the Setting for deleting the revisions.

File

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

Code

function node_revision_delete_cron() {
  $total_revision_to_keep = variable_get('node_revision_delete_number');
  $node_revision_delete_content_type = explode(',', variable_get('node_revision_delete_content_type'));
  $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 'run_now_only':
      $execute_revision_delete = 1;
      variable_set('node_revision_delete_time', 0);
      variable_set('node_revision_delete_last_execute', 0);
      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) {
    $query = db_select('node', 'n');
    $query
      ->condition('n.type', array(
      $node_revision_delete_content_type,
    ), 'IN')
      ->fields('n');
    $node_result = $query
      ->execute();
    foreach ($node_result as $node_array) {
      $revision_id_array = array();
      $revision_array = node_revision_list($node_array);
      foreach ($revision_array as $revision_array) {
        $revision_id_array[] = $revision_array->vid;
      }
      $revision_id_array_deleted = array_slice($revision_id_array, $total_revision_to_keep);
      foreach ($revision_id_array_deleted as $revision_id) {
        node_revision_delete($revision_id);
      }
    }
  }
}