TYPO3 API  SVNRelease
class.orphan_records.php
Go to the documentation of this file.
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: Orphan records
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_orphan_records extends tx_lowlevel_cleaner_core
00039  *   63:     function tx_lowlevel_orphan_records()
00040  *   96:     function main()
00041  *  151:     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 Orphan Records
00051  *
00052  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00053  * @package TYPO3
00054  * @subpackage tx_lowlevel
00055  */
00056 class tx_lowlevel_orphan_records extends tx_lowlevel_cleaner_core {
00057 
00058     /**
00059      * Constructor
00060      *
00061      * @return  [type]      ...
00062      */
00063     function tx_lowlevel_orphan_records()   {
00064         parent::tx_lowlevel_cleaner_core();
00065 
00066             // Setting up help:
00067         $this->cli_options[] = array('--echotree level', 'When "level" is set to 1 or higher you will see the page of the page tree outputted as it is traversed. A value of 2 for "level" will show even more information.');
00068         $this->cli_help['name'] = 'orphan_records -- To find records that has lost their connection with the page tree';
00069         $this->cli_help['description'] = trim('
00070 Assumptions:
00071 - That all actively used records on the website from TCA configured tables are located in the page tree exclusively.
00072 
00073 All records managed by TYPO3 via the TCA array configuration has to belong to a page in the page tree, either directly or indirectly as a version of another record.
00074 VERY TIME, CPU and MEMORY intensive operation since the full page tree is looked up!
00075 
00076 Automatic Repair of Errors:
00077 - Silently deleting the orphaned records. In theory they should not be used anywhere in the system, but there could be references. See below for more details on this matter.
00078 
00079 Manual repair suggestions:
00080 - Possibly re-connect orphaned records to page tree by setting their "pid" field to a valid page id. A lookup in the sys_refindex table can reveal if there are references to a orphaned record. If there are such references (from records that are not themselves orphans) you might consider to re-connect the record to the page tree, otherwise it should be safe to delete it.
00081 ');
00082         $this->cli_help['todo'] = trim('
00083 - Implement a check for references to orphaned records and if a reference comes from a record that is not orphaned itself, we might rather like to re-connect the record to the page tree.
00084 - Implement that orphans can be fixed by setting the PID to a certain page instead of deleting.');
00085 
00086         $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner orphan_records -s -r
00087 Will report orphan uids from TCA tables.';
00088     }
00089 
00090     /**
00091      * Find orphan records
00092      * VERY CPU and memory intensive since it will look up the whole page tree!
00093      *
00094      * @return  array
00095      */
00096     function main() {
00097         global $TYPO3_DB;
00098 
00099             // Initialize result array:
00100         $resultArray = array(
00101             'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
00102             'headers' => array(
00103                 'orphans' => array('Index of orphaned records','',3),
00104                 'misplaced_at_rootlevel' => array('Records that should not be at root level but are.','Fix manually by moving record into page tree',2),
00105                 'misplaced_inside_tree' => array('Records that should be at root level but are not.','Fix manually by moving record to tree root',2),
00106                 'illegal_record_under_versioned_page' => array('Records that cannot be attached to a versioned page','(Listed under orphaned records so is fixed along with orphans.)',2),
00107             ),
00108             'orphans' => array(),
00109             'misplaced_at_rootlevel' => array(),                // Subset of "all": Those that should not be at root level but are. [Warning: Fix by moving record into page tree]
00110             'misplaced_inside_tree' => array(),                 // Subset of "all": Those that are inside page tree but should be at root level [Warning: Fix by setting PID to zero]
00111             'illegal_record_under_versioned_page' => array(),
00112         );
00113 
00114         $startingPoint = 0; // zero = tree root, must use tree root if you wish to reverse selection to find orphans!
00115         $pt = t3lib_div::milliseconds();
00116 
00117         $this->genTree($startingPoint,1000,(int)$this->cli_argValue('--echotree'));
00118 
00119         $resultArray['misplaced_at_rootlevel'] = $this->recStats['misplaced_at_rootlevel'];
00120         $resultArray['misplaced_inside_tree'] = $this->recStats['misplaced_inside_tree'];
00121         $resultArray['illegal_record_under_versioned_page'] = $this->recStats['illegal_record_under_versioned_page'];
00122 
00123             // Find orphans:
00124         foreach($GLOBALS['TCA'] as $tableName => $cfg)  {
00125 
00126             $idList = is_array($this->recStats['all'][$tableName]) && count($this->recStats['all'][$tableName]) ? implode(',',$this->recStats['all'][$tableName]) : 0;
00127 
00128                 // Select all records belonging to page:
00129             $orphanRecords =    $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00130                                     'uid',
00131                                     $tableName,
00132                                     'uid NOT IN ('.$idList.')',
00133                                     '','uid','','uid'
00134                                 );
00135 
00136             if (count($orphanRecords))  {
00137                 $resultArray['orphans'][$tableName] = array();
00138                 foreach($orphanRecords as $oR)  {
00139                     $resultArray['orphans'][$tableName][$oR['uid']] = $oR['uid'];
00140                 }
00141             }
00142         }
00143 
00144         return $resultArray;
00145     }
00146 
00147     /**
00148      * Mandatory autofix function
00149      * Will run auto-fix on the result array. Echos status during processing.
00150      *
00151      * @param   array       Result array from main() function
00152      * @return  void
00153      */
00154     function main_autoFix($resultArray) {
00155 
00156             // Putting "pages" table in the bottom:
00157         if (isset($resultArray['orphans']['pages']))    {
00158             $_pages = $resultArray['orphans']['pages'];
00159             unset($resultArray['orphans']['pages']);
00160             $resultArray['orphans']['pages'] = $_pages;
00161         }
00162 
00163             // Traversing records:
00164         foreach($resultArray['orphans'] as $table => $list) {
00165             echo 'Removing orphans from table "'.$table.'":'.LF;
00166             foreach($list as $uid)  {
00167                 echo '  Flushing orphan record "'.$table.':'.$uid.'": ';
00168                 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
00169                     echo $bypass;
00170                 } else {
00171 
00172                         // Execute CMD array:
00173                     $tce = t3lib_div::makeInstance('t3lib_TCEmain');
00174                     $tce->stripslashes_values = FALSE;
00175                     $tce->start(array(),array());
00176                     $tce->deleteRecord($table,$uid, TRUE, TRUE);    // Notice, we are deleting pages with no regard to subpages/subrecords - we do this since they should also be included in the set of orphans of course!
00177 
00178                         // Return errors if any:
00179                     if (count($tce->errorLog))  {
00180                         echo '  ERROR from "TCEmain":'.LF.'TCEmain:'.implode(LF.'TCEmain:',$tce->errorLog);
00181                     } else echo 'DONE';
00182                 }
00183                 echo LF;
00184             }
00185         }
00186     }
00187 }
00188 
00189 ?>