[TASK] Use a cast to float instead of doubleval
[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\ReferenceIndex;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Looking for missing relations.
24 */
25 class MissingRelationsCommand extends CleanerCommand
26 {
27 /**
28 * @var bool
29 */
30 public $checkRefIndex = true;
31
32 /**
33 * Constructor
34 */
35 public function __construct()
36 {
37 parent::__construct();
38 // Setting up help:
39 $this->cli_help['name'] = 'missing_relations -- Find all record references pointing to a non-existing record.';
40 $this->cli_help['description'] = trim('
41 Assumptions:
42 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
43 - all database references to check are integers greater than zero
44 - 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
45 Records may be missing for these reasons (except software bugs):
46 - someone deleted the record which is technically not an error although it might be a mistake that someone did so.
47 - after flushing published versions and/or deleted-flagged records a number of new missing references might appear; those were pointing to records just flushed.
48
49 Automatic Repair of Errors:
50 - Only managed references are repaired (TCA-configured).
51 - Offline Version Records and Non Existing Records: Reference is removed
52
53 Manual repair suggestions:
54 - For soft references you should investigate each case and edit the content accordingly.
55 - 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.
56
57 NOTICE: Uses the Reference Index Table (sys_refindex) for analysis. Update it before use!
58 ');
59 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_relations -s -r
60 Reports missing relations';
61 }
62
63 /**
64 * Find relations pointing to non-existing records
65 * Fix methods: API in \TYPO3\CMS\Core\Database\ReferenceIndex that allows to
66 * change the value of a reference (or remove it) [Only for managed relations!]
67 *
68 * @return array
69 */
70 public function main()
71 {
72 // Initialize result array:
73 $listExplain = ' Shows the missing record as header and underneath a list of record fields in which the references are found. ' . $this->label_infoString;
74 $resultArray = [
75 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
76 'headers' => [
77 'offlineVersionRecords_m' => ['Offline version records (managed)', 'These records are offline versions having a pid=-1 and references should never occur directly to their uids.' . $listExplain, 3],
78 'deletedRecords_m' => ['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],
79 'nonExistingRecords_m' => ['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],
80 // 3 = error
81 'offlineVersionRecords_s' => ['Offline version records (softref)', 'See above.' . $listExplain, 2],
82 'deletedRecords_s' => ['Deleted-flagged records (softref)', 'See above.' . $listExplain, 2],
83 'nonExistingRecords_s' => ['Non-existing records to which there are references (softref)', 'See above.' . $listExplain, 2]
84 ],
85 'offlineVersionRecords_m' => [],
86 'deletedRecords_m' => [],
87 'nonExistingRecords_m' => [],
88 'offlineVersionRecords_s' => [],
89 'deletedRecords_s' => [],
90 'nonExistingRecords_s' => []
91 ];
92
93 // Select DB relations from reference table
94 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
95 $rowIterator = $queryBuilder
96 ->select('ref_uid', 'ref_table', 'softref_key', 'hash', 'tablename', 'recuid', 'field', 'flexpointer', 'deleted')
97 ->from('sys_refindex')
98 ->where(
99 $queryBuilder->expr()->neq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
100 $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
101 )
102 ->orderBy('sorting', 'DESC')
103 ->execute();
104
105 $tempExists = [];
106 while ($rec = $rowIterator->fetch()) {
107 $suffix = $rec['softref_key'] != '' ? '_s' : '_m';
108 $idx = $rec['ref_table'] . ':' . $rec['ref_uid'];
109 // Get referenced record:
110 if (!isset($tempExists[$idx])) {
111 $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'] : ''));
112 }
113 // Compile info string for location of reference:
114 $infoString = $this->infoStr($rec);
115 // Handle missing file:
116 if ($tempExists[$idx]['uid']) {
117 if ($tempExists[$idx]['pid'] == -1) {
118 $resultArray['offlineVersionRecords' . $suffix][$idx][$rec['hash']] = $infoString;
119 ksort($resultArray['offlineVersionRecords' . $suffix][$idx]);
120 } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
121 $resultArray['deletedRecords' . $suffix][$idx][$rec['hash']] = $infoString;
122 ksort($resultArray['deletedRecords' . $suffix][$idx]);
123 }
124 } else {
125 $resultArray['nonExistingRecords' . $suffix][$idx][$rec['hash']] = $infoString;
126 ksort($resultArray['nonExistingRecords' . $suffix][$idx]);
127 }
128 }
129
130 ksort($resultArray['offlineVersionRecords_m']);
131 ksort($resultArray['deletedRecords_m']);
132 ksort($resultArray['nonExistingRecords_m']);
133 ksort($resultArray['offlineVersionRecords_s']);
134 ksort($resultArray['deletedRecords_s']);
135 ksort($resultArray['nonExistingRecords_s']);
136 return $resultArray;
137 }
138
139 /**
140 * Mandatory autofix function
141 * Will run auto-fix on the result array. Echos status during processing.
142 *
143 * @param array $resultArray Result array from main() function
144 * @return void
145 */
146 public function main_autoFix($resultArray)
147 {
148 $trav = ['offlineVersionRecords_m', 'nonExistingRecords_m'];
149 foreach ($trav as $tk) {
150 echo 'Processing managed "' . $tk . '"...' . LF;
151 foreach ($resultArray[$tk] as $key => $value) {
152 foreach ($value as $hash => $recReference) {
153 echo ' Removing reference to ' . $key . ' in record "' . $recReference . '": ';
154 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
155 echo $bypass;
156 } else {
157 $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
158 $error = $sysRefObj->setReferenceValue($hash, null);
159 if ($error) {
160 echo ' TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
161 } else {
162 echo 'DONE';
163 }
164 }
165 echo LF;
166 }
167 }
168 }
169 }
170 }