/web/core/modules/workspaces/src/WorkspaceAssociation.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 249 lines · 144 code · 35 blank · 70 comment · 11 complexity · 190a9b3350e5795632d3f2546a750c2f MD5 · raw file

  1. <?php
  2. namespace Drupal\workspaces;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Entity\EntityTypeManagerInterface;
  5. use Drupal\Core\Entity\RevisionableInterface;
  6. use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
  7. /**
  8. * Provides a class for CRUD operations on workspace associations.
  9. */
  10. class WorkspaceAssociation implements WorkspaceAssociationInterface {
  11. /**
  12. * The table for the workspace association storage.
  13. */
  14. const TABLE = 'workspace_association';
  15. /**
  16. * The database connection.
  17. *
  18. * @var \Drupal\Core\Database\Connection
  19. */
  20. protected $database;
  21. /**
  22. * The entity type manager.
  23. *
  24. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  25. */
  26. protected $entityTypeManager;
  27. /**
  28. * The workspace repository service.
  29. *
  30. * @var \Drupal\workspaces\WorkspaceRepositoryInterface
  31. */
  32. protected $workspaceRepository;
  33. /**
  34. * Constructs a WorkspaceAssociation object.
  35. *
  36. * @param \Drupal\Core\Database\Connection $connection
  37. * A database connection for reading and writing path aliases.
  38. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  39. * The entity type manager for querying revisions.
  40. * @param \Drupal\workspaces\WorkspaceRepositoryInterface $workspace_repository
  41. * The Workspace repository service.
  42. */
  43. public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager, WorkspaceRepositoryInterface $workspace_repository) {
  44. $this->database = $connection;
  45. $this->entityTypeManager = $entity_type_manager;
  46. $this->workspaceRepository = $workspace_repository;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function trackEntity(RevisionableInterface $entity, WorkspaceInterface $workspace) {
  52. // Determine all workspaces that might be affected by this change.
  53. $affected_workspaces = $this->workspaceRepository->getDescendantsAndSelf($workspace->id());
  54. // Get the currently tracked revision for this workspace.
  55. $tracked = $this->getTrackedEntities($workspace->id(), $entity->getEntityTypeId(), [$entity->id()]);
  56. $tracked_revision_id = NULL;
  57. if (isset($tracked[$entity->getEntityTypeId()])) {
  58. $tracked_revision_id = key($tracked[$entity->getEntityTypeId()]);
  59. }
  60. $transaction = $this->database->startTransaction();
  61. try {
  62. // Update all affected workspaces that were tracking the current revision.
  63. // This means they are inheriting content and should be updated.
  64. if ($tracked_revision_id) {
  65. $this->database->update(static::TABLE)
  66. ->fields([
  67. 'target_entity_revision_id' => $entity->getRevisionId(),
  68. ])
  69. ->condition('workspace', $affected_workspaces, 'IN')
  70. ->condition('target_entity_type_id', $entity->getEntityTypeId())
  71. ->condition('target_entity_id', $entity->id())
  72. // Only update descendant workspaces if they have the same initial
  73. // revision, which means they are currently inheriting content.
  74. ->condition('target_entity_revision_id', $tracked_revision_id)
  75. ->execute();
  76. }
  77. // Insert a new index entry for each workspace that is not tracking this
  78. // entity yet.
  79. $missing_workspaces = array_diff($affected_workspaces, $this->getEntityTrackingWorkspaceIds($entity));
  80. if ($missing_workspaces) {
  81. $insert_query = $this->database->insert(static::TABLE)
  82. ->fields([
  83. 'workspace',
  84. 'target_entity_revision_id',
  85. 'target_entity_type_id',
  86. 'target_entity_id',
  87. ]);
  88. foreach ($missing_workspaces as $workspace_id) {
  89. $insert_query->values([
  90. 'workspace' => $workspace_id,
  91. 'target_entity_type_id' => $entity->getEntityTypeId(),
  92. 'target_entity_id' => $entity->id(),
  93. 'target_entity_revision_id' => $entity->getRevisionId(),
  94. ]);
  95. }
  96. $insert_query->execute();
  97. }
  98. }
  99. catch (\Exception $e) {
  100. $transaction->rollBack();
  101. watchdog_exception('workspaces', $e);
  102. throw $e;
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function workspaceInsert(WorkspaceInterface $workspace) {
  109. // When a new workspace has been saved, we need to copy all the associations
  110. // of its parent.
  111. if ($workspace->hasParent()) {
  112. $this->initializeWorkspace($workspace);
  113. }
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getTrackedEntities($workspace_id, $entity_type_id = NULL, $entity_ids = NULL) {
  119. $query = $this->database->select(static::TABLE);
  120. $query
  121. ->fields(static::TABLE, ['target_entity_type_id', 'target_entity_id', 'target_entity_revision_id'])
  122. ->orderBy('target_entity_revision_id', 'ASC')
  123. ->condition('workspace', $workspace_id);
  124. if ($entity_type_id) {
  125. $query->condition('target_entity_type_id', $entity_type_id, '=');
  126. if ($entity_ids) {
  127. $query->condition('target_entity_id', $entity_ids, 'IN');
  128. }
  129. }
  130. $tracked_revisions = [];
  131. foreach ($query->execute() as $record) {
  132. $tracked_revisions[$record->target_entity_type_id][$record->target_entity_revision_id] = $record->target_entity_id;
  133. }
  134. return $tracked_revisions;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getAssociatedRevisions($workspace_id, $entity_type_id, $entity_ids = NULL) {
  140. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  141. $storage = $this->entityTypeManager->getStorage($entity_type_id);
  142. // If the entity type is not using core's default entity storage, we can't
  143. // assume the table mapping layout so we have to return only the latest
  144. // tracked revisions.
  145. if (!$storage instanceof SqlContentEntityStorage) {
  146. return $this->getTrackedEntities($workspace_id, $entity_type_id, $entity_ids)[$entity_type_id];
  147. }
  148. $entity_type = $storage->getEntityType();
  149. $table_mapping = $storage->getTableMapping();
  150. $workspace_field = $table_mapping->getColumnNames($entity_type->get('revision_metadata_keys')['workspace'])['target_id'];
  151. $id_field = $table_mapping->getColumnNames($entity_type->getKey('id'))['value'];
  152. $revision_id_field = $table_mapping->getColumnNames($entity_type->getKey('revision'))['value'];
  153. $query = $this->database->select($entity_type->getRevisionTable(), 'revision');
  154. $query->leftJoin($entity_type->getBaseTable(), 'base', "[revision].[$id_field] = [base].[$id_field]");
  155. $query
  156. ->fields('revision', [$revision_id_field, $id_field])
  157. ->condition("revision.$workspace_field", $workspace_id)
  158. ->where("[revision].[$revision_id_field] > [base].[$revision_id_field]")
  159. ->orderBy("revision.$revision_id_field", 'ASC');
  160. // Restrict the result to a set of entity ID's if provided.
  161. if ($entity_ids) {
  162. $query->condition("revision.$id_field", $entity_ids, 'IN');
  163. }
  164. return $query->execute()->fetchAllKeyed();
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function getEntityTrackingWorkspaceIds(RevisionableInterface $entity) {
  170. $query = $this->database->select(static::TABLE)
  171. ->fields(static::TABLE, ['workspace'])
  172. ->condition('target_entity_type_id', $entity->getEntityTypeId())
  173. ->condition('target_entity_id', $entity->id());
  174. return $query->execute()->fetchCol();
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function postPublish(WorkspaceInterface $workspace) {
  180. $this->deleteAssociations($workspace->id());
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function deleteAssociations($workspace_id, $entity_type_id = NULL, $entity_ids = NULL) {
  186. $query = $this->database->delete(static::TABLE)
  187. ->condition('workspace', $workspace_id);
  188. if ($entity_type_id) {
  189. $query->condition('target_entity_type_id', $entity_type_id, '=');
  190. if ($entity_ids) {
  191. $query->condition('target_entity_id', $entity_ids, 'IN');
  192. }
  193. }
  194. $query->execute();
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function initializeWorkspace(WorkspaceInterface $workspace) {
  200. if ($parent_id = $workspace->parent->target_id) {
  201. $indexed_rows = $this->database->select(static::TABLE);
  202. $indexed_rows->addExpression(':new_id', 'workspace', [
  203. ':new_id' => $workspace->id(),
  204. ]);
  205. $indexed_rows->fields(static::TABLE, [
  206. 'target_entity_type_id',
  207. 'target_entity_id',
  208. 'target_entity_revision_id',
  209. ]);
  210. $indexed_rows->condition('workspace', $parent_id);
  211. $this->database->insert(static::TABLE)->from($indexed_rows)->execute();
  212. }
  213. }
  214. }