12540fc541cf00e4ab418ab2e519016ff66cf73f
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / class.missing_relations.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Cleaner module: Missing relations
29 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
30 *
31 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
32 */
33 /**
34 * [CLASS/FUNCTION INDEX of SCRIPT]
35 *
36 *
37 *
38 * 56: class tx_lowlevel_missing_relations extends tx_lowlevel_cleaner_core
39 * 65: function tx_lowlevel_missing_relations()
40 * 101: function main()
41 * 173: function main_autoFix($resultArray)
42 *
43 * TOTAL FUNCTIONS: 3
44 * (This index is automatically created/updated by the extension "extdeveval")
45 *
46 */
47
48
49 /**
50 * Looking for missing relations.
51 *
52 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
53 * @package TYPO3
54 * @subpackage tx_lowlevel
55 */
56 class tx_lowlevel_missing_relations extends tx_lowlevel_cleaner_core {
57
58 var $checkRefIndex = TRUE;
59
60 /**
61 * Constructor
62 *
63 * @return void
64 */
65 function tx_lowlevel_missing_relations() {
66 parent::tx_lowlevel_cleaner_core();
67
68 // Setting up help:
69 $this->cli_help['name'] = 'missing_relations -- Find all record references pointing to a non-existing record.';
70 $this->cli_help['description'] = trim('
71 Assumptions:
72 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
73 - all database references to check are integers greater than zero
74 - 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
75 Records may be missing for these reasons (except software bugs):
76 - someone deleted the record which is technically not an error although it might be a mistake that someone did so.
77 - after flushing published versions and/or deleted-flagged records a number of new missing references might appear; those were pointing to records just flushed.
78
79 Automatic Repair of Errors:
80 - Only managed references are repaired (TCA-configured).
81 - Offline Version Records and Non Existing Records: Reference is removed
82
83 Manual repair suggestions:
84 - For soft references you should investigate each case and edit the content accordingly.
85 - 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.
86
87 NOTICE: Uses the Reference Index Table (sys_refindex) for analysis. Update it before use!
88 ');
89
90 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_relations -s -r
91 Reports missing relations';
92 }
93
94
95 /**
96 * Find relations pointing to non-existing records
97 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it) [Only for managed relations!]
98 *
99 * @return array
100 */
101 function main() {
102 global $TYPO3_DB;
103
104 // Initialize result array:
105 $listExplain = ' Shows the missing record as header and underneath a list of record fields in which the references are found. '.$this->label_infoString;
106 $resultArray = array(
107 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
108 'headers' => array(
109 '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),
110 '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),
111 '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), // 3 = error
112 'offlineVersionRecords_s' => array('Offline version records (softref)','See above.'.$listExplain,2),
113 'deletedRecords_s' => array('Deleted-flagged records (softref)','See above.'.$listExplain,2),
114 'nonExistingRecords_s' => array('Non-existing records to which there are references (softref)','See above.'.$listExplain,2),
115 ),
116 'offlineVersionRecords_m' => array(),
117 'deletedRecords_m' => array(),
118 'nonExistingRecords_m' => array(),
119 'offlineVersionRecords_s' => array(),
120 'deletedRecords_s' => array(),
121 'nonExistingRecords_s' => array(),
122 );
123
124 // Select DB relations from reference table
125 $recs = $TYPO3_DB->exec_SELECTgetRows(
126 '*',
127 'sys_refindex',
128 'ref_table!='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'). // Assuming that any other key will be a table name!
129 ' AND ref_uid>0'.
130 $filterClause,
131 '',
132 'sorting DESC'
133 );
134
135 // Traverse the records
136 $tempExists = array();
137 if (is_array($recs)) {
138 foreach($recs as $rec) {
139 $suffix = $rec['softref_key']!='' ? '_s' : '_m';
140 $idx = $rec['ref_table'].':'.$rec['ref_uid'];
141
142 // Get referenced record:
143 if (!isset($tempExists[$idx])) {
144 $tempExists[$idx] = t3lib_BEfunc::getRecordRaw($rec['ref_table'],'uid='.intval($rec['ref_uid']),'uid,pid'.($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] ? ','.$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] : ''));
145 }
146
147 // Compile info string for location of reference:
148 $infoString = $this->infoStr($rec);
149
150 // Handle missing file:
151 if ($tempExists[$idx]['uid']) {
152 if ($tempExists[$idx]['pid']==-1) {
153 $resultArray['offlineVersionRecords'.$suffix][$idx][$rec['hash']] = $infoString;
154 ksort($resultArray['offlineVersionRecords'.$suffix][$idx]);
155 } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
156 $resultArray['deletedRecords'.$suffix][$idx][$rec['hash']] = $infoString;
157 ksort($resultArray['deletedRecords'.$suffix][$idx]);
158 }
159 } else {
160 $resultArray['nonExistingRecords'.$suffix][$idx][$rec['hash']] = $infoString;
161 ksort($resultArray['nonExistingRecords'.$suffix][$idx]);
162 }
163 }
164 }
165
166 ksort($resultArray['offlineVersionRecords_m']);
167 ksort($resultArray['deletedRecords_m']);
168 ksort($resultArray['nonExistingRecords_m']);
169 ksort($resultArray['offlineVersionRecords_s']);
170 ksort($resultArray['deletedRecords_s']);
171 ksort($resultArray['nonExistingRecords_s']);
172
173 return $resultArray;
174 }
175
176 /**
177 * Mandatory autofix function
178 * Will run auto-fix on the result array. Echos status during processing.
179 *
180 * @param array Result array from main() function
181 * @return void
182 */
183 function main_autoFix($resultArray) {
184
185 $trav = array('offlineVersionRecords_m', 'nonExistingRecords_m');
186 foreach($trav as $tk) {
187 echo 'Processing managed "'.$tk.'"...'.LF;
188 foreach($resultArray[$tk] as $key => $value) {
189 foreach($value as $hash => $recReference) {
190 echo ' Removing reference to '.$key.' in record "'.$recReference.'": ';
191 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
192 echo $bypass;
193 } else {
194 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
195 $error = $sysRefObj->setReferenceValue($hash,NULL);
196 if ($error) {
197 echo ' t3lib_refindex::setReferenceValue(): '.$error.LF;
198 } else echo 'DONE';
199 }
200 echo LF;
201 }
202 }
203 }
204 }
205 }
206
207 ?>