975e0a061dbdfb1b7c7208cebf54b20a76b1c3f5
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / class.versions.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 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: Versions of records
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
35 /**
36 * Looking for versions of records
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class tx_lowlevel_versions extends tx_lowlevel_cleaner_core {
43
44 /**
45 * Constructor
46 *
47 * @return void
48 */
49 function __construct() {
50 parent::__construct();
51
52 // Setting up help:
53 $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.');
54 $this->cli_options[] = array('--pid id', 'Setting start page in page tree. Default is the page tree root, 0 (zero)');
55 $this->cli_options[] = array('--depth int', 'Setting traversal depth. 0 (zero) will only analyse start page (see --pid), 1 will traverse one level of subpages etc.');
56
57 $this->cli_options[] = array('--flush-live', 'If set, not only published versions from Live workspace are flushed, but ALL versions from Live workspace (which are offline of course)');
58
59 $this->cli_help['name'] = 'versions -- To find information about versions and workspaces in the system';
60 $this->cli_help['description'] = trim('
61 Traversing page tree and finding versions, categorizing them by various properties.
62 Published versions from the Live workspace are registered. So are all offline versions from Live workspace in general. Further, versions in non-existing workspaces are found.
63
64 Automatic Repair:
65 - Deleting (completely) published versions from LIVE workspace OR _all_ offline versions from Live workspace (toogle by --flush-live)
66 - Resetting workspace for versions where workspace is deleted. (You might want to run this tool again after this operation to clean out those new elements in the Live workspace)
67 - Deleting unused placeholders
68 ');
69
70 $this->cli_help['examples'] = '';
71 }
72
73 /**
74 * Find orphan records
75 * VERY CPU and memory intensive since it will look up the whole page tree!
76 *
77 * @return array
78 */
79 function main() {
80 global $TYPO3_DB;
81
82 // Initialize result array:
83 $resultArray = array(
84 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
85 'headers' => array(
86 'versions' => array('All versions','Showing all versions of records found',0),
87 'versions_published' => array('All published versions','This is all records that has been published and can therefore be removed permanently',1),
88 'versions_liveWS' => array('All versions in Live workspace','This is all records that are offline versions in the Live workspace. You may wish to flush these if you only use workspaces for versioning since then you might find lots of versions piling up in the live workspace which have simply been disconnected from the workspace before they were published.',1),
89 'versions_lost_workspace' => array('Versions outside a workspace','Versions that has lost their connection to a workspace in TYPO3.',3),
90 'versions_inside_versioned_page' => array('Versions in versions','Versions inside an already versioned page. Something that is confusing to users and therefore should not happen but is technically possible.',2),
91 'versions_unused_placeholders' => array('Unused placeholder records','Placeholder records which are not used anymore by offline versions.',2),
92 'versions_move_placeholders_ok' => array('Move placeholders','Move-to placeholder records which has good integrity',0),
93 'versions_move_placeholders_bad' => array('Move placeholders with bad integrity','Move-to placeholder records which has bad integrity',2),
94 'versions_move_id_check' => array('Checking if t3ver_move_id is correct','t3ver_move_id must only be set with online records having t3ver_state=3.',2),
95 ),
96 'versions' => array(),
97 );
98
99 $startingPoint = $this->cli_isArg('--pid') ? t3lib_utility_Math::forceIntegerInRange($this->cli_argValue('--pid'),0) : 0;
100 $depth = $this->cli_isArg('--depth') ? t3lib_utility_Math::forceIntegerInRange($this->cli_argValue('--depth'),0) : 1000;
101 $this->genTree($startingPoint,$depth,(int)$this->cli_argValue('--echotree'));
102
103 $resultArray['versions'] = $this->recStats['versions'];
104 $resultArray['versions_published'] = $this->recStats['versions_published'];
105 $resultArray['versions_liveWS'] = $this->recStats['versions_liveWS'];
106 $resultArray['versions_lost_workspace'] = $this->recStats['versions_lost_workspace'];
107 $resultArray['versions_inside_versioned_page'] = $this->recStats['versions_inside_versioned_page'];
108
109 // Finding all placeholders with no records attached!
110 $resultArray['versions_unused_placeholders'] = array();
111 foreach($GLOBALS['TCA'] as $table => $cfg) {
112 if ($cfg['ctrl']['versioningWS']) {
113 $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid',$table,'t3ver_state=1 AND pid>=0'.t3lib_BEfunc::deleteClause($table));
114 foreach($placeHolders as $phrec) {
115 if (count(t3lib_BEfunc::selectVersionsOfRecord($table, $phrec['uid'], 'uid'))<=1) {
116 $resultArray['versions_unused_placeholders'][t3lib_div::shortmd5($table.':'.$phrec['uid'])] = $table.':'.$phrec['uid'];
117 }
118 }
119 }
120 }
121 asort($resultArray['versions_unused_placeholders']);
122
123 // Finding all move placeholders with inconsistencies:
124 $resultArray['versions_move_placeholders_ok'] = array();
125 $resultArray['versions_move_placeholders_bad'] = array();
126 foreach($GLOBALS['TCA'] as $table => $cfg) {
127 if ((int)$cfg['ctrl']['versioningWS']>=2) {
128 $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,t3ver_move_id,t3ver_wsid,t3ver_state',$table,'t3ver_state=3 AND pid>=0'.t3lib_BEfunc::deleteClause($table));
129 foreach($placeHolders as $phrec) {
130 $shortID = t3lib_div::shortmd5($table.':'.$phrec['uid']);
131 if ((int)$phrec['t3ver_wsid']!=0) {
132 $phrecCopy = $phrec;
133 if (t3lib_BEfunc::movePlhOL($table,$phrec)) {
134 if ($wsAlt = t3lib_BEfunc::getWorkspaceVersionOfRecord($phrecCopy['t3ver_wsid'], $table, $phrec['uid'], 'uid,pid,t3ver_state')) {
135 if ($wsAlt['t3ver_state']!=4) {
136 $resultArray['versions_move_placeholders_bad'][$shortID] = array($table.':'.$phrec['uid'],'State for version was not "4" as it should be!',$phrecCopy);
137 } else {
138 $resultArray['versions_move_placeholders_ok'][$shortID] = array(
139 $table.':'.$phrec['uid'],
140 'PLH' => $phrecCopy,
141 'online' => $phrec,
142 'PNT' => $wsAlt
143 );
144 }
145 } else {
146 $resultArray['versions_move_placeholders_bad'][$shortID] = array($table.':'.$phrec['uid'],'No version was found for online record to be moved. A version must exist.',$phrecCopy);
147 }
148 } else {
149 $resultArray['versions_move_placeholders_bad'][$shortID] = array($table.':'.$phrec['uid'],'Did not find online record for "t3ver_move_id" value '.$phrec['t3ver_move_id'],$phrec);
150 }
151 } else {
152 $resultArray['versions_move_placeholders_bad'][$shortID] = array($table.':'.$phrec['uid'],'Placeholder was not assigned a workspace value in t3ver_wsid.',$phrec);
153 }
154 }
155 }
156 }
157
158 ksort($resultArray['versions_move_placeholders_ok']);
159 ksort($resultArray['versions_move_placeholders_bad']);
160
161 // Finding move_id_check inconsistencies:
162 $resultArray['versions_move_id_check'] = array();
163 foreach($GLOBALS['TCA'] as $table => $cfg) {
164 if ((int)$cfg['ctrl']['versioningWS']>=2) {
165 $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,t3ver_move_id,t3ver_wsid,t3ver_state',$table,'t3ver_move_id<>0'.t3lib_BEfunc::deleteClause($table));
166 foreach($placeHolders as $phrec) {
167 if ((int)$phrec['t3ver_state']==3) {
168 if ($phrec['pid']!=-1) {
169 // OK
170 } else {
171 $resultArray['versions_move_id_check'][] = array($table.':'.$phrec['uid'],'Record was offline, must not be!',$phrec);
172 }
173 } else {
174 $resultArray['versions_move_id_check'][] = array($table.':'.$phrec['uid'],'Record had t3ver_move_id set to "'.$phrec['t3ver_move_id'].'" while having t3ver_state='.$phrec['t3ver_state'],$phrec);
175 }
176 }
177 }
178 }
179
180 return $resultArray;
181 }
182
183 /**
184 * Mandatory autofix function
185 * Will run auto-fix on the result array. Echos status during processing.
186 *
187 * @param array Result array from main() function
188 * @return void
189 */
190 function main_autoFix($resultArray) {
191
192 $kk = $this->cli_isArg('--flush-live') ? 'versions_liveWS' : 'versions_published';
193
194 // Putting "pages" table in the bottom:
195 if (isset($resultArray[$kk]['pages'])) {
196 $_pages = $resultArray[$kk]['pages'];
197 unset($resultArray[$kk]['pages']);
198 $resultArray[$kk]['pages'] = $_pages;
199 }
200
201 // Traversing records:
202 foreach($resultArray[$kk] as $table => $list) {
203 echo 'Flushing published records from table "'.$table.'":'.LF;
204 foreach($list as $uid) {
205 echo ' Flushing record "'.$table.':'.$uid.'": ';
206
207 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
208 echo $bypass;
209 } else {
210
211 // Execute CMD array:
212 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
213 $tce->stripslashes_values = FALSE;
214 $tce->start(array(),array());
215 $tce->deleteEl($table,$uid, TRUE, TRUE);
216
217 // Return errors if any:
218 if (count($tce->errorLog)) {
219 echo ' ERROR from "TCEmain":'.LF.'TCEmain:'.implode(LF.'TCEmain:',$tce->errorLog);
220 } else echo 'DONE';
221 }
222 echo LF;
223 }
224 }
225
226 // Traverse workspace:
227 foreach($resultArray['versions_lost_workspace'] as $table => $list) {
228 echo 'Resetting workspace to zero for records from table "'.$table.'":'.LF;
229 foreach($list as $uid) {
230 echo ' Flushing record "'.$table.':'.$uid.'": ';
231 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
232 echo $bypass;
233 } else {
234 $fields_values = array(
235 't3ver_wsid' => 0
236 );
237 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($uid),$fields_values);
238 echo 'DONE';
239 }
240 echo LF;
241 }
242 }
243
244 // Delete unused placeholders
245 foreach($resultArray['versions_unused_placeholders'] as $recID) {
246 list($table,$uid) = explode(':',$recID);
247 echo 'Deleting unused placeholder (soft) "'.$table.':'.$uid.'": ';
248 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
249 echo $bypass;
250 } else {
251
252 // Execute CMD array:
253 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
254 $tce->stripslashes_values = FALSE;
255 $tce->start(array(),array());
256 $tce->deleteAction($table, $uid);
257
258 // Return errors if any:
259 if (count($tce->errorLog)) {
260 echo ' ERROR from "TCEmain":'.LF.'TCEmain:'.implode(LF.'TCEmain:',$tce->errorLog);
261 } else echo 'DONE';
262 }
263 echo LF;
264 }
265 }
266 }
267
268 ?>