TYPO3 CMS  TYPO3_7-6
MissingRelationsCommand.php
Go to the documentation of this file.
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 
21 
26 {
30  public $checkRefIndex = true;
31 
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 
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  // Select DB relations from reference table
94  $db = $GLOBALS['TYPO3_DB'];
95  $result = $db->exec_SELECTquery('ref_uid,ref_table,softref_key,hash,tablename,recuid,field,flexpointer,deleted', 'sys_refindex', 'ref_table <> ' . $db->fullQuoteStr('_FILE', 'sys_refindex') . ' AND ref_uid > 0', '', 'sorting DESC');
96  if ($result) {
97  $tempExists = [];
98  while ($rec = $db->sql_fetch_assoc($result)) {
99  $suffix = $rec['softref_key'] != '' ? '_s' : '_m';
100  $idx = $rec['ref_table'] . ':' . $rec['ref_uid'];
101  // Get referenced record:
102  if (!isset($tempExists[$idx])) {
103  $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'] : ''));
104  }
105  // Compile info string for location of reference:
106  $infoString = $this->infoStr($rec);
107  // Handle missing file:
108  if ($tempExists[$idx]['uid']) {
109  if ($tempExists[$idx]['pid'] == -1) {
110  $resultArray['offlineVersionRecords' . $suffix][$idx][$rec['hash']] = $infoString;
111  ksort($resultArray['offlineVersionRecords' . $suffix][$idx]);
112  } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
113  $resultArray['deletedRecords' . $suffix][$idx][$rec['hash']] = $infoString;
114  ksort($resultArray['deletedRecords' . $suffix][$idx]);
115  }
116  } else {
117  $resultArray['nonExistingRecords' . $suffix][$idx][$rec['hash']] = $infoString;
118  ksort($resultArray['nonExistingRecords' . $suffix][$idx]);
119  }
120  }
121  $db->sql_free_result($result);
122  }
123  ksort($resultArray['offlineVersionRecords_m']);
124  ksort($resultArray['deletedRecords_m']);
125  ksort($resultArray['nonExistingRecords_m']);
126  ksort($resultArray['offlineVersionRecords_s']);
127  ksort($resultArray['deletedRecords_s']);
128  ksort($resultArray['nonExistingRecords_s']);
129  return $resultArray;
130  }
131 
139  public function main_autoFix($resultArray)
140  {
141  $trav = ['offlineVersionRecords_m', 'nonExistingRecords_m'];
142  foreach ($trav as $tk) {
143  echo 'Processing managed "' . $tk . '"...' . LF;
144  foreach ($resultArray[$tk] as $key => $value) {
145  foreach ($value as $hash => $recReference) {
146  echo ' Removing reference to ' . $key . ' in record "' . $recReference . '": ';
147  if ($bypass = $this->cli_noExecutionCheck($recReference)) {
148  echo $bypass;
149  } else {
150  $sysRefObj = GeneralUtility::makeInstance(ReferenceIndex::class);
151  $error = $sysRefObj->setReferenceValue($hash, null);
152  if ($error) {
153  echo ' TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
154  } else {
155  echo 'DONE';
156  }
157  }
158  echo LF;
159  }
160  }
161  }
162  }
163 }
static getRecordRaw($table, $where='', $fields=' *')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']