893d6aafbeb6c841e147fb15603271ec291dbe97
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / clmods / class.missing_relations.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (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'].chr(10).chr(10).$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 } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) {
155 $resultArray['deletedRecords'.$suffix][$idx][$rec['hash']] = $infoString;
156 }
157 } else {
158 $resultArray['nonExistingRecords'.$suffix][$idx][$rec['hash']] = $infoString;
159 }
160 }
161 }
162
163 return $resultArray;
164 }
165
166 /**
167 * Mandatory autofix function
168 * Will run auto-fix on the result array. Echos status during processing.
169 *
170 * @param array Result array from main() function
171 * @return void
172 */
173 function main_autoFix($resultArray) {
174
175 $trav = array('offlineVersionRecords_m', 'nonExistingRecords_m');
176 foreach($trav as $tk) {
177 echo 'Processing managed "'.$tk.'"...'.chr(10);
178 foreach($resultArray[$tk] as $key => $value) {
179 foreach($value as $hash => $recReference) {
180 echo ' Removing reference to '.$key.' in record "'.$recReference.'": ';
181 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
182 echo $bypass;
183 } else {
184 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
185 $error = $sysRefObj->setReferenceValue($hash,NULL);
186 if ($error) {
187 echo ' t3lib_refindex::setReferenceValue(): '.$error.chr(10);
188 } else echo 'DONE';
189 }
190 echo chr(10);
191 }
192 }
193 }
194 }
195 }
196
197 ?>