TYPO3  7.6
RteImagesCommand.php
Go to the documentation of this file.
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 
18 
23 {
27  public $checkRefIndex = true;
28 
32  protected $fileProcObj = null;
33 
37  public function __construct()
38  {
39  parent::__construct();
40  // Setting up help:
41  $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.';
42  $this->cli_help['description'] = trim('
43 Assumptions:
44 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
45 - that all RTEmagic image files in the database are registered with the soft reference parser "images"
46 - images found in deleted records are included (means that you might find lost RTEmagic images after flushing deleted records)
47 
48 The assumptions are not requirements by the TYPO3 API but reflects the de facto implementation of most TYPO3 installations.
49 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.
50 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!
51 
52 Automatic Repair of Errors:
53 - Will search for double-usages of RTEmagic images and make copies as required.
54 - 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.
55 
56 Manual repair suggestions:
57 - Missing files: Re-insert missing files or edit record where the reference is found.
58 ');
59  $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner rte_images -s -r
60 Reports problems with RTE images';
61  }
62 
70  public function main()
71  {
72  // Initialize result array:
73  $resultArray = array(
74  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
75  'headers' => array(
76  '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),
77  'RTEmagicFilePairs' => array('Statistical info about RTEmagic files', '(copy used as index)', 0),
78  '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),
79  'missingFiles' => array('Missing RTEmagic image files', 'These files are not found in the file system! Should be corrected!', 3),
80  '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)
81  ),
82  'RTEmagicFilePairs' => array(),
83  'doubleFiles' => array(),
84  'completeFileList' => array(),
85  'missingFiles' => array(),
86  'lostFiles' => array()
87  );
88  // Select all RTEmagic files in the reference table (only from soft references of course)
89  $recs = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('_FILE', 'sys_refindex') . ' AND ref_string LIKE ' . $GLOBALS['TYPO3_DB']->fullQuoteStr('%/RTEmagic%', 'sys_refindex') . ' AND softref_key=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('images', 'sys_refindex'), '', 'sorting DESC');
90  // Traverse the files and put into a large table:
91  if (is_array($recs)) {
92  foreach ($recs as $rec) {
93  $filename = basename($rec['ref_string']);
94  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($filename, 'RTEmagicC_')) {
95  $original = 'RTEmagicP_' . preg_replace('/\\.[[:alnum:]]+$/', '', substr($filename, 10));
96  $infoString = $this->infoStr($rec);
97  // Build index:
98  $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists'] = @is_file((PATH_site . $rec['ref_string']));
99  $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original'] = substr($rec['ref_string'], 0, -strlen($filename)) . $original;
100  $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists'] = @is_file((PATH_site . $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']));
101  $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['count']++;
102  $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'][$rec['hash']] = $infoString;
103  $resultArray['completeFileList'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']]++;
104  $resultArray['completeFileList'][$rec['ref_string']]++;
105  // Missing files:
106  if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists']) {
107  $resultArray['missingFiles'][$rec['ref_string']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
108  }
109  if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists']) {
110  $resultArray['missingFiles'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
111  }
112  }
113  }
114  // Searching for duplicates:
115  foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
116  if ($fileInfo['count'] > 1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
117  $resultArray['doubleFiles'][$fileName] = $fileInfo['usedIn'];
118  }
119  }
120  }
121  // Now, ask for RTEmagic files inside uploads/ folder:
122  $cleanerModules = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules'];
123  $cleanerMode = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($cleanerModules['lost_files'][0]);
124  $resLostFiles = $cleanerMode->main(array(), false, true);
125  if (is_array($resLostFiles['RTEmagicFiles'])) {
126  foreach ($resLostFiles['RTEmagicFiles'] as $fileName) {
127  if (!isset($resultArray['completeFileList'][$fileName])) {
128  $resultArray['lostFiles'][$fileName] = $fileName;
129  }
130  }
131  }
132  ksort($resultArray['RTEmagicFilePairs']);
133  ksort($resultArray['completeFileList']);
134  ksort($resultArray['missingFiles']);
135  ksort($resultArray['doubleFiles']);
136  ksort($resultArray['lostFiles']);
137  return $resultArray;
138  }
139 
147  public function main_autoFix($resultArray)
148  {
149  $limitTo = $this->cli_args['--AUTOFIX'][0];
150  if (is_array($resultArray['doubleFiles'])) {
151  if (!$limitTo || $limitTo === 'doubleFiles') {
152  echo 'FIXING double-usages of RTE files in uploads/: ' . LF;
153  foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
154  // Only fix something if there is a usage count of more than 1 plus if both original and copy exists:
155  if ($fileInfo['count'] > 1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
156  // Traverse all records using the file:
157  $c = 0;
158  foreach ($fileInfo['usedIn'] as $hash => $recordID) {
159  if ($c == 0) {
160  echo ' Keeping file ' . $fileName . ' for record ' . $recordID . LF;
161  } else {
162  // CODE below is adapted from \TYPO3\CMS\Impexp\ImportExport where there is support for duplication of RTE images:
163  echo ' Copying file ' . basename($fileName) . ' for record ' . $recordID . ' ';
164  // Initialize; Get directory prefix for file and set the original name:
165  $dirPrefix = dirname($fileName) . '/';
166  $rteOrigName = basename($fileInfo['original']);
167  // If filename looks like an RTE file, and the directory is in "uploads/", then process as a RTE file!
168  if ($rteOrigName && \TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($dirPrefix, 'uploads/') && @is_dir((PATH_site . $dirPrefix))) {
169  // RTE:
170  // From the "original" RTE filename, produce a new "original" destination filename which is unused.
171  $fileProcObj = $this->getFileProcObj();
172  $origDestName = $fileProcObj->getUniqueName($rteOrigName, PATH_site . $dirPrefix);
173  // Create copy file name:
174  $pI = pathinfo($fileName);
175  $copyDestName = dirname($origDestName) . '/RTEmagicC_' . substr(basename($origDestName), 10) . '.' . $pI['extension'];
176  if (!@is_file($copyDestName) && !@is_file($origDestName) && $origDestName === \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($origDestName) && $copyDestName === \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($copyDestName)) {
177  echo ' to ' . basename($copyDestName);
178  if ($bypass = $this->cli_noExecutionCheck($fileName)) {
179  echo $bypass;
180  } else {
181  // Making copies:
182  \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . $fileInfo['original'], $origDestName);
183  \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . $fileName, $copyDestName);
184  clearstatcache();
185  if (@is_file($copyDestName)) {
186  $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
187  $error = $sysRefObj->setReferenceValue($hash, \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($copyDestName));
188  if ($error) {
189  echo ' - ERROR: TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
190  die;
191  } else {
192  echo ' - DONE';
193  }
194  } else {
195  echo ' - ERROR: File "' . $copyDestName . '" was not created!';
196  }
197  }
198  } else {
199  echo ' - ERROR: Could not construct new unique names for file!';
200  }
201  } else {
202  echo ' - ERROR: Maybe directory of file was not within "uploads/"?';
203  }
204  echo LF;
205  }
206  $c++;
207  }
208  }
209  }
210  } else {
211  echo 'Bypassing fixing of double-usages since --AUTOFIX was not "doubleFiles"' . LF;
212  }
213  }
214  if (is_array($resultArray['lostFiles'])) {
215  if ($limitTo === 'lostFiles') {
216  echo 'Removing lost RTEmagic files from folders inside uploads/: ' . LF;
217  foreach ($resultArray['lostFiles'] as $key => $value) {
218  $absFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($value);
219  echo 'Deleting file: "' . $absFileName . '": ';
220  if ($bypass = $this->cli_noExecutionCheck($absFileName)) {
221  echo $bypass;
222  } else {
223  if ($absFileName && @is_file($absFileName)) {
224  unlink($absFileName);
225  echo 'DONE';
226  } else {
227  echo ' ERROR: File "' . $absFileName . '" was not found!';
228  }
229  }
230  echo LF;
231  }
232  }
233  } else {
234  echo 'Bypassing fixing of double-usages since --AUTOFIX was not "lostFiles"' . LF;
235  }
236  }
237 
243  public function getFileProcObj()
244  {
245  if (!is_object($this->fileProcObj)) {
246  $this->fileProcObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ExtendedFileUtility::class);
247  $this->fileProcObj->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
248  $this->fileProcObj->setActionPermissions();
249  }
250  return $this->fileProcObj;
251  }
252 }