‪TYPO3CMS  10.4
ReferenceIndexUpdater.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
34 {
40  protected $updateRegistry = [];
41 
50  protected $updateRegistryToItem = [];
51 
57  protected $dropRegistry = [];
58 
66  public function registerForUpdate(string $table, int $uid, int $workspace): void
67  {
68  if ($workspace && !‪BackendUtility::isTableWorkspaceEnabled($table)) {
69  // If a user is in some workspace and changes relations of not workspace aware
70  // records, the reference index update needs to be performed as if the user
71  // is in live workspace. This is detected here and the update is registered for live.
72  $workspace = 0;
73  }
74  if (!isset($this->updateRegistry[$workspace][$table])) {
75  $this->updateRegistry[$workspace][$table] = [];
76  }
77  if (!in_array($uid, $this->updateRegistry[$workspace][$table], true)) {
78  $this->updateRegistry[$workspace][$table][] = $uid;
79  }
80  }
81 
93  public function registerUpdateForReferencesToItem(string $table, int $uid, int $workspace, int $targetWorkspace = null): void
94  {
95  if ($workspace && !‪BackendUtility::isTableWorkspaceEnabled($table)) {
96  // If a user is in some workspace and changes relations of not workspace aware
97  // records, the reference index update needs to be performed as if the user
98  // is in live workspace. This is detected here and the update is registered for live.
99  $workspace = 0;
100  }
101  if ($targetWorkspace === null) {
102  $targetWorkspace = $workspace;
103  }
104  if (!isset($this->‪updateRegistryToItem[$workspace][$table])) {
105  $this->‪updateRegistryToItem[$workspace][$table] = [];
106  }
108  'uid' => $uid,
109  'targetWorkspace' => $targetWorkspace
110  ];
111  if (!in_array(‪$recordAndTargetWorkspace, $this->‪updateRegistryToItem[$workspace][$table], true)) {
112  $this->‪updateRegistryToItem[$workspace][$table][] = ‪$recordAndTargetWorkspace;
113  }
114  }
115 
126  public function ‪registerForDrop(string $table, int $uid, int $workspace): void
127  {
128  if ($workspace && !‪BackendUtility::isTableWorkspaceEnabled($table)) {
129  // If a user is in some workspace and changes relations of not workspace aware
130  // records, the reference index update needs to be performed as if the user
131  // is in live workspace. This is detected here and the update is registered for live.
132  $workspace = 0;
133  }
134  if (!isset($this->dropRegistry[$workspace][$table])) {
135  $this->dropRegistry[$workspace][$table] = [];
136  }
137  if (!in_array($uid, $this->dropRegistry[$workspace][$table], true)) {
138  $this->dropRegistry[$workspace][$table][] = $uid;
139  }
140  }
141 
145  public function ‪update(): void
146  {
147  // Register updates to an item for update
148  foreach ($this->‪updateRegistryToItem as $workspace => $tableArray) {
149  foreach ($tableArray as $table => $recordArray) {
150  foreach ($recordArray as $item) {
151  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
152  $statement = $queryBuilder
153  ->select('tablename', 'recuid')
154  ->from('sys_refindex')
155  ->where(
156  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter($table)),
157  $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($item['uid'], \PDO::PARAM_INT)),
158  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
159  $queryBuilder->expr()->eq('workspace', $queryBuilder->createNamedParameter($workspace, \PDO::PARAM_INT))
160  )
161  ->execute();
162  while ($row = $statement->fetch()) {
163  $this->registerForUpdate($row['tablename'], (int)$row['recuid'], $item['targetWorkspace']);
164  }
165  }
166  }
167  }
168  $this->‪updateRegistryToItem = [];
169 
170  // Drop rows from reference index if requested. Note this is performed *after* update-to-item, to
171  // find rows pointing to a record and register updates before rows are dropped. Needed if a record
172  // changes the workspace during publish: In this case all records pointing to the record in a workspace
173  // need to be registered for update for live workspace and after that the workspace rows can be dropped.
174  foreach ($this->dropRegistry as $workspace => $tableArray) {
175  foreach ($tableArray as $table => $uidArray) {
176  foreach ($uidArray as $uid) {
177  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
178  $queryBuilder->delete('sys_refindex')
179  ->where(
180  $queryBuilder->expr()->eq('workspace', $queryBuilder->createNamedParameter($workspace, \PDO::PARAM_INT)),
181  $queryBuilder->expr()->orX(
182  $queryBuilder->expr()->andX(
183  $queryBuilder->expr()->eq('tablename', $queryBuilder->createNamedParameter($table)),
184  $queryBuilder->expr()->eq('recuid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
185  ),
186  $queryBuilder->expr()->andX(
187  $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter($table)),
188  $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
189  )
190  )
191  )
192  ->execute();
193  }
194  }
195  }
196  $this->dropRegistry = [];
197 
198  // Perform reference index updates
199  $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
200  $referenceIndex->enableRuntimeCache();
201  foreach ($this->updateRegistry as $workspace => $tableArray) {
202  $referenceIndex->setWorkspaceId($workspace);
203  foreach ($tableArray as $table => $uidArray) {
204  foreach ($uidArray as $uid) {
205  $referenceIndex->updateRefIndexTable($table, $uid);
206  }
207  }
208  }
209  $this->updateRegistry = [];
210  }
211 }
‪TYPO3\CMS\Core\DataHandling\ReferenceIndexUpdater\updateRegistryToItem
‪array< int, $updateRegistry=array();protected array< int, $updateRegistryToItem=array();protected array< int, $dropRegistry=array();public function registerForUpdate(string $table, int $uid, int $workspace):void { if( $workspace &&!BackendUtility::isTableWorkspaceEnabled( $table)) { $workspace=0;} if(!isset( $this->updateRegistry[ $workspace][ $table])) { $this->updateRegistry[ $workspace][ $table]=[];} if(!in_array( $uid, $this->updateRegistry[ $workspace][ $table], true)) { $this->updateRegistry[ $workspace][ $table][]=$uid;} } public function registerUpdateForReferencesToItem(string $table, int $uid, int $workspace, int $targetWorkspace=null):void { if( $workspace &&!BackendUtility::isTableWorkspaceEnabled( $table)) { $workspace=0;} if( $targetWorkspace===null) { $targetWorkspace=$workspace;} if(!isset( $this->updateRegistryToItem[ $workspace][ $table])) { $this-> updateRegistryToItem[$workspace][$table]
Definition: ReferenceIndexUpdater.php:102
‪TYPO3\CMS\Core\Database\ReferenceIndex
Definition: ReferenceIndex.php:48
‪TYPO3\CMS\Core\DataHandling
Definition: DataHandler.php:16
‪TYPO3\CMS\Core\DataHandling\ReferenceIndexUpdater\registerForDrop
‪if(!in_array($recordAndTargetWorkspace, $this->updateRegistryToItem[$workspace][$table], true)) registerForDrop(string $table, int $uid, int $workspace)
Definition: ReferenceIndexUpdater.php:123
‪TYPO3\CMS\Backend\Utility\BackendUtility\isTableWorkspaceEnabled
‪static bool isTableWorkspaceEnabled($table)
Definition: BackendUtility.php:4021
‪TYPO3\CMS\Core\DataHandling\ReferenceIndexUpdater\$recordAndTargetWorkspace
‪$recordAndTargetWorkspace
Definition: ReferenceIndexUpdater.php:104
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\DataHandling\ReferenceIndexUpdater
Definition: ReferenceIndexUpdater.php:34
‪TYPO3\CMS\Core\DataHandling\ReferenceIndexUpdater\update
‪update()
Definition: ReferenceIndexUpdater.php:142