TYPO3 CMS  TYPO3_7-6
MissingFilesCommand.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'] = 'missing_files -- Find all file references from records pointing to a missing (non-existing) file.';
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 - relevant soft reference parsers applied everywhere file references are used inline
39 
40 Files may be missing for these reasons (except software bugs):
41 - someone manually deleted the file inside fileadmin/ or another user maintained folder. If the reference was a soft reference (opposite to a TCEmain managed file relation from "group" type fields), technically it is not an error although it might be a mistake that someone did so.
42 - someone manually deleted the file inside the uploads/ folder (typically containing managed files) which is an error since no user interaction should take place there.
43 
44 Automatic Repair of Errors:
45 - Managed files (TCA/FlexForm attachments): Will silently remove the reference from the record since the file is missing. For this reason you might prefer a manual approach instead.
46 - Soft References: Requires manual fix if you consider it an error.
47 
48 Manual repair suggestions:
49 - Managed files: You might be able to locate the file and re-insert it in the correct location. However, no automatic fix can do that for you.
50 - Soft References: You should investigate each case and edit the content accordingly. A soft reference to a file could be in an HTML image tag (for example <img src="missing_file.jpg" />) and you would have to either remove the whole tag, change the filename or re-create the missing file.
51 ');
52  $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_files -s -r
53 This will show you missing files in the TYPO3 system and only report back if errors were found.';
54  }
55 
63  public function main()
64  {
65  // Initialize result array:
66  $listExplain = ' Shows the relative filename of missing file as header and under a list of record fields in which the references are found. ' . $this->label_infoString;
67  $resultArray = [
68  'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
69  'headers' => [
70  'managedFilesMissing' => ['List of missing files managed by TCEmain', $listExplain, 3],
71  'softrefFilesMissing' => ['List of missing files registered as a soft reference', $listExplain, 2]
72  ],
73  'managedFilesMissing' => [],
74  'softrefFilesMissing' => []
75  ];
76  // Select all files in the reference table
77  $recs = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('_FILE', 'sys_refindex'), '', 'sorting DESC');
78  // Traverse the files and put into a large table:
79  if (is_array($recs)) {
80  foreach ($recs as $rec) {
81  // Compile info string for location of reference:
82  $infoString = $this->infoStr($rec);
83  // Handle missing file:
84  if (!@is_file((PATH_site . $rec['ref_string']))) {
85  if ((string)$rec['softref_key'] == '') {
86  $resultArrayIndex = 'managedFilesMissing';
87  } else {
88  $resultArrayIndex = 'softrefFilesMissing';
89  }
90  $resultArray[$resultArrayIndex][$rec['ref_string']][$rec['hash']] = $infoString;
91  ksort($resultArray[$resultArrayIndex][$rec['ref_string']]);
92  }
93  }
94  }
95  ksort($resultArray['managedFilesMissing']);
96  ksort($resultArray['softrefFilesMissing']);
97  return $resultArray;
98  }
99 
107  public function main_autoFix($resultArray)
108  {
109  foreach ($resultArray['managedFilesMissing'] as $key => $value) {
110  echo 'Processing file: ' . $key . LF;
111  $c = 0;
112  foreach ($value as $hash => $recReference) {
113  echo ' Removing reference in record "' . $recReference . '": ';
114  if ($bypass = $this->cli_noExecutionCheck($recReference)) {
115  echo $bypass;
116  } else {
117  $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ReferenceIndex::class);
118  $error = $sysRefObj->setReferenceValue($hash, null);
119  if ($error) {
120  echo ' TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
121  echo 'missing_files: exit on error' . LF;
122  die;
123  } else {
124  echo 'DONE';
125  }
126  }
127  echo LF;
128  }
129  }
130  }
131 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']