[!!!][TASK] Migrate Lowlevel "missing_relations" command to Symfony Console
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / Command / MissingRelationsCommand.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Lowlevel\Command;
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 use Symfony\Component\Console\Command\Command;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Input\InputOption;
21 use Symfony\Component\Console\Output\OutputInterface;
22 use Symfony\Component\Console\Style\SymfonyStyle;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
24 use TYPO3\CMS\Core\Database\ConnectionPool;
25 use TYPO3\CMS\Core\Database\ReferenceIndex;
26 use TYPO3\CMS\Core\Utility\ArrayUtility;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Finds references and soft-references to
31 * - records which are marked as deleted (e.g. still in the system as reminder)
32 * - offline versions (references should never point to offline versions)
33 * - non-existing records (records which have been deleted not via DataHandler)
34 *
35 * The later (non-soft-reference variants) can be automatically fixed by simply removing
36 * the references from the refindex.
37 */
38 class MissingRelationsCommand extends Command
39 {
40
41 /**
42 * Configure the command by defining the name, options and arguments
43 */
44 public function configure()
45 {
46 $this
47 ->setDescription('Find all record references pointing to a non-existing record')
48 ->setHelp('
49 Assumptions:
50 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
51 - all database references to check are integers greater than zero
52 - does not check if a referenced record is inside an offline branch, another workspace etc. which could make the reference useless in reality or otherwise question integrity
53 Records may be missing for these reasons (except software bugs):
54 - someone deleted the record which is technically not an error although it might be a mistake that someone did so.
55 - after flushing published versions and/or deleted-flagged records a number of new missing references might appear; those were pointing to records just flushed.
56
57 An automatic repair is only possible for managed references are (not for soft references), for
58 offline versions records and non-existing records. If you just want to list them, use the --dry-run option.
59 The references in this case are removed.
60
61 If the option "--dry-run" is not set, all managed files (TCA/FlexForm attachments) will silently remove the references
62 to non-existing and offline version records.
63 All soft references with relations to non-existing records, offline versions and deleted records
64 require manual fix if you consider it an error.
65
66 Manual repair suggestions:
67 - For soft references you should investigate each case and edit the content accordingly.
68 - References to deleted records can theoretically be removed since a deleted record cannot be selected and hence
69 your website should not be affected by removal of the reference. On the other hand it does not hurt to ignore it
70 for now. To have this automatically fixed you must first flush the deleted records after which remaining
71 references will appear as pointing to Non Existing Records and can now be removed with the automatic fix.
72
73 If you want to get more detailed information, use the --verbose option.')
74 ->addOption(
75 'dry-run',
76 null,
77 InputOption::VALUE_NONE,
78 'If this option is set, the references will not be removed, but just the output which references would be deleted are shown'
79 )
80 ->addOption(
81 'update-refindex',
82 null,
83 InputOption::VALUE_NONE,
84 'Setting this option automatically updates the reference index and does not ask on command line. Alternatively, use -n to avoid the interactive mode'
85 );
86 }
87
88 /**
89 * Executes the command to
90 * - optionally update the reference index (to have clean data)
91 * - find data in sys_refindex (softrefs and regular references) where the reference points to a non-existing record or offline version
92 * - remove these files if --dry-run is not set (not possible for refindexes)
93 *
94 * @param InputInterface $input
95 * @param OutputInterface $output
96 *
97 * @return void
98 */
99 protected function execute(InputInterface $input, OutputInterface $output)
100 {
101 $io = new SymfonyStyle($input, $output);
102 $io->title($this->getDescription());
103
104 $dryRun = $input->hasOption('dry-run') && $input->getOption('dry-run') != false ? true : false;
105
106 // Update the reference index
107 $this->updateReferenceIndex($input, $io);
108
109 $results = $this->findRelationsToNonExistingRecords();
110
111 // Display soft references to non-existing records
112 if ($io->isVerbose() && count($results['nonExistingRecordsInSoftReferenceRelations'])) {
113 $io->note([
114 'Found ' . count($results['nonExistingRecordsInSoftReferenceRelations']) . ' non-existing records that are still being soft-referenced in the following locations.',
115 'These relations cannot be removed automatically and need manual repair.'
116 ]);
117 $io->listing($results['nonExistingRecordsInSoftReferenceRelations']);
118 }
119
120 // Display soft references to offline version records
121 // These records are offline versions having a pid=-1 and references should never occur directly to their uids.
122 if ($io->isVerbose() && count($results['offlineVersionRecordsInSoftReferenceRelations'])) {
123 $io->note([
124 'Found ' . count($results['offlineVersionRecordsInSoftReferenceRelations']) . ' soft-references pointing to offline versions, which should never be referenced directly.',
125 'These relations cannot be removed automatically and need manual repair.'
126 ]);
127 $io->listing($results['offlineVersionRecordsInSoftReferenceRelations']);
128 }
129
130 // Display references to deleted records
131 // These records are deleted with a flag but references are still pointing at them.
132 // Keeping the references is useful if you undelete the referenced records later, otherwise the references
133 // are lost completely when the deleted records are flushed at some point. Notice that if those records listed
134 // are themselves deleted (marked with "DELETED") it is not a problem.
135 if ($io->isVerbose() && count($results['deletedRecords'])) {
136 $io->note([
137 'Found ' . count($results['deletedRecords']) . ' references pointing to deleted records.',
138 'Keeping the references is useful if you undelete the referenced records later, otherwise the references' .
139 'are lost completely when the deleted records are flushed at some point. Notice that if those records listed' .
140 'are themselves deleted (marked with "DELETED") it is not a problem.',
141 ]);
142 $io->listing($results['deletedRecords']);
143 }
144
145 // soft references which link to deleted records
146 if ($io->isVerbose() && count($results['deletedRecordsInSoftReferenceRelations'])) {
147 $io->note([
148 'Found ' . count($results['deletedRecordsInSoftReferenceRelations']) . ' soft references pointing to deleted records.',
149 'Keeping the references is useful if you undelete the referenced records later, otherwise the references' .
150 'are lost completely when the deleted records are flushed at some point. Notice that if those records listed' .
151 'are themselves deleted (marked with "DELETED") it is not a problem.',
152 ]);
153 $io->listing($results['deletedRecordsInSoftReferenceRelations']);
154 }
155
156 // Find missing references
157 if (count($results['offlineVersionRecords']) || count($results['nonExistingRecords'])) {
158 $io->note([
159 'Found ' . count($results['nonExistingRecords']) . ' references to non-existing records ' .
160 'and ' . count($results['offlineVersionRecords']) . ' references directly linked to offline versions.'
161 ]);
162
163 $this->removeReferencesToMissingRecords(
164 $results['offlineVersionRecords'],
165 $results['nonExistingRecords'],
166 $dryRun, $io);
167 $io->success('All references were updated accordingly.');
168 } else {
169 $io->success('Nothing to do, no missing relations found. Everything is in place.');
170 }
171 }
172
173 /**
174 * Function to update the reference index
175 * - if the option --update-refindex is set, do it
176 * - otherwise, if in interactive mode (not having -n set), ask the user
177 * - otherwise assume everything is fine
178 *
179 * @param InputInterface $input holds information about entered parameters
180 * @param SymfonyStyle $io necessary for outputting information
181 * @return void
182 */
183 protected function updateReferenceIndex(InputInterface $input, SymfonyStyle $io)
184 {
185 // Check for reference index to update
186 $io->note('Finding missing records referenced by TYPO3 requires a clean reference index (sys_refindex)');
187 if ($input->hasOption('update-refindex') && $input->getOption('update-refindex')) {
188 $updateReferenceIndex = true;
189 } elseif ($input->isInteractive()) {
190 $updateReferenceIndex = $io->confirm('Should the reference index be updated right now?', false);
191 } else {
192 $updateReferenceIndex = false;
193 }
194
195 // Update the reference index
196 if ($updateReferenceIndex) {
197 $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
198 $referenceIndex->updateIndex(false, !$io->isQuiet());
199 } else {
200 $io->writeln('Reference index is assumed to be up to date, continuing.');
201 }
202 }
203
204 /**
205 * Find relations pointing to non-existing records (in managed references or soft-references)
206 *
207 * @return array an array of records within sys_refindex
208 */
209 protected function findRelationsToNonExistingRecords(): array
210 {
211 $deletedRecords = [];
212 $deletedRecordsInSoftReferenceRelations = [];
213 $nonExistingRecords = [];
214 $nonExistingRecordsInSoftReferenceRelations = [];
215 $offlineVersionRecords = [];
216 $offlineVersionRecordsInSoftReferenceRelations = [];
217
218 // Select DB relations from reference table
219 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
220 $rowIterator = $queryBuilder
221 ->select('ref_uid', 'ref_table', 'softref_key', 'hash', 'tablename', 'recuid', 'field', 'flexpointer', 'deleted')
222 ->from('sys_refindex')
223 ->where(
224 $queryBuilder->expr()->neq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
225 $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
226 )
227 ->execute();
228
229 $existingRecords = [];
230 while ($rec = $rowIterator->fetch()) {
231 $isSoftReference = !empty($rec['softref_key']);
232 $idx = $rec['ref_table'] . ':' . $rec['ref_uid'];
233 // Get referenced record:
234 if (!isset($existingRecords[$idx])) {
235 $existingRecords[$idx] = BackendUtility::getRecordRaw(
236 $rec['ref_table'],
237 'uid=' . (int)$rec['ref_uid'],
238 'uid,pid' . (isset($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']) ? ',' . $GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] : '')
239 );
240 }
241 // Compile info string for location of reference:
242 $infoString = $this->formatReferenceIndexEntryToString($rec);
243 // Handle missing file:
244 if ($existingRecords[$idx]['uid']) {
245 // Record exists, but is a reference to an offline version
246 if ((int)$existingRecords[$idx]['pid'] === -1) {
247 if ($isSoftReference) {
248 $offlineVersionRecordsInSoftReferenceRelations[] = $infoString;
249 } else {
250 $offlineVersionRecords[$idx][$rec['hash']] = $infoString;
251 }
252 // reference to a deleted record
253 } elseif (isset($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']) && $existingRecords[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
254 if ($isSoftReference) {
255 $deletedRecordsInSoftReferenceRelations[] = $infoString;
256 } else {
257 $deletedRecords[] = $infoString;
258 }
259 }
260 } else {
261 if ($isSoftReference) {
262 $nonExistingRecordsInSoftReferenceRelations[] = $infoString;
263 } else {
264 $nonExistingRecords[$idx][$rec['hash']] = $infoString;
265 }
266 }
267 }
268
269 return [
270 // Non-existing records to which there are references (managed)
271 // These references can safely be removed since there is no record found in the database at all.
272 'nonExistingRecords' => ArrayUtility::sortByKeyRecursive($nonExistingRecords),
273 // Non-existing records to which there are references (softref)
274 'nonExistingRecordsInSoftReferenceRelations' => ArrayUtility::sortByKeyRecursive($nonExistingRecordsInSoftReferenceRelations),
275 // Offline version records (managed)
276 // These records are offline versions having a pid=-1 and references should never occur directly to their uids.
277 'offlineVersionRecords' => ArrayUtility::sortByKeyRecursive($offlineVersionRecords),
278 // Offline version records (softref)
279 'offlineVersionRecordsInSoftReferenceRelations' => ArrayUtility::sortByKeyRecursive($offlineVersionRecordsInSoftReferenceRelations),
280 // Deleted-flagged records (managed)
281 // These records are deleted with a flag but references are still pointing at them.
282 // Keeping the references is useful if you undelete the referenced records later, otherwise the references
283 // are lost completely when the deleted records are flushed at some point. Notice that if those records listed
284 // are themselves deleted (marked with "DELETED") it is not a problem.
285 'deletedRecords' => ArrayUtility::sortByKeyRecursive($deletedRecords),
286 // Deleted-flagged records (softref)
287 'deletedRecordsInSoftReferenceRelations' => ArrayUtility::sortByKeyRecursive($deletedRecordsInSoftReferenceRelations),
288 ];
289 }
290
291 /**
292 * Removes all references to non-existing records or offline versions
293 *
294 * @param array $offlineVersionRecords Contains the records of offline versions of sys_refindex which need to be removed
295 * @param array $nonExistingRecords Contains the records non-existing records of sys_refindex which need to be removed
296 * @param bool $dryRun if set, the references are just displayed, but not removed
297 * @param SymfonyStyle $io the IO object for output
298 * @return void
299 */
300 protected function removeReferencesToMissingRecords(
301 array $offlineVersionRecords,
302 array $nonExistingRecords,
303 bool $dryRun,
304 SymfonyStyle $io
305 ) {
306 // Remove references to offline records
307 foreach ($offlineVersionRecords as $fileName => $references) {
308 if ($io->isVeryVerbose()) {
309 $io->writeln('Removing references in offline versions which there are references pointing towards.');
310 }
311 foreach ($references as $hash => $recordReference) {
312 $io->writeln('Removing reference in record "' . $recordReference . '" (Hash: ' . $hash . ')');
313 if (!$dryRun) {
314 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
315 $error = $sysRefObj->setReferenceValue($hash, null);
316 if ($error) {
317 $io->error('ReferenceIndex::setReferenceValue() reported "' . $error . '"');
318 }
319 }
320 }
321 }
322
323 // Remove references to non-existing records
324 foreach ($nonExistingRecords as $fileName => $references) {
325 if ($io->isVeryVerbose()) {
326 $io->writeln('Removing references to non-existing records.');
327 }
328 foreach ($references as $hash => $recordReference) {
329 $io->writeln('Removing reference in record "' . $recordReference . '" (Hash: ' . $hash . ')');
330 if (!$dryRun) {
331 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
332 $error = $sysRefObj->setReferenceValue($hash, null);
333 if ($error) {
334 $io->error('ReferenceIndex::setReferenceValue() reported "' . $error . '"');
335 }
336 }
337 }
338 }
339 }
340
341 /**
342 * Formats a sys_refindex entry to something readable
343 *
344 * @param array $record
345 * @return string
346 */
347 protected function formatReferenceIndexEntryToString(array $record): string
348 {
349 return $record['tablename']
350 . ':' . $record['recuid']
351 . ':' . $record['field']
352 . ($record['flexpointer'] ? ':' . $record['flexpointer'] : '')
353 . ($record['softref_key'] ? ':' . $record['softref_key'] . ' (Soft Reference) ' : '')
354 . ($record['deleted'] ? ' (DELETED)' : '');
355 }
356 }