Fixing SVN directory structure
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / clmods / class.versions.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (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 * [CLASS/FUNCTION INDEX of SCRIPT]
35 *
36 *
37 *
38 * 56: class tx_lowlevel_versions extends tx_lowlevel_cleaner_core
39 * 63: function tx_lowlevel_versions()
40 * 88: function main()
41 * 122: function main_autoFix($resultArray)
42 *
43 * TOTAL FUNCTIONS: 3
44 * (This index is automatically created/updated by the extension "extdeveval")
45 *
46 */
47
48
49 /**
50 * Looking for versions of records
51 *
52 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
53 * @package TYPO3
54 * @subpackage tx_lowlevel
55 */
56 class tx_lowlevel_versions extends tx_lowlevel_cleaner_core {
57
58 /**
59 * Constructor
60 *
61 * @return void
62 */
63 function tx_lowlevel_versions() {
64 parent::tx_lowlevel_cleaner_core();
65
66 // Setting up help:
67 $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.');
68 $this->cli_options[] = array('--pid id', 'Setting start page in page tree. Default is the page tree root, 0 (zero)');
69 $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.');
70
71 $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)');
72
73 $this->cli_help['name'] = 'versions -- To find information about versions and workspaces in the system';
74 $this->cli_help['description'] = trim('
75 Traversing page tree and finding versions, categorizing them by various properties.
76 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.
77
78 Automatic Repair:
79 - Deleting (completely) published versions from LIVE workspace OR _all_ offline versions from Live workspace (toogle by --flush-live)
80 - 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)
81 - Deleting unused placeholders
82 ');
83
84 $this->cli_help['examples'] = '';
85 }
86
87 /**
88 * Find orphan records
89 * VERY CPU and memory intensive since it will look up the whole page tree!
90 *
91 * @return array
92 */
93 function main() {
94 global $TYPO3_DB;
95
96 // Initialize result array:
97 $resultArray = array(
98 'message' => $this->cli_help['name'].chr(10).chr(10).$this->cli_help['description'],
99 'headers' => array(
100 'versions' => array('All versions','Showing all versions of records found',0),
101 'versions_published' => array('All published versions','This is all records that has been published and can therefore be removed permanently',1),
102 '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),
103 'versions_lost_workspace' => array('Versions outside a workspace','Versions that has lost their connection to a workspace in TYPO3.',3),
104 '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),
105 'versions_unused_placeholders' => array('Unused placeholder records','Placeholder records which are not used anymore by offline versions.',2)
106 ),
107 'versions' => array(),
108 );
109
110 $startingPoint = $this->cli_isArg('--pid') ? t3lib_div::intInRange($this->cli_argValue('--pid'),0) : 0;
111 $depth = $this->cli_isArg('--depth') ? t3lib_div::intInRange($this->cli_argValue('--depth'),0) : 1000;
112 $this->genTree($startingPoint,$depth,(int)$this->cli_argValue('--echotree'));
113
114 $resultArray['versions'] = $this->recStats['versions'];
115 $resultArray['versions_published'] = $this->recStats['versions_published'];
116 $resultArray['versions_liveWS'] = $this->recStats['versions_liveWS'];
117 $resultArray['versions_lost_workspace'] = $this->recStats['versions_lost_workspace'];
118 $resultArray['versions_inside_versioned_page'] = $this->recStats['versions_inside_versioned_page'];
119
120 // Finding all placeholders with no records attached!
121 $resultArray['versions_unused_placeholders'] = array();
122 foreach($GLOBALS['TCA'] as $table => $cfg) {
123 if ($cfg['ctrl']['versioningWS']) {
124 $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid',$table,'t3ver_state=1 AND pid>=0'.t3lib_BEfunc::deleteClause($table));
125 foreach($placeHolders as $phrec) {
126 if (count(t3lib_BEfunc::selectVersionsOfRecord($table, $phrec['uid'], 'uid'))<=1) {
127 $resultArray['versions_unused_placeholders'][] = $table.':'.$phrec['uid'];
128 }
129 }
130 }
131 }
132
133 return $resultArray;
134 }
135
136 /**
137 * Mandatory autofix function
138 * Will run auto-fix on the result array. Echos status during processing.
139 *
140 * @param array Result array from main() function
141 * @return void
142 */
143 function main_autoFix($resultArray) {
144
145 $kk = $this->cli_isArg('--flush-live') ? 'versions_liveWS' : 'versions_published';
146
147 // Putting "pages" table in the bottom:
148 if (isset($resultArray[$kk]['pages'])) {
149 $_pages = $resultArray[$kk]['pages'];
150 unset($resultArray[$kk]['pages']);
151 $resultArray[$kk]['pages'] = $_pages;
152 }
153
154 // Traversing records:
155 foreach($resultArray[$kk] as $table => $list) {
156 echo 'Flushing published records from table "'.$table.'":'.chr(10);
157 foreach($list as $uid) {
158 echo ' Flushing record "'.$table.':'.$uid.'": ';
159
160 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
161 echo $bypass;
162 } else {
163
164 // Execute CMD array:
165 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
166 $tce->stripslashes_values = FALSE;
167 $tce->start(array(),array());
168 $tce->deleteEl($table,$uid, TRUE, TRUE);
169
170 // Return errors if any:
171 if (count($tce->errorLog)) {
172 echo ' ERROR from "TCEmain":'.chr(10).'TCEmain:'.implode(chr(10).'TCEmain:',$tce->errorLog);
173 } else echo 'DONE';
174 }
175 echo chr(10);
176 }
177 }
178
179 // Traverse workspace:
180 foreach($resultArray['versions_lost_workspace'] as $table => $list) {
181 echo 'Resetting workspace to zero for records from table "'.$table.'":'.chr(10);
182 foreach($list as $uid) {
183 echo ' Flushing record "'.$table.':'.$uid.'": ';
184 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
185 echo $bypass;
186 } else {
187 $fields_values = array(
188 't3ver_wsid' => 0
189 );
190 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($uid),$fields_values);
191 echo 'DONE';
192 }
193 echo chr(10);
194 }
195 }
196
197 // Delete unused placeholders
198 foreach($resultArray['versions_unused_placeholders'] as $recID) {
199 list($table,$uid) = explode(':',$recID);
200 echo 'Deleting unused placeholder (soft) "'.$table.':'.$uid.'": ';
201 if ($bypass = $this->cli_noExecutionCheck($table.':'.$uid)) {
202 echo $bypass;
203 } else {
204
205 // Execute CMD array:
206 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
207 $tce->stripslashes_values = FALSE;
208 $tce->start(array(),array());
209 $tce->deleteAction($table, $uid);
210
211 // Return errors if any:
212 if (count($tce->errorLog)) {
213 echo ' ERROR from "TCEmain":'.chr(10).'TCEmain:'.implode(chr(10).'TCEmain:',$tce->errorLog);
214 } else echo 'DONE';
215 }
216 echo chr(10);
217 }
218 }
219 }
220
221 ?>