2 namespace TYPO3\CMS\Lowlevel;
32 parent::__construct();
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(
'
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)
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!
45 Automatic Repair of Errors:
46 - The multi-referenced file is copied under a new name and references updated.
48 Manual repair suggestions:
49 - None that can not be handled by the automatic repair.
51 $this->
cli_help[
'examples'] =
'/.../cli_dispatch.phpsh lowlevel_cleaner double_files -s -r
52 This will check the system for double files relations.';
66 'message' => $this->
cli_help[
'name'] . LF . LF . $this->
cli_help[
'description'],
68 'multipleReferencesList_count' => array(
'Number of multi-reference files',
'(See below)', 0),
69 'singleReferencesList_count' => array(
'Number of files correctly referenced',
'The amount of correct 1-1 references', 0),
70 '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),
71 '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),
72 '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),
73 'warnings' => array(
'Warnings picked up',
'', 2)
75 'multipleReferencesList_count' => array(
'count' => 0),
76 'singleReferencesList_count' => array(
'count' => 0),
77 'multipleReferencesList' => array(),
78 'dirname_registry' => array(),
79 'missingFiles' => array(),
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');
86 if (is_array($recs)) {
87 foreach ($recs as $rec) {
89 $infoString = $this->
infoStr($rec);
91 $resultArray[
'dirname_registry'][dirname($rec[
'ref_string'])][($rec[
'tablename'] .
':' . $rec[
'field'])]++;
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']]);
98 if (isset($tempCount[$rec[
'ref_string']])) {
99 if (!is_array($resultArray[
'multipleReferencesList'][$rec[
'ref_string']])) {
100 $resultArray[
'multipleReferencesList'][$rec[
'ref_string']] = array();
101 $resultArray[
'multipleReferencesList'][$rec[
'ref_string']][$tempCount[$rec[
'ref_string']][1]] = $tempCount[$rec[
'ref_string']][0];
103 $resultArray[
'multipleReferencesList'][$rec[
'ref_string']][$rec[
'hash']] = $infoString;
104 ksort($resultArray[
'multipleReferencesList'][$rec[
'ref_string']]);
106 $tempCount[$rec[
'ref_string']] = array($infoString, $rec[
'hash']);
110 ksort($resultArray[
'missingFiles']);
111 ksort($resultArray[
'multipleReferencesList']);
113 $resultArray[
'multipleReferencesList_count'][
'count'] = count($resultArray[
'multipleReferencesList']);
114 $resultArray[
'singleReferencesList_count'][
'count'] = count($tempCount) - $resultArray[
'multipleReferencesList_count'][
'count'];
116 ksort($resultArray[
'dirname_registry']);
117 foreach ($resultArray[
'dirname_registry'] as $dir => $temp) {
118 ksort($resultArray[
'dirname_registry'][$dir]);
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));
135 foreach ($resultArray[
'multipleReferencesList'] as $key => $value) {
136 $absFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($key);
137 if ($absFileName && @is_file($absFileName)) {
138 echo
'Processing file: ' . $key . LF;
140 foreach ($value as $hash => $recReference) {
142 echo
' Keeping ' . $key .
' for record "' . $recReference .
'"' . LF;
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 .
'": ';
151 \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($absFileName, $newName);
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));
157 echo
' ERROR: TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
163 echo
' ERROR: File "' . $newName .
'" was not created!';
171 echo
' ERROR: File "' . $absFileName .
'" was not found!';