2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Install\Updates
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Database\ConnectionPool
;
18 use TYPO3\CMS\Core\Registry
;
19 use TYPO3\CMS\Core\Utility\GeneralUtility
;
20 use TYPO3\CMS\Install\Updates\RowUpdater\L10nModeUpdater
;
21 use TYPO3\CMS\Install\Updates\RowUpdater\RowUpdaterInterface
;
24 * This is a generic updater to migrate content of TCA rows.
26 * Multiple classes implementing interface "RowUpdateInterface" can be
27 * registered here, each for a specific update purpose.
29 * The updater fetches each row of all TCA registered tables and
30 * visits the client classes who may modify the row content.
32 * The updater remembers for each class if it run through, so the updater
33 * will be shown again if a new updater class is registered that has not
36 * A start position pointer is stored in the registry that is updated during
37 * the run process, so if for instance the PHP process runs into a timeout,
38 * the job can restart at the position it stopped.
40 class DatabaseRowsUpdateWizard
extends AbstractUpdate
43 * @var string Title of this updater
45 protected $title = 'Execute database migrations on single rows';
48 * @var array Single classes that may update rows
50 protected $rowUpdater = [
51 L10nModeUpdater
::class,
55 * Checks if an update is needed by looking up in registry if all
56 * registered update row classes are marked as done or not.
58 * @param string &$description The description for the update
59 * @return bool Whether an update is needed (TRUE) or not (FALSE)
61 public function checkForUpdate(&$description)
63 $updateNeeded = false;
64 $rowUpdaterNotExecuted = $this->getRowUpdatersToExecute();
65 if (!empty($rowUpdaterNotExecuted)) {
72 $description = 'Some row updaters have not been executed:';
73 foreach ($rowUpdaterNotExecuted as $rowUpdateClassName) {
74 $rowUpdater = GeneralUtility
::makeInstance($rowUpdateClassName);
75 if (!$rowUpdater instanceof RowUpdaterInterface
) {
76 throw new \
RuntimeException(
77 'Row updater must implement RowUpdaterInterface',
81 $description .= '<br />' . htmlspecialchars($rowUpdater->getTitle());
88 * Performs the configuration update.
90 * @param array &$databaseQueries Queries done in this update - not filled for this updater
91 * @param mixed &$customMessages Custom messages
93 * @throws \Doctrine\DBAL\ConnectionException
96 public function performUpdate(array &$databaseQueries, &$customMessages)
98 $registry = GeneralUtility
::makeInstance(Registry
::class);
100 // If rows from the target table that is updated and the sys_registry table are on the
101 // same connection, the row update statement and sys_registry position update will be
102 // handled in a transaction to have an atomic operation in case of errors during execution.
103 $connectionPool = GeneralUtility
::makeInstance(ConnectionPool
::class);
104 $connectionForSysRegistry = $connectionPool->getConnectionForTable('sys_registry');
106 /** @var RowUpdaterInterface[] $rowUpdaterInstances */
107 $rowUpdaterInstances = [];
108 // Single row updater instances are created only once for this method giving
109 // them a chance to set up local properties during hasPotentialUpdateForTable()
110 // and using that in updateTableRow()
111 foreach ($this->getRowUpdatersToExecute() as $rowUpdater) {
112 $rowUpdaterInstance = GeneralUtility
::makeInstance($rowUpdater);
113 if (!$rowUpdaterInstance instanceof RowUpdaterInterface
) {
114 throw new \
RuntimeException(
115 'Row updater must implement RowUpdaterInterface',
119 $rowUpdaterInstances[] = $rowUpdaterInstance;
122 // Scope of the row updater is to update all rows that have TCA,
123 // our list of tables is just the list of loaded TCA tables.
124 $listOfAllTables = array_keys($GLOBALS['TCA']);
126 // In case the PHP ended for whatever reason, fetch the last position from registry
127 // and throw away all tables before that start point.
128 sort($listOfAllTables);
129 reset($listOfAllTables);
130 $firstTable = current($listOfAllTables);
131 $startPosition = $this->getStartPosition($firstTable);
132 foreach ($listOfAllTables as $table) {
133 if ($table === $startPosition['table']) {
136 unset($listOfAllTables[$table]);
140 // Ask each row updater if it potentially has field updates for rows of a table
141 $tableToUpdaterList = [];
142 foreach ($listOfAllTables as $table) {
143 foreach ($rowUpdaterInstances as $updater) {
144 if ($updater->hasPotentialUpdateForTable($table)) {
145 if (!is_array($tableToUpdaterList[$table])) {
146 $tableToUpdaterList[$table] = [];
148 $tableToUpdaterList[$table][] = $updater;
153 // Iterate through all rows of all tables that have potential row updaters attached,
154 // feed each single row to each updater and finally update each row in database if
155 // a row updater changed a fields
156 foreach ($tableToUpdaterList as $table => $updaters) {
157 /** @var RowUpdaterInterface[] $updaters */
158 $connectionForTable = $connectionPool->getConnectionForTable($table);
159 $queryBuilder = $connectionPool->getQueryBuilderForTable($table);
160 $queryBuilder->getRestrictions()->removeAll();
161 $queryBuilder->select('*')
164 if ($table === $startPosition['table']) {
165 $queryBuilder->where(
166 $queryBuilder->expr()->gt('uid', $queryBuilder->createNamedParameter($startPosition['uid']))
169 $statement = $queryBuilder->execute();
170 $rowCountWithoutUpdate = 0;
171 while ($row = $rowBefore = $statement->fetch()) {
172 foreach ($updaters as $updater) {
173 $row = $updater->updateTableRow($table, $row);
175 $updatedFields = array_diff_assoc($row, $rowBefore);
176 if (empty($updatedFields)) {
177 // Updaters changed no field of that row
178 $rowCountWithoutUpdate ++
;
179 if ($rowCountWithoutUpdate >= 200) {
180 // Update startPosition if there were many rows without data change
183 'uid' => $row['uid'],
185 $registry->set('installUpdateRows', 'rowUpdatePosition', $startPosition);
186 $rowCountWithoutUpdate = 0;
189 $rowCountWithoutUpdate = 0;
192 'uid' => $rowBefore['uid'],
194 if ($connectionForSysRegistry === $connectionForTable) {
195 // Target table and sys_registry table are on the same connection, use a transaction
196 $connectionForTable->beginTransaction();
198 $connectionForTable->update(
202 'uid' => $rowBefore['uid'],
205 $connectionForTable->update(
208 'entry_value' => serialize($startPosition),
211 'entry_namespace' => 'installUpdateRows',
212 'entry_key' => 'rowUpdatePosition',
215 $connectionForTable->commit();
216 } catch (\Exception
$up) {
217 $connectionForTable->rollBack();
221 // Different connections for table and sys_registry -> execute two
222 // distinct queries and hope for the best.
223 $connectionForTable->update(
227 'uid' => $rowBefore['uid'],
230 $connectionForSysRegistry->update(
233 'entry_value' => serialize($startPosition),
236 'entry_namespace' => 'installUpdateRows',
237 'entry_key' => 'rowUpdatePosition',
245 // Ready with updates, remove position information from sys_registry
246 $registry->remove('installUpdateRows', 'rowUpdatePosition');
247 // Mark row updaters that were executed as done
248 foreach ($rowUpdaterInstances as $updater) {
249 $this->setRowUpdaterExecuted($updater);
256 * Return an array of class names that are not yet marked as done.
258 * @return array Class names
260 protected function getRowUpdatersToExecute(): array
262 $doneRowUpdater = GeneralUtility
::makeInstance(Registry
::class)->get('installUpdateRows', 'rowUpdatersDone', []);
263 return array_diff($this->rowUpdater
, $doneRowUpdater);
267 * Mark a single updater as done
269 * @param RowUpdaterInterface $updater
271 protected function setRowUpdaterExecuted(RowUpdaterInterface
$updater)
273 $registry = GeneralUtility
::makeInstance(Registry
::class);
274 $doneRowUpdater = $registry->get('installUpdateRows', 'rowUpdatersDone', []);
275 $doneRowUpdater[] = get_class($updater);
276 $registry->set('installUpdateRows', 'rowUpdatersDone', $doneRowUpdater);
280 * Return an array with table / uid combination that specifies the start position the
281 * update row process should start with.
283 * @param string $firstTable Table name of the first TCA in case the start position needs to be initialized
284 * @return array New start position
286 protected function getStartPosition(string $firstTable): array
288 $registry = GeneralUtility
::makeInstance(Registry
::class);
289 $startPosition = $registry->get('installUpdateRows', 'rowUpdaterPosition', []);
290 if (empty($startPosition)) {
292 'table' => $firstTable,
295 $registry->set('installUpdateRows', 'rowUpdatePosition', $startPosition);
297 return $startPosition;