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