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