You are here

function node_revision_delete_cron in Node Revision Delete 7.3

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

Implements hook_cron().

6 string references to 'node_revision_delete_cron'
drush_node_revision_delete_nrd_delete_cron_run in ./node_revision_delete.drush.inc
Callback for the nrd-delete-cron-run command.
node_revision_delete_admin_settings_form in ./node_revision_delete.admin.inc
Form constructor for Node Revision Delete administration form.
node_revision_delete_admin_settings_form_submit in ./node_revision_delete.admin.inc
Form submit handler for the Node Revision Delete administration form.
node_revision_delete_install in ./node_revision_delete.install
Implements hook_install().
node_revision_delete_uninstall in ./node_revision_delete.install
Implements hook_uninstall().

... See full list

File

./node_revision_delete.module, line 247

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.
  $node_revision_delete_track = variable_get('node_revision_delete_track', array());
  if (empty($node_revision_delete_track)) {
    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');
  $current_time = time();
  $time_difference = $current_time - $node_revision_delete_last_execute;
  if ($node_revision_delete_time >= 0 && $time_difference >= $node_revision_delete_time) {
    variable_set('node_revision_delete_last_execute', $current_time);

    // Getting the number of revisions to remove in each cron run.
    $max = variable_get('node_revision_delete_cron', 50);

    // Flag used to count how many have been deleted in this cron run.
    $total_deleted = 0;
    foreach ($node_revision_delete_track as $content_type => $content_type_info) {
      $candidates = _node_revision_delete_candidates($content_type, $content_type_info['minimum_revisions_to_keep'], $content_type_info['minimum_age_to_delete'], $content_type_info['when_to_delete']);
      $candidate_nids = array_values(array_unique(array_column($candidates, 'nid')));
      if (!empty($candidate_nids)) {
        foreach ($candidate_nids as $nid) {
          $deleted_revisions = _node_revision_delete_do_delete($nid, $node_revision_delete_track[$content_type]['minimum_revisions_to_keep']);
          $total_deleted += $deleted_revisions->count;
          if ($total_deleted >= $max) {
            break 2;
          }
        }
      }
    }
    drupal_set_message(t('Deleted @total revisions.', array(
      '@total' => $total_deleted,
    )));
  }
}