3 namespace Drupal\content_moderation;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\DatabaseExceptionWrapper;
7 use Drupal\Core\Database\SchemaObjectExistsException;
10 * Tracks metadata about revisions across entities.
12 class RevisionTracker implements RevisionTrackerInterface {
15 * The name of the SQL table we use for tracking.
22 * The database connection.
24 * @var \Drupal\Core\Database\Connection
26 protected $connection;
29 * Constructs a new RevisionTracker.
31 * @param \Drupal\Core\Database\Connection $connection
32 * The database connection.
33 * @param string $table
34 * The table that should be used for tracking.
36 public function __construct(Connection $connection, $table = 'content_revision_tracker') {
37 $this->connection = $connection;
38 $this->tableName = $table;
44 public function setLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id) {
46 $this->recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id);
48 catch (DatabaseExceptionWrapper $e) {
49 $this->ensureTableExists();
50 $this->recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id);
57 * Records the latest revision of a given entity.
59 * @param string $entity_type_id
60 * The machine name of the type of entity.
61 * @param string $entity_id
62 * The Entity ID in question.
63 * @param string $langcode
64 * The langcode of the revision we're saving. Each language has its own
65 * effective tree of entity revisions, so in different languages
66 * different revisions will be "latest".
67 * @param int $revision_id
68 * The revision ID that is now the latest revision.
71 * One of the valid returns from a merge query's execute method.
73 protected function recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id) {
74 return $this->connection->merge($this->tableName)
76 'entity_type' => $entity_type_id,
77 'entity_id' => $entity_id,
78 'langcode' => $langcode,
81 'revision_id' => $revision_id,
87 * Checks if the table exists and create it if not.
90 * TRUE if the table was created, FALSE otherwise.
92 protected function ensureTableExists() {
94 if (!$this->connection->schema()->tableExists($this->tableName)) {
95 $this->connection->schema()->createTable($this->tableName, $this->schemaDefinition());
99 catch (SchemaObjectExistsException $e) {
100 // If another process has already created the table, attempting to
101 // recreate it will throw an exception. In this case just catch the
102 // exception and do nothing.
109 * Defines the schema for the tracker table.
112 * The schema API definition for the SQL storage table.
114 protected function schemaDefinition() {
116 'description' => 'Tracks the latest revision for any entity',
119 'description' => 'The entity type',
120 'type' => 'varchar_ascii',
126 'description' => 'The entity ID',
133 'description' => 'The language of the entity revision',
140 'description' => 'The latest revision ID for this entity',
146 'primary key' => ['entity_type', 'entity_id', 'langcode'],