You are here

function _revision_deletion_get_list in Revision Deletion 7

Get the list of revisions to delete.

Parameters

int $nid: Specific node to retrieve.

string $header: Optional table header for table sorting.

Return value

array Objects of all revisions.

3 calls to _revision_deletion_get_list()
revision_deletion_admin_overview in ./revision_deletion.admin.inc
Form builder for the revisions overview administration form.
revision_deletion_cron in ./revision_deletion.module
Implements hook_cron().
revision_deletion_node_overview in ./revision_deletion.admin.inc
Form builder for the node revisions administration form.

File

./revision_deletion.helpers.inc, line 19
Helper functions.

Code

function _revision_deletion_get_list($nid = NULL, $header = NULL) {
  $now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
  $aged = $now - variable_get('revision_deletion_age');
  $subselect = db_select('node_revision', 'r')
    ->fields('r', array(
    'nid',
  ))
    ->condition('r.timestamp', is_null($nid) ? $aged : $now, '<');
  if ($nid) {
    $subselect
      ->condition('r.nid', $nid);
  }
  $nodes = variable_get('revision_deletion', array());
  if ($nodes) {
    $subselect
      ->condition('n.type', $nodes, 'IN');
  }
  $subselect
    ->join('node', 'n', 'r.nid = n.nid AND r.vid <> n.vid');
  $query = db_select('node_revision', 'r');
  $query
    ->fields('n', array(
    'title',
    'type',
    'status',
  ));
  $query
    ->addField('n', 'vid', 'current');
  $query
    ->fields('r', array(
    'nid',
    'vid',
    'timestamp',
    'uid',
    'log',
  ));
  $query
    ->condition('r.timestamp', is_null($nid) ? $aged : $now, '<');
  $query
    ->condition('n.nid', $subselect, 'IN');
  $query
    ->join('node', 'n', 'r.nid = n.nid');
  if ($header) {
    $query = $query
      ->extend('TableSort')
      ->orderByHeader($header);
  }
  else {
    $query
      ->orderBy('r.nid')
      ->orderBy('r.timestamp', 'DESC');
  }
  $query
    ->extend('PagerDefault')
    ->limit(50);
  $result = $query
    ->execute();
  $data = array();
  $previous_date = $previous_nid = $previous_vid = NULL;
  $keeper = FALSE;
  $format = variable_get('date_format_short', variable_get('date_format_short_custom', 'Y M j'));
  $format = trim(str_replace(array(
    'H',
    'i',
    ':',
  ), '', $format));
  foreach ($result as $node) {
    $note = '';
    $node->select = TRUE;
    $node->class = 'revision-selected';
    $node->date = format_date($node->timestamp, 'custom', $format);

    // See if previous was an original.
    if ($node->nid != $previous_nid) {
      $previous_nid = $node->nid;
      $oldest = db_query("SELECT vid FROM {node_revision} WHERE nid = :nid ORDER BY timestamp ASC LIMIT 1", array(
        ':nid' => $node->nid,
      ))
        ->fetchField();
    }
    $node->oldest = $oldest;

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

      // If it's not old enough yet, keep next.
      $keep_current = variable_get('revision_deletion_list_keep_current');
      if ($now - $node->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) {
        $note = '<em>' . t('Young current') . '</em>';
        $node->select = FALSE;
        $node->class = 'revision-young';
        $keeper = FALSE;
      }

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

        // Is it the last for the date?
        $keep_date_last = variable_get('revision_deletion_list_keep_date_last');
        if ($node->date != $previous_date && $keep_date_last) {
          $note = '<em>' . t('Last for !date', array(
            '!date' => $node->date,
          )) . '</em>';
          $node->select = FALSE;
          $node->class = 'revision-last';
        }
      }
    }
    $node->notes = variable_get('revision_deletion_list_show_notes') ? $note : '';

    // Save it all to be returned.
    $data[$node->vid] = $node;
    $previous_vid = $node->vid;
    $previous_date = $node->date;
  }
  return $data;
}