TYPO3 CMS  TYPO3_6-2
LostFilesCommand.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  $this->cli_options[] = array('--excludePath [path-list]', 'Comma separated list of paths to exclude. Example: "uploads/[path1],uploads/[path2],..."');
37  // Setting up help:
38  $this->cli_help['name'] = 'lost_files -- Looking for files in the uploads/ folder which does not have a reference in TYPO3 managed records.';
39  $this->cli_help['description'] = trim('
40 Assumptions:
41 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
42 - that all contents in the uploads folder are files attached to TCA records and exclusively managed by TCEmain through "group" type fields
43 - exceptions are: index.html and .htaccess files (ignored)
44 - exceptions are: RTEmagic* image files (ignored)
45 - files found in deleted records are included (otherwise you would see a false list of lost files)
46 
47 The assumptions are not requirements by the TYPO3 API but reflects the de facto implementation of most TYPO3 installations and therefore a practical approach to cleaning up the uploads/ folder.
48 Therefore, if all "group" type fields in TCA and flexforms are positioned inside the uploads/ folder and if no files inside are managed manually it should be safe to clean out files with no relations found in the system.
49 Under such circumstances there should theoretically be no lost files in the uploads/ folder since TCEmain should have managed relations automatically including adding and deleting files.
50 However, there is at least one reason known to why files might be found lost and that is when FlexForms are used. In such a case a change of/in the Data Structure XML (or the ability of the system to find the Data Structure definition!) used for the flexform could leave lost files behind. This is not unlikely to happen when records are deleted. More details can be found in a note to the function TYPO3\\CMS\\Backend\\Utility\\BackendUtility::getFlexFormDS()
51 Another scenario could of course be de-installation of extensions which managed files in the uploads/ folders.
52 
53 Automatic Repair of Errors:
54 - Simply delete lost files (Warning: First, make sure those files are not used somewhere TYPO3 does not know about! See the assumptions above).
55 ');
56  $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner lost_files -s -r
57 Will report lost files.';
58  }
59 
71  public function main() {
72  global $TYPO3_DB;
73  // Initialize result array:
74  $resultArray = array(
75  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
76  'headers' => array(
77  'managedFiles' => array('Files related to TYPO3 records and managed by TCEmain', 'These files you definitely want to keep.', 0),
78  'ignoredFiles' => array('Ignored files (index.html, .htaccess etc.)', 'These files are allowed in uploads/ folder', 0),
79  'RTEmagicFiles' => array('RTE magic images - those found (and ignored)', 'These files are also allowed in some uploads/ folders as RTEmagic images.', 0),
80  'lostFiles' => array('Lost files - those you can delete', 'You can delete these files!', 3),
81  'warnings' => array('Warnings picked up', '', 2)
82  ),
83  'managedFiles' => array(),
84  'ignoredFiles' => array(),
85  'RTEmagicFiles' => array(),
86  'lostFiles' => array(),
87  'warnings' => array()
88  );
89  // Get all files:
90  $fileArr = array();
91  $fileArr = \TYPO3\CMS\Core\Utility\GeneralUtility::getAllFilesAndFoldersInPath($fileArr, PATH_site . 'uploads/');
93  $excludePaths = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->cli_argValue('--excludePath', 0), TRUE);
94  // Traverse files and for each, look up if its found in the reference index.
95  foreach ($fileArr as $key => $value) {
96  $include = TRUE;
97  foreach ($excludePaths as $exclPath) {
98  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($value, $exclPath)) {
99  $include = FALSE;
100  }
101  }
102  $shortKey = \TYPO3\CMS\Core\Utility\GeneralUtility::shortmd5($value);
103  if ($include) {
104  // First, allow "index.html", ".htaccess" files since they are often used for good reasons
105  if (substr($value, -11) == '/index.html' || substr($value, -10) == '/.htaccess') {
106  unset($fileArr[$key]);
107  $resultArray['ignoredFiles'][$shortKey] = $value;
108  } else {
109  // Looking for a reference from a field which is NOT a soft reference (thus, only fields with a proper TCA/Flexform configuration)
110  $recs = $TYPO3_DB->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex') . ' AND ref_string=' . $TYPO3_DB->fullQuoteStr($value, 'sys_refindex') . ' AND softref_key=' . $TYPO3_DB->fullQuoteStr('', 'sys_refindex'), '', 'sorting DESC');
111  // If found, unset entry:
112  if (count($recs)) {
113  unset($fileArr[$key]);
114  $resultArray['managedFiles'][$shortKey] = $value;
115  if (count($recs) > 1) {
116  $resultArray['warnings'][$shortKey] = 'Warning: File "' . $value . '" had ' . count($recs) . ' references from group-fields, should have only one!';
117  }
118  } else {
119  // When here it means the file was not found. So we test if it has a RTEmagic-image name and if so, we allow it:
120  if (preg_match('/^RTEmagic[P|C]_/', basename($value))) {
121  unset($fileArr[$key]);
122  $resultArray['RTEmagicFiles'][$shortKey] = $value;
123  } else {
124  // We conclude that the file is lost...:
125  unset($fileArr[$key]);
126  $resultArray['lostFiles'][$shortKey] = $value;
127  }
128  }
129  }
130  }
131  }
132  asort($resultArray['ignoredFiles']);
133  asort($resultArray['managedFiles']);
134  asort($resultArray['RTEmagicFiles']);
135  asort($resultArray['lostFiles']);
136  asort($resultArray['warnings']);
137  // $fileArr variable should now be empty with all contents transferred to the result array keys.
138  return $resultArray;
139  }
140 
149  public function main_autoFix($resultArray) {
150  foreach ($resultArray['lostFiles'] as $key => $value) {
152  echo 'Deleting file: "' . $absFileName . '": ';
153  if ($bypass = $this->cli_noExecutionCheck($absFileName)) {
154  echo $bypass;
155  } else {
156  if ($absFileName && @is_file($absFileName)) {
157  unlink($absFileName);
158  echo 'DONE';
159  } else {
160  echo ' ERROR: File "' . $absFileName . '" was not found!';
161  }
162  }
163  echo LF;
164  }
165  }
166 
167 }
static getAllFilesAndFoldersInPath(array $fileArr, $path, $extList='', $regDirs=FALSE, $recursivityLevels=99, $excludePattern='')
static isFirstPartOfStr($str, $partStr)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)
static removePrefixPathFromList(array $fileArr, $prefixToRemove)