e31fa21492cf4fa93d95f6d6ef183e761dd9852f
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / clmods / class.missing_files.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (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 * [CLASS/FUNCTION INDEX of SCRIPT]
35 *
36 *
37 *
38 * 56: class tx_lowlevel_missing_files extends tx_lowlevel_cleaner_core
39 * 65: function tx_lowlevel_missing_files()
40 * 98: function main()
41 * 154: function main_autoFix($resultArray)
42 *
43 * TOTAL FUNCTIONS: 3
44 * (This index is automatically created/updated by the extension "extdeveval")
45 *
46 */
47
48
49 /**
50 * Looking for missing files.
51 *
52 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
53 * @package TYPO3
54 * @subpackage tx_lowlevel
55 */
56 class tx_lowlevel_missing_files extends tx_lowlevel_cleaner_core {
57
58 var $checkRefIndex = TRUE;
59
60 /**
61 * Constructor
62 *
63 * @return void
64 */
65 function tx_lowlevel_missing_files() {
66 parent::tx_lowlevel_cleaner_core();
67
68 // Setting up help:
69 $this->cli_help['name'] = 'missing_files -- Find all file references from records pointing to a missing (non-existing) file.';
70 $this->cli_help['description'] = trim('
71 Assumptions:
72 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
73 - relevant soft reference parsers applied everywhere file references are used inline
74
75 Files may be missing for these reasons (except software bugs):
76 - 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.
77 - 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.
78
79 Automatic Repair of Errors:
80 - 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.
81 - Soft References: Requires manual fix if you consider it an error.
82
83 Manual repair suggestions:
84 - 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.
85 - 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.
86 ');
87
88 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_files -s -r
89 This will show you missing files in the TYPO3 system and only report back if errors were found.';
90 }
91
92 /**
93 * Find file references that points to non-existing files in system
94 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it)
95 *
96 * @return array
97 */
98 function main() {
99 global $TYPO3_DB;
100
101 // Initialize result array:
102 $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;
103 $resultArray = array(
104 'message' => $this->cli_help['name'].chr(10).chr(10).$this->cli_help['description'],
105 'headers' => array(
106 'managedFilesMissing' => array('List of missing files managed by TCEmain', $listExplain, 3),
107 'softrefFilesMissing' => array('List of missing files registered as a soft reference', $listExplain, 2),
108 ),
109 'managedFilesMissing' => array(),
110 'softrefFilesMissing' => array(),
111 );
112
113
114 // Select all files in the reference table
115 $recs = $TYPO3_DB->exec_SELECTgetRows(
116 '*',
117 'sys_refindex',
118 'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'),
119 '',
120 'sorting DESC'
121 );
122
123 // Traverse the files and put into a large table:
124 if (is_array($recs)) {
125 foreach($recs as $rec) {
126
127 // Compile info string for location of reference:
128 $infoString = $this->infoStr($rec);
129
130 // Handle missing file:
131 if (!@is_file(PATH_site.$rec['ref_string'])) {
132
133 if ((string)$rec['softref_key']=='') {
134 $resultArrayIndex = 'managedFilesMissing';
135 } else {
136 $resultArrayIndex = 'softrefFilesMissing';
137 }
138
139 $resultArray[$resultArrayIndex][$rec['ref_string']][$rec['hash']] = $infoString;
140 }
141 }
142 }
143
144 return $resultArray;
145 }
146
147 /**
148 * Mandatory autofix function
149 * Will run auto-fix on the result array. Echos status during processing.
150 *
151 * @param array Result array from main() function
152 * @return void
153 */
154 function main_autoFix($resultArray) {
155 foreach($resultArray['managedFilesMissing'] as $key => $value) {
156 echo 'Processing file: '.$key.chr(10);
157 $c=0;
158 foreach($value as $hash => $recReference) {
159 echo ' Removing reference in record "'.$recReference.'": ';
160 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
161 echo $bypass;
162 } else {
163 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
164 $error = $sysRefObj->setReferenceValue($hash,NULL);
165 if ($error) {
166 echo ' t3lib_refindex::setReferenceValue(): '.$error.chr(10);
167 echo 'missing_files: exit on error'.chr(10);
168 exit;
169 } else echo "DONE";
170 }
171 echo chr(10);
172 }
173 }
174 }
175 }
176
177 ?>