You are here

function revision_deletion_get_list in Revision Deletion 6

Same name and namespace in other branches
  1. 5 revision_deletion.module \revision_deletion_get_list()

Get the list of revisions to delete.

Parameters

$nid - specific node to retrieve.:

Return value

array containing objects of all revisions.

3 calls to revision_deletion_get_list()
revision_deletion_auto_form in ./revision_deletion.module
Get the list of revisions to auto-delete.
revision_deletion_cron in ./revision_deletion.module
Implementation of hook_cron().
revision_deletion_list_form in ./revision_deletion.module
Form to list all revisions of a node.

File

./revision_deletion.module, line 139
Node Revision Deletion, written by Greg Holsclaw

Code

function revision_deletion_get_list($nid = NULL) {
  $nodes = variable_get('revision_delete', array());
  $show_conditional = variable_get('revision_delete_list_show_conditional', 1);
  $keep_original = variable_get('revision_delete_list_keep_original', 0);
  $keep_date_last = variable_get('revision_delete_list_keep_date_last', 0);
  $keep_current = variable_get('revision_delete_list_keep_current', 1209600);
  $limit = variable_get('revision_delete_limit', 20);
  $now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
  $fmt = variable_get('date_format_short', variable_get('date_format_short_custom', 'Y M j'));
  $fmt = trim(str_replace(array(
    'H',
    'i',
    ':',
  ), '', $fmt));
  $aged = $now - variable_get('revision_delete_age', 2419200);

  // If there is a nid specified, skip the age check and show all revisions.
  if (is_null($nid)) {
    $args = array(
      $aged,
    );
  }
  else {
    $args = array(
      $now,
    );
  }
  $args = array_merge($args, $nodes);
  $conds = 'AND n.type IN(' . db_placeholders($nodes, 'text') . ') ';
  if ($nid) {
    $conds .= "AND r.nid={$nid} ";
    $args[] = $nid;
  }

  // Ignore the Coder flag here; the code is correct and secure.
  $query = "SELECT n.title, r.nid, r.vid, r.timestamp, r.uid, r.log, n.type, n.vid AS current, n.status FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid WHERE n.nid IN (SELECT r.nid FROM {node_revisions} r INNER JOIN {node} n ON r.nid = n.nid AND r.vid <> n.vid WHERE r.timestamp < %d {$conds} ) ORDER BY r.nid, r.timestamp DESC";

  //  drupal_set_message('<strong>query</strong> = '.$query .', <strong>arguments</strong> = '. print_r($args, TRUE));
  if ($limit > 0) {
    $result = pager_query($query, $limit, 0, NULL, $args);
  }
  else {
    $result = db_query($query, $args);
  }
  $data = array();
  $prev_date = $prev_nid = $prev_vid = NULL;
  $keeper = FALSE;
  while ($row = db_fetch_object($result)) {
    $vid = $row->vid;
    $msg = '';
    $row->select = TRUE;
    $row->class = 'revision-selected';
    $row->date = format_date($row->timestamp, 'custom', $fmt, 0);

    // See if previous was an original.
    if ($row->nid != $prev_nid) {
      $prev_nid = $row->nid;
      $oldest = db_result(db_query_range("SELECT vid FROM {node_revisions} WHERE nid=%d ORDER BY timestamp ASC", $row->nid, 0, 1));
    }
    $row->oldest = $oldest;

    // Is this the current revision?
    if ($row->current == $vid) {
      $msg = '<em>' . t('Current revision') . '</em>';
      $row->select = FALSE;
      $row->class = 'revision-current';

      // If it's not old enough yet, keep next.
      if ($now - $rev->timestamp < $keep_current) {
        $keeper = TRUE;
      }
    }
    else {

      // If this is the next oldest revision and the newset isn't old enough, keep this one too.
      if ($keeper) {
        $msg = '<em>' . t('Young current') . '</em>';
        $form['select'][$vid]['#default_value'] = FALSE;
        $keeper = FALSE;
        $row->class = 'revision-young';
      }

      // Is this the original?
      if ($vid == $oldest && $keep_original) {
        $msg = '<em>' . t('Original version') . '</em>';
        $row->select = FALSE;
        $row->class = 'revision-original';
      }
      else {

        // Is it the last for the date?
        if ($row->date != $prev_date && $keep_date_last) {
          $msg = '<em>' . t('Last for !date', array(
            '!date' => $row->date,
          )) . '</em>';
          $row->select = FALSE;
          $row->class = 'revision-last';
        }
      }
    }
    $row->msgs = $show_conditional ? $msg : '';

    // Save it all to be returned.
    $data[$vid] = $row;
    $prev_vid = $vid;
    $prev_date = $row->date;
  }
  return $data;
}