baf4ab81ac819c4989dd7063e5fb5de53435c117
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / CleanerCommand.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
20 use TYPO3\CMS\Core\Database\ReferenceIndex;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24
25 /**
26 * Core functions for cleaning and analysing
27 *
28 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
29 */
30 class CleanerCommand extends \TYPO3\CMS\Core\Controller\CommandLineController
31 {
32 /**
33 * @var bool
34 */
35 public $genTree_traverseDeleted = true;
36
37 /**
38 * @var bool
39 */
40 public $genTree_traverseVersions = true;
41
42 /**
43 * @var string
44 */
45 public $label_infoString = 'The list of records is organized as [table]:[uid]:[field]:[flexpointer]:[softref_key]';
46
47 /**
48 * @var array
49 */
50 public $pagetreePlugins = [];
51
52 /**
53 * @var array
54 */
55 public $cleanerModules = [];
56
57 /**
58 * @var array
59 */
60 protected $recStats = [];
61
62 /**
63 * @var array
64 */
65 protected $workspaceIndex = [0 => true];
66
67 /**
68 * Constructor
69 */
70 public function __construct()
71 {
72 // Running parent class constructor
73 parent::__construct();
74 $this->cleanerModules = (array)$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules'];
75 // Adding options to help archive:
76 $this->cli_options[] = ['-r', 'Execute this tool, otherwise help is shown'];
77 $this->cli_options[] = ['-v level', 'Verbosity level 0-3', 'The value of level can be:
78 0 = all output
79 1 = info and greater (default)
80 2 = warnings and greater
81 3 = errors'];
82 $this->cli_options[] = ['--refindex mode', 'Mode for reference index handling for operations that require a clean reference index ("update"/"ignore")', 'Options are "check" (default), "update" and "ignore". By default, the reference index is checked before running analysis that require a clean index. If the check fails, the analysis is not run. You can choose to bypass this completely (using value "ignore") or ask to have the index updated right away before the analysis (using value "update")'];
83 $this->cli_options[] = ['--AUTOFIX [testName]', 'Repairs errors that can be automatically fixed.', 'Only add this option after having run the test without it so you know what will happen when you add this option! The optional parameter "[testName]" works for some tool keys to limit the fixing to a particular test.'];
84 $this->cli_options[] = ['--dryrun', 'With --AUTOFIX it will only simulate a repair process', 'You may like to use this to see what the --AUTOFIX option will be doing. It will output the whole process like if a fix really occurred but nothing is in fact happening'];
85 $this->cli_options[] = ['--YES', 'Implicit YES to all questions', 'Use this with EXTREME care. The option "-i" is not affected by this option.'];
86 $this->cli_options[] = ['-i', 'Interactive', 'Will ask you before running the AUTOFIX on each element.'];
87 $this->cli_options[] = ['--filterRegex expr', 'Define an expression for preg_match() that must match the element ID in order to auto repair it', 'The element ID is the string in quotation marks when the text \'Cleaning ... in "ELEMENT ID"\'. "expr" is the expression for preg_match(). To match for example "Nature3.JPG" and "Holiday3.JPG" you can use "/.*3.JPG/". To match for example "Image.jpg" and "Image.JPG" you can use "/.*.jpg/i". Try a --dryrun first to see what the matches are!'];
88 $this->cli_options[] = ['--showhowto', 'Displays HOWTO file for cleaner script.'];
89 // Setting help texts:
90 $this->cli_help['name'] = 'lowlevel_cleaner -- Analysis and clean-up tools for TYPO3 installations';
91 $this->cli_help['synopsis'] = 'toolkey ###OPTIONS###';
92 $this->cli_help['description'] = 'Dispatches to various analysis and clean-up tools which can plug into the API of this script. Typically you can run tests that will take longer than the usual max execution time of PHP. Such tasks could be checking for orphan records in the page tree or flushing all published versions in the system. For the complete list of options, please explore each of the \'toolkey\' keywords below:
93
94 ' . implode('
95 ', array_keys($this->cleanerModules));
96 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_files -s -r
97 This will show you missing files in the TYPO3 system and only report back if errors were found.';
98 $this->cli_help['author'] = 'Kasper Skaarhoej, (c) 2006';
99 }
100
101 /**************************
102 *
103 * CLI functionality
104 *
105 *************************/
106 /**
107 * CLI engine
108 *
109 * @param array $argv Command line arguments
110 * @return string
111 * @deprecated the Cleaner Command is deprecated since TYPO3 v8 and will be removed in TYPO3 v9, use a separate CLI Command instead
112 */
113 public function cli_main($argv)
114 {
115 GeneralUtility::logDeprecatedFunction();
116 $this->cli_setArguments($argv);
117
118 // Force user to admin state and set workspace to "Live":
119 $GLOBALS['BE_USER']->user['admin'] = 1;
120 $GLOBALS['BE_USER']->setWorkspace(0);
121 // Print Howto:
122 if ($this->cli_isArg('--showhowto')) {
123 $howto = file_get_contents(ExtensionManagementUtility::extPath('lowlevel') . 'README.rst');
124 echo wordwrap($howto, 120) . LF;
125 die;
126 }
127 // Print help
128 $analysisType = (string)$this->cli_args['_DEFAULT'][1];
129 if (!$analysisType) {
130 $this->cli_validateArgs();
131 $this->cli_help();
132 die;
133 }
134
135 if (is_array($this->cleanerModules[$analysisType])) {
136 $cleanerMode = GeneralUtility::getUserObj($this->cleanerModules[$analysisType][0]);
137 $cleanerMode->cli_validateArgs();
138 // Run it...
139 if ($this->cli_isArg('-r')) {
140 if (!$cleanerMode->checkRefIndex || $this->cli_referenceIndexCheck()) {
141 $res = $cleanerMode->main();
142 $this->cli_printInfo($analysisType, $res);
143 // Autofix...
144 if ($this->cli_isArg('--AUTOFIX')) {
145 if ($this->cli_isArg('--YES') || $this->cli_keyboardInput_yes('
146
147 NOW Running --AUTOFIX on result. OK?' . ($this->cli_isArg('--dryrun') ? ' (--dryrun simulation)' : ''))) {
148 $cleanerMode->main_autofix($res);
149 } else {
150 $this->cli_echo('ABORTING AutoFix...
151 ', 1);
152 }
153 }
154 }
155 } else {
156 // Help only...
157 $cleanerMode->cli_help();
158 die;
159 }
160 } else {
161 $this->cli_echo('ERROR: Analysis Type \'' . $analysisType . '\' is unknown.
162 ', 1);
163 die;
164 }
165 }
166
167 /**
168 * Checks reference index
169 *
170 * @return bool TRUE if reference index was OK (either OK, updated or ignored)
171 */
172 public function cli_referenceIndexCheck()
173 {
174 // Reference index option:
175 $refIndexMode = isset($this->cli_args['--refindex']) ? $this->cli_args['--refindex'][0] : 'check';
176 switch ($refIndexMode) {
177 case 'check':
178
179 case 'update':
180 $refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
181 list($headerContent, $bodyContent, $errorCount) = $refIndexObj->updateIndex($refIndexMode === 'check', $this->cli_echo());
182 if ($errorCount && $refIndexMode === 'check') {
183 $ok = false;
184 $this->cli_echo('ERROR: Reference Index Check failed! (run with \'--refindex update\' to fix)
185 ', 1);
186 } else {
187 $ok = true;
188 }
189 break;
190 case 'ignore':
191 $this->cli_echo('Reference Index Check: Bypassing reference index check...
192 ');
193 $ok = true;
194 break;
195 default:
196 $this->cli_echo('ERROR: Wrong value for --refindex argument.
197 ', 1);
198 die();
199
200 }
201 return $ok;
202 }
203
204 /**
205 * @param string $matchString
206 * @return string If string, it's the reason for not executing. Returning FALSE means it should execute.
207 */
208 public function cli_noExecutionCheck($matchString)
209 {
210 // Check for filter:
211 if ($this->cli_isArg('--filterRegex') && ($regex = $this->cli_argValue('--filterRegex', 0))) {
212 if (!preg_match($regex, $matchString)) {
213 return 'BYPASS: Filter Regex "' . $regex . '" did not match string "' . $matchString . '"';
214 }
215 }
216 // Check for interactive mode
217 if ($this->cli_isArg('-i')) {
218 if (!$this->cli_keyboardInput_yes(' EXECUTE?')) {
219 return 'BYPASS...';
220 }
221 }
222 // Check for
223 if ($this->cli_isArg('--dryrun')) {
224 return 'BYPASS: --dryrun set';
225 }
226 }
227
228 /**
229 * Formats a result array from a test so it fits output in the shell
230 *
231 * @param string $header Name of the test (eg. function name)
232 * @param array $res Result array from an analyze function
233 */
234 public function cli_printInfo($header, $res)
235 {
236 $detailLevel = MathUtility::forceIntegerInRange($this->cli_isArg('-v') ? $this->cli_argValue('-v') : 1, 0, 3);
237 $silent = !$this->cli_echo();
238 $severity = [
239 0 => 'MESSAGE',
240 1 => 'INFO',
241 2 => 'WARNING',
242 3 => 'ERROR'
243 ];
244 // Header output:
245 if ($detailLevel <= 1) {
246 $this->cli_echo('*********************************************
247 ' . $header . LF . '*********************************************
248 ');
249 $this->cli_echo(wordwrap(trim($res['message'])) . LF . LF);
250 }
251 // Traverse headers for output:
252 if (is_array($res['headers'])) {
253 foreach ($res['headers'] as $key => $value) {
254 if ($detailLevel <= (int)$value[2]) {
255 if (is_array($res[$key]) && (count($res[$key]) || !$silent)) {
256 // Header and explanaion:
257 $this->cli_echo('---------------------------------------------' . LF, 1);
258 $this->cli_echo('[' . $header . ']' . LF, 1);
259 $this->cli_echo($value[0] . ' [' . $severity[$value[2]] . ']' . LF, 1);
260 $this->cli_echo('---------------------------------------------' . LF, 1);
261 if (trim($value[1])) {
262 $this->cli_echo('Explanation: ' . wordwrap(trim($value[1])) . LF . LF, 1);
263 }
264 }
265 // Content:
266 if (is_array($res[$key])) {
267 if (count($res[$key])) {
268 if ($this->cli_echo('', 1)) {
269 print_r($res[$key]);
270 }
271 } else {
272 $this->cli_echo('(None)' . LF . LF);
273 }
274 } else {
275 $this->cli_echo($res[$key] . LF . LF);
276 }
277 }
278 }
279 }
280 }
281
282 /**************************
283 *
284 * Page tree traversal
285 *
286 *************************/
287 /**
288 * Traverses the FULL/part of page tree, mainly to register ALL validly connected records (to find orphans) but also to register deleted records, versions etc.
289 * Output (in $this->recStats) can be useful for multiple purposes.
290 *
291 * @param int $rootID Root page id from where to start traversal. Use "0" (zero) to have full page tree (necessary when spotting orphans, otherwise you can run it on parts only)
292 * @param int $depth Depth to traverse. zero is do not traverse at all. 1 = 1 sublevel, 1000= 1000 sublevels (all...)
293 * @param int $echoLevel If >0, will echo information about the traversal process.
294 * @param string $callBack Call back function (from this class or subclass)
295 */
296 public function genTree($rootID, $depth = 1000, $echoLevel = 0, $callBack = '')
297 {
298 // Initialize:
299 if (ExtensionManagementUtility::isLoaded('workspaces')) {
300 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
301 ->getQueryBuilderForTable('sys_workspace');
302
303 $queryBuilder->getRestrictions()
304 ->removeAll()
305 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
306
307 $workspaceRecords = $queryBuilder
308 ->select('uid', 'title')
309 ->from('sys_workspace')
310 ->execute()
311 ->fetchAll();
312
313 foreach ($workspaceRecords as $workspaceRecord) {
314 $this->workspaceIndex[$workspaceRecord['uid']] = true;
315 }
316 }
317
318 $this->recStats = [
319 'all' => [],
320 // All records connected in tree including versions (the reverse are orphans). All Info and Warning categories below are included here (and therefore safe if you delete the reverse of the list)
321 'deleted' => [],
322 // Subset of "alL" that are deleted-flagged [Info]
323 'versions' => [],
324 // Subset of "all" which are offline versions (pid=-1). [Info]
325 'versions_published' => [],
326 // Subset of "versions" that is a count of 1 or more (has been published) [Info]
327 'versions_liveWS' => [],
328 // Subset of "versions" that exists in live workspace [Info]
329 'versions_lost_workspace' => [],
330 // Subset of "versions" that doesn't belong to an existing workspace [Warning: Fix by move to live workspace]
331 'versions_inside_versioned_page' => [],
332 // Subset of "versions" This is versions of elements found inside an already versioned branch / page. In real life this can work out, but is confusing and the backend should prevent this from happening to people. [Warning: Fix by deleting those versions (or publishing them)]
333 'illegal_record_under_versioned_page' => [],
334 // If a page is "element" or "page" version and records are found attached to it, they might be illegally attached, so this will tell you. [Error: Fix by deleting orphans since they are not registered in "all" category]
335 'misplaced_at_rootlevel' => [],
336 // Subset of "all": Those that should not be at root level but are. [Warning: Fix by moving record into page tree]
337 'misplaced_inside_tree' => []
338 ];
339 // Start traversal:
340 $this->genTree_traverse($rootID, $depth, $echoLevel, $callBack);
341 // Sort recStats (for diff'able displays)
342 foreach ($this->recStats as $kk => $vv) {
343 foreach ($this->recStats[$kk] as $tables => $recArrays) {
344 ksort($this->recStats[$kk][$tables]);
345 }
346 ksort($this->recStats[$kk]);
347 }
348 if ($echoLevel > 0) {
349 echo LF . LF;
350 }
351 }
352
353 /**
354 * Recursive traversal of page tree:
355 *
356 * @param int $rootID Page root id (must be online, valid page record - or zero for page tree root)
357 * @param int $depth Depth
358 * @param int $echoLevel Echo Level
359 * @param string $callBack Call back function (from this class or subclass)
360 * @param bool $versionSwapmode DON'T set from outside, internal. (indicates we are inside a version of a page)
361 * @param int $rootIsVersion DON'T set from outside, internal. (1: Indicates that rootID is a version of a page, 2: ...that it is even a version of a version (which triggers a warning!)
362 * @param string $accumulatedPath Internal string that accumulates the path
363 * @access private
364 */
365 public function genTree_traverse($rootID, $depth, $echoLevel = 0, $callBack = '', $versionSwapmode = false, $rootIsVersion = 0, $accumulatedPath = '')
366 {
367 // Register page:
368 $this->recStats['all']['pages'][$rootID] = $rootID;
369
370 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
371 $queryBuilder->getRestrictions()->removeAll();
372
373 $pageRecord = $queryBuilder->select('deleted', 'title', 't3ver_count', 't3ver_wsid')
374 ->from('pages')
375 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT)))
376 ->execute()
377 ->fetch();
378
379 $accumulatedPath .= '/' . $pageRecord['title'];
380 // Register if page is deleted:
381 if ($pageRecord['deleted']) {
382 $this->recStats['deleted']['pages'][$rootID] = $rootID;
383 }
384 // If rootIsVersion is set it means that the input rootID is that of a version of a page. See below where the recursive call is made.
385 if ($rootIsVersion) {
386 $this->recStats['versions']['pages'][$rootID] = $rootID;
387 // If it has been published and is in archive now...
388 if ($pageRecord['t3ver_count'] >= 1 && $pageRecord['t3ver_wsid'] == 0) {
389 $this->recStats['versions_published']['pages'][$rootID] = $rootID;
390 }
391 // If it has been published and is in archive now...
392 if ($pageRecord['t3ver_wsid'] == 0) {
393 $this->recStats['versions_liveWS']['pages'][$rootID] = $rootID;
394 }
395 // If it doesn't belong to a workspace...
396 if (!isset($this->workspaceIndex[$pageRecord['t3ver_wsid']])) {
397 $this->recStats['versions_lost_workspace']['pages'][$rootID] = $rootID;
398 }
399 // In case the rootID is a version inside a versioned page
400 if ($rootIsVersion == 2) {
401 $this->recStats['versions_inside_versioned_page']['pages'][$rootID] = $rootID;
402 }
403 }
404 if ($echoLevel > 0) {
405 echo LF . $accumulatedPath . ' [' . $rootID . ']' . ($pageRecord['deleted'] ? ' (DELETED)' : '') . ($this->recStats['versions_published']['pages'][$rootID] ? ' (PUBLISHED)' : '');
406 }
407 if ($echoLevel > 1 && $this->recStats['versions_lost_workspace']['pages'][$rootID]) {
408 echo LF . ' ERROR! This version belongs to non-existing workspace (' . $pageRecord['t3ver_wsid'] . ')!';
409 }
410 if ($echoLevel > 1 && $this->recStats['versions_inside_versioned_page']['pages'][$rootID]) {
411 echo LF . ' WARNING! This version is inside an already versioned page or branch!';
412 }
413 // Call back:
414 if ($callBack) {
415 $this->{$callBack}('pages', $rootID, $echoLevel, $versionSwapmode, $rootIsVersion);
416 }
417 // Traverse tables of records that belongs to page:
418 foreach ($GLOBALS['TCA'] as $tableName => $cfg) {
419 if ($tableName !== 'pages') {
420 // Select all records belonging to page:
421 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
422 ->getQueryBuilderForTable($tableName);
423
424 $queryBuilder->getRestrictions()->removeAll();
425
426 $queryBuilder
427 ->select('uid')
428 ->from($tableName)
429 ->where(
430 $queryBuilder->expr()->eq(
431 'pid',
432 $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT)
433 )
434 );
435
436 if ($GLOBALS['TCA'][$tableName]['ctrl']['delete']) {
437 $queryBuilder->addSelect($GLOBALS['TCA'][$tableName]['ctrl']['delete']);
438 }
439
440 if (!$this->genTree_traverseDeleted) {
441 $queryBuilder->getRestrictions()->add(DeletedRestriction::class);
442 }
443
444 $result = $queryBuilder->execute();
445
446 $count = $result->rowCount();
447 if ($count) {
448 if ($echoLevel == 2) {
449 echo LF . ' \\-' . $tableName . ' (' . $count . ')';
450 }
451 }
452 while ($rowSub = $result->fetch()) {
453 if ($echoLevel == 3) {
454 echo LF . ' \\-' . $tableName . ':' . $rowSub['uid'];
455 }
456 // If the rootID represents an "element" or "page" version type, we must check if the record from this table is allowed to belong to this:
457 if ($versionSwapmode) {
458 // This is illegal records under a versioned page - therefore not registered in $this->recStats['all'] so they should be orphaned:
459 $this->recStats['illegal_record_under_versioned_page'][$tableName][$rowSub['uid']] = $rowSub['uid'];
460 if ($echoLevel > 1) {
461 echo LF . ' ERROR! Illegal record (' . $tableName . ':' . $rowSub['uid'] . ') under versioned page!';
462 }
463 } else {
464 $this->recStats['all'][$tableName][$rowSub['uid']] = $rowSub['uid'];
465 // Register deleted:
466 if ($GLOBALS['TCA'][$tableName]['ctrl']['delete'] && $rowSub[$GLOBALS['TCA'][$tableName]['ctrl']['delete']]) {
467 $this->recStats['deleted'][$tableName][$rowSub['uid']] = $rowSub['uid'];
468 if ($echoLevel == 3) {
469 echo ' (DELETED)';
470 }
471 }
472 // Check location of records regarding tree root:
473 if (!$GLOBALS['TCA'][$tableName]['ctrl']['rootLevel'] && $rootID == 0) {
474 $this->recStats['misplaced_at_rootlevel'][$tableName][$rowSub['uid']] = $rowSub['uid'];
475 if ($echoLevel > 1) {
476 echo LF . ' ERROR! Misplaced record (' . $tableName . ':' . $rowSub['uid'] . ') on rootlevel!';
477 }
478 }
479 if ($GLOBALS['TCA'][$tableName]['ctrl']['rootLevel'] == 1 && $rootID > 0) {
480 $this->recStats['misplaced_inside_tree'][$tableName][$rowSub['uid']] = $rowSub['uid'];
481 if ($echoLevel > 1) {
482 echo LF . ' ERROR! Misplaced record (' . $tableName . ':' . $rowSub['uid'] . ') inside page tree!';
483 }
484 }
485 // Traverse plugins:
486 if ($callBack) {
487 $this->{$callBack}($tableName, $rowSub['uid'], $echoLevel, $versionSwapmode, $rootIsVersion);
488 }
489 // Add any versions of those records:
490 if ($this->genTree_traverseVersions) {
491 $versions = BackendUtility::selectVersionsOfRecord($tableName, $rowSub['uid'], 'uid,t3ver_wsid,t3ver_count' . ($GLOBALS['TCA'][$tableName]['ctrl']['delete'] ? ',' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] : ''), null, true);
492 if (is_array($versions)) {
493 foreach ($versions as $verRec) {
494 if (!$verRec['_CURRENT_VERSION']) {
495 if ($echoLevel == 3) {
496 echo LF . ' \\-[#OFFLINE VERSION: WS#' . $verRec['t3ver_wsid'] . '/Cnt:' . $verRec['t3ver_count'] . '] ' . $tableName . ':' . $verRec['uid'] . ')';
497 }
498 $this->recStats['all'][$tableName][$verRec['uid']] = $verRec['uid'];
499 // Register deleted:
500 if ($GLOBALS['TCA'][$tableName]['ctrl']['delete'] && $verRec[$GLOBALS['TCA'][$tableName]['ctrl']['delete']]) {
501 $this->recStats['deleted'][$tableName][$verRec['uid']] = $verRec['uid'];
502 if ($echoLevel == 3) {
503 echo ' (DELETED)';
504 }
505 }
506 // Register version:
507 $this->recStats['versions'][$tableName][$verRec['uid']] = $verRec['uid'];
508 if ($verRec['t3ver_count'] >= 1 && $verRec['t3ver_wsid'] == 0) {
509 // Only register published versions in LIVE workspace (published versions in draft workspaces are allowed)
510 $this->recStats['versions_published'][$tableName][$verRec['uid']] = $verRec['uid'];
511 if ($echoLevel == 3) {
512 echo ' (PUBLISHED)';
513 }
514 }
515 if ($verRec['t3ver_wsid'] == 0) {
516 $this->recStats['versions_liveWS'][$tableName][$verRec['uid']] = $verRec['uid'];
517 }
518 if (!isset($this->workspaceIndex[$verRec['t3ver_wsid']])) {
519 $this->recStats['versions_lost_workspace'][$tableName][$verRec['uid']] = $verRec['uid'];
520 if ($echoLevel > 1) {
521 echo LF . ' ERROR! Version (' . $tableName . ':' . $verRec['uid'] . ') belongs to non-existing workspace (' . $verRec['t3ver_wsid'] . ')!';
522 }
523 }
524 // In case we are inside a versioned branch, there should not exists versions inside that "branch".
525 if ($versionSwapmode) {
526 $this->recStats['versions_inside_versioned_page'][$tableName][$verRec['uid']] = $verRec['uid'];
527 if ($echoLevel > 1) {
528 echo LF . ' ERROR! This version (' . $tableName . ':' . $verRec['uid'] . ') is inside an already versioned page or branch!';
529 }
530 }
531 // Traverse plugins:
532 if ($callBack) {
533 $this->{$callBack}($tableName, $verRec['uid'], $echoLevel, $versionSwapmode, $rootIsVersion);
534 }
535 }
536 }
537 }
538 unset($versions);
539 }
540 }
541 }
542 }
543 }
544 unset($resSub);
545 unset($rowSub);
546 // Find subpages to root ID and traverse (only when rootID is not a version or is a branch-version):
547 if ($depth > 0) {
548 $depth--;
549 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
550 ->getQueryBuilderForTable('pages');
551
552 $queryBuilder->getRestrictions()->removeAll();
553 if (!$this->genTree_traverseDeleted) {
554 $queryBuilder->getRestrictions()->add(DeletedRestriction::class);
555 }
556
557 $queryBuilder
558 ->select('uid')
559 ->from('pages')
560 ->where(
561 $queryBuilder->expr()->eq(
562 'pid',
563 $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT)
564 )
565 )
566 ->orderBy('sorting');
567
568 $result = $queryBuilder->execute();
569 while ($row = $result->fetch()) {
570 $this->genTree_traverse($row['uid'], $depth, $echoLevel, $callBack, $versionSwapmode, 0, $accumulatedPath);
571 }
572 }
573 // Add any versions of pages
574 if ($rootID > 0 && $this->genTree_traverseVersions) {
575 $versions = BackendUtility::selectVersionsOfRecord('pages', $rootID, 'uid,t3ver_oid,t3ver_wsid,t3ver_count', null, true);
576 if (is_array($versions)) {
577 foreach ($versions as $verRec) {
578 if (!$verRec['_CURRENT_VERSION']) {
579 $this->genTree_traverse($verRec['uid'], $depth, $echoLevel, $callBack, true, $versionSwapmode ? 2 : 1, $accumulatedPath . ' [#OFFLINE VERSION: WS#' . $verRec['t3ver_wsid'] . '/Cnt:' . $verRec['t3ver_count'] . ']');
580 }
581 }
582 }
583 }
584 }
585
586 /**************************
587 *
588 * Helper functions
589 *
590 *************************/
591 /**
592 * Compile info-string
593 *
594 * @param array $rec Input record from sys_refindex
595 * @return string String identifying the main record of the reference
596 */
597 public function infoStr($rec)
598 {
599 return $rec['tablename'] . ':' . $rec['recuid'] . ':' . $rec['field'] . ':' . $rec['flexpointer'] . ':' . $rec['softref_key'] . ($rec['deleted'] ? ' (DELETED)' : '');
600 }
601 }