TYPO3 CMS  TYPO3_6-2
DoubleFilesCommand.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'] = 'double_files -- Looking for files from TYPO3 managed records which are referenced more than one time (only one time allowed)';
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 - files found in deleted records are included (otherwise you would see a false list of lost files)
42 
43 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.
44 This tool will expose when such files are referenced from multiple locations which is considered an integrity error.
45 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.
46 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!
47 
48 Automatic Repair of Errors:
49 - The multi-referenced file is copied under a new name and references updated.
50 
51 Manual repair suggestions:
52 - None that can not be handled by the automatic repair.
53 ');
54  $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner double_files -s -r
55 This will check the system for double files relations.';
56  }
57 
66  public function main() {
67  global $TYPO3_DB;
68  // Initialize result array:
69  $resultArray = array(
70  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
71  'headers' => array(
72  'multipleReferencesList_count' => array('Number of multi-reference files', '(See below)', 0),
73  'singleReferencesList_count' => array('Number of files correctly referenced', 'The amount of correct 1-1 references', 0),
74  'multipleReferencesList' => array('Entries with files having multiple references', 'These are serious problems that should be resolved ASAP to prevent data loss! ' . $this->label_infoString, 3),
75  'dirname_registry' => array('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),
76  'missingFiles' => array('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),
77  'warnings' => array('Warnings picked up', '', 2)
78  ),
79  'multipleReferencesList_count' => array('count' => 0),
80  'singleReferencesList_count' => array('count' => 0),
81  'multipleReferencesList' => array(),
82  'dirname_registry' => array(),
83  'missingFiles' => array(),
84  'warnings' => array()
85  );
86  // Select all files in the reference table not found by a soft reference parser (thus TCA configured)
87  $recs = $TYPO3_DB->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex') . ' AND softref_key=' . $TYPO3_DB->fullQuoteStr('', 'sys_refindex'), '', 'sorting DESC');
88  // Traverse the files and put into a large table:
89  $tempCount = array();
90  if (is_array($recs)) {
91  foreach ($recs as $rec) {
92  // Compile info string for location of reference:
93  $infoString = $this->infoStr($rec);
94  // Registering occurencies in directories:
95  $resultArray['dirname_registry'][dirname($rec['ref_string'])][($rec['tablename'] . ':' . $rec['field'])]++;
96  // Handle missing file:
97  if (!@is_file((PATH_site . $rec['ref_string']))) {
98  $resultArray['missingFiles'][$rec['ref_string']][$rec['hash']] = $infoString;
99  ksort($resultArray['missingFiles'][$rec['ref_string']]);
100  }
101  // Add entry if file has multiple references pointing to it:
102  if (isset($tempCount[$rec['ref_string']])) {
103  if (!is_array($resultArray['multipleReferencesList'][$rec['ref_string']])) {
104  $resultArray['multipleReferencesList'][$rec['ref_string']] = array();
105  $resultArray['multipleReferencesList'][$rec['ref_string']][$tempCount[$rec['ref_string']][1]] = $tempCount[$rec['ref_string']][0];
106  }
107  $resultArray['multipleReferencesList'][$rec['ref_string']][$rec['hash']] = $infoString;
108  ksort($resultArray['multipleReferencesList'][$rec['ref_string']]);
109  } else {
110  $tempCount[$rec['ref_string']] = array($infoString, $rec['hash']);
111  }
112  }
113  }
114  ksort($resultArray['missingFiles']);
115  ksort($resultArray['multipleReferencesList']);
116  // Add count for multi-references:
117  $resultArray['multipleReferencesList_count']['count'] = count($resultArray['multipleReferencesList']);
118  $resultArray['singleReferencesList_count']['count'] = count($tempCount) - $resultArray['multipleReferencesList_count']['count'];
119  // Sort dirname registry and add warnings for directories outside uploads/
120  ksort($resultArray['dirname_registry']);
121  foreach ($resultArray['dirname_registry'] as $dir => $temp) {
122  ksort($resultArray['dirname_registry'][$dir]);
123  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($dir, 'uploads/')) {
124  $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));
125  }
126  }
127  return $resultArray;
128  }
129 
138  public function main_autoFix($resultArray) {
139  foreach ($resultArray['multipleReferencesList'] as $key => $value) {
141  if ($absFileName && @is_file($absFileName)) {
142  echo 'Processing file: ' . $key . LF;
143  $c = 0;
144  foreach ($value as $hash => $recReference) {
145  if ($c == 0) {
146  echo ' Keeping ' . $key . ' for record "' . $recReference . '"' . LF;
147  } else {
148  // Create unique name for file:
149  $fileFunc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\BasicFileUtility');
150  $newName = $fileFunc->getUniqueName(basename($key), dirname($absFileName));
151  echo ' Copying ' . $key . ' to ' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($newName) . ' for record "' . $recReference . '": ';
152  if ($bypass = $this->cli_noExecutionCheck($recReference)) {
153  echo $bypass;
154  } else {
156  clearstatcache();
157  if (@is_file($newName)) {
158  $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\ReferenceIndex');
159  $error = $sysRefObj->setReferenceValue($hash, basename($newName));
160  if ($error) {
161  echo ' ERROR: TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
162  die;
163  } else {
164  echo 'DONE';
165  }
166  } else {
167  echo ' ERROR: File "' . $newName . '" was not created!';
168  }
169  }
170  echo LF;
171  }
172  $c++;
173  }
174  } else {
175  echo ' ERROR: File "' . $absFileName . '" was not found!';
176  }
177  }
178  }
179 
180 }
static isFirstPartOfStr($str, $partStr)
die
Definition: index.php:6
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)
static upload_copy_move($source, $destination)