TYPO3 CMS  TYPO3_7-6
DoubleFilesCommand.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 
21 {
25  public $checkRefIndex = true;
26 
30  public function __construct()
31  {
32  parent::__construct();
33  // Setting up help:
34  $this->cli_help['name'] = 'double_files -- Looking for files from TYPO3 managed records which are referenced more than one time (only one time allowed)';
35  $this->cli_help['description'] = trim('
36 Assumptions:
37 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
38 - files found in deleted records are included (otherwise you would see a false list of lost files)
39 
40 Files attached to records in TYPO3 using a "group" type configuration in TCA or FlexForm DataStructure are managed exclusively by the system and there must always exist a 1-1 reference between the file and the reference in the record.
41 This tool will expose when such files are referenced from multiple locations which is considered an integrity error.
42 If a multi-reference is found it was typically created because the record was copied or modified outside of TCEmain which will otherwise maintain the relations correctly.
43 Multi-references should be resolved to 1-1 references as soon as possible. The danger of keeping multi-references is that if the file is removed from one of the refering records it will actually be deleted in the file system, leaving missing files for the remaining referers!
44 
45 Automatic Repair of Errors:
46 - The multi-referenced file is copied under a new name and references updated.
47 
48 Manual repair suggestions:
49 - None that can not be handled by the automatic repair.
50 ');
51  $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner double_files -s -r
52 This will check the system for double files relations.';
53  }
54 
62  public function main()
63  {
64  // Initialize result array:
65  $resultArray = [
66  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
67  'headers' => [
68  'multipleReferencesList_count' => ['Number of multi-reference files', '(See below)', 0],
69  'singleReferencesList_count' => ['Number of files correctly referenced', 'The amount of correct 1-1 references', 0],
70  'multipleReferencesList' => ['Entries with files having multiple references', 'These are serious problems that should be resolved ASAP to prevent data loss! ' . $this->label_infoString, 3],
71  'dirname_registry' => ['Registry of directories in which files are found.', 'Registry includes which table/field pairs store files in them plus how many files their store.', 0],
72  'missingFiles' => ['Tracking missing files', '(Extra feature, not related to tracking of double references. Further, the list may include more files than found in the missing_files()-test because this list includes missing files from deleted records.)', 0],
73  'warnings' => ['Warnings picked up', '', 2]
74  ],
75  'multipleReferencesList_count' => ['count' => 0],
76  'singleReferencesList_count' => ['count' => 0],
77  'multipleReferencesList' => [],
78  'dirname_registry' => [],
79  'missingFiles' => [],
80  'warnings' => []
81  ];
82  // Select all files in the reference table not found by a soft reference parser (thus TCA configured)
83  $recs = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('_FILE', 'sys_refindex') . ' AND softref_key=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('', 'sys_refindex'), '', 'sorting DESC');
84  // Traverse the files and put into a large table:
85  $tempCount = [];
86  if (is_array($recs)) {
87  foreach ($recs as $rec) {
88  // Compile info string for location of reference:
89  $infoString = $this->infoStr($rec);
90  // Registering occurencies in directories:
91  $resultArray['dirname_registry'][dirname($rec['ref_string'])][$rec['tablename'] . ':' . $rec['field']]++;
92  // Handle missing file:
93  if (!@is_file((PATH_site . $rec['ref_string']))) {
94  $resultArray['missingFiles'][$rec['ref_string']][$rec['hash']] = $infoString;
95  ksort($resultArray['missingFiles'][$rec['ref_string']]);
96  }
97  // Add entry if file has multiple references pointing to it:
98  if (isset($tempCount[$rec['ref_string']])) {
99  if (!is_array($resultArray['multipleReferencesList'][$rec['ref_string']])) {
100  $resultArray['multipleReferencesList'][$rec['ref_string']] = [];
101  $resultArray['multipleReferencesList'][$rec['ref_string']][$tempCount[$rec['ref_string']][1]] = $tempCount[$rec['ref_string']][0];
102  }
103  $resultArray['multipleReferencesList'][$rec['ref_string']][$rec['hash']] = $infoString;
104  ksort($resultArray['multipleReferencesList'][$rec['ref_string']]);
105  } else {
106  $tempCount[$rec['ref_string']] = [$infoString, $rec['hash']];
107  }
108  }
109  }
110  ksort($resultArray['missingFiles']);
111  ksort($resultArray['multipleReferencesList']);
112  // Add count for multi-references:
113  $resultArray['multipleReferencesList_count']['count'] = count($resultArray['multipleReferencesList']);
114  $resultArray['singleReferencesList_count']['count'] = count($tempCount) - $resultArray['multipleReferencesList_count']['count'];
115  // Sort dirname registry and add warnings for directories outside uploads/
116  ksort($resultArray['dirname_registry']);
117  foreach ($resultArray['dirname_registry'] as $dir => $temp) {
118  ksort($resultArray['dirname_registry'][$dir]);
119  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($dir, 'uploads/')) {
120  $resultArray['warnings'][\TYPO3\CMS\Core\Utility\GeneralUtility::shortmd5($dir)] = 'Directory "' . $dir . '" was outside uploads/ which is unusual practice in TYPO3 although not forbidden. Directory used by the following table:field pairs: ' . implode(',', array_keys($temp));
121  }
122  }
123  return $resultArray;
124  }
125 
133  public function main_autoFix($resultArray)
134  {
135  foreach ($resultArray['multipleReferencesList'] as $key => $value) {
137  if ($absFileName && @is_file($absFileName)) {
138  echo 'Processing file: ' . $key . LF;
139  $c = 0;
140  foreach ($value as $hash => $recReference) {
141  if ($c == 0) {
142  echo ' Keeping ' . $key . ' for record "' . $recReference . '"' . LF;
143  } else {
144  // Create unique name for file:
145  $fileFunc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\File\BasicFileUtility::class);
146  $newName = $fileFunc->getUniqueName(basename($key), dirname($absFileName));
147  echo ' Copying ' . $key . ' to ' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($newName) . ' for record "' . $recReference . '": ';
148  if ($bypass = $this->cli_noExecutionCheck($recReference)) {
149  echo $bypass;
150  } else {
152  clearstatcache();
153  if (@is_file($newName)) {
154  $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
155  $error = $sysRefObj->setReferenceValue($hash, basename($newName));
156  if ($error) {
157  echo ' ERROR: TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
158  die;
159  } else {
160  echo 'DONE';
161  }
162  } else {
163  echo ' ERROR: File "' . $newName . '" was not created!';
164  }
165  }
166  echo LF;
167  }
168  $c++;
169  }
170  } else {
171  echo ' ERROR: File "' . $absFileName . '" was not found!';
172  }
173  }
174  }
175 }
static isFirstPartOfStr($str, $partStr)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static upload_copy_move($source, $destination)