[TASK] Remove deprecated code for 6.0
[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 * Find file references that points to non-existing files in system
80 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it)
81 *
82 * @return array
83 */
84 function main() {
85 global $TYPO3_DB;
86
87 // Initialize result array:
88 $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;
89 $resultArray = array(
90 'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
91 'headers' => array(
92 'managedFilesMissing' => array('List of missing files managed by TCEmain', $listExplain, 3),
93 'softrefFilesMissing' => array('List of missing files registered as a soft reference', $listExplain, 2),
94 ),
95 'managedFilesMissing' => array(),
96 'softrefFilesMissing' => array(),
97 );
98
99
100 // Select all files in the reference table
101 $recs = $TYPO3_DB->exec_SELECTgetRows(
102 '*',
103 'sys_refindex',
104 'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'),
105 '',
106 'sorting DESC'
107 );
108
109 // Traverse the files and put into a large table:
110 if (is_array($recs)) {
111 foreach($recs as $rec) {
112
113 // Compile info string for location of reference:
114 $infoString = $this->infoStr($rec);
115
116 // Handle missing file:
117 if (!@is_file(PATH_site.$rec['ref_string'])) {
118
119 if ((string)$rec['softref_key']=='') {
120 $resultArrayIndex = 'managedFilesMissing';
121 } else {
122 $resultArrayIndex = 'softrefFilesMissing';
123 }
124
125 $resultArray[$resultArrayIndex][$rec['ref_string']][$rec['hash']] = $infoString;
126 ksort($resultArray[$resultArrayIndex][$rec['ref_string']]); // Sort by array key.
127 }
128 }
129 }
130
131 ksort($resultArray['managedFilesMissing']);
132 ksort($resultArray['softrefFilesMissing']);
133
134 return $resultArray;
135 }
136
137 /**
138 * Mandatory autofix function
139 * Will run auto-fix on the result array. Echos status during processing.
140 *
141 * @param array Result array from main() function
142 * @return void
143 */
144 function main_autoFix($resultArray) {
145 foreach($resultArray['managedFilesMissing'] as $key => $value) {
146 echo 'Processing file: '.$key.LF;
147 $c=0;
148 foreach($value as $hash => $recReference) {
149 echo ' Removing reference in record "'.$recReference.'": ';
150 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
151 echo $bypass;
152 } else {
153 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
154 $error = $sysRefObj->setReferenceValue($hash,NULL);
155 if ($error) {
156 echo ' t3lib_refindex::setReferenceValue(): '.$error.LF;
157 echo 'missing_files: exit on error'.LF;
158 exit;
159 } else echo "DONE";
160 }
161 echo LF;
162 }
163 }
164 }
165 }
166
167 ?>