1ee73594c4180a1cd7721fb3903800d2af022c61
[Packages/TYPO3.CMS.git] / typo3 / sysext / integrity / Classes / OrphanRecordsCommand.php
1 <?php
2 namespace TYPO3\CMS\Integrity;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Cleaner module: Orphan records
31 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * Looking for Orphan Records
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class OrphanRecordsCommand extends \TYPO3\CMS\Integrity\CleanerCommand {
43
44 /**
45 * Constructor
46 *
47 * @todo Define visibility
48 */
49 public function __construct() {
50 parent::__construct();
51 // Setting up help:
52 $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.');
53 $this->cli_help['name'] = 'orphan_records -- To find records that has lost their connection with the page tree';
54 $this->cli_help['description'] = trim('
55 Assumptions:
56 - That all actively used records on the website from TCA configured tables are located in the page tree exclusively.
57
58 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.
59 VERY TIME, CPU and MEMORY intensive operation since the full page tree is looked up!
60
61 Automatic Repair of Errors:
62 - 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.
63
64 Manual repair suggestions:
65 - 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.
66 ');
67 $this->cli_help['todo'] = trim('
68 - 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.
69 - Implement that orphans can be fixed by setting the PID to a certain page instead of deleting.');
70 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner orphan_records -s -r
71 Will report orphan uids from TCA tables.';
72 }
73
74 /**
75 * Find orphan records
76 * VERY CPU and memory intensive since it will look up the whole page tree!
77 *
78 * @return array
79 * @todo Define visibility
80 */
81 public function main() {
82 global $TYPO3_DB;
83 // Initialize result array:
84 $resultArray = array(
85 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
86 'headers' => array(
87 'orphans' => array('Index of orphaned records', '', 3),
88 'misplaced_at_rootlevel' => array('Records that should not be at root level but are.', 'Fix manually by moving record into page tree', 2),
89 'misplaced_inside_tree' => array('Records that should be at root level but are not.', 'Fix manually by moving record to tree root', 2),
90 '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)
91 ),
92 'orphans' => array(),
93 'misplaced_at_rootlevel' => array(),
94 // Subset of "all": Those that should not be at root level but are. [Warning: Fix by moving record into page tree]
95 'misplaced_inside_tree' => array(),
96 // Subset of "all": Those that are inside page tree but should be at root level [Warning: Fix by setting PID to zero]
97 'illegal_record_under_versioned_page' => array()
98 );
99 // zero = tree root, must use tree root if you wish to reverse selection to find orphans!
100 $startingPoint = 0;
101 $pt = \TYPO3\CMS\Core\Utility\GeneralUtility::milliseconds();
102 $this->genTree($startingPoint, 1000, (int) $this->cli_argValue('--echotree'));
103 $resultArray['misplaced_at_rootlevel'] = $this->recStats['misplaced_at_rootlevel'];
104 $resultArray['misplaced_inside_tree'] = $this->recStats['misplaced_inside_tree'];
105 $resultArray['illegal_record_under_versioned_page'] = $this->recStats['illegal_record_under_versioned_page'];
106 // Find orphans:
107 foreach ($GLOBALS['TCA'] as $tableName => $cfg) {
108 $idList = is_array($this->recStats['all'][$tableName]) && count($this->recStats['all'][$tableName]) ? implode(',', $this->recStats['all'][$tableName]) : 0;
109 // Select all records belonging to page:
110 $orphanRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $tableName, 'uid NOT IN (' . $idList . ')', '', 'uid', '', 'uid');
111 if (count($orphanRecords)) {
112 $resultArray['orphans'][$tableName] = array();
113 foreach ($orphanRecords as $oR) {
114 $resultArray['orphans'][$tableName][$oR['uid']] = $oR['uid'];
115 }
116 }
117 }
118 return $resultArray;
119 }
120
121 /**
122 * Mandatory autofix function
123 * Will run auto-fix on the result array. Echos status during processing.
124 *
125 * @param array $resultArray Result array from main() function
126 * @return void
127 * @todo Define visibility
128 */
129 public function main_autoFix($resultArray) {
130 // Putting "pages" table in the bottom:
131 if (isset($resultArray['orphans']['pages'])) {
132 $_pages = $resultArray['orphans']['pages'];
133 unset($resultArray['orphans']['pages']);
134 $resultArray['orphans']['pages'] = $_pages;
135 }
136 // Traversing records:
137 foreach ($resultArray['orphans'] as $table => $list) {
138 echo 'Removing orphans from table "' . $table . '":' . LF;
139 foreach ($list as $uid) {
140 echo ' Flushing orphan record "' . $table . ':' . $uid . '": ';
141 if ($bypass = $this->cli_noExecutionCheck($table . ':' . $uid)) {
142 echo $bypass;
143 } else {
144 // Execute CMD array:
145 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
146 $tce->stripslashes_values = FALSE;
147 $tce->start(array(), array());
148 // Notice, we are deleting pages with no regard to subpages/subrecords - we do this
149 // since they should also be included in the set of orphans of course!
150 $tce->deleteRecord($table, $uid, TRUE, TRUE);
151 // Return errors if any:
152 if (count($tce->errorLog)) {
153 echo ' ERROR from "TCEmain":' . LF . 'TCEmain:' . implode((LF . 'TCEmain:'), $tce->errorLog);
154 } else {
155 echo 'DONE';
156 }
157 }
158 echo LF;
159 }
160 }
161 }
162
163 }
164
165
166 ?>