[API][!!!] Fix API compatibility break
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / class.missing_files.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Cleaner module: Missing files
29 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
30 *
31 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
32 */
33
34
35 /**
36 * Looking for missing files.
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class tx_lowlevel_missing_files extends tx_lowlevel_cleaner_core {
43
44 var $checkRefIndex = TRUE;
45
46 /**
47 * Constructor
48 *
49 * @return void
50 */
51 function __construct() {
52 parent::__construct();
53
54 // Setting up help:
55 $this->cli_help['name'] = 'missing_files -- Find all file references from records pointing to a missing (non-existing) file.';
56 $this->cli_help['description'] = trim('
57 Assumptions:
58 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
59 - relevant soft reference parsers applied everywhere file references are used inline
60
61 Files may be missing for these reasons (except software bugs):
62 - 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.
63 - 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.
64
65 Automatic Repair of Errors:
66 - 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.
67 - Soft References: Requires manual fix if you consider it an error.
68
69 Manual repair suggestions:
70 - 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.
71 - 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.
72 ');
73
74 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_files -s -r
75 This will show you missing files in the TYPO3 system and only report back if errors were found.';
76 }
77
78 /**
79 * Compatibility constructor.
80 *
81 * @deprecated since TYPO3 4.6 and will be removed in TYPO3 4.8. Use __construct() instead.
82 */
83 public function tx_lowlevel_missing_files() {
84 t3lib_div::logDeprecatedFunction();
85 // Note: we cannot call $this->__construct() here because it would call the derived class constructor and cause recursion
86 // This code uses official PHP behavior (http://www.php.net/manual/en/language.oop5.basic.php) when $this in the
87 // statically called non-static method inherits $this from the caller's scope.
88 tx_lowlevel_missing_files::__construct();
89 }
90
91 /**
92 * Find file references that points to non-existing files in system
93 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it)
94 *
95 * @return array
96 */
97 function main() {
98 global $TYPO3_DB;
99
100 // Initialize result array:
101 $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;
102 $resultArray = array(
103 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
104 'headers' => array(
105 'managedFilesMissing' => array('List of missing files managed by TCEmain', $listExplain, 3),
106 'softrefFilesMissing' => array('List of missing files registered as a soft reference', $listExplain, 2),
107 ),
108 'managedFilesMissing' => array(),
109 'softrefFilesMissing' => array(),
110 );
111
112
113 // Select all files in the reference table
114 $recs = $TYPO3_DB->exec_SELECTgetRows(
115 '*',
116 'sys_refindex',
117 'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'),
118 '',
119 'sorting DESC'
120 );
121
122 // Traverse the files and put into a large table:
123 if (is_array($recs)) {
124 foreach($recs as $rec) {
125
126 // Compile info string for location of reference:
127 $infoString = $this->infoStr($rec);
128
129 // Handle missing file:
130 if (!@is_file(PATH_site.$rec['ref_string'])) {
131
132 if ((string)$rec['softref_key']=='') {
133 $resultArrayIndex = 'managedFilesMissing';
134 } else {
135 $resultArrayIndex = 'softrefFilesMissing';
136 }
137
138 $resultArray[$resultArrayIndex][$rec['ref_string']][$rec['hash']] = $infoString;
139 ksort($resultArray[$resultArrayIndex][$rec['ref_string']]); // Sort by array key.
140 }
141 }
142 }
143
144 ksort($resultArray['managedFilesMissing']);
145 ksort($resultArray['softrefFilesMissing']);
146
147 return $resultArray;
148 }
149
150 /**
151 * Mandatory autofix function
152 * Will run auto-fix on the result array. Echos status during processing.
153 *
154 * @param array Result array from main() function
155 * @return void
156 */
157 function main_autoFix($resultArray) {
158 foreach($resultArray['managedFilesMissing'] as $key => $value) {
159 echo 'Processing file: '.$key.LF;
160 $c=0;
161 foreach($value as $hash => $recReference) {
162 echo ' Removing reference in record "'.$recReference.'": ';
163 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
164 echo $bypass;
165 } else {
166 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
167 $error = $sysRefObj->setReferenceValue($hash,NULL);
168 if ($error) {
169 echo ' t3lib_refindex::setReferenceValue(): '.$error.LF;
170 echo 'missing_files: exit on error'.LF;
171 exit;
172 } else echo "DONE";
173 }
174 echo LF;
175 }
176 }
177 }
178 }
179
180 ?>