2 namespace TYPO3\CMS\Lowlevel;
32 parent::__construct();
34 $this->
cli_help[
'name'] =
'missing_relations -- Find all record references pointing to a non-existing record.';
35 $this->
cli_help[
'description'] = trim(
'
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.
44 Automatic Repair of Errors:
45 - Only managed references are repaired (TCA-configured).
46 - Offline Version Records and Non Existing Records: Reference is removed
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.
52 NOTICE: Uses the Reference Index Table (sys_refindex) for analysis. Update it before use!
54 $this->
cli_help[
'examples'] =
'/.../cli_dispatch.phpsh lowlevel_cleaner missing_relations -s -r
55 Reports missing relations';
68 $listExplain =
' Shows the missing record as header and underneath a list of record fields in which the references are found. ' .
$this->label_infoString;
70 'message' => $this->
cli_help[
'name'] . LF . LF . $this->
cli_help[
'description'],
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),
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)
80 'offlineVersionRecords_m' => array(),
81 'deletedRecords_m' => array(),
82 'nonExistingRecords_m' => array(),
83 'offlineVersionRecords_s' => array(),
84 'deletedRecords_s' => array(),
85 'nonExistingRecords_s' => array()
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');
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'];
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'] :
''));
100 $infoString = $this->
infoStr($rec);
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]);
111 $resultArray[
'nonExistingRecords' . $suffix][$idx][$rec[
'hash']] = $infoString;
112 ksort($resultArray[
'nonExistingRecords' . $suffix][$idx]);
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']);
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 .
'": ';
143 $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
144 $error = $sysRefObj->setReferenceValue($hash, null);
146 echo
' TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;