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