You are here

class RevisionTracker in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/RevisionTracker.php \Drupal\workbench_moderation\RevisionTracker

Tracks metadata about revisions across entities.

Hierarchy

Expanded class hierarchy of RevisionTracker

1 string reference to 'RevisionTracker'
workbench_moderation.services.yml in ./workbench_moderation.services.yml
workbench_moderation.services.yml
1 service uses RevisionTracker
workbench_moderation.revision_tracker in ./workbench_moderation.services.yml
Drupal\workbench_moderation\RevisionTracker

File

src/RevisionTracker.php, line 12

Namespace

Drupal\workbench_moderation
View source
class RevisionTracker implements RevisionTrackerInterface {

  /**
   * The name of the SQL table we use for tracking.
   *
   * @var string
   */
  protected $tableName;

  /**
   * Constructs a new RevisionTracker.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * RevisionTracker constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param string $table
   *   The table that should be used for tracking.
   */
  public function __construct(Connection $connection, $table = 'workbench_revision_tracker') {
    $this->connection = $connection;
    $this->tableName = $table;
  }

  /**
   * {@inheritdoc}
   */
  public function setLatestRevision($entity_type, $entity_id, $langcode, $revision_id) {
    try {
      $this
        ->recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id);
    } catch (DatabaseExceptionWrapper $e) {
      $this
        ->ensureTableExists();
      $this
        ->recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id);
    }
    return $this;
  }

  /**
   * Records the latest revision of a given entity.
   *
   * @param string $entity_type
   *   The machine name of the type of entity.
   * @param string $entity_id
   *   The Entity ID in question.
   * @param string $langcode
   *   The langcode of the revision we're saving. Each language has its own
   *   effective tree of entity revisions, so in different languages
   *   different revisions will be "latest".
   * @param string $revision_id
   *   The revision ID that is now the latest revision.
   *
   * @return int
   *   One of the valid returns from a merge query's execute method.
   */
  protected function recordLatestRevision($entity_type, $entity_id, $langcode, $revision_id) {
    return $this->connection
      ->merge($this->tableName)
      ->keys([
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
      'langcode' => $langcode,
    ])
      ->fields([
      'revision_id' => $revision_id,
    ])
      ->execute();
  }

  /**
   * Checks if the table exists and create it if not.
   *
   * @return bool
   *   TRUE if the table was created, FALSE otherwise.
   */
  protected function ensureTableExists() {
    try {
      if (!$this->connection
        ->schema()
        ->tableExists($this->tableName)) {
        $this->connection
          ->schema()
          ->createTable($this->tableName, $this
          ->schemaDefinition());
        return TRUE;
      }
    } catch (SchemaObjectExistsException $e) {

      // If another process has already created the table, attempting to
      // recreate it will throw an exception. In this case just catch the
      // exception and do nothing.
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Defines the schema for the tracker table.
   *
   * @return array
   *   The schema API definition for the SQL storage table.
   */
  protected function schemaDefinition() {
    $schema = [
      'description' => 'Tracks the latest revision for any entity',
      'fields' => [
        'entity_type' => [
          'description' => 'The entity type',
          'type' => 'varchar_ascii',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ],
        'entity_id' => [
          'description' => 'The entity ID',
          'type' => 'int',
          'length' => 255,
          'not null' => TRUE,
          'default' => 0,
        ],
        'langcode' => [
          'description' => 'The language of the entity revision',
          'type' => 'varchar',
          'length' => 12,
          'not null' => TRUE,
          'default' => '',
        ],
        'revision_id' => [
          'description' => 'The latest revision ID for this entity',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ],
      ],
      'primary key' => [
        'entity_type',
        'entity_id',
        'langcode',
      ],
    ];
    return $schema;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RevisionTracker::$connection protected property Constructs a new RevisionTracker.
RevisionTracker::$tableName protected property The name of the SQL table we use for tracking.
RevisionTracker::ensureTableExists protected function Checks if the table exists and create it if not.
RevisionTracker::recordLatestRevision protected function Records the latest revision of a given entity.
RevisionTracker::schemaDefinition protected function Defines the schema for the tracker table.
RevisionTracker::setLatestRevision public function Sets the latest revision of a given entity. Overrides RevisionTrackerInterface::setLatestRevision
RevisionTracker::__construct public function RevisionTracker constructor.