[TASK] Merge ext:integrity with ext:lowlevel
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / LostFilesCommand.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: Lost files
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 * Looking for Lost files
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class LostFilesCommand extends CleanerCommand {
43
44 /**
45 * @todo Define visibility
46 */
47 public $checkRefIndex = TRUE;
48
49 /**
50 * Constructor
51 *
52 * @todo Define visibility
53 */
54 public function __construct() {
55 parent::__construct();
56 $this->cli_options[] = array('--excludePath [path-list]', 'Comma separated list of paths to exclude. Example: "uploads/[path1],uploads/[path2],..."');
57 // Setting up help:
58 $this->cli_help['name'] = 'lost_files -- Looking for files in the uploads/ folder which does not have a reference in TYPO3 managed records.';
59 $this->cli_help['description'] = trim('
60 Assumptions:
61 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
62 - that all contents in the uploads folder are files attached to TCA records and exclusively managed by TCEmain through "group" type fields
63 - exceptions are: index.html and .htaccess files (ignored)
64 - exceptions are: RTEmagic* image files (ignored)
65 - files found in deleted records are included (otherwise you would see a false list of lost files)
66
67 The assumptions are not requirements by the TYPO3 API but reflects the de facto implementation of most TYPO3 installations and therefore a practical approach to cleaning up the uploads/ folder.
68 Therefore, if all "group" type fields in TCA and flexforms are positioned inside the uploads/ folder and if no files inside are managed manually it should be safe to clean out files with no relations found in the system.
69 Under such circumstances there should theoretically be no lost files in the uploads/ folder since TCEmain should have managed relations automatically including adding and deleting files.
70 However, there is at least one reason known to why files might be found lost and that is when FlexForms are used. In such a case a change of/in the Data Structure XML (or the ability of the system to find the Data Structure definition!) used for the flexform could leave lost files behind. This is not unlikely to happen when records are deleted. More details can be found in a note to the function TYPO3\\CMS\\Backend\\Utility\\BackendUtility::getFlexFormDS()
71 Another scenario could of course be de-installation of extensions which managed files in the uploads/ folders.
72
73 Automatic Repair of Errors:
74 - Simply delete lost files (Warning: First, make sure those files are not used somewhere TYPO3 does not know about! See the assumptions above).
75 ');
76 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner lost_files -s -r
77 Will report lost files.';
78 }
79
80 /**
81 * Find lost files in uploads/ folder
82 * FIX METHOD: Simply delete the file...
83 *
84 * TODO: Add parameter to exclude filepath
85 * TODO: Add parameter to list more file names/patterns to ignore
86 * TODO: Add parameter to include RTEmagic images
87 *
88 * @return array
89 * @todo Define visibility
90 */
91 public function main() {
92 global $TYPO3_DB;
93 // Initialize result array:
94 $resultArray = array(
95 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
96 'headers' => array(
97 'managedFiles' => array('Files related to TYPO3 records and managed by TCEmain', 'These files you definitely want to keep.', 0),
98 'ignoredFiles' => array('Ignored files (index.html, .htaccess etc.)', 'These files are allowed in uploads/ folder', 0),
99 'RTEmagicFiles' => array('RTE magic images - those found (and ignored)', 'These files are also allowed in some uploads/ folders as RTEmagic images.', 0),
100 'lostFiles' => array('Lost files - those you can delete', 'You can delete these files!', 3),
101 'warnings' => array('Warnings picked up', '', 2)
102 ),
103 'managedFiles' => array(),
104 'ignoredFiles' => array(),
105 'RTEmagicFiles' => array(),
106 'lostFiles' => array(),
107 'warnings' => array()
108 );
109 // Get all files:
110 $fileArr = array();
111 $fileArr = \TYPO3\CMS\Core\Utility\GeneralUtility::getAllFilesAndFoldersInPath($fileArr, PATH_site . 'uploads/');
112 $fileArr = \TYPO3\CMS\Core\Utility\GeneralUtility::removePrefixPathFromList($fileArr, PATH_site);
113 $excludePaths = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->cli_argValue('--excludePath', 0), 1);
114 // Traverse files and for each, look up if its found in the reference index.
115 foreach ($fileArr as $key => $value) {
116 $include = TRUE;
117 foreach ($excludePaths as $exclPath) {
118 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($value, $exclPath)) {
119 $include = FALSE;
120 }
121 }
122 $shortKey = \TYPO3\CMS\Core\Utility\GeneralUtility::shortmd5($value);
123 if ($include) {
124 // First, allow "index.html", ".htaccess" files since they are often used for good reasons
125 if (substr($value, -11) == '/index.html' || substr($value, -10) == '/.htaccess') {
126 unset($fileArr[$key]);
127 $resultArray['ignoredFiles'][$shortKey] = $value;
128 } else {
129 // Looking for a reference from a field which is NOT a soft reference (thus, only fields with a proper TCA/Flexform configuration)
130 $recs = $TYPO3_DB->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex') . ' AND ref_string=' . $TYPO3_DB->fullQuoteStr($value, 'sys_refindex') . ' AND softref_key=' . $TYPO3_DB->fullQuoteStr('', 'sys_refindex'), '', 'sorting DESC');
131 // If found, unset entry:
132 if (count($recs)) {
133 unset($fileArr[$key]);
134 $resultArray['managedFiles'][$shortKey] = $value;
135 if (count($recs) > 1) {
136 $resultArray['warnings'][$shortKey] = 'Warning: File "' . $value . '" had ' . count($recs) . ' references from group-fields, should have only one!';
137 }
138 } else {
139 // When here it means the file was not found. So we test if it has a RTEmagic-image name and if so, we allow it:
140 if (preg_match('/^RTEmagic[P|C]_/', basename($value))) {
141 unset($fileArr[$key]);
142 $resultArray['RTEmagicFiles'][$shortKey] = $value;
143 } else {
144 // We conclude that the file is lost...:
145 unset($fileArr[$key]);
146 $resultArray['lostFiles'][$shortKey] = $value;
147 }
148 }
149 }
150 }
151 }
152 asort($resultArray['ignoredFiles']);
153 asort($resultArray['managedFiles']);
154 asort($resultArray['RTEmagicFiles']);
155 asort($resultArray['lostFiles']);
156 asort($resultArray['warnings']);
157 // $fileArr variable should now be empty with all contents transferred to the result array keys.
158 return $resultArray;
159 }
160
161 /**
162 * Mandatory autofix function
163 * Will run auto-fix on the result array. Echos status during processing.
164 *
165 * @param array $resultArray Result array from main() function
166 * @return void
167 * @todo Define visibility
168 */
169 public function main_autoFix($resultArray) {
170 foreach ($resultArray['lostFiles'] as $key => $value) {
171 $absFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($value);
172 echo 'Deleting file: "' . $absFileName . '": ';
173 if ($bypass = $this->cli_noExecutionCheck($absFileName)) {
174 echo $bypass;
175 } else {
176 if ($absFileName && @is_file($absFileName)) {
177 unlink($absFileName);
178 echo 'DONE';
179 } else {
180 echo ' ERROR: File "' . $absFileName . '" was not found!';
181 }
182 }
183 echo LF;
184 }
185 }
186
187 }
188
189
190 ?>