TYPO3 CMS  TYPO3_7-6
VersionsCommand.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Lowlevel;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
25 {
29  public function __construct()
30  {
31  parent::__construct();
32  // Setting up help:
33  $this->cli_options[] = ['--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.'];
34  $this->cli_options[] = ['--pid id', 'Setting start page in page tree. Default is the page tree root, 0 (zero)'];
35  $this->cli_options[] = ['--depth int', 'Setting traversal depth. 0 (zero) will only analyse start page (see --pid), 1 will traverse one level of subpages etc.'];
36  $this->cli_options[] = ['--flush-live', 'If set, not only published versions from Live workspace are flushed, but ALL versions from Live workspace (which are offline of course)'];
37  $this->cli_help['name'] = 'versions -- To find information about versions and workspaces in the system';
38  $this->cli_help['description'] = trim('
39 Traversing page tree and finding versions, categorizing them by various properties.
40 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.
41 
42 Automatic Repair:
43 - Deleting (completely) published versions from LIVE workspace OR _all_ offline versions from Live workspace (toggle by --flush-live)
44 - 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)
45 - Deleting unused placeholders
46 ');
47  $this->cli_help['examples'] = '';
48  }
49 
56  public function main()
57  {
58  // Initialize result array:
59  $resultArray = [
60  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
61  'headers' => [
62  'versions' => ['All versions', 'Showing all versions of records found', 0],
63  'versions_published' => ['All published versions', 'This is all records that has been published and can therefore be removed permanently', 1],
64  'versions_liveWS' => ['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],
65  'versions_lost_workspace' => ['Versions outside a workspace', 'Versions that has lost their connection to a workspace in TYPO3.', 3],
66  'versions_inside_versioned_page' => ['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],
67  'versions_unused_placeholders' => ['Unused placeholder records', 'Placeholder records which are not used anymore by offline versions.', 2],
68  'versions_move_placeholders_ok' => ['Move placeholders', 'Move-to placeholder records which has good integrity', 0],
69  'versions_move_placeholders_bad' => ['Move placeholders with bad integrity', 'Move-to placeholder records which has bad integrity', 2],
70  'versions_move_id_check' => ['Checking if t3ver_move_id is correct', 't3ver_move_id must only be set with online records having t3ver_state=3.', 2]
71  ],
72  'versions' => []
73  ];
74  $startingPoint = $this->cli_isArg('--pid') ? \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->cli_argValue('--pid'), 0) : 0;
75  $depth = $this->cli_isArg('--depth') ? \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->cli_argValue('--depth'), 0) : 1000;
76  $this->genTree($startingPoint, $depth, (int)$this->cli_argValue('--echotree'));
77  $resultArray['versions'] = $this->recStats['versions'];
78  $resultArray['versions_published'] = $this->recStats['versions_published'];
79  $resultArray['versions_liveWS'] = $this->recStats['versions_liveWS'];
80  $resultArray['versions_lost_workspace'] = $this->recStats['versions_lost_workspace'];
81  $resultArray['versions_inside_versioned_page'] = $this->recStats['versions_inside_versioned_page'];
82  // Finding all placeholders with no records attached!
83  $resultArray['versions_unused_placeholders'] = [];
84  foreach ($GLOBALS['TCA'] as $table => $cfg) {
85  if ($cfg['ctrl']['versioningWS']) {
86  $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
87  'uid,pid',
88  $table,
89  't3ver_state=' . new VersionState(VersionState::NEW_PLACEHOLDER) . ' AND pid>=0' . BackendUtility::deleteClause($table)
90  );
91  foreach ($placeHolders as $phrec) {
92  if (count(BackendUtility::selectVersionsOfRecord($table, $phrec['uid'], 'uid', '*', null)) <= 1) {
93  $resultArray['versions_unused_placeholders'][GeneralUtility::shortmd5($table . ':' . $phrec['uid'])] = $table . ':' . $phrec['uid'];
94  }
95  }
96  }
97  }
98  asort($resultArray['versions_unused_placeholders']);
99  // Finding all move placeholders with inconsistencies:
100  $resultArray['versions_move_placeholders_ok'] = [];
101  $resultArray['versions_move_placeholders_bad'] = [];
102  foreach ($GLOBALS['TCA'] as $table => $cfg) {
104  $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
105  'uid,pid,t3ver_move_id,t3ver_wsid,t3ver_state',
106  $table,
107  't3ver_state=' . new VersionState(VersionState::MOVE_PLACEHOLDER) . ' AND pid>=0' . BackendUtility::deleteClause($table)
108  );
109  foreach ($placeHolders as $phrec) {
110  $shortID = GeneralUtility::shortmd5($table . ':' . $phrec['uid']);
111  if ((int)$phrec['t3ver_wsid'] != 0) {
112  $phrecCopy = $phrec;
113  if (BackendUtility::movePlhOL($table, $phrec)) {
114  if ($wsAlt = BackendUtility::getWorkspaceVersionOfRecord($phrecCopy['t3ver_wsid'], $table, $phrec['uid'], 'uid,pid,t3ver_state')) {
115  if (!VersionState::cast($wsAlt['t3ver_state'])->equals(VersionState::MOVE_POINTER)) {
116  $resultArray['versions_move_placeholders_bad'][$shortID] = [$table . ':' . $phrec['uid'], 'State for version was not "4" as it should be!', $phrecCopy];
117  } else {
118  $resultArray['versions_move_placeholders_ok'][$shortID] = [
119  $table . ':' . $phrec['uid'],
120  'PLH' => $phrecCopy,
121  'online' => $phrec,
122  'PNT' => $wsAlt
123  ];
124  }
125  } else {
126  $resultArray['versions_move_placeholders_bad'][$shortID] = [$table . ':' . $phrec['uid'], 'No version was found for online record to be moved. A version must exist.', $phrecCopy];
127  }
128  } else {
129  $resultArray['versions_move_placeholders_bad'][$shortID] = [$table . ':' . $phrec['uid'], 'Did not find online record for "t3ver_move_id" value ' . $phrec['t3ver_move_id'], $phrec];
130  }
131  } else {
132  $resultArray['versions_move_placeholders_bad'][$shortID] = [$table . ':' . $phrec['uid'], 'Placeholder was not assigned a workspace value in t3ver_wsid.', $phrec];
133  }
134  }
135  }
136  }
137  ksort($resultArray['versions_move_placeholders_ok']);
138  ksort($resultArray['versions_move_placeholders_bad']);
139  // Finding move_id_check inconsistencies:
140  $resultArray['versions_move_id_check'] = [];
141  foreach ($GLOBALS['TCA'] as $table => $cfg) {
143  $placeHolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,t3ver_move_id,t3ver_wsid,t3ver_state', $table, 't3ver_move_id<>0' . BackendUtility::deleteClause($table));
144  foreach ($placeHolders as $phrec) {
145  if (VersionState::cast($phrec['t3ver_state'])->equals(VersionState::MOVE_PLACEHOLDER)) {
146  if ($phrec['pid'] != -1) {
147  } else {
148  $resultArray['versions_move_id_check'][] = [$table . ':' . $phrec['uid'], 'Record was offline, must not be!', $phrec];
149  }
150  } else {
151  $resultArray['versions_move_id_check'][] = [$table . ':' . $phrec['uid'], 'Record had t3ver_move_id set to "' . $phrec['t3ver_move_id'] . '" while having t3ver_state=' . $phrec['t3ver_state'], $phrec];
152  }
153  }
154  }
155  }
156  return $resultArray;
157  }
158 
166  public function main_autoFix($resultArray)
167  {
168  $kk = $this->cli_isArg('--flush-live') ? 'versions_liveWS' : 'versions_published';
169  // Putting "pages" table in the bottom:
170  if (isset($resultArray[$kk]['pages'])) {
171  $_pages = $resultArray[$kk]['pages'];
172  unset($resultArray[$kk]['pages']);
173  $resultArray[$kk]['pages'] = $_pages;
174  }
175  // Traversing records:
176  foreach ($resultArray[$kk] as $table => $list) {
177  echo 'Flushing published records from table "' . $table . '":' . LF;
178  foreach ($list as $uid) {
179  echo ' Flushing record "' . $table . ':' . $uid . '": ';
180  if ($bypass = $this->cli_noExecutionCheck($table . ':' . $uid)) {
181  echo $bypass;
182  } else {
183  // Execute CMD array:
184  $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
185  $tce->stripslashes_values = false;
186  $tce->start([], []);
187  $tce->deleteEl($table, $uid, true, true);
188  // Return errors if any:
189  if (count($tce->errorLog)) {
190  echo ' ERROR from "TCEmain":' . LF . 'TCEmain:' . implode((LF . 'TCEmain:'), $tce->errorLog);
191  } else {
192  echo 'DONE';
193  }
194  }
195  echo LF;
196  }
197  }
198  // Traverse workspace:
199  foreach ($resultArray['versions_lost_workspace'] as $table => $list) {
200  echo 'Resetting workspace to zero for records from table "' . $table . '":' . LF;
201  foreach ($list as $uid) {
202  echo ' Flushing record "' . $table . ':' . $uid . '": ';
203  if ($bypass = $this->cli_noExecutionCheck($table . ':' . $uid)) {
204  echo $bypass;
205  } else {
206  $fields_values = [
207  't3ver_wsid' => 0
208  ];
209  $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . (int)$uid, $fields_values);
210  echo 'DONE';
211  }
212  echo LF;
213  }
214  }
215  // Delete unused placeholders
216  foreach ($resultArray['versions_unused_placeholders'] as $recID) {
217  list($table, $uid) = explode(':', $recID);
218  echo 'Deleting unused placeholder (soft) "' . $table . ':' . $uid . '": ';
219  if ($bypass = $this->cli_noExecutionCheck($table . ':' . $uid)) {
220  echo $bypass;
221  } else {
222  // Execute CMD array:
223  $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
224  $tce->stripslashes_values = false;
225  $tce->start([], []);
226  $tce->deleteAction($table, $uid);
227  // Return errors if any:
228  if (count($tce->errorLog)) {
229  echo ' ERROR from "TCEmain":' . LF . 'TCEmain:' . implode((LF . 'TCEmain:'), $tce->errorLog);
230  } else {
231  echo 'DONE';
232  }
233  }
234  echo LF;
235  }
236  }
237 }
static getWorkspaceVersionOfRecord($workspace, $table, $uid, $fields=' *')
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
genTree($rootID, $depth=1000, $echoLevel=0, $callBack='')
$uid
Definition: server.php:38
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static selectVersionsOfRecord($table, $uid, $fields=' *', $workspace=0, $includeDeletedRecords=false, $row=null)
static deleteClause($table, $tableAlias='')