You are here

class NodeRevisionGenerate in Node Revision Delete 8

Class NodeRevisionGenerate.

@package Drupal\node_revision_generate

Hierarchy

Expanded class hierarchy of NodeRevisionGenerate

1 file declares its use of NodeRevisionGenerate
NodeRevisionGenerateTest.php in modules/node_revision_generate/tests/src/Unit/NodeRevisionGenerateTest.php
1 string reference to 'NodeRevisionGenerate'
node_revision_generate.services.yml in modules/node_revision_generate/node_revision_generate.services.yml
modules/node_revision_generate/node_revision_generate.services.yml
1 service uses NodeRevisionGenerate
node_revision_generate in modules/node_revision_generate/node_revision_generate.services.yml
Drupal\node_revision_generate\NodeRevisionGenerate

File

modules/node_revision_generate/src/NodeRevisionGenerate.php, line 16

Namespace

Drupal\node_revision_generate
View source
class NodeRevisionGenerate implements NodeRevisionGenerateInterface {
  use StringTranslationTrait;

  /**
   * A date time instance.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Drupal\Core\Database\Driver\mysql\Connection definition.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructor.
   *
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The date time instance.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(TimeInterface $time, Connection $connection, TranslationInterface $string_translation) {
    $this->time = $time;
    $this->connection = $connection;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableNodesForRevisions(array $bundles, $revisions_age) {

    // Variable with the placeholders arguments needed for the expression.
    $interval_time = [
      ':interval' => $revisions_age,
      ':current_time' => $this->time
        ->getRequestTime(),
    ];
    $query = $this->connection
      ->select('node_field_data', 'node');

    // Get/check the last revision (vid).
    $query
      ->leftJoin('node_revision', 'revision', 'node.vid = revision.vid');

    // Get the node id to generate revisions.
    $query
      ->addField('node', 'nid');

    // Get the node id to generate revisions.
    $query
      ->addField('revision', 'revision_timestamp');

    // Get nodes with title to avoid some error on save it.
    $query
      ->isNotNull('node.title');

    // Get nodes of selected content types (bundles).
    $query
      ->condition('node.type', $bundles, 'IN');

    // Get only the published nodes.
    $query
      ->condition('node.status', 1);

    // Check the next date to generate the revision be <= current date.
    $query
      ->where('revision.revision_timestamp + :interval <= :current_time', $interval_time);

    // Return the available nodes ids and its next revision date, as array.
    return $query
      ->execute()
      ->fetchAll();
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionCreationBatch(array $nodes_for_revisions, $revisions_number, $revisions_age) {

    // Defining the batch builder.
    $batch_builder = new BatchBuilder();
    $batch_builder
      ->setTitle($this
      ->t('Generating revisions'))
      ->setInitMessage($this
      ->t('Starting to create revisions.'))
      ->setProgressMessage($this
      ->t('Processed @current out of @total (@percentage%). Estimated time: @estimate.'))
      ->setErrorMessage($this
      ->t('The revision creation process has encountered an error.'))
      ->setFinishCallback([
      NodeRevisionGenerateBatch::class,
      'finish',
    ]);

    // Building batch operations, one per revision.
    foreach ($nodes_for_revisions as $node) {
      $revision_timestamp = $node->revision_timestamp;

      // Initializing variables.
      $i = 0;
      $revision_timestamp += $revisions_age;

      // Adding operations.
      while ($i < $revisions_number && $revision_timestamp <= $this->time
        ->getRequestTime()) {

        // Adding the operation.
        $batch_builder
          ->addOperation([
          NodeRevisionGenerateBatch::class,
          'generateRevisions',
        ], [
          $node->nid,
          $revision_timestamp,
        ]);
        $revision_timestamp += $revisions_age;
        $i++;
      }
    }
    return $batch_builder
      ->toArray();
  }

  /**
   * {@inheritdoc}
   */
  public function existsNodesContentType($content_type) {
    $query = $this->connection
      ->select('node');

    // Get the node id to generate revisions.
    $query
      ->addField('node', 'nid');

    // Get nodes of selected content types (bundles).
    $query
      ->condition('type', $content_type);

    // Return the available nodes ids and its next revision date, as array.
    return (bool) $query
      ->countQuery()
      ->execute()
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeRevisionGenerate::$connection protected property Drupal\Core\Database\Driver\mysql\Connection definition.
NodeRevisionGenerate::$time protected property A date time instance.
NodeRevisionGenerate::existsNodesContentType public function Returns if exists nodes of a content type. Overrides NodeRevisionGenerateInterface::existsNodesContentType
NodeRevisionGenerate::getAvailableNodesForRevisions public function Get the available nodes to generate revisions. Overrides NodeRevisionGenerateInterface::getAvailableNodesForRevisions
NodeRevisionGenerate::getRevisionCreationBatch public function Return the revision creation batch definition. Overrides NodeRevisionGenerateInterface::getRevisionCreationBatch
NodeRevisionGenerate::__construct public function Constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.