|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00027 /** 00028 * Cleaner module: Missing relations 00029 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php 00030 * 00031 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00032 */ 00033 /** 00034 * [CLASS/FUNCTION INDEX of SCRIPT] 00035 * 00036 * 00037 * 00038 * 56: class tx_lowlevel_missing_relations extends tx_lowlevel_cleaner_core 00039 * 65: function tx_lowlevel_missing_relations() 00040 * 101: function main() 00041 * 173: function main_autoFix($resultArray) 00042 * 00043 * TOTAL FUNCTIONS: 3 00044 * (This index is automatically created/updated by the extension "extdeveval") 00045 * 00046 */ 00047 00048 00049 /** 00050 * Looking for missing relations. 00051 * 00052 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00053 * @package TYPO3 00054 * @subpackage tx_lowlevel 00055 */ 00056 class tx_lowlevel_missing_relations extends tx_lowlevel_cleaner_core { 00057 00058 var $checkRefIndex = TRUE; 00059 00060 /** 00061 * Constructor 00062 * 00063 * @return void 00064 */ 00065 function tx_lowlevel_missing_relations() { 00066 parent::tx_lowlevel_cleaner_core(); 00067 00068 // Setting up help: 00069 $this->cli_help['name'] = 'missing_relations -- Find all record references pointing to a non-existing record.'; 00070 $this->cli_help['description'] = trim(' 00071 Assumptions: 00072 - a perfect integrity of the reference index table (always update the reference index table before using this tool!) 00073 - all database references to check are integers greater than zero 00074 - 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 00075 Records may be missing for these reasons (except software bugs): 00076 - someone deleted the record which is technically not an error although it might be a mistake that someone did so. 00077 - after flushing published versions and/or deleted-flagged records a number of new missing references might appear; those were pointing to records just flushed. 00078 00079 Automatic Repair of Errors: 00080 - Only managed references are repaired (TCA-configured). 00081 - Offline Version Records and Non Existing Records: Reference is removed 00082 00083 Manual repair suggestions: 00084 - For soft references you should investigate each case and edit the content accordingly. 00085 - 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. 00086 00087 NOTICE: Uses the Reference Index Table (sys_refindex) for analysis. Update it before use! 00088 '); 00089 00090 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_relations -s -r 00091 Reports missing relations'; 00092 } 00093 00094 00095 /** 00096 * Find relations pointing to non-existing records 00097 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it) [Only for managed relations!] 00098 * 00099 * @return array 00100 */ 00101 function main() { 00102 global $TYPO3_DB; 00103 00104 // Initialize result array: 00105 $listExplain = ' Shows the missing record as header and underneath a list of record fields in which the references are found. '.$this->label_infoString; 00106 $resultArray = array( 00107 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'], 00108 'headers' => array( 00109 '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), 00110 '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), 00111 '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 00112 'offlineVersionRecords_s' => array('Offline version records (softref)','See above.'.$listExplain,2), 00113 'deletedRecords_s' => array('Deleted-flagged records (softref)','See above.'.$listExplain,2), 00114 'nonExistingRecords_s' => array('Non-existing records to which there are references (softref)','See above.'.$listExplain,2), 00115 ), 00116 'offlineVersionRecords_m' => array(), 00117 'deletedRecords_m' => array(), 00118 'nonExistingRecords_m' => array(), 00119 'offlineVersionRecords_s' => array(), 00120 'deletedRecords_s' => array(), 00121 'nonExistingRecords_s' => array(), 00122 ); 00123 00124 // Select DB relations from reference table 00125 $recs = $TYPO3_DB->exec_SELECTgetRows( 00126 '*', 00127 'sys_refindex', 00128 'ref_table!='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'). // Assuming that any other key will be a table name! 00129 ' AND ref_uid>0'. 00130 $filterClause, 00131 '', 00132 'sorting DESC' 00133 ); 00134 00135 // Traverse the records 00136 $tempExists = array(); 00137 if (is_array($recs)) { 00138 foreach($recs as $rec) { 00139 $suffix = $rec['softref_key']!='' ? '_s' : '_m'; 00140 $idx = $rec['ref_table'].':'.$rec['ref_uid']; 00141 00142 // Get referenced record: 00143 if (!isset($tempExists[$idx])) { 00144 $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'] : '')); 00145 } 00146 00147 // Compile info string for location of reference: 00148 $infoString = $this->infoStr($rec); 00149 00150 // Handle missing file: 00151 if ($tempExists[$idx]['uid']) { 00152 if ($tempExists[$idx]['pid']==-1) { 00153 $resultArray['offlineVersionRecords'.$suffix][$idx][$rec['hash']] = $infoString; 00154 ksort($resultArray['offlineVersionRecords'.$suffix][$idx]); 00155 } elseif ($GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete'] && $tempExists[$idx][$GLOBALS['TCA'][$rec['ref_table']]['ctrl']['delete']]) { 00156 $resultArray['deletedRecords'.$suffix][$idx][$rec['hash']] = $infoString; 00157 ksort($resultArray['deletedRecords'.$suffix][$idx]); 00158 } 00159 } else { 00160 $resultArray['nonExistingRecords'.$suffix][$idx][$rec['hash']] = $infoString; 00161 ksort($resultArray['nonExistingRecords'.$suffix][$idx]); 00162 } 00163 } 00164 } 00165 00166 ksort($resultArray['offlineVersionRecords_m']); 00167 ksort($resultArray['deletedRecords_m']); 00168 ksort($resultArray['nonExistingRecords_m']); 00169 ksort($resultArray['offlineVersionRecords_s']); 00170 ksort($resultArray['deletedRecords_s']); 00171 ksort($resultArray['nonExistingRecords_s']); 00172 00173 return $resultArray; 00174 } 00175 00176 /** 00177 * Mandatory autofix function 00178 * Will run auto-fix on the result array. Echos status during processing. 00179 * 00180 * @param array Result array from main() function 00181 * @return void 00182 */ 00183 function main_autoFix($resultArray) { 00184 00185 $trav = array('offlineVersionRecords_m', 'nonExistingRecords_m'); 00186 foreach($trav as $tk) { 00187 echo 'Processing managed "'.$tk.'"...'.LF; 00188 foreach($resultArray[$tk] as $key => $value) { 00189 foreach($value as $hash => $recReference) { 00190 echo ' Removing reference to '.$key.' in record "'.$recReference.'": '; 00191 if ($bypass = $this->cli_noExecutionCheck($recReference)) { 00192 echo $bypass; 00193 } else { 00194 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex'); 00195 $error = $sysRefObj->setReferenceValue($hash,NULL); 00196 if ($error) { 00197 echo ' t3lib_refindex::setReferenceValue(): '.$error.LF; 00198 } else echo 'DONE'; 00199 } 00200 echo LF; 00201 } 00202 } 00203 } 00204 } 00205 } 00206 00207 ?>
1.8.0