|
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
|
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Install\Updates; |
|
19
|
|
|
|
|
20
|
|
|
use TYPO3\CMS\Core\Database\Connection; |
|
21
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
|
22
|
|
|
use TYPO3\CMS\Core\Registry; |
|
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
24
|
|
|
use TYPO3\CMS\Install\Updates\RowUpdater\RowUpdaterInterface; |
|
25
|
|
|
use TYPO3\CMS\Install\Updates\RowUpdater\WorkspaceVersionRecordsMigration; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This is a generic updater to migrate content of TCA rows. |
|
29
|
|
|
* |
|
30
|
|
|
* Multiple classes implementing interface "RowUpdateInterface" can be |
|
31
|
|
|
* registered here, each for a specific update purpose. |
|
32
|
|
|
* |
|
33
|
|
|
* The updater fetches each row of all TCA registered tables and |
|
34
|
|
|
* visits the client classes who may modify the row content. |
|
35
|
|
|
* |
|
36
|
|
|
* The updater remembers for each class if it run through, so the updater |
|
37
|
|
|
* will be shown again if a new updater class is registered that has not |
|
38
|
|
|
* been run yet. |
|
39
|
|
|
* |
|
40
|
|
|
* A start position pointer is stored in the registry that is updated during |
|
41
|
|
|
* the run process, so if for instance the PHP process runs into a timeout, |
|
42
|
|
|
* the job can restart at the position it stopped. |
|
43
|
|
|
* @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API. |
|
44
|
|
|
*/ |
|
45
|
|
|
class DatabaseRowsUpdateWizard implements UpgradeWizardInterface, RepeatableInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var array Single classes that may update rows |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $rowUpdater = [ |
|
51
|
|
|
WorkspaceVersionRecordsMigration::class, |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @internal |
|
56
|
|
|
* @return string[] |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getAvailableRowUpdater(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->rowUpdater; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string Unique identifier of this updater |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getIdentifier(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return 'databaseRowsUpdateWizard'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string Title of this updater |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getTitle(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return 'Execute database migrations on single rows'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string Longer description of this updater |
|
81
|
|
|
* @throws \RuntimeException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getDescription(): string |
|
84
|
|
|
{ |
|
85
|
|
|
$rowUpdaterNotExecuted = $this->getRowUpdatersToExecute(); |
|
86
|
|
|
$description = 'Row updaters that have not been executed:'; |
|
87
|
|
|
foreach ($rowUpdaterNotExecuted as $rowUpdateClassName) { |
|
88
|
|
|
$rowUpdater = GeneralUtility::makeInstance($rowUpdateClassName); |
|
89
|
|
|
if (!$rowUpdater instanceof RowUpdaterInterface) { |
|
90
|
|
|
throw new \RuntimeException( |
|
91
|
|
|
'Row updater must implement RowUpdaterInterface', |
|
92
|
|
|
1484066647 |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
$description .= LF . $rowUpdater->getTitle(); |
|
96
|
|
|
} |
|
97
|
|
|
return $description; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool True if at least one row updater is not marked done |
|
102
|
|
|
*/ |
|
103
|
|
|
public function updateNecessary(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return !empty($this->getRowUpdatersToExecute()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string[] All new fields and tables must exist |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getPrerequisites(): array |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
|
|
DatabaseUpdatedPrerequisite::class |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Performs the configuration update. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
public function executeUpdate(): bool |
|
126
|
|
|
{ |
|
127
|
|
|
$registry = GeneralUtility::makeInstance(Registry::class); |
|
128
|
|
|
|
|
129
|
|
|
// If rows from the target table that is updated and the sys_registry table are on the |
|
130
|
|
|
// same connection, the row update statement and sys_registry position update will be |
|
131
|
|
|
// handled in a transaction to have an atomic operation in case of errors during execution. |
|
132
|
|
|
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
133
|
|
|
$connectionForSysRegistry = $connectionPool->getConnectionForTable('sys_registry'); |
|
134
|
|
|
|
|
135
|
|
|
/** @var RowUpdaterInterface[] $rowUpdaterInstances */ |
|
136
|
|
|
$rowUpdaterInstances = []; |
|
137
|
|
|
// Single row updater instances are created only once for this method giving |
|
138
|
|
|
// them a chance to set up local properties during hasPotentialUpdateForTable() |
|
139
|
|
|
// and using that in updateTableRow() |
|
140
|
|
|
foreach ($this->getRowUpdatersToExecute() as $rowUpdater) { |
|
141
|
|
|
$rowUpdaterInstance = GeneralUtility::makeInstance($rowUpdater); |
|
142
|
|
|
if (!$rowUpdaterInstance instanceof RowUpdaterInterface) { |
|
143
|
|
|
throw new \RuntimeException( |
|
144
|
|
|
'Row updater must implement RowUpdaterInterface', |
|
145
|
|
|
1484071612 |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
$rowUpdaterInstances[] = $rowUpdaterInstance; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Scope of the row updater is to update all rows that have TCA, |
|
152
|
|
|
// our list of tables is just the list of loaded TCA tables. |
|
153
|
|
|
$listOfAllTables = array_keys($GLOBALS['TCA']); |
|
154
|
|
|
|
|
155
|
|
|
// In case the PHP ended for whatever reason, fetch the last position from registry |
|
156
|
|
|
// and throw away all tables before that start point. |
|
157
|
|
|
sort($listOfAllTables); |
|
158
|
|
|
reset($listOfAllTables); |
|
159
|
|
|
$firstTable = current($listOfAllTables); |
|
160
|
|
|
$startPosition = $this->getStartPosition($firstTable); |
|
161
|
|
|
foreach ($listOfAllTables as $key => $table) { |
|
162
|
|
|
if ($table === $startPosition['table']) { |
|
163
|
|
|
break; |
|
164
|
|
|
} |
|
165
|
|
|
unset($listOfAllTables[$key]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Ask each row updater if it potentially has field updates for rows of a table |
|
169
|
|
|
$tableToUpdaterList = []; |
|
170
|
|
|
foreach ($listOfAllTables as $table) { |
|
171
|
|
|
foreach ($rowUpdaterInstances as $updater) { |
|
172
|
|
|
if ($updater->hasPotentialUpdateForTable($table)) { |
|
173
|
|
|
if (!is_array($tableToUpdaterList[$table])) { |
|
174
|
|
|
$tableToUpdaterList[$table] = []; |
|
175
|
|
|
} |
|
176
|
|
|
$tableToUpdaterList[$table][] = $updater; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// Iterate through all rows of all tables that have potential row updaters attached, |
|
182
|
|
|
// feed each single row to each updater and finally update each row in database if |
|
183
|
|
|
// a row updater changed a fields |
|
184
|
|
|
foreach ($tableToUpdaterList as $table => $updaters) { |
|
185
|
|
|
/** @var RowUpdaterInterface[] $updaters */ |
|
186
|
|
|
$connectionForTable = $connectionPool->getConnectionForTable($table); |
|
187
|
|
|
$queryBuilder = $connectionPool->getQueryBuilderForTable($table); |
|
188
|
|
|
$queryBuilder->getRestrictions()->removeAll(); |
|
189
|
|
|
$queryBuilder->select('*') |
|
190
|
|
|
->from($table) |
|
191
|
|
|
->orderBy('uid'); |
|
192
|
|
|
if ($table === $startPosition['table']) { |
|
193
|
|
|
$queryBuilder->where( |
|
194
|
|
|
$queryBuilder->expr()->gt('uid', $queryBuilder->createNamedParameter($startPosition['uid'])) |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
$statement = $queryBuilder->execute(); |
|
198
|
|
|
$rowCountWithoutUpdate = 0; |
|
199
|
|
|
while ($row = $rowBefore = $statement->fetch()) { |
|
200
|
|
|
foreach ($updaters as $updater) { |
|
201
|
|
|
$row = $updater->updateTableRow($table, $row); |
|
202
|
|
|
} |
|
203
|
|
|
$updatedFields = array_diff_assoc($row, $rowBefore); |
|
204
|
|
|
if (empty($updatedFields)) { |
|
205
|
|
|
// Updaters changed no field of that row |
|
206
|
|
|
$rowCountWithoutUpdate++; |
|
207
|
|
|
if ($rowCountWithoutUpdate >= 200) { |
|
208
|
|
|
// Update startPosition if there were many rows without data change |
|
209
|
|
|
$startPosition = [ |
|
210
|
|
|
'table' => $table, |
|
211
|
|
|
'uid' => $row['uid'], |
|
212
|
|
|
]; |
|
213
|
|
|
$registry->set('installUpdateRows', 'rowUpdatePosition', $startPosition); |
|
214
|
|
|
$rowCountWithoutUpdate = 0; |
|
215
|
|
|
} |
|
216
|
|
|
} else { |
|
217
|
|
|
$rowCountWithoutUpdate = 0; |
|
218
|
|
|
$startPosition = [ |
|
219
|
|
|
'table' => $table, |
|
220
|
|
|
'uid' => $rowBefore['uid'], |
|
221
|
|
|
]; |
|
222
|
|
|
if ($connectionForSysRegistry === $connectionForTable) { |
|
223
|
|
|
// Target table and sys_registry table are on the same connection, use a transaction |
|
224
|
|
|
$connectionForTable->beginTransaction(); |
|
225
|
|
|
try { |
|
226
|
|
|
$this->updateOrDeleteRow( |
|
227
|
|
|
$connectionForTable, |
|
228
|
|
|
$connectionForTable, |
|
229
|
|
|
$table, |
|
230
|
|
|
(int)$rowBefore['uid'], |
|
231
|
|
|
$updatedFields, |
|
232
|
|
|
$startPosition |
|
233
|
|
|
); |
|
234
|
|
|
$connectionForTable->commit(); |
|
235
|
|
|
} catch (\Exception $up) { |
|
236
|
|
|
$connectionForTable->rollBack(); |
|
237
|
|
|
throw $up; |
|
238
|
|
|
} |
|
239
|
|
|
} else { |
|
240
|
|
|
// Different connections for table and sys_registry -> execute two |
|
241
|
|
|
// distinct queries and hope for the best. |
|
242
|
|
|
$this->updateOrDeleteRow( |
|
243
|
|
|
$connectionForTable, |
|
244
|
|
|
$connectionForSysRegistry, |
|
245
|
|
|
$table, |
|
246
|
|
|
(int)$rowBefore['uid'], |
|
247
|
|
|
$updatedFields, |
|
248
|
|
|
$startPosition |
|
249
|
|
|
); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
// Ready with updates, remove position information from sys_registry |
|
256
|
|
|
$registry->remove('installUpdateRows', 'rowUpdatePosition'); |
|
257
|
|
|
// Mark row updaters that were executed as done |
|
258
|
|
|
foreach ($rowUpdaterInstances as $updater) { |
|
259
|
|
|
$this->setRowUpdaterExecuted($updater); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return true; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Return an array of class names that are not yet marked as done. |
|
267
|
|
|
* |
|
268
|
|
|
* @return array Class names |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function getRowUpdatersToExecute(): array |
|
271
|
|
|
{ |
|
272
|
|
|
$doneRowUpdater = GeneralUtility::makeInstance(Registry::class)->get('installUpdateRows', 'rowUpdatersDone', []); |
|
273
|
|
|
return array_diff($this->rowUpdater, $doneRowUpdater); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Mark a single updater as done |
|
278
|
|
|
* |
|
279
|
|
|
* @param RowUpdaterInterface $updater |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function setRowUpdaterExecuted(RowUpdaterInterface $updater) |
|
282
|
|
|
{ |
|
283
|
|
|
$registry = GeneralUtility::makeInstance(Registry::class); |
|
284
|
|
|
$doneRowUpdater = $registry->get('installUpdateRows', 'rowUpdatersDone', []); |
|
285
|
|
|
$doneRowUpdater[] = get_class($updater); |
|
286
|
|
|
$registry->set('installUpdateRows', 'rowUpdatersDone', $doneRowUpdater); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Return an array with table / uid combination that specifies the start position the |
|
291
|
|
|
* update row process should start with. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $firstTable Table name of the first TCA in case the start position needs to be initialized |
|
294
|
|
|
* @return array New start position |
|
295
|
|
|
*/ |
|
296
|
|
|
protected function getStartPosition(string $firstTable): array |
|
297
|
|
|
{ |
|
298
|
|
|
$registry = GeneralUtility::makeInstance(Registry::class); |
|
299
|
|
|
$startPosition = $registry->get('installUpdateRows', 'rowUpdatePosition', []); |
|
300
|
|
|
if (empty($startPosition)) { |
|
301
|
|
|
$startPosition = [ |
|
302
|
|
|
'table' => $firstTable, |
|
303
|
|
|
'uid' => 0, |
|
304
|
|
|
]; |
|
305
|
|
|
$registry->set('installUpdateRows', 'rowUpdatePosition', $startPosition); |
|
306
|
|
|
} |
|
307
|
|
|
return $startPosition; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param Connection $connectionForTable |
|
312
|
|
|
* @param string $table |
|
313
|
|
|
* @param array $updatedFields |
|
314
|
|
|
* @param int $uid |
|
315
|
|
|
* @param Connection $connectionForSysRegistry |
|
316
|
|
|
* @param array $startPosition |
|
317
|
|
|
*/ |
|
318
|
|
|
protected function updateOrDeleteRow(Connection $connectionForTable, Connection $connectionForSysRegistry, string $table, int $uid, array $updatedFields, array $startPosition): void |
|
319
|
|
|
{ |
|
320
|
|
|
$deleteField = $GLOBALS['TCA'][$table]['ctrl']['delete'] ?? null; |
|
321
|
|
|
if ($deleteField === null && $updatedFields['deleted'] === 1) { |
|
322
|
|
|
$connectionForTable->delete( |
|
323
|
|
|
$table, |
|
324
|
|
|
[ |
|
325
|
|
|
'uid' => $uid, |
|
326
|
|
|
] |
|
327
|
|
|
); |
|
328
|
|
|
} else { |
|
329
|
|
|
$connectionForTable->update( |
|
330
|
|
|
$table, |
|
331
|
|
|
$updatedFields, |
|
332
|
|
|
[ |
|
333
|
|
|
'uid' => $uid, |
|
334
|
|
|
] |
|
335
|
|
|
); |
|
336
|
|
|
} |
|
337
|
|
|
$connectionForSysRegistry->update( |
|
338
|
|
|
'sys_registry', |
|
339
|
|
|
[ |
|
340
|
|
|
'entry_value' => serialize($startPosition), |
|
341
|
|
|
], |
|
342
|
|
|
[ |
|
343
|
|
|
'entry_namespace' => 'installUpdateRows', |
|
344
|
|
|
'entry_key' => 'rowUpdatePosition', |
|
345
|
|
|
] |
|
346
|
|
|
); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|