Fixing SVN directory structure
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / clmods / clmods / class.double_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: Double 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 * 58: class tx_lowlevel_double_files extends tx_lowlevel_cleaner_core
39 * 67: function tx_lowlevel_double_files()
40 * 99: function main()
41 * 182: 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 require_once (PATH_t3lib.'class.t3lib_basicfilefunc.php');
50
51 /**
52 * Looking for double files
53 *
54 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
55 * @package TYPO3
56 * @subpackage tx_lowlevel
57 */
58 class tx_lowlevel_double_files extends tx_lowlevel_cleaner_core {
59
60 var $checkRefIndex = TRUE;
61
62 /**
63 * Constructor
64 *
65 * @return void
66 */
67 function tx_lowlevel_double_files() {
68 parent::tx_lowlevel_cleaner_core();
69
70 // Setting up help:
71 $this->cli_help['name'] = 'double_files -- Looking for files from TYPO3 managed records which are referenced more than one time (only one time allowed)';
72 $this->cli_help['description'] = trim('
73 Assumptions:
74 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
75 - files found in deleted records are included (otherwise you would see a false list of lost files)
76
77 Files attached to records in TYPO3 using a "group" type configuration in TCA or FlexForm DataStructure are managed exclusively by the system and there must always exist a 1-1 reference between the file and the reference in the record.
78 This tool will expose when such files are referenced from multiple locations which is considered an integrity error.
79 If a multi-reference is found it was typically created because the record was copied or modified outside of TCEmain which will otherwise maintain the relations correctly.
80 Multi-references should be resolved to 1-1 references as soon as possible. The danger of keeping multi-references is that if the file is removed from one of the refering records it will actually be deleted in the file system, leaving missing files for the remaining referers!
81
82 Automatic Repair of Errors:
83 - The multi-referenced file is copied under a new name and references updated.
84
85 Manual repair suggestions:
86 - None that can not be handled by the automatic repair.
87 ');
88
89 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner double_files -s -r
90 This will check the system for double files relations.';
91 }
92
93 /**
94 * Find managed files which are referred to more than one time
95 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (we could copy the file) or remove reference
96 *
97 * @return array
98 */
99 function main() {
100 global $TYPO3_DB;
101
102 // Initialize result array:
103 $resultArray = array(
104 'message' => $this->cli_help['name'].chr(10).chr(10).$this->cli_help['description'],
105 'headers' => array(
106 'multipleReferencesList_count' => array('Number of multi-reference files','(See below)',0),
107 'singleReferencesList_count' => array('Number of files correctly referenced','The amount of correct 1-1 references',0),
108 'multipleReferencesList' => array('Entries with files having multiple references','These are serious problems that should be resolved ASAP to prevent data loss! '.$this->label_infoString,3),
109 'dirname_registry' => array('Registry of directories in which files are found.','Registry includes which table/field pairs store files in them plus how many files their store.',0),
110 'missingFiles' => array('Tracking missing files','(Extra feature, not related to tracking of double references. Further, the list may include more files than found in the missing_files()-test because this list includes missing files from deleted records.)',0),
111 'warnings' => array('Warnings picked up','',2)
112 ),
113 'multipleReferencesList_count' => 0,
114 'singleReferencesList_count' => 0,
115 'multipleReferencesList' => array(),
116 'dirname_registry' => array(),
117 'missingFiles' => array(),
118 'warnings' => array()
119 );
120
121 // Select all files in the reference table not found by a soft reference parser (thus TCA configured)
122 $recs = $TYPO3_DB->exec_SELECTgetRows(
123 '*',
124 'sys_refindex',
125 'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex').
126 ' AND softref_key='.$TYPO3_DB->fullQuoteStr('', 'sys_refindex'),
127 '',
128 'sorting DESC'
129 );
130
131 // Traverse the files and put into a large table:
132 $tempCount = array();
133 if (is_array($recs)) {
134 foreach($recs as $rec) {
135
136 // Compile info string for location of reference:
137 $infoString = $this->infoStr($rec);
138
139 // Registering occurencies in directories:
140 $resultArray['dirname_registry'][dirname($rec['ref_string'])][$rec['tablename'].':'.$rec['field']]++;
141
142 // Handle missing file:
143 if (!@is_file(PATH_site.$rec['ref_string'])) {
144 $resultArray['missingFiles'][$rec['ref_string']][$rec['hash']] = $infoString;
145 }
146
147 // Add entry if file has multiple references pointing to it:
148 if (isset($tempCount[$rec['ref_string']])) {
149 if (!is_array($resultArray['multipleReferencesList'][$rec['ref_string']])) {
150 $resultArray['multipleReferencesList'][$rec['ref_string']] = array();
151 $resultArray['multipleReferencesList'][$rec['ref_string']][$tempCount[$rec['ref_string']][1]] = $tempCount[$rec['ref_string']][0];
152 }
153 $resultArray['multipleReferencesList'][$rec['ref_string']][$rec['hash']] = $infoString;
154 } else {
155 $tempCount[$rec['ref_string']] = array($infoString,$rec['hash']);
156 }
157 }
158 }
159
160 // Add count for multi-references:
161 $resultArray['multipleReferencesList_count'] = count($resultArray['multipleReferencesList']);
162 $resultArray['singleReferencesList_count'] = count($tempCount) - $resultArray['multipleReferencesList_count'];
163
164 // Sort dirname registry and add warnings for directories outside uploads/
165 ksort($resultArray['dirname_registry']);
166 foreach($resultArray['dirname_registry'] as $dir => $temp) {
167 if (!t3lib_div::isFirstPartOfStr($dir,'uploads/')) {
168 $resultArray['warnings'][] = 'Directory "'.$dir.'" was outside uploads/ which is unusual practice in TYPO3 although not forbidden. Directory used by the following table:field pairs: '.implode(',',array_keys($temp));
169 }
170 }
171
172 return $resultArray;
173 }
174
175 /**
176 * Mandatory autofix function
177 * Will run auto-fix on the result array. Echos status during processing.
178 *
179 * @param array Result array from main() function
180 * @return void
181 */
182 function main_autoFix($resultArray) {
183 foreach($resultArray['multipleReferencesList'] as $key => $value) {
184 $absFileName = t3lib_div::getFileAbsFileName($key);
185 if ($absFileName && @is_file($absFileName)) {
186 echo 'Processing file: '.$key.chr(10);
187 $c=0;
188 foreach($value as $hash => $recReference) {
189 if ($c==0) {
190 echo ' Keeping '.$key.' for record "'.$recReference.'"'.chr(10);
191 } else {
192 // Create unique name for file:
193 $fileFunc = t3lib_div::makeInstance('t3lib_basicFileFunctions');
194 $newName = $fileFunc->getUniqueName(basename($key), dirname($absFileName));
195 echo ' Copying '.$key.' to '.substr($newName,strlen(PATH_site)).' for record "'.$recReference.'": ';
196
197 if ($bypass = $this->cli_noExecutionCheck($recReference)) {
198 echo $bypass;
199 } else {
200 t3lib_div::upload_copy_move($absFileName,$newName);
201 clearstatcache();
202
203 if (@is_file($newName)) {
204 $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
205 $error = $sysRefObj->setReferenceValue($hash,basename($newName));
206 if ($error) {
207 echo ' ERROR: t3lib_refindex::setReferenceValue(): '.$error.chr(10);
208 exit;
209 } else echo "DONE";
210 } else {
211 echo ' ERROR: File "'.$newName.'" was not created!';
212 }
213 }
214 echo chr(10);
215 }
216 $c++;
217 }
218 } else {
219 echo ' ERROR: File "'.$absFileName.'" was not found!';
220 }
221 }
222 }
223 }
224
225 ?>