You are here

public function NodeRevisionDelete::getCandidatesRevisions in Node Revision Delete 8

Return the list of candidate revisions to be deleted.

Parameters

string $content_type: Content type machine name.

int $number: The number of revisions to return.

Return value

array Array of vids.

Overrides NodeRevisionDeleteInterface::getCandidatesRevisions

1 call to NodeRevisionDelete::getCandidatesRevisions()
NodeRevisionDelete::getCandidatesRevisionsByNumber in src/NodeRevisionDelete.php
Return a number of candidate revisions to be deleted.

File

src/NodeRevisionDelete.php, line 315

Class

NodeRevisionDelete
Class NodeRevisionDelete.

Namespace

Drupal\node_revision_delete

Code

public function getCandidatesRevisions($content_type, $number = PHP_INT_MAX) {

  // @TODO check if the method can be improved.
  if (!is_int($number) && $number < 0) {
    throw new \InvalidArgumentException("\$number parameter must be a positive integer");
  }
  $candidate_revisions = [];

  // Getting the content type config.
  $content_type_config = $this
    ->getContentTypeConfigWithRelativeTime($content_type);
  if (!empty($content_type_config)) {

    // Getting the candidate nodes.
    $candidate_nodes = $this
      ->getCandidatesNodes($content_type);
    foreach ($candidate_nodes as $candidate_node) {
      $sub_query = $this->connection
        ->select('node_field_data', 'n');
      $sub_query
        ->join('node_revision', 'r', 'r.nid = n.nid');
      $sub_query
        ->fields('r', [
        'vid',
        'revision_timestamp',
      ]);
      $sub_query
        ->condition('n.nid', $candidate_node);
      $sub_query
        ->condition('changed', $content_type_config['when_to_delete'], '<');
      if ($this->configFactory
        ->get($this->configurationFileName)
        ->get('delete_newer')) {
        $sub_query
          ->where('n.vid <> r.vid');
      }
      else {
        $sub_query
          ->where('n.vid > r.vid');
      }
      $sub_query
        ->groupBy('n.nid');
      $sub_query
        ->groupBy('r.vid');
      $sub_query
        ->groupBy('r.revision_timestamp');
      $sub_query
        ->orderBy('revision_timestamp', 'DESC');

      // We need to reduce in 1 because we don't want to count the default
      // vid. We excluded the default revision in the where call.
      $sub_query
        ->range($content_type_config['minimum_revisions_to_keep'] - 1, $number);

      // Allow other modules to alter candidates query.
      $sub_query
        ->addTag('node_revision_delete_candidate_revisions');
      $sub_query
        ->addTag('node_revision_delete_candidate_revisions_' . $content_type);
      $query = $this->connection
        ->select($sub_query, 't');
      $query
        ->fields('t', [
        'vid',
      ]);
      $query
        ->condition('revision_timestamp', $content_type_config['minimum_age_to_delete'], '<');
      $candidate_revisions = array_merge($candidate_revisions, $query
        ->execute()
        ->fetchCol());
    }
  }
  return $candidate_revisions;
}