[TASK] Merge ext:integrity with ext:lowlevel
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / CleanFlexformCommand.php
1 <?php
2 namespace TYPO3\CMS\Lowlevel;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Cleaner module: cleanflexform
31 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * cleanflexform
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class CleanFlexformCommand extends CleanerCommand {
43
44 /**
45 * Constructor
46 *
47 * @todo Define visibility
48 */
49 public function __construct() {
50 parent::__construct();
51 // Setting up help:
52 $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.');
53 $this->cli_options[] = array('--pid id', 'Setting start page in page tree. Default is the page tree root, 0 (zero)');
54 $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.');
55 $this->cli_help['name'] = 'cleanflexform -- Find flexform fields with unclean XML';
56 $this->cli_help['description'] = trim('
57 Traversing page tree and finding records with FlexForm fields with XML that could be cleaned up. This will just remove obsolete data garbage.
58
59 Automatic Repair:
60 Cleaning XML for FlexForm fields.
61 ');
62 $this->cli_help['examples'] = '';
63 }
64
65 /**
66 * Find orphan records
67 * VERY CPU and memory intensive since it will look up the whole page tree!
68 *
69 * @return array
70 * @todo Define visibility
71 */
72 public function main() {
73 global $TYPO3_DB;
74 // Initialize result array:
75 $resultArray = array(
76 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
77 'headers' => array(
78 'dirty' => array('', '', 2)
79 ),
80 'dirty' => array()
81 );
82 $startingPoint = $this->cli_isArg('--pid') ? \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->cli_argValue('--pid'), 0) : 0;
83 $depth = $this->cli_isArg('--depth') ? \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->cli_argValue('--depth'), 0) : 1000;
84 $this->cleanFlexForm_dirtyFields =& $resultArray['dirty'];
85 // Do not repair flexform data in deleted records.
86 $this->genTree_traverseDeleted = FALSE;
87 $this->genTree($startingPoint, $depth, (int) $this->cli_argValue('--echotree'), 'main_parseTreeCallBack');
88 asort($resultArray);
89 return $resultArray;
90 }
91
92 /**
93 * Call back function for page tree traversal!
94 *
95 * @param string $tableName Table name
96 * @param integer $uid UID of record in processing
97 * @param integer $echoLevel Echo level (see calling function
98 * @param string $versionSwapmode Version swap mode on that level (see calling function
99 * @param integer $rootIsVersion Is root version (see calling function
100 * @return void
101 * @todo Define visibility
102 */
103 public function main_parseTreeCallBack($tableName, $uid, $echoLevel, $versionSwapmode, $rootIsVersion) {
104 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($tableName);
105 foreach ($GLOBALS['TCA'][$tableName]['columns'] as $colName => $config) {
106 if ($config['config']['type'] == 'flex') {
107 if ($echoLevel > 2) {
108 echo LF . ' [cleanflexform:] Field "' . $colName . '" in ' . $tableName . ':' . $uid . ' was a flexform and...';
109 }
110 $recRow = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordRaw($tableName, 'uid=' . intval($uid));
111 $flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
112 if ($recRow[$colName]) {
113 // Clean XML:
114 $newXML = $flexObj->cleanFlexFormXML($tableName, $colName, $recRow);
115 if (md5($recRow[$colName]) != md5($newXML)) {
116 if ($echoLevel > 2) {
117 echo ' was DIRTY, needs cleanup!';
118 }
119 $this->cleanFlexForm_dirtyFields[\TYPO3\CMS\Core\Utility\GeneralUtility::shortMd5($tableName . ':' . $uid . ':' . $colName)] = $tableName . ':' . $uid . ':' . $colName;
120 } else {
121 if ($echoLevel > 2) {
122 echo ' was CLEAN';
123 }
124 }
125 } elseif ($echoLevel > 2) {
126 echo ' was EMPTY';
127 }
128 }
129 }
130 }
131
132 /**
133 * Mandatory autofix function
134 * Will run auto-fix on the result array. Echos status during processing.
135 *
136 * @param array $resultArray Result array from main() function
137 * @return void
138 * @todo Define visibility
139 */
140 public function main_autoFix($resultArray) {
141 foreach ($resultArray['dirty'] as $fieldID) {
142 list($table, $uid, $field) = explode(':', $fieldID);
143 echo 'Cleaning XML in "' . $fieldID . '": ';
144 if ($bypass = $this->cli_noExecutionCheck($fieldID)) {
145 echo $bypass;
146 } else {
147 // Clean XML:
148 $data = array();
149 $recRow = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordRaw($table, 'uid=' . intval($uid));
150 $flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
151 if ($recRow[$field]) {
152 $data[$table][$uid][$field] = $flexObj->cleanFlexFormXML($table, $field, $recRow);
153 }
154 // Execute Data array:
155 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
156 $tce->stripslashes_values = FALSE;
157 $tce->dontProcessTransformations = TRUE;
158 $tce->bypassWorkspaceRestrictions = TRUE;
159 $tce->bypassFileHandling = TRUE;
160 // Check has been done previously that there is a backend user which is Admin and also in live workspace
161 $tce->start($data, array());
162 $tce->process_datamap();
163 // Return errors if any:
164 if (count($tce->errorLog)) {
165 echo ' ERROR from "TCEmain":' . LF . 'TCEmain:' . implode((LF . 'TCEmain:'), $tce->errorLog);
166 } else {
167 echo 'DONE';
168 }
169 }
170 echo LF;
171 }
172 }
173
174 }
175
176
177 ?>