[BUGFIX] Missing relations command: optimize SQL
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / MissingRelationsCommand.php
1 <?php
2 namespace TYPO3\CMS\Lowlevel;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
20 use TYPO3\CMS\Core\Database\ReferenceIndex;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Looking for missing relations.
25 */
26 class MissingRelationsCommand extends CleanerCommand
27 {
28 /**
29 * @var bool
30 */
31 public $checkRefIndex = true;
32
33 /**
34 * Constructor
35 */
36 public function __construct()
37 {
38 parent::__construct();
39 // Setting up help:
40 $this->cli_help['name'] = 'missing_relations -- Find all record references pointing to a non-existing record.';
41 $this->cli_help['description'] = trim('
42 Assumptions:
43 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
44 - all database references to check are integers greater than zero
45 - 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
46 Records may be missing for these reasons (except software bugs):
47 - someone deleted the record which is technically not an error although it might be a mistake that someone did so.
48 - after flushing published versions and/or deleted-flagged records a number of new missing references might appear; those were pointing to records just flushed.
49
50 Automatic Repair of Errors:
51 - Only managed references are repaired (TCA-configured).
52 - Offline Version Records and Non Existing Records: Reference is removed
53
54 Manual repair suggestions:
55 - For soft references you should investigate each case and edit the content accordingly.
56 - References to deleted records can theoretically be removed since a deleted record cannot be selected and hence your website should not be affected by removal of the reference. On the other hand it does not hurt to ignore it for now. To have this automatically fixed you must first flush the deleted records after which remaining references will appear as pointing to Non Existing Records and can now be removed with the automatic fix.
57
58 NOTICE: Uses the Reference Index Table (sys_refindex) for analysis. Update it before use!
59 ');
60 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_relations -s -r
61 Reports missing relations';
62 }
63
64 /**
65 * Find relations pointing to non-existing records
66 * Fix methods: API in \TYPO3\CMS\Core\Database\ReferenceIndex that allows to
67 * change the value of a reference (or remove it) [Only for managed relations!]
68 *
69 * @return array
70 */
71 public function main()
72 {
73 // Initialize result array:
74 $listExplain = ' Shows the missing record as header and underneath a list of record fields in which the references are found. ' . $this->label_infoString;
75 $resultArray = array(
76 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
77 'headers' => array(
78 'offlineVersionRecords_m' => array('Offline version records (managed)', 'These records are offline versions having a pid=-1 and references should never occur directly to their uids.' . $listExplain, 3),
79 'deletedRecords_m' => array('Deleted-flagged records (managed)', 'These records are deleted with a flag but references are still pointing at them. Keeping the references is useful if you undelete the referenced records later, otherwise the references are lost completely when the deleted records are flushed at some point. Notice that if those records listed are themselves deleted (marked with "DELETED") it is not a problem.' . $listExplain, 2),
80 'nonExistingRecords_m' => array('Non-existing records to which there are references (managed)', 'These references can safely be removed since there is no record found in the database at all.' . $listExplain, 3),
81 // 3 = error
82 'offlineVersionRecords_s' => array('Offline version records (softref)', 'See above.' . $listExplain, 2),
83 'deletedRecords_s' => array('Deleted-flagged records (softref)', 'See above.' . $listExplain, 2),
84 'nonExistingRecords_s' => array('Non-existing records to which there are references (softref)', 'See above.' . $listExplain, 2)
85 ),
86 'offlineVersionRecords_m' => array(),
87 'deletedRecords_m' => array(),
88 'nonExistingRecords_m' => array(),
89 'offlineVersionRecords_s' => array(),
90 'deletedRecords_s' => array(),
91 'nonExistingRecords_s' => array()
92 );
93
94 // Select DB relations from reference table
95 /** @var QueryBuilder $queryBuilder */
96 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
97 $rowIterator = $queryBuilder
98 ->select('ref_uid', 'ref_table', 'softref_key', 'hash', 'tablename', 'recuid', 'field', 'flexpointer', 'deleted')
99 ->from('sys_refindex')
100 ->where(
101 $queryBuilder->expr()->neq('ref_table', $queryBuilder->quote('_FILE'))
102 )
103 ->andWhere(
104 $queryBuilder->expr()->gt('ref_uid', 0)
105 )
106 ->orderBy('sorting', 'DESC')
107 ->execute();
108
109 $tempExists = array();
110 while ($rec = $rowIterator->fetch()) {
111 $suffix = $rec['softref_key'] != '' ? '_s' : '_m';
112 $idx = $rec['ref_table'] . ':' . $rec['ref_uid'];
113 // Get referenced record:
114 if (!isset($tempExists[$idx])) {
115 $tempExists[$idx] = BackendUtility::getRecordRaw($rec['ref_table'], 'uid=' . (int)$rec['ref_uid'], 'uid,pid' . ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] ? ',' . $GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] : ''));
116 }
117 // Compile info string for location of reference:
118 $infoString = $this->infoStr($rec);
119 // Handle missing file:
120 if ($tempExists[$idx]['uid']) {
121 if ($tempExists[$idx]['pid'] == -1) {
122 $resultArray['offlineVersionRecords' . $suffix][$idx][$rec['hash']] = $infoString;
123 ksort($resultArray['offlineVersionRecords' . $suffix][$idx]);
124 } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
125 $resultArray['deletedRecords' . $suffix][$idx][$rec['hash']] = $infoString;
126 ksort($resultArray['deletedRecords' . $suffix][$idx]);
127 }
128 } else {
129 $resultArray['nonExistingRecords' . $suffix][$idx][$rec['hash']] = $infoString;
130 ksort($resultArray['nonExistingRecords' . $suffix][$idx]);
131 }
132 }
133
134 ksort($resultArray['offlineVersionRecords_m']);
135 ksort($resultArray['deletedRecords_m']);
136 ksort($resultArray['nonExistingRecords_m']);
137 ksort($resultArray['offlineVersionRecords_s']);
138 ksort($resultArray['deletedRecords_s']);
139 ksort($resultArray['nonExistingRecords_s']);
140 return $resultArray;
141 }
142
143 /**
144 * Mandatory autofix function
145 * Will run auto-fix on the result array. Echos status during processing.
146 *
147 * @param array $resultArray Result array from main() function
148 * @return void
149 */
150 public function main_autoFix($resultArray)
151 {
152 $trav = array('offlineVersionRecords_m', 'nonExistingRecords_m');
153 foreach ($trav as $tk) {
154 echo 'Processing managed "' . $tk . '"...' . LF;
155 foreach ($resultArray[$tk] as $key => $value) {
156 foreach ($value as $hash => $recReference) {
157 echo ' Removing reference to ' . $key . ' in record "' . $recReference . '": ';
158 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
159 echo $bypass;
160 } else {
161 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
162 $error = $sysRefObj->setReferenceValue($hash, null);
163 if ($error) {
164 echo ' TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
165 } else {
166 echo 'DONE';
167 }
168 }
169 echo LF;
170 }
171 }
172 }
173 }
174 }