2 declare(strict_types = 1);
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;
47 ->setDescription(
'Find all record references pointing to a non-existing record')
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.
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.
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.
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.
73 If you want to get more detailed information, use the --verbose option.')
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'
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'
102 $io =
new SymfonyStyle($input,
$output);
103 $io->title($this->getDescription());
105 $dryRun = $input->hasOption(
'dry-run') && $input->getOption(
'dry-run') !=
false ? true :
false;
113 if ($io->isVerbose() && count($results[
'nonExistingRecordsInSoftReferenceRelations'])) {
115 'Found ' . count($results[
'nonExistingRecordsInSoftReferenceRelations']) .
' non-existing records that are still being soft-referenced in the following locations.',
116 'These relations cannot be removed automatically and need manual repair.'
118 $io->listing($results[
'nonExistingRecordsInSoftReferenceRelations']);
123 if ($io->isVerbose() && count($results[
'offlineVersionRecordsInSoftReferenceRelations'])) {
125 'Found ' . count($results[
'offlineVersionRecordsInSoftReferenceRelations']) .
' soft-references pointing to offline versions, which should never be referenced directly.',
126 'These relations cannot be removed automatically and need manual repair.'
128 $io->listing($results[
'offlineVersionRecordsInSoftReferenceRelations']);
136 if ($io->isVerbose() && count($results[
'deletedRecords'])) {
138 'Found ' . count($results[
'deletedRecords']) .
' references pointing to deleted records.',
139 'Keeping the references is useful if you undelete the referenced records later, otherwise the references' .
140 'are lost completely when the deleted records are flushed at some point. Notice that if those records listed' .
141 'are themselves deleted (marked with "DELETED") it is not a problem.',
143 $io->listing($results[
'deletedRecords']);
147 if ($io->isVerbose() && count($results[
'deletedRecordsInSoftReferenceRelations'])) {
149 'Found ' . count($results[
'deletedRecordsInSoftReferenceRelations']) .
' soft references pointing to deleted records.',
150 'Keeping the references is useful if you undelete the referenced records later, otherwise the references' .
151 'are lost completely when the deleted records are flushed at some point. Notice that if those records listed' .
152 'are themselves deleted (marked with "DELETED") it is not a problem.',
154 $io->listing($results[
'deletedRecordsInSoftReferenceRelations']);
158 if (count($results[
'offlineVersionRecords']) || count($results[
'nonExistingRecords'])) {
160 'Found ' . count($results[
'nonExistingRecords']) .
' references to non-existing records ' .
161 'and ' . count($results[
'offlineVersionRecords']) .
' references directly linked to offline versions.'
165 $results[
'offlineVersionRecords'],
166 $results[
'nonExistingRecords'],
170 $io->success(
'All references were updated accordingly.');
172 $io->success(
'Nothing to do, no missing relations found. Everything is in place.');
188 $io->note(
'Finding missing records referenced by TYPO3 requires a clean reference index (sys_refindex)');
189 if ($input->hasOption(
'update-refindex') && $input->getOption(
'update-refindex')) {
190 $updateReferenceIndex =
true;
191 } elseif ($input->isInteractive()) {
192 $updateReferenceIndex = $io->confirm(
'Should the reference index be updated right now?',
false);
194 $updateReferenceIndex =
false;
198 if ($updateReferenceIndex) {
199 $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
200 $referenceIndex->updateIndex(
false, !$io->isQuiet());
202 $io->writeln(
'Reference index is assumed to be up to date, continuing.');
213 $deletedRecords = [];
214 $deletedRecordsInSoftReferenceRelations = [];
215 $nonExistingRecords = [];
216 $nonExistingRecordsInSoftReferenceRelations = [];
217 $offlineVersionRecords = [];
218 $offlineVersionRecordsInSoftReferenceRelations = [];
221 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'sys_refindex');
222 $rowIterator = $queryBuilder
223 ->select(
'ref_uid',
'ref_table',
'softref_key',
'hash',
'tablename',
'recuid',
'field',
'flexpointer',
'deleted')
224 ->from(
'sys_refindex')
226 $queryBuilder->expr()->neq(
'ref_table', $queryBuilder->createNamedParameter(
'_FILE', \PDO::PARAM_STR)),
227 $queryBuilder->expr()->gt(
'ref_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
231 $existingRecords = [];
232 while ($rec = $rowIterator->fetch()) {
233 $isSoftReference = !empty($rec[
'softref_key']);
234 $idx = $rec[
'ref_table'] .
':' . $rec[
'ref_uid'];
236 if (!isset($existingRecords[$idx])) {
237 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
238 ->getQueryBuilderForTable($rec[
'ref_table']);
239 $queryBuilder->getRestrictions()->removeAll();
241 $selectFields = [
'uid',
'pid'];
242 if (isset(
$GLOBALS[
'TCA'][$rec[
'ref_table']][
'ctrl'][
'delete'])) {
243 $selectFields[] =
$GLOBALS[
'TCA'][$rec[
'ref_table']][
'ctrl'][
'delete'];
246 $existingRecords[$idx] = $queryBuilder
247 ->select(...$selectFields)
248 ->from($rec[
'ref_table'])
250 $queryBuilder->expr()->eq(
252 $queryBuilder->createNamedParameter($rec[
'ref_uid'], \PDO::PARAM_INT)
261 if ($existingRecords[$idx][
'uid']) {
263 if ((
int)$existingRecords[$idx][
'pid'] === -1) {
264 if ($isSoftReference) {
265 $offlineVersionRecordsInSoftReferenceRelations[] = $infoString;
267 $offlineVersionRecords[$idx][$rec[
'hash']] = $infoString;
270 } elseif (isset(
$GLOBALS[
'TCA'][$rec[
'ref_table']][
'ctrl'][
'delete']) && $existingRecords[$idx][
$GLOBALS[
'TCA'][$rec[
'ref_table']][
'ctrl'][
'delete']]) {
271 if ($isSoftReference) {
272 $deletedRecordsInSoftReferenceRelations[] = $infoString;
274 $deletedRecords[] = $infoString;
278 if ($isSoftReference) {
279 $nonExistingRecordsInSoftReferenceRelations[] = $infoString;
281 $nonExistingRecords[$idx][$rec[
'hash']] = $infoString;
317 array $offlineVersionRecords,
318 array $nonExistingRecords,
323 foreach ($offlineVersionRecords as $fileName => $references) {
324 if ($io->isVeryVerbose()) {
325 $io->writeln(
'Removing references in offline versions which there are references pointing towards.');
327 foreach ($references as $hash => $recordReference) {
328 $io->writeln(
'Removing reference in record "' . $recordReference .
'" (Hash: ' . $hash .
')');
330 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
331 $error = $sysRefObj->setReferenceValue($hash,
null);
333 $io->error(
'ReferenceIndex::setReferenceValue() reported "' . $error .
'"');
340 foreach ($nonExistingRecords as $fileName => $references) {
341 if ($io->isVeryVerbose()) {
342 $io->writeln(
'Removing references to non-existing records.');
344 foreach ($references as $hash => $recordReference) {
345 $io->writeln(
'Removing reference in record "' . $recordReference .
'" (Hash: ' . $hash .
')');
347 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
348 $error = $sysRefObj->setReferenceValue($hash,
null);
350 $io->error(
'ReferenceIndex::setReferenceValue() reported "' . $error .
'"');
365 return $record[
'tablename']
366 .
':' . $record[
'recuid']
367 .
':' . $record[
'field']
368 . ($record[
'flexpointer'] ?
':' . $record[
'flexpointer'] :
'')
369 . ($record[
'softref_key'] ?
':' . $record[
'softref_key'] .
' (Soft Reference) ' :
'')
370 . ($record[
'deleted'] ?
' (DELETED)' :
'');