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