[BUGFIX] Remove NOT NULL from optional text fields in system extensions
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / OrphanRecordsCommand.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 /**
18 * Looking for Orphan Records
19 */
20 class OrphanRecordsCommand extends CleanerCommand
21 {
22 /**
23 * Constructor
24 */
25 public function __construct()
26 {
27 parent::__construct();
28 // Setting up help:
29 $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.');
30 $this->cli_help['name'] = 'orphan_records -- To find records that has lost their connection with the page tree';
31 $this->cli_help['description'] = trim('
32 Assumptions:
33 - That all actively used records on the website from TCA configured tables are located in the page tree exclusively.
34
35 All records managed by TYPO3 via the TCA array configuration has to belong to a page in the page tree, either directly or indirectly as a version of another record.
36 VERY TIME, CPU and MEMORY intensive operation since the full page tree is looked up!
37
38 Automatic Repair of Errors:
39 - Silently deleting the orphaned records. In theory they should not be used anywhere in the system, but there could be references. See below for more details on this matter.
40
41 Manual repair suggestions:
42 - Possibly re-connect orphaned records to page tree by setting their "pid" field to a valid page id. A lookup in the sys_refindex table can reveal if there are references to a orphaned record. If there are such references (from records that are not themselves orphans) you might consider to re-connect the record to the page tree, otherwise it should be safe to delete it.
43 ');
44 $this->cli_help['todo'] = trim('
45 - Implement a check for references to orphaned records and if a reference comes from a record that is not orphaned itself, we might rather like to re-connect the record to the page tree.
46 - Implement that orphans can be fixed by setting the PID to a certain page instead of deleting.');
47 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner orphan_records -s -r
48 Will report orphan uids from TCA tables.';
49 }
50
51 /**
52 * Find orphan records
53 * VERY CPU and memory intensive since it will look up the whole page tree!
54 *
55 * @return array
56 */
57 public function main()
58 {
59 // Initialize result array:
60 $resultArray = array(
61 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
62 'headers' => array(
63 'orphans' => array('Index of orphaned records', '', 3),
64 'misplaced_at_rootlevel' => array('Records that should not be at root level but are.', 'Fix manually by moving record into page tree', 2),
65 'misplaced_inside_tree' => array('Records that should be at root level but are not.', 'Fix manually by moving record to tree root', 2),
66 'illegal_record_under_versioned_page' => array('Records that cannot be attached to a versioned page', '(Listed under orphaned records so is fixed along with orphans.)', 2)
67 ),
68 'orphans' => array(),
69 'misplaced_at_rootlevel' => array(),
70 // Subset of "all": Those that should not be at root level but are. [Warning: Fix by moving record into page tree]
71 'misplaced_inside_tree' => array(),
72 // Subset of "all": Those that are inside page tree but should be at root level [Warning: Fix by setting PID to zero]
73 'illegal_record_under_versioned_page' => array()
74 );
75 // zero = tree root, must use tree root if you wish to reverse selection to find orphans!
76 $startingPoint = 0;
77 $pt = \TYPO3\CMS\Core\Utility\GeneralUtility::milliseconds();
78 $this->genTree($startingPoint, 1000, (int)$this->cli_argValue('--echotree'));
79 $resultArray['misplaced_at_rootlevel'] = $this->recStats['misplaced_at_rootlevel'];
80 $resultArray['misplaced_inside_tree'] = $this->recStats['misplaced_inside_tree'];
81 $resultArray['illegal_record_under_versioned_page'] = $this->recStats['illegal_record_under_versioned_page'];
82 // Find orphans:
83 foreach ($GLOBALS['TCA'] as $tableName => $cfg) {
84 $idList = is_array($this->recStats['all'][$tableName]) && count($this->recStats['all'][$tableName]) ? implode(',', $this->recStats['all'][$tableName]) : 0;
85 // Select all records belonging to page:
86 $orphanRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $tableName, 'uid NOT IN (' . $idList . ')', '', 'uid', '', 'uid');
87 if (count($orphanRecords)) {
88 $resultArray['orphans'][$tableName] = array();
89 foreach ($orphanRecords as $oR) {
90 $resultArray['orphans'][$tableName][$oR['uid']] = $oR['uid'];
91 }
92 }
93 }
94 return $resultArray;
95 }
96
97 /**
98 * Mandatory autofix function
99 * Will run auto-fix on the result array. Echos status during processing.
100 *
101 * @param array $resultArray Result array from main() function
102 * @return void
103 */
104 public function main_autoFix($resultArray)
105 {
106 // Putting "pages" table in the bottom:
107 if (isset($resultArray['orphans']['pages'])) {
108 $_pages = $resultArray['orphans']['pages'];
109 unset($resultArray['orphans']['pages']);
110 $resultArray['orphans']['pages'] = $_pages;
111 }
112 // Traversing records:
113 foreach ($resultArray['orphans'] as $table => $list) {
114 echo 'Removing orphans from table "' . $table . '":' . LF;
115 foreach ($list as $uid) {
116 echo ' Flushing orphan record "' . $table . ':' . $uid . '": ';
117 if ($bypass = $this->cli_noExecutionCheck($table . ':' . $uid)) {
118 echo $bypass;
119 } else {
120 // Execute CMD array:
121 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
122 $tce->stripslashes_values = false;
123 $tce->start(array(), array());
124 // Notice, we are deleting pages with no regard to subpages/subrecords - we do this
125 // since they should also be included in the set of orphans of course!
126 $tce->deleteRecord($table, $uid, true, true);
127 // Return errors if any:
128 if (count($tce->errorLog)) {
129 echo ' ERROR from "TCEmain":' . LF . 'TCEmain:' . implode((LF . 'TCEmain:'), $tce->errorLog);
130 } else {
131 echo 'DONE';
132 }
133 }
134 echo LF;
135 }
136 }
137 }
138 }