[TASK] Remove deprecated genTreeStatus in DatabaseIntegrityCheck
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Integrity / DatabaseIntegrityCheck.php
1 <?php
2 namespace TYPO3\CMS\Core\Integrity;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31
32 /**
33 * This class holds functions used by the TYPO3 backend to check the integrity of the database (The DBint module, 'lowlevel' extension)
34 *
35 * Depends on: Depends on \TYPO3\CMS\Core\Database\RelationHandler
36 *
37 * @todo Need to really extend this class when the tcemain library has been updated and the whole API is better defined. There are some known bugs in this library. Further it would be nice with a facility to not only analyze but also clean up!
38 * @see SC_mod_tools_dbint_index::func_relations(), SC_mod_tools_dbint_index::func_records()
39 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
40 */
41 class DatabaseIntegrityCheck {
42
43 /**
44 * @var boolean If set, genTree() includes deleted pages. This is default.
45 * @todo Define visibility
46 */
47 public $genTree_includeDeleted = TRUE;
48
49 /**
50 * @var boolean If set, genTree() includes versionized pages/records. This is default.
51 * @todo Define visibility
52 */
53 public $genTree_includeVersions = TRUE;
54
55 /**
56 * @var boolean If set, genTree() includes records from pages.
57 * @todo Define visibility
58 */
59 public $genTree_includeRecords = FALSE;
60
61 /**
62 * @var string Extra where-clauses for the tree-selection
63 * @todo Define visibility
64 */
65 public $perms_clause = '';
66
67 /**
68 * @var int If set, genTree() generates HTML, that visualizes the tree.
69 * @todo Define visibility
70 */
71 public $genTree_makeHTML = 0;
72
73 // Internal
74 /**
75 * @var array Will hold id/rec pairs from genTree()
76 * @todo Define visibility
77 */
78 public $page_idArray = array();
79
80 /**
81 * @var array
82 * @todo Define visibility
83 */
84 public $rec_idArray = array();
85
86 /**
87 * @var string Will hold the HTML-code visualising the tree. genTree()
88 * @todo Define visibility
89 */
90 public $genTree_HTML = '';
91
92 /**
93 * @var string
94 * @todo Define visibility
95 */
96 public $backPath = '';
97
98 // Internal
99 /**
100 * @var array
101 * @todo Define visibility
102 */
103 public $checkFileRefs = array();
104
105 /**
106 * @var array From the select-fields
107 * @todo Define visibility
108 */
109 public $checkSelectDBRefs = array();
110
111 /**
112 * @var array From the group-fields
113 * @todo Define visibility
114 */
115 public $checkGroupDBRefs = array();
116
117 /**
118 * @var array Statistics
119 * @todo Define visibility
120 */
121 public $recStats = array(
122 'allValid' => array(),
123 'published_versions' => array(),
124 'deleted' => array()
125 );
126
127 /**
128 * @var array
129 * @todo Define visibility
130 */
131 public $lRecords = array();
132
133 /**
134 * @var string
135 * @todo Define visibility
136 */
137 public $lostPagesList = '';
138
139 /**
140 * Generates a list of Page-uid's that corresponds to the tables in the tree.
141 * This list should ideally include all records in the pages-table.
142 *
143 * @param integer $theID a pid (page-record id) from which to start making the tree
144 * @param string $depthData HTML-code (image-tags) used when this function calls itself recursively.
145 * @param boolean $versions Internal variable, don't set from outside!
146 * @return void
147 * @todo Define visibility
148 */
149 public function genTree($theID, $depthData, $versions = FALSE) {
150 if ($versions) {
151 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title,doktype,deleted,t3ver_wsid,t3ver_id,t3ver_count' . (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('cms') ? ',hidden' : ''), 'pages', 'pid=-1 AND t3ver_oid=' . (int)$theID . ' ' . (!$this->genTree_includeDeleted ? 'AND deleted=0' : '') . $this->perms_clause, '', 'sorting');
152 } else {
153 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title,doktype,deleted' . (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('cms') ? ',hidden' : ''), 'pages', 'pid=' . (int)$theID . ' ' . (!$this->genTree_includeDeleted ? 'AND deleted=0' : '') . $this->perms_clause, '', 'sorting');
154 }
155 // Traverse the records selected:
156 $a = 0;
157 $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
158 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
159 // Prepare the additional label used in the HTML output in case of versions:
160 if ($versions) {
161 $versionLabel = '[v1.' . $row['t3ver_id'] . '; WS#' . $row['t3ver_wsid'] . ']';
162 } else {
163 $versionLabel = '';
164 }
165 $a++;
166 $newID = $row['uid'];
167 // Build HTML output:
168 if ($this->genTree_makeHTML) {
169 $this->genTree_HTML .= LF . '<div><span class="nobr">';
170 $PM = 'join';
171 $LN = $a == $c ? 'blank' : 'line';
172 $BTM = $a == $c ? 'bottom' : '';
173 $this->genTree_HTML .= $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($this->backPath, ('gfx/ol/' . $PM . $BTM . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />' . $versionLabel . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('pages', $row) . htmlspecialchars(($row['uid'] . ': ' . \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs(strip_tags($row['title']), 50))) . '</span></div>';
174 }
175 // Register various data for this item:
176 $this->page_idArray[$newID] = $row;
177 $this->recStats['all_valid']['pages'][$newID] = $newID;
178 if ($row['deleted']) {
179 $this->recStats['deleted']['pages'][$newID] = $newID;
180 }
181 if ($versions && $row['t3ver_count'] >= 1) {
182 $this->recStats['published_versions']['pages'][$newID] = $newID;
183 }
184 if ($row['deleted']) {
185 $this->recStats['deleted']++;
186 }
187 if ($row['hidden']) {
188 $this->recStats['hidden']++;
189 }
190 $this->recStats['doktype'][$row['doktype']]++;
191 // Create the HTML code prefix for recursive call:
192 $genHTML = $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($this->backPath, ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />' . $versionLabel;
193 // If all records should be shown, do so:
194 if ($this->genTree_includeRecords) {
195 foreach ($GLOBALS['TCA'] as $tableName => $cfg) {
196 if ($tableName != 'pages') {
197 $this->genTree_records($newID, $this->genTree_HTML ? $genHTML : '', $tableName);
198 }
199 }
200 }
201 // Add sub pages:
202 $this->genTree($newID, $this->genTree_HTML ? $genHTML : '');
203 // If versions are included in the tree, add those now:
204 if ($this->genTree_includeVersions) {
205 $this->genTree($newID, $this->genTree_HTML ? $genHTML : '', TRUE);
206 }
207 }
208 $GLOBALS['TYPO3_DB']->sql_free_result($res);
209 }
210
211 /**
212 * @param integer $theID a pid (page-record id) from which to start making the tree
213 * @param string $depthData HTML-code used when this function calls itself recursively.
214 * @param string $table Table to get the records from
215 * @param boolean $versions Internal variable, don't set from outside!
216 * @return void
217 * @todo Define visibility
218 */
219 public function genTree_records($theID, $depthData, $table = '', $versions = FALSE) {
220 if ($versions) {
221 // Select all records from table pointing to this page:
222 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
223 BackendUtility::getCommonSelectFields($table),
224 $table,
225 'pid=-1 AND t3ver_oid=' . (int)$theID . (!$this->genTree_includeDeleted ? BackendUtility::deleteClause($table) : '')
226 );
227 } else {
228 // Select all records from table pointing to this page:
229 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
230 BackendUtility::getCommonSelectFields($table),
231 $table,
232 'pid=' . (int)$theID . (!$this->genTree_includeDeleted ? BackendUtility::deleteClause($table) : '')
233 );
234 }
235 // Traverse selected:
236 $a = 0;
237 $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
238 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
239 // Prepare the additional label used in the HTML output in case of versions:
240 if ($versions) {
241 $versionLabel = '[v1.' . $row['t3ver_id'] . '; WS#' . $row['t3ver_wsid'] . ']';
242 } else {
243 $versionLabel = '';
244 }
245 $a++;
246 $newID = $row['uid'];
247 // Build HTML output:
248 if ($this->genTree_makeHTML) {
249 $this->genTree_HTML .= LF . '<div><span class="nobr">';
250 $PM = 'join';
251 $LN = $a == $c ? 'blank' : 'line';
252 $BTM = $a == $c ? 'bottom' : '';
253 $this->genTree_HTML .= $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($this->backPath, ('gfx/ol/' . $PM . $BTM . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />' . $versionLabel . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord($table, $row, array('title' => $table)) . htmlspecialchars(($row['uid'] . ': ' . BackendUtility::getRecordTitle($table, $row))) . '</span></div>';
254 }
255 // Register various data for this item:
256 $this->rec_idArray[$table][$newID] = $row;
257 $this->recStats['all_valid'][$table][$newID] = $newID;
258 if ($row['deleted']) {
259 $this->recStats['deleted'][$table][$newID] = $newID;
260 }
261 if ($versions && $row['t3ver_count'] >= 1 && $row['t3ver_wsid'] == 0) {
262 $this->recStats['published_versions'][$table][$newID] = $newID;
263 }
264 // Select all versions of this record:
265 if ($this->genTree_includeVersions && $GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
266 $genHTML = $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($this->backPath, ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />';
267 $this->genTree_records($newID, $genHTML, $table, TRUE);
268 }
269 }
270 $GLOBALS['TYPO3_DB']->sql_free_result($res);
271 }
272
273 /**
274 * Fills $this->lRecords with the records from all tc-tables that are not attached to a PID in the pid-list.
275 *
276 * @param string $pid_list list of pid's (page-record uid's). This list is probably made by genTree()
277 * @return void
278 * @todo Define visibility
279 */
280 public function lostRecords($pid_list) {
281 $this->lostPagesList = '';
282 if ($pid_list) {
283 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
284 $pid_list_tmp = $pid_list;
285 if (!isset($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) || !$GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
286 // Remove preceding "-1," for non-versioned tables
287 $pid_list_tmp = preg_replace('/^\\-1,/', '', $pid_list_tmp);
288 }
289 $garbage = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,' . $GLOBALS['TCA'][$table]['ctrl']['label'], $table, 'pid NOT IN (' . $pid_list_tmp . ')');
290 $lostIdList = array();
291 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($garbage)) {
292 $this->lRecords[$table][$row['uid']] = array(
293 'uid' => $row['uid'],
294 'pid' => $row['pid'],
295 'title' => strip_tags(BackendUtility::getRecordTitle($table, $row))
296 );
297 $lostIdList[] = $row['uid'];
298 }
299 $GLOBALS['TYPO3_DB']->sql_free_result($garbage);
300 if ($table == 'pages') {
301 $this->lostPagesList = implode(',', $lostIdList);
302 }
303 }
304 }
305 }
306
307 /**
308 * Fixes lost record from $table with uid $uid by setting the PID to zero.
309 * If there is a disabled column for the record that will be set as well.
310 *
311 * @param string $table Database tablename
312 * @param integer $uid The uid of the record which will have the PID value set to 0 (zero)
313 * @return boolean TRUE if done.
314 * @todo Define visibility
315 */
316 public function fixLostRecord($table, $uid) {
317 if ($table && $GLOBALS['TCA'][$table] && $uid && is_array($this->lRecords[$table][$uid]) && $GLOBALS['BE_USER']->user['admin']) {
318 $updateFields = array();
319 $updateFields['pid'] = 0;
320 // If possible a lost record restored is hidden as default
321 if ($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']) {
322 $updateFields[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']] = 1;
323 }
324 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . (int)$uid, $updateFields);
325 return TRUE;
326 } else {
327 return FALSE;
328 }
329 }
330
331 /**
332 * Counts records from $GLOBALS['TCA']-tables that ARE attached to an existing page.
333 *
334 * @param string $pid_list list of pid's (page-record uid's). This list is probably made by genTree()
335 * @return array an array with the number of records from all $GLOBALS['TCA']-tables that are attached to a PID in the pid-list.
336 * @todo Define visibility
337 */
338 public function countRecords($pid_list) {
339 $list = array();
340 $list_n = array();
341 if ($pid_list) {
342 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
343 $pid_list_tmp = $pid_list;
344 if (!isset($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) || !$GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
345 // Remove preceding "-1," for non-versioned tables
346 $pid_list_tmp = preg_replace('/^\\-1,/', '', $pid_list_tmp);
347 }
348 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, 'pid IN (' . $pid_list_tmp . ')');
349 if ($count) {
350 $list[$table] = $count;
351 }
352 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, 'pid IN (' . $pid_list_tmp . ')' . BackendUtility::deleteClause($table));
353 if ($count) {
354 $list_n[$table] = $count;
355 }
356 }
357 }
358 return array('all' => $list, 'non_deleted' => $list_n);
359 }
360
361 /**
362 * Finding relations in database based on type 'group' (files or database-uid's in a list)
363 *
364 * @param string $mode $mode = file, $mode = db, $mode = '' (all...)
365 * @return array An array with all fields listed that somehow are references to other records (foreign-keys) or files
366 * @todo Define visibility
367 */
368 public function getGroupFields($mode) {
369 $result = array();
370 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
371 $cols = $GLOBALS['TCA'][$table]['columns'];
372 foreach ($cols as $field => $config) {
373 if ($config['config']['type'] == 'group') {
374 if ((!$mode || $mode == 'file') && $config['config']['internal_type'] == 'file' || (!$mode || $mode == 'db') && $config['config']['internal_type'] == 'db') {
375 $result[$table][] = $field;
376 }
377 }
378 if ((!$mode || $mode == 'db') && $config['config']['type'] == 'select' && $config['config']['foreign_table']) {
379 $result[$table][] = $field;
380 }
381 }
382 if ($result[$table]) {
383 $result[$table] = implode(',', $result[$table]);
384 }
385 }
386 return $result;
387 }
388
389 /**
390 * Finds all fields that hold filenames from uploadfolder
391 *
392 * @param string $uploadfolder Path to uploadfolder
393 * @return array An array with all fields listed that have references to files in the $uploadfolder
394 * @todo Define visibility
395 */
396 public function getFileFields($uploadfolder) {
397 $result = array();
398 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
399 $cols = $GLOBALS['TCA'][$table]['columns'];
400 foreach ($cols as $field => $config) {
401 if ($config['config']['type'] == 'group' && $config['config']['internal_type'] == 'file' && $config['config']['uploadfolder'] == $uploadfolder) {
402 $result[] = array($table, $field);
403 }
404 }
405 }
406 return $result;
407 }
408
409 /**
410 * Returns an array with arrays of table/field pairs which are allowed to hold references to the input table name - according to $GLOBALS['TCA']
411 *
412 * @param string $theSearchTable Table name
413 * @return array
414 * @todo Define visibility
415 */
416 public function getDBFields($theSearchTable) {
417 $result = array();
418 foreach ($GLOBALS['TCA'] as $table => $tableConf) {
419 $cols = $GLOBALS['TCA'][$table]['columns'];
420 foreach ($cols as $field => $config) {
421 if ($config['config']['type'] == 'group' && $config['config']['internal_type'] == 'db') {
422 if (trim($config['config']['allowed']) == '*' || strstr($config['config']['allowed'], $theSearchTable)) {
423 $result[] = array($table, $field);
424 }
425 } elseif ($config['config']['type'] == 'select' && $config['config']['foreign_table'] == $theSearchTable) {
426 $result[] = array($table, $field);
427 }
428 }
429 }
430 return $result;
431 }
432
433 /**
434 * This selects non-empty-records from the tables/fields in the fkey_array generated by getGroupFields()
435 *
436 * @param array $fkey_arrays Array with tables/fields generated by getGroupFields()
437 * @return void
438 * @see getGroupFields()
439 * @todo Define visibility
440 */
441 public function selectNonEmptyRecordsWithFkeys($fkey_arrays) {
442 if (is_array($fkey_arrays)) {
443 foreach ($fkey_arrays as $table => $field_list) {
444 if ($GLOBALS['TCA'][$table] && trim($field_list)) {
445 $fieldArr = explode(',', $field_list);
446 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')) {
447 $fields = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
448 $field = array_shift($fieldArr);
449 $cl_fl = $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'I' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'N' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'R' ? $field . '<>0' : $field . '<>\'\'';
450 foreach ($fieldArr as $field) {
451 $cl_fl .= $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'I' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'N' || $GLOBALS['TYPO3_DB']->MetaType($fields[$field]['type'], $table) == 'R' ? ' OR ' . $field . '<>0' : ' OR ' . $field . '<>\'\'';
452 }
453 unset($fields);
454 } else {
455 $cl_fl = implode('<>\'\' OR ', $fieldArr) . '<>\'\'';
456 }
457 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,' . $field_list, $table, $cl_fl);
458 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
459 foreach ($fieldArr as $field) {
460 if (trim($row[$field])) {
461 $fieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
462 if ($fieldConf['type'] == 'group') {
463 if ($fieldConf['internal_type'] == 'file') {
464 // Files...
465 if ($fieldConf['MM']) {
466 $tempArr = array();
467 /** @var $dbAnalysis \TYPO3\CMS\Core\Database\RelationHandler */
468 $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
469 $dbAnalysis->start('', 'files', $fieldConf['MM'], $row['uid']);
470 foreach ($dbAnalysis->itemArray as $somekey => $someval) {
471 if ($someval['id']) {
472 $tempArr[] = $someval['id'];
473 }
474 }
475 } else {
476 $tempArr = explode(',', trim($row[$field]));
477 }
478 foreach ($tempArr as $file) {
479 $file = trim($file);
480 if ($file) {
481 $this->checkFileRefs[$fieldConf['uploadfolder']][$file] += 1;
482 }
483 }
484 }
485 if ($fieldConf['internal_type'] == 'db') {
486 /** @var $dbAnalysis \TYPO3\CMS\Core\Database\RelationHandler */
487 $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
488 $dbAnalysis->start($row[$field], $fieldConf['allowed'], $fieldConf['MM'], $row['uid'], $table, $fieldConf);
489 foreach ($dbAnalysis->itemArray as $tempArr) {
490 $this->checkGroupDBRefs[$tempArr['table']][$tempArr['id']] += 1;
491 }
492 }
493 }
494 if ($fieldConf['type'] == 'select' && $fieldConf['foreign_table']) {
495 /** @var $dbAnalysis \TYPO3\CMS\Core\Database\RelationHandler */
496 $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
497 $dbAnalysis->start($row[$field], $fieldConf['foreign_table'], $fieldConf['MM'], $row['uid'], $table, $fieldConf);
498 foreach ($dbAnalysis->itemArray as $tempArr) {
499 if ($tempArr['id'] > 0) {
500 $this->checkGroupDBRefs[$fieldConf['foreign_table']][$tempArr['id']] += 1;
501 }
502 }
503 }
504 }
505 }
506 }
507 $GLOBALS['TYPO3_DB']->sql_free_result($mres);
508 }
509 }
510 }
511 }
512
513 /**
514 * Depends on selectNonEmpty.... to be executed first!!
515 *
516 * @return array Report over files; keys are "moreReferences", "noReferences", "noFile", "error
517 * @todo Define visibility
518 */
519 public function testFileRefs() {
520 $output = array();
521 // Handle direct references with upload folder setting (workaround)
522 $newCheckFileRefs = array();
523 foreach ($this->checkFileRefs as $folder => $files) {
524 // Only direct references without a folder setting
525 if ($folder !== '') {
526 $newCheckFileRefs[$folder] = $files;
527 continue;
528 }
529 foreach ($files as $file => $references) {
530 // Direct file references have often many references (removes occurences in the moreReferences section of the result array)
531 if ($references > 1) {
532 $references = 1;
533 }
534 // The directory must be empty (prevents checking of the root directory)
535 $directory = dirname($file);
536 if ($directory !== '') {
537 $newCheckFileRefs[$directory][basename($file)] = $references;
538 }
539 }
540 }
541 $this->checkFileRefs = $newCheckFileRefs;
542 foreach ($this->checkFileRefs as $folder => $fileArr) {
543 $path = PATH_site . $folder;
544 if (@is_dir($path) && @is_readable($path)) {
545 $d = dir($path);
546 while ($entry = $d->read()) {
547 if (@is_file(($path . '/' . $entry))) {
548 if (isset($fileArr[$entry])) {
549 if ($fileArr[$entry] > 1) {
550 $temp = $this->whereIsFileReferenced($folder, $entry);
551 $tempList = '';
552 foreach ($temp as $inf) {
553 $tempList .= '[' . $inf['table'] . '][' . $inf['uid'] . '][' . $inf['field'] . '] (pid:' . $inf['pid'] . ') - ';
554 }
555 $output['moreReferences'][] = array($path, $entry, $fileArr[$entry], $tempList);
556 }
557 unset($fileArr[$entry]);
558 } else {
559 // Contains workaround for direct references
560 if (!strstr($entry, 'index.htm') && !preg_match(('/^' . preg_quote($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/'), $folder)) {
561 $output['noReferences'][] = array($path, $entry);
562 }
563 }
564 }
565 }
566 $d->close();
567 $tempCounter = 0;
568 foreach ($fileArr as $file => $value) {
569 // Workaround for direct file references
570 if (preg_match('/^' . preg_quote($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/', $folder)) {
571 $file = $folder . '/' . $file;
572 $folder = '';
573 $path = substr(PATH_site, 0, -1);
574 }
575 $temp = $this->whereIsFileReferenced($folder, $file);
576 $tempList = '';
577 foreach ($temp as $inf) {
578 $tempList .= '[' . $inf['table'] . '][' . $inf['uid'] . '][' . $inf['field'] . '] (pid:' . $inf['pid'] . ') - ';
579 }
580 $tempCounter++;
581 $output['noFile'][substr($path, -3) . '_' . substr($file, 0, 3) . '_' . $tempCounter] = array($path, $file, $tempList);
582 }
583 } else {
584 $output['error'][] = array($path);
585 }
586 }
587 return $output;
588 }
589
590 /**
591 * Depends on selectNonEmpty.... to be executed first!!
592 *
593 * @param array $theArray Table with key/value pairs being table names and arrays with uid numbers
594 * @return string HTML Error message
595 * @todo Define visibility
596 */
597 public function testDBRefs($theArray) {
598 $result = '';
599 foreach ($theArray as $table => $dbArr) {
600 if ($GLOBALS['TCA'][$table]) {
601 $idlist = array_keys($dbArr);
602 $theList = implode(',', $idlist);
603 if ($theList) {
604 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN (' . $theList . ')' . BackendUtility::deleteClause($table));
605 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
606 if (isset($dbArr[$row['uid']])) {
607 unset($dbArr[$row['uid']]);
608 } else {
609 $result .= 'Strange Error. ...<br />';
610 }
611 }
612 $GLOBALS['TYPO3_DB']->sql_free_result($mres);
613 foreach ($dbArr as $theId => $theC) {
614 $result .= 'There are ' . $theC . ' records pointing to this missing or deleted record; [' . $table . '][' . $theId . ']<br />';
615 }
616 }
617 } else {
618 $result .= 'Codeerror. Table is not a table...<br />';
619 }
620 }
621 return $result;
622 }
623
624 /**
625 * Finding all references to record based on table/uid
626 *
627 * @param string $searchTable Table name
628 * @param integer $id Uid of database record
629 * @return array Array with other arrays containing information about where references was found
630 * @todo Define visibility
631 */
632 public function whereIsRecordReferenced($searchTable, $id) {
633 // Gets tables / Fields that reference to files
634 $fileFields = $this->getDBFields($searchTable);
635 $theRecordList = array();
636 foreach ($fileFields as $info) {
637 $table = $info[0];
638 $field = $info[1];
639 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ',' . $field, $table, $field . ' LIKE \'%' . $GLOBALS['TYPO3_DB']->quoteStr($id, $table) . '%\'');
640 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
641 // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
642 $fieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
643 $allowedTables = $fieldConf['type'] == 'group' ? $fieldConf['allowed'] : $fieldConf['foreign_table'];
644 /** @var $dbAnalysis \TYPO3\CMS\Core\Database\RelationHandler */
645 $dbAnalysis = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
646 $dbAnalysis->start($row[$field], $allowedTables, $fieldConf['MM'], $row['uid'], $table, $fieldConf);
647 foreach ($dbAnalysis->itemArray as $tempArr) {
648 if ($tempArr['table'] == $searchTable && $tempArr['id'] == $id) {
649 $theRecordList[] = array('table' => $table, 'uid' => $row['uid'], 'field' => $field, 'pid' => $row['pid']);
650 }
651 }
652 }
653 $GLOBALS['TYPO3_DB']->sql_free_result($mres);
654 }
655 return $theRecordList;
656 }
657
658 /**
659 * Finding all references to file based on uploadfolder / filename
660 *
661 * @param string $uploadfolder Upload folder where file is found
662 * @param string $filename Filename to search for
663 * @return array Array with other arrays containing information about where references was found
664 * @todo Define visibility
665 */
666 public function whereIsFileReferenced($uploadfolder, $filename) {
667 // Gets tables / Fields that reference to files
668 $fileFields = $this->getFileFields($uploadfolder);
669 $theRecordList = array();
670 foreach ($fileFields as $info) {
671 $table = $info[0];
672 $field = $info[1];
673 $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ',' . $field, $table, $field . ' LIKE \'%' . $GLOBALS['TYPO3_DB']->quoteStr($filename, $table) . '%\'');
674 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
675 // Now this is the field, where the reference COULD come from.
676 // But we're not guaranteed, so we must carefully examine the data.
677 $tempArr = explode(',', trim($row[$field]));
678 foreach ($tempArr as $file) {
679 $file = trim($file);
680 if ($file == $filename) {
681 $theRecordList[] = array('table' => $table, 'uid' => $row['uid'], 'field' => $field, 'pid' => $row['pid']);
682 }
683 }
684 }
685 $GLOBALS['TYPO3_DB']->sql_free_result($mres);
686 }
687 return $theRecordList;
688 }
689
690 }