[TASK] Clean up phpDoc comments in typo3/sysext/
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / class.rte_images.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Cleaner module: RTE magicc images
29 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
30 *
31 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
32 */
33
34 /**
35 * Looking for RTE images integrity
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 * @package TYPO3
39 * @subpackage tx_lowlevel
40 */
41 class tx_lowlevel_rte_images extends tx_lowlevel_cleaner_core {
42
43 var $checkRefIndex = TRUE;
44
45 /**
46 * Constructor
47 */
48 function __construct() {
49 parent::__construct();
50
51 // Setting up help:
52 $this->cli_help['name'] = 'rte_images -- Looking up all occurencies of RTEmagic images in the database and check existence of parent and copy files on the file system plus report possibly lost files of this type.';
53 $this->cli_help['description'] = trim('
54 Assumptions:
55 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
56 - that all RTEmagic image files in the database are registered with the soft reference parser "images"
57 - images found in deleted records are included (means that you might find lost RTEmagic images after flushing deleted records)
58
59 The assumptions are not requirements by the TYPO3 API but reflects the de facto implementation of most TYPO3 installations.
60 However, many custom fields using an RTE will probably not have the "images" soft reference parser registered and so the index will be incomplete and not listing all RTEmagic image files.
61 The consequence of this limitation is that you should be careful if you wish to delete lost RTEmagic images - they could be referenced from a field not parsed by the "images" soft reference parser!
62
63 Automatic Repair of Errors:
64 - Will search for double-usages of RTEmagic images and make copies as required.
65 - Lost files can be deleted automatically by setting the value "lostFiles" as an optional parameter to --AUTOFIX, but otherwise delete them manually if you do not recognize them as used somewhere the system does not know about.
66
67 Manual repair suggestions:
68 - Missing files: Re-insert missing files or edit record where the reference is found.
69 ');
70
71 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner rte_images -s -r
72 Reports problems with RTE images';
73 }
74
75 /**
76 * Analyse situation with RTE magic images. (still to define what the most useful output is).
77 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (we could copy the files) or remove reference
78 *
79 * @return array
80 */
81 function main() {
82 global $TYPO3_DB;
83
84 // Initialize result array:
85 $resultArray = array(
86 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
87 'headers' => array(
88 'completeFileList' => array('Complete list of used RTEmagic files', 'Both parent and copy are listed here including usage count (which should in theory all be "1"). This list does not exclude files that might be missing.', 1),
89 'RTEmagicFilePairs' => array('Statistical info about RTEmagic files', '(copy used as index)', 0),
90 'doubleFiles' => array('Duplicate RTEmagic image files', 'These files are RTEmagic images found used in multiple records! RTEmagic images should be used by only one record at a time. A large amount of such images probably stems from previous versions of TYPO3 (before 4.2) which did not support making copies automatically of RTEmagic images in case of new copies / versions.', 3),
91 'missingFiles' => array('Missing RTEmagic image files', 'These files are not found in the file system! Should be corrected!', 3),
92 'lostFiles' => array('Lost RTEmagic files from uploads/', 'These files you might be able to delete but only if _all_ RTEmagic images are found by the soft reference parser. If you are using the RTE in third-party extensions it is likely that the soft reference parser is not applied correctly to their RTE and thus these "lost" files actually represent valid RTEmagic images, just not registered. Lost files can be auto-fixed but only if you specifically set "lostFiles" as parameter to the --AUTOFIX option.', 2),
93 ),
94 'RTEmagicFilePairs' => array(),
95 'doubleFiles' => array(),
96 'completeFileList' => array(),
97 'missingFiles' => array(),
98 'lostFiles' => array(),
99 );
100
101 // Select all RTEmagic files in the reference table (only from soft references of course)
102 $recs = $TYPO3_DB->exec_SELECTgetRows(
103 '*',
104 'sys_refindex',
105 'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex').
106 ' AND ref_string LIKE '.$TYPO3_DB->fullQuoteStr('%/RTEmagic%', 'sys_refindex').
107 ' AND softref_key='.$TYPO3_DB->fullQuoteStr('images', 'sys_refindex'),
108 '',
109 'sorting DESC'
110 );
111
112 // Traverse the files and put into a large table:
113 if (is_array($recs)) {
114 foreach ($recs as $rec) {
115 $filename = basename($rec['ref_string']);
116 if (t3lib_div::isFirstPartOfStr($filename, 'RTEmagicC_')) {
117 $original = 'RTEmagicP_'.preg_replace('/\.[[:alnum:]]+$/', '', substr($filename, 10));
118 $infoString = $this->infoStr($rec);
119
120 // Build index:
121 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists'] = @is_file(PATH_site.$rec['ref_string']);
122 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original'] = substr($rec['ref_string'], 0, -strlen($filename)).$original;
123 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists'] = @is_file(PATH_site.$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']);
124 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['count']++;
125 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'][$rec['hash']] = $infoString;
126
127 $resultArray['completeFileList'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']]++;
128 $resultArray['completeFileList'][$rec['ref_string']]++;
129
130 // Missing files:
131 if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists']) {
132 $resultArray['missingFiles'][$rec['ref_string']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
133 }
134 if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists']) {
135 $resultArray['missingFiles'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
136 }
137 }
138 }
139
140 // Searching for duplicates:
141 foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
142 if ($fileInfo['count']>1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
143 $resultArray['doubleFiles'][$fileName] = $fileInfo['usedIn'];
144 }
145 }
146 }
147
148 // Now, ask for RTEmagic files inside uploads/ folder:
149 $cleanerModules = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules'];
150 $cleanerMode = t3lib_div::getUserObj($cleanerModules['lost_files'][0]);
151 $resLostFiles = $cleanerMode->main(array(), FALSE, TRUE);
152 if (is_array($resLostFiles['RTEmagicFiles'])) {
153 foreach ($resLostFiles['RTEmagicFiles'] as $fileName) {
154 if (!isset($resultArray['completeFileList'][$fileName])) {
155 $resultArray['lostFiles'][$fileName] = $fileName;
156 }
157 }
158 }
159
160
161 ksort($resultArray['RTEmagicFilePairs']);
162 ksort($resultArray['completeFileList']);
163 ksort($resultArray['missingFiles']);
164 ksort($resultArray['doubleFiles']);
165 ksort($resultArray['lostFiles']);
166
167 return $resultArray;
168 }
169
170 /**
171 * Mandatory autofix function
172 * Will run auto-fix on the result array. Echos status during processing.
173 *
174 * @param array $resultArray Result array from main() function
175 * @return void
176 */
177 function main_autoFix($resultArray) {
178
179 $limitTo = $this->cli_args['--AUTOFIX'][0];
180
181 if (is_array($resultArray['doubleFiles'])) {
182 if (!$limitTo || $limitTo==='doubleFiles') {
183
184 echo 'FIXING double-usages of RTE files in uploads/: '.LF;
185 foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
186
187 // Only fix something if there is a usage count of more than 1 plus if both original and copy exists:
188 if ($fileInfo['count']>1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
189
190 // Traverse all records using the file:
191 $c = 0;
192 foreach ($fileInfo['usedIn'] as $hash => $recordID) {
193 if ($c == 0) {
194 echo ' Keeping file '.$fileName.' for record '.$recordID.LF;
195 } else {
196 // CODE below is adapted from "class.tx_impexp.php" where there is support for duplication of RTE images:
197 echo ' Copying file '.basename($fileName).' for record '.$recordID.' ';
198
199 // Initialize; Get directory prefix for file and set the original name:
200 $dirPrefix = dirname($fileName).'/';
201 $rteOrigName = basename($fileInfo['original']);
202
203 // If filename looks like an RTE file, and the directory is in "uploads/", then process as a RTE file!
204 if ($rteOrigName && t3lib_div::isFirstPartOfStr($dirPrefix, 'uploads/') && @is_dir(PATH_site.$dirPrefix)) { // RTE:
205
206 // From the "original" RTE filename, produce a new "original" destination filename which is unused.
207 $fileProcObj = $this->getFileProcObj();
208 $origDestName = $fileProcObj->getUniqueName($rteOrigName, PATH_site.$dirPrefix);
209
210 // Create copy file name:
211 $pI = pathinfo($fileName);
212 $copyDestName = dirname($origDestName).'/RTEmagicC_'.substr(basename($origDestName), 10).'.'.$pI['extension'];
213 if (!@is_file($copyDestName) && !@is_file($origDestName)
214 && $origDestName===t3lib_div::getFileAbsFileName($origDestName) && $copyDestName===t3lib_div::getFileAbsFileName($copyDestName)) {
215
216 echo ' to '.basename($copyDestName);
217
218 if ($bypass = $this->cli_noExecutionCheck($fileName)) {
219 echo $bypass;
220 } else {
221 // Making copies:
222 t3lib_div::upload_copy_move(PATH_site.$fileInfo['original'], $origDestName);
223 t3lib_div::upload_copy_move(PATH_site.$fileName, $copyDestName);
224 clearstatcache();
225
226 if (@is_file($copyDestName)) {
227 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
228 $error = $sysRefObj->setReferenceValue($hash, substr($copyDestName, strlen(PATH_site)));
229 if ($error) {
230 echo ' - ERROR: t3lib_refindex::setReferenceValue(): '.$error.LF;
231 exit;
232 } else echo " - DONE";
233 } else {
234 echo ' - ERROR: File "'.$copyDestName.'" was not created!';
235 }
236 }
237 } else echo ' - ERROR: Could not construct new unique names for file!';
238 } else echo ' - ERROR: Maybe directory of file was not within "uploads/"?';
239 echo LF;
240 }
241 $c++;
242 }
243 }
244 }
245 } else echo 'Bypassing fixing of double-usages since --AUTOFIX was not "doubleFiles"'.LF;
246 }
247
248
249 if (is_array($resultArray['lostFiles'])) {
250 if ($limitTo === 'lostFiles') {
251 echo 'Removing lost RTEmagic files from folders inside uploads/: '.LF;
252
253 foreach ($resultArray['lostFiles'] as $key => $value) {
254 $absFileName = t3lib_div::getFileAbsFileName($value);
255 echo 'Deleting file: "'.$absFileName.'": ';
256 if ($bypass = $this->cli_noExecutionCheck($absFileName)) {
257 echo $bypass;
258 } else {
259 if ($absFileName && @is_file($absFileName)) {
260 unlink($absFileName);
261 echo 'DONE';
262 } else {
263 echo ' ERROR: File "'.$absFileName.'" was not found!';
264 }
265 }
266 echo LF;
267 }
268 }
269 } else echo 'Bypassing fixing of double-usages since --AUTOFIX was not "lostFiles"'.LF;
270 }
271
272 /**
273 * Returns file processing object, initialized only once.
274 *
275 * @return object File processor object
276 */
277 function getFileProcObj() {
278 if (!is_object($this->fileProcObj)) {
279 $this->fileProcObj = t3lib_div::makeInstance('t3lib_extFileFunctions');
280 $this->fileProcObj->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
281 $this->fileProcObj->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
282 }
283 return $this->fileProcObj;
284 }
285 }
286
287 ?>