Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / content_moderation / src / RevisionTracker.php
1 <?php
2
3 namespace Drupal\content_moderation;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\DatabaseExceptionWrapper;
7 use Drupal\Core\Database\SchemaObjectExistsException;
8
9 /**
10  * Tracks metadata about revisions across entities.
11  */
12 class RevisionTracker implements RevisionTrackerInterface {
13
14   /**
15    * The name of the SQL table we use for tracking.
16    *
17    * @var string
18    */
19   protected $tableName;
20
21   /**
22    * The database connection.
23    *
24    * @var \Drupal\Core\Database\Connection
25    */
26   protected $connection;
27
28   /**
29    * Constructs a new RevisionTracker.
30    *
31    * @param \Drupal\Core\Database\Connection $connection
32    *   The database connection.
33    * @param string $table
34    *   The table that should be used for tracking.
35    */
36   public function __construct(Connection $connection, $table = 'content_revision_tracker') {
37     $this->connection = $connection;
38     $this->tableName = $table;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function setLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id) {
45     try {
46       $this->recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id);
47     }
48     catch (DatabaseExceptionWrapper $e) {
49       $this->ensureTableExists();
50       $this->recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id);
51     }
52
53     return $this;
54   }
55
56   /**
57    * Records the latest revision of a given entity.
58    *
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.
69    *
70    * @return int
71    *   One of the valid returns from a merge query's execute method.
72    */
73   protected function recordLatestRevision($entity_type_id, $entity_id, $langcode, $revision_id) {
74     return $this->connection->merge($this->tableName)
75       ->keys([
76         'entity_type' => $entity_type_id,
77         'entity_id' => $entity_id,
78         'langcode' => $langcode,
79       ])
80       ->fields([
81         'revision_id' => $revision_id,
82       ])
83       ->execute();
84   }
85
86   /**
87    * Checks if the table exists and create it if not.
88    *
89    * @return bool
90    *   TRUE if the table was created, FALSE otherwise.
91    */
92   protected function ensureTableExists() {
93     try {
94       if (!$this->connection->schema()->tableExists($this->tableName)) {
95         $this->connection->schema()->createTable($this->tableName, $this->schemaDefinition());
96         return TRUE;
97       }
98     }
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.
103       return TRUE;
104     }
105     return FALSE;
106   }
107
108   /**
109    * Defines the schema for the tracker table.
110    *
111    * @return array
112    *   The schema API definition for the SQL storage table.
113    */
114   protected function schemaDefinition() {
115     $schema = [
116       'description' => 'Tracks the latest revision for any entity',
117       'fields' => [
118         'entity_type' => [
119           'description' => 'The entity type',
120           'type' => 'varchar_ascii',
121           'length' => 255,
122           'not null' => TRUE,
123           'default' => '',
124         ],
125         'entity_id' => [
126           'description' => 'The entity ID',
127           'type' => 'int',
128           'length' => 255,
129           'not null' => TRUE,
130           'default' => 0,
131         ],
132         'langcode' => [
133           'description' => 'The language of the entity revision',
134           'type' => 'varchar',
135           'length' => 12,
136           'not null' => TRUE,
137           'default' => '',
138         ],
139         'revision_id' => [
140           'description' => 'The latest revision ID for this entity',
141           'type' => 'int',
142           'not null' => TRUE,
143           'default' => 0,
144         ],
145       ],
146       'primary key' => ['entity_type', 'entity_id', 'langcode'],
147     ];
148
149     return $schema;
150   }
151
152 }