class.t3lib_admin.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains a class for evaluation of database integrity according to $TCA
00029  * Most of these functions are considered obsolete!
00030  *
00031  * $Id: class.t3lib_admin.php 3437 2008-03-16 16:22:11Z flyguide $
00032  * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
00033  * XHTML compliant
00034  *
00035  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00036  */
00037 /**
00038  * [CLASS/FUNCTION INDEX of SCRIPT]
00039  *
00040  *
00041  *
00042  *   93: class t3lib_admin
00043  *  128:     function genTree($theID, $depthData, $versions=FALSE)
00044  *  217:     function genTree_records($theID, $depthData, $table='', $versions=FALSE)
00045  *  292:     function genTreeStatus()
00046  *  315:     function lostRecords($pid_list)
00047  *  346:     function fixLostRecord($table,$uid)
00048  *  367:     function countRecords($pid_list)
00049  *  395:     function getGroupFields($mode)
00050  *  429:     function getFileFields($uploadfolder)
00051  *  452:     function getDBFields($theSearchTable)
00052  *  480:     function selectNonEmptyRecordsWithFkeys($fkey_arrays)
00053  *  569:     function testFileRefs ()
00054  *  620:     function testDBRefs($theArray)
00055  *  658:     function whereIsRecordReferenced($searchTable,$id)
00056  *  695:     function whereIsFileReferenced($uploadfolder,$filename)
00057  *
00058  * TOTAL FUNCTIONS: 14
00059  * (This index is automatically created/updated by the extension "extdeveval")
00060  *
00061  */
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 /**
00083  * This class holds functions used by the TYPO3 backend to check the integrity of the database (The DBint module, 'lowlevel' extension)
00084  *
00085  * Depends on:      Depends on loaddbgroup from t3lib/
00086  *
00087  * @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!
00088  * @see SC_mod_tools_dbint_index::func_relations(), SC_mod_tools_dbint_index::func_records()
00089  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00090  * @package TYPO3
00091  * @subpackage t3lib
00092  */
00093 class t3lib_admin {
00094     var $genTree_includeDeleted = TRUE;     // if set, genTree() includes deleted pages. This is default.
00095     var $genTree_includeVersions = TRUE;        // if set, genTree() includes verisonized pages/records. This is default.
00096     var $genTree_includeRecords = FALSE;        // if set, genTree() includes records from pages.
00097     var $perms_clause = '';             // extra where-clauses for the tree-selection
00098     var $genTree_makeHTML = 0;          // if set, genTree() generates HTML, that visualizes the tree.
00099 
00100         // internal
00101     var $page_idArray = Array();        // Will hod id/rec pais from genTree()
00102     var $rec_idArray = Array();
00103     var $getTree_HTML = '';         // Will hold the HTML-code visualising the tree. genTree()
00104     var $backPath = '';
00105 
00106         // internal
00107     var $checkFileRefs = Array();
00108     var $checkSelectDBRefs = Array();   // From the select-fields
00109     var $checkGroupDBRefs = Array();    // From the group-fields
00110 
00111     var $recStats = Array(
00112         'allValid' => array(),
00113         'published_versions' => array(),
00114         'deleted' => array(),
00115     );
00116     var $lRecords = Array();
00117     var $lostPagesList = '';
00118 
00119 
00120     /**
00121      * Generates a list of Page-uid's that corresponds to the tables in the tree. This list should ideally include all records in the pages-table.
00122      *
00123      * @param   integer     a pid (page-record id) from which to start making the tree
00124      * @param   string      HTML-code (image-tags) used when this function calls itself recursively.
00125      * @param   boolean     Internal variable, don't set from outside!
00126      * @return  void
00127      */
00128     function genTree($theID, $depthData, $versions=FALSE)   {
00129 
00130         if ($versions)  {
00131             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00132                 'uid,title,doktype,deleted,t3ver_wsid,t3ver_id,t3ver_count,t3ver_swapmode'.(t3lib_extMgm::isLoaded('cms')?',hidden':''),
00133                 'pages',
00134                 'pid=-1 AND t3ver_oid='.intval($theID).' '.((!$this->genTree_includeDeleted)?'AND deleted=0':'').$this->perms_clause,
00135                 '',
00136                 'sorting'
00137             );
00138         } else {
00139             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00140                 'uid,title,doktype,deleted'.(t3lib_extMgm::isLoaded('cms')?',hidden':''),
00141                 'pages',
00142                 'pid='.intval($theID).' '.((!$this->genTree_includeDeleted)?'AND deleted=0':'').$this->perms_clause,
00143                 '',
00144                 'sorting'
00145             );
00146         }
00147 
00148             // Traverse the records selected:
00149         $a = 0;
00150         $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
00151         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))  {
00152 
00153                 // Prepare the additional label used in the HTML output in case of versions:
00154             if ($versions)  {
00155                 $versionLabel = '[v1.'.$row['t3ver_id'].'; WS#'.$row['t3ver_wsid'].']';
00156             } else $versionLabel='';
00157 
00158             $a++;
00159             $newID = $row['uid'];
00160 
00161                 // Build HTML output:
00162             if ($this->genTree_makeHTML)    {
00163                 $this->genTree_HTML.=chr(10).'<div><span class="nobr">';
00164                 $PM = 'join';
00165                 $LN = ($a==$c)?'blank':'line';
00166                 $BTM = ($a==$c)?'bottom':'';
00167                 $this->genTree_HTML.= $depthData.
00168                     '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" alt="" />'.
00169                     $versionLabel.
00170                     t3lib_iconWorks::getIconImage('pages',$row,$this->backPath,'align="top"').
00171                     htmlspecialchars($row['uid'].': '.t3lib_div::fixed_lgd_cs(strip_tags($row['title']),50)).'</span></div>';
00172             }
00173 
00174                 // Register various data for this item:
00175             $this->page_idArray[$newID]=$row;
00176 
00177             $this->recStats['all_valid']['pages'][$newID] = $newID;
00178 #           if ($versions)  $this->recStats['versions']['pages'][$newID] = $newID;
00179             if ($row['deleted'])    $this->recStats['deleted']['pages'][$newID] = $newID;
00180             if ($versions && $row['t3ver_count']>=1) {
00181                 $this->recStats['published_versions']['pages'][$newID] = $newID;
00182             }
00183 
00184             if ($row['deleted']) {$this->recStat['deleted']++;}
00185             if ($row['hidden']) {$this->recStat['hidden']++;}
00186             $this->recStat['doktype'][$row['doktype']]++;
00187 
00188                 // Create the HTML code prefix for recursive call:
00189             $genHTML =  $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' align="top" alt="" />'.$versionLabel;
00190 
00191                 // If all records should be shown, do so:
00192             if ($this->genTree_includeRecords)  {
00193                 foreach($GLOBALS['TCA'] as $tableName => $cfg)  {
00194                     if ($tableName!='pages') {
00195                         $this->genTree_records($newID, $this->genTree_HTML ? $genHTML : '', $tableName);
00196                     }
00197                 }
00198             }
00199 
00200                 // Add sub pages:
00201             $this->genTree($newID, $this->genTree_HTML ? $genHTML : '');
00202 
00203                 // If versions are included in the tree, add those now:
00204             if ($this->genTree_includeVersions) {
00205                 $this->genTree($newID, $this->genTree_HTML ? $genHTML : '', TRUE);
00206             }
00207         }
00208     }
00209 
00210     /**
00211      * @param   [type]      $theID: ...
00212      * @param   [type]      $depthData: ...
00213      * @param   [type]      $table: ...
00214      * @param   [type]      $versions: ...
00215      * @return  [type]      ...
00216      */
00217     function genTree_records($theID, $depthData, $table='', $versions=FALSE)    {
00218         global $TCA;
00219 
00220         if ($versions)  {
00221                 // Select all records from table pointing to this page:
00222             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00223                 t3lib_BEfunc::getCommonSelectFields($table),
00224                 $table,
00225                 'pid=-1 AND t3ver_oid='.intval($theID).
00226                     (!$this->genTree_includeDeleted?t3lib_BEfunc::deleteClause($table):'')
00227             );
00228         } else {
00229                 // Select all records from table pointing to this page:
00230             $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00231                 t3lib_BEfunc::getCommonSelectFields($table),
00232                 $table,
00233                 'pid='.intval($theID).
00234                     (!$this->genTree_includeDeleted?t3lib_BEfunc::deleteClause($table):'')
00235             );
00236         }
00237 
00238             // Traverse selected:
00239         $a = 0;
00240         $c = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
00241         while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))  {
00242 
00243                 // Prepare the additional label used in the HTML output in case of versions:
00244             if ($versions)  {
00245                 $versionLabel = '[v1.'.$row['t3ver_id'].'; WS#'.$row['t3ver_wsid'].']';
00246             } else $versionLabel='';
00247 
00248             $a++;
00249             $newID = $row['uid'];
00250 
00251                 // Build HTML output:
00252             if ($this->genTree_makeHTML)    {
00253                 $this->genTree_HTML.=chr(10).'<div><span class="nobr">';
00254                 $PM = 'join';
00255                 $LN = ($a==$c)?'blank':'line';
00256                 $BTM = ($a==$c)?'bottom':'';
00257                 $this->genTree_HTML.= $depthData.
00258                     '<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" alt="" />'.
00259                     $versionLabel.
00260                     t3lib_iconWorks::getIconImage($table,$row,$this->backPath,'align="top" title="'.$table.'"').htmlspecialchars($row['uid'].': '.t3lib_BEfunc::getRecordTitle($table,$row)).'</span></div>';
00261             }
00262 
00263                 // Register various data for this item:
00264             $this->rec_idArray[$table][$newID]=$row;
00265 
00266             $this->recStats['all_valid'][$table][$newID] = $newID;
00267 #           $this->recStats[$versions?'versions':'live'][$table][$newID] = $newID;
00268             if ($row['deleted'])    $this->recStats['deleted'][$table][$newID] = $newID;
00269             if ($versions && $row['t3ver_count']>=1 && $row['t3ver_wsid']==0) {
00270                 $this->recStats['published_versions'][$table][$newID] = $newID;
00271             }
00272 
00273 #           if ($row['deleted']) {$this->recStat['deleted']++;}
00274 #           if ($row['hidden']) {$this->recStat['hidden']++;}
00275 
00276 
00277 
00278                 // Select all versions of this record:
00279             if ($this->genTree_includeVersions && $TCA[$table]['ctrl']['versioningWS']) {
00280                 $genHTML =  $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' align="top" alt="" />';
00281 
00282                 $this->genTree_records($newID, $genHTML, $table, TRUE);
00283             }
00284         }
00285     }
00286 
00287     /**
00288      * [Describe function...]
00289      *
00290      * @return  [type]      ...
00291      */
00292     function genTreeStatus($root=0) {
00293         $this->genTree_includeDeleted = TRUE;       // if set, genTree() includes deleted pages. This is default.
00294         $this->genTree_includeVersions = TRUE;      // if set, genTree() includes verisonized pages/records. This is default.
00295         $this->genTree_includeRecords = TRUE;       // if set, genTree() includes records from pages.
00296         $this->perms_clause = '';                   // extra where-clauses for the tree-selection
00297         $this->genTree_makeHTML = 0;                // if set, genTree() generates HTML, that visualizes the tree.
00298 
00299         $this->genTree($root,'');
00300 
00301         return $this->recStats;
00302     }
00303 
00304 
00305 
00306 
00307 
00308 
00309     /**
00310      * Fills $this->lRecords with the records from all tc-tables that are not attached to a PID in the pid-list.
00311      *
00312      * @param   string      list of pid's (page-record uid's). This list is probably made by genTree()
00313      * @return  void
00314      */
00315     function lostRecords($pid_list) {
00316         global $TCA;
00317         reset($TCA);
00318         $this->lostPagesList='';
00319         if ($pid_list)  {
00320             while (list($table)=each($TCA)) {
00321                 t3lib_div::loadTCA($table);
00322 
00323                 $pid_list_tmp = $pid_list;
00324                 if (!isset($TCA[$table]['ctrl']['versioningWS']) || !$TCA[$table]['ctrl']['versioningWS'])  {
00325                         // Remove preceding "-1," for non-versioned tables
00326                     $pid_list_tmp = preg_replace('/^\-1,/','',$pid_list_tmp);
00327                 }
00328 
00329                 $garbage = $GLOBALS['TYPO3_DB']->exec_SELECTquery (
00330                                 'uid,pid,'.$TCA[$table]['ctrl']['label'],
00331                                 $table,
00332                                 'pid NOT IN ('.$pid_list_tmp.')'
00333                             );
00334                 $lostIdList = array();
00335                 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($garbage))  {
00336                     $this->lRecords[$table][$row['uid']]=Array('uid'=>$row['uid'], 'pid'=>$row['pid'], 'title'=> strip_tags($row[$TCA[$table]['ctrl']['label']]) );
00337                     $lostIdList[]=$row['uid'];
00338                 }
00339                 if ($table=='pages')    {
00340                     $this->lostPagesList=implode(',',$lostIdList);
00341                 }
00342             }
00343         }
00344     }
00345 
00346     /**
00347      * Fixes lost record from $table with uid $uid by setting the PID to zero. If there is a disabled column for the record that will be set as well.
00348      *
00349      * @param   string      Database tablename
00350      * @param   integer     The uid of the record which will have the PID value set to 0 (zero)
00351      * @return  boolean     True if done.
00352      */
00353     function fixLostRecord($table,$uid) {
00354         if ($table && $GLOBALS['TCA'][$table] && $uid && is_array($this->lRecords[$table][$uid]) && $GLOBALS['BE_USER']->user['admin']) {
00355 
00356             $updateFields = array();
00357             $updateFields['pid'] = 0;
00358             if ($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'])   {   // If possible a lost record restored is hidden as default
00359                 $updateFields[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']] = 1;
00360             }
00361 
00362             $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
00363 
00364             return TRUE;
00365         } else return FALSE;
00366     }
00367 
00368     /**
00369      * Counts records from $TCA-tables that ARE attached to an existing page.
00370      *
00371      * @param   string      list of pid's (page-record uid's). This list is probably made by genTree()
00372      * @return  array       an array with the number of records from all $TCA-tables that are attached to a PID in the pid-list.
00373      */
00374     function countRecords($pid_list)    {
00375         global $TCA;
00376         reset($TCA);
00377         $list=Array();
00378         $list_n=Array();
00379         if ($pid_list)  {
00380             while (list($table)=each($TCA)) {
00381                 t3lib_div::loadTCA($table);
00382 
00383                 $pid_list_tmp = $pid_list;
00384                 if (!isset($TCA[$table]['ctrl']['versioningWS']) || !$TCA[$table]['ctrl']['versioningWS'])  {
00385                         // Remove preceding "-1," for non-versioned tables
00386                     $pid_list_tmp = preg_replace('/^\-1,/','',$pid_list_tmp);
00387                 }
00388 
00389                 $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list_tmp.')');
00390                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
00391                     $list[$table]=$row[0];
00392                 }
00393 
00394                 $count = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, 'pid IN ('.$pid_list_tmp.')'.t3lib_BEfunc::deleteClause($table));
00395                 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($count)) {
00396                     $list_n[$table]=$row[0];
00397                 }
00398             }
00399         }
00400         return array('all' => $list, 'non_deleted' => $list_n);
00401     }
00402 
00403     /**
00404      * Finding relations in database based on type 'group' (files or database-uid's in a list)
00405      *
00406      * @param   string      $mode = file, $mode = db, $mode = '' (all...)
00407      * @return  array       An array with all fields listed that somehow are references to other records (foreign-keys) or files
00408      */
00409     function getGroupFields($mode)  {
00410         global $TCA;
00411         reset ($TCA);
00412         $result = Array();
00413         while (list($table)=each($TCA)) {
00414             t3lib_div::loadTCA($table);
00415             $cols = $TCA[$table]['columns'];
00416             reset ($cols);
00417             while (list($field,$config)=each($cols))    {
00418                 if ($config['config']['type']=='group') {
00419                     if (
00420                         ((!$mode||$mode=='file') && $config['config']['internal_type']=='file') ||
00421                         ((!$mode||$mode=='db') && $config['config']['internal_type']=='db')
00422                         )   {
00423                         $result[$table][]=$field;
00424                     }
00425                 }
00426                 if ( (!$mode||$mode=='db') && $config['config']['type']=='select' && $config['config']['foreign_table'])    {
00427                     $result[$table][]=$field;
00428                 }
00429             }
00430             if ($result[$table])    {
00431                 $result[$table] = implode(',',$result[$table]);
00432             }
00433         }
00434         return $result;
00435     }
00436 
00437     /**
00438      * Finds all fields that hold filenames from uploadfolder
00439      *
00440      * @param   string      Path to uploadfolder
00441      * @return  array       An array with all fields listed that have references to files in the $uploadfolder
00442      */
00443     function getFileFields($uploadfolder)   {
00444         global $TCA;
00445         reset ($TCA);
00446         $result = Array();
00447         while (list($table)=each($TCA)) {
00448             t3lib_div::loadTCA($table);
00449             $cols = $TCA[$table]['columns'];
00450             reset ($cols);
00451             while (list($field,$config)=each($cols))    {
00452                 if ($config['config']['type']=='group' && $config['config']['internal_type']=='file' && $config['config']['uploadfolder']==$uploadfolder)   {
00453                     $result[]=Array($table,$field);
00454                 }
00455             }
00456         }
00457         return $result;
00458     }
00459 
00460     /**
00461      * Returns an array with arrays of table/field pairs which are allowed to hold references to the input table name - according to $TCA
00462      *
00463      * @param   string      Table name
00464      * @return  array
00465      */
00466     function getDBFields($theSearchTable)   {
00467         global $TCA;
00468         $result = Array();
00469         reset ($TCA);
00470         while (list($table)=each($TCA)) {
00471             t3lib_div::loadTCA($table);
00472             $cols = $TCA[$table]['columns'];
00473             reset ($cols);
00474             while (list($field,$config)=each($cols))    {
00475                 if ($config['config']['type']=='group' && $config['config']['internal_type']=='db') {
00476                     if (trim($config['config']['allowed'])=='*' || strstr($config['config']['allowed'],$theSearchTable))    {
00477                         $result[]=Array($table,$field);
00478                     }
00479                 } else if ($config['config']['type']=='select' && $config['config']['foreign_table']==$theSearchTable)  {
00480                     $result[]=Array($table,$field);
00481                 }
00482             }
00483         }
00484         return $result;
00485     }
00486 
00487     /**
00488      * This selects non-empty-records from the tables/fields in the fkey_array generated by getGroupFields()
00489      *
00490      * @param   array       Array with tables/fields generated by getGroupFields()
00491      * @return  void
00492      * @see getGroupFields()
00493      */
00494     function selectNonEmptyRecordsWithFkeys($fkey_arrays)   {
00495         global $TCA;
00496         if (is_array($fkey_arrays)) {
00497             reset($fkey_arrays);
00498             while (list($table,$field_list)=each($fkey_arrays)) {
00499                 if ($TCA[$table] && trim($field_list))  {
00500                     t3lib_div::loadTCA($table);
00501                     $fieldArr = explode(',',$field_list);
00502 
00503                     if(t3lib_extMgm::isLoaded('dbal')) {
00504                         $fields = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
00505                         reset($fields);
00506                         list(,$field)=each($fieldArr);
00507                         $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') ?
00508                         $field.'!=0' : $field.'!=\'\'';
00509                         while (list(,$field)=each($fieldArr))   {
00510                             $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') ?
00511                             ' OR '.$field.'!=0' : ' OR '.$field.'!=\'\'';
00512                         }
00513                         unset($fields);
00514                     }
00515                     else {
00516                     $cl_fl = implode ('!="" OR ',$fieldArr). '!=""';
00517                     }
00518 
00519                     $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,'.$field_list, $table, $cl_fl);
00520                     while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00521                         reset($fieldArr);
00522                         while (list(,$field)=each($fieldArr))   {
00523                             if (trim($row[$field]))     {
00524                                 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00525                                 if ($fieldConf['type']=='group')    {
00526                                     if ($fieldConf['internal_type']=='file')    {
00527                                         // files...
00528                                         if ($fieldConf['MM'])   {
00529                                             $tempArr=array();
00530                                             $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00531                                             $dbAnalysis->start('','files',$fieldConf['MM'],$row['uid']);
00532                                             reset($dbAnalysis->itemArray);
00533                                             while (list($somekey,$someval)=each($dbAnalysis->itemArray))    {
00534                                                 if ($someval['id']) {
00535                                                     $tempArr[]=$someval['id'];
00536                                                 }
00537                                             }
00538                                         } else {
00539                                             $tempArr = explode(',',trim($row[$field]));
00540                                         }
00541                                         reset($tempArr);
00542                                         while (list(,$file)=each($tempArr)) {
00543                                             $file = trim($file);
00544                                             if ($file)  {
00545                                                 $this->checkFileRefs[$fieldConf['uploadfolder']][$file]+=1;
00546                                             }
00547                                         }
00548                                     }
00549                                     if ($fieldConf['internal_type']=='db')  {
00550                                         // dbs - group
00551                                         $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00552                                         $dbAnalysis->start($row[$field],$fieldConf['allowed'],$fieldConf['MM'],$row['uid'], $table, $fieldConf);
00553                                         reset($dbAnalysis->itemArray);
00554                                         while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00555                                             $this->checkGroupDBRefs[$tempArr['table']][$tempArr['id']]+=1;
00556                                         }
00557                                     }
00558                                 }
00559                                 if ($fieldConf['type']=='select' && $fieldConf['foreign_table'])    {
00560                                     // dbs - select
00561                                     $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00562                                     $dbAnalysis->start($row[$field],$fieldConf['foreign_table'],$fieldConf['MM'],$row['uid'], $table, $fieldConf);
00563                                     reset($dbAnalysis->itemArray);
00564                                     while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00565                                         if ($tempArr['id']>0)   {
00566                                             $this->checkGroupDBRefs[$fieldConf['foreign_table']][$tempArr['id']]+=1;
00567                                         }
00568                                     }
00569                                 }
00570                             }
00571                         }
00572                     }
00573                 }
00574             }
00575         }
00576     }
00577 
00578     /**
00579      * Depends on selectNonEmpty.... to be executed first!!
00580      *
00581      * @return  array       Report over files; keys are "moreReferences", "noReferences", "noFile", "error"
00582      */
00583     function testFileRefs ()    {
00584         $output=Array();
00585         reset($this->checkFileRefs);
00586         while(list($folder,$fileArr)=each($this->checkFileRefs))    {
00587             $path = PATH_site.$folder;
00588             if (@is_dir($path)) {
00589                 $d = dir($path);
00590                 while($entry=$d->read()) {
00591                     if (@is_file($path.'/'.$entry)) {
00592                         if (isset($fileArr[$entry]))    {
00593                             if ($fileArr[$entry] > 1)   {
00594                                 $temp = $this->whereIsFileReferenced($folder,$entry);
00595                                 $tempList = '';
00596                                 while(list(,$inf)=each($temp))  {
00597                                     $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00598                                 }
00599                                 $output['moreReferences'][] = Array($path,$entry,$fileArr[$entry],$tempList);
00600                             }
00601                             unset($fileArr[$entry]);
00602                         } else {
00603                             if (!strstr($entry,'index.htm'))    {
00604                                 $output['noReferences'][] = Array($path,$entry);
00605                             }
00606                         }
00607                     }
00608                 }
00609                 $d->close();
00610                 reset($fileArr);
00611                 $tempCounter=0;
00612                 while(list($file,)=each($fileArr))  {
00613                     $temp = $this->whereIsFileReferenced($folder,$file);
00614                     $tempList = '';
00615                     while(list(,$inf)=each($temp))  {
00616                         $tempList.='['.$inf['table'].']['.$inf['uid'].']['.$inf['field'].'] (pid:'.$inf['pid'].') - ';
00617                     }
00618                     $tempCounter++;
00619                     $output['noFile'][substr($path,-3).'_'.substr($file,0,3).'_'.$tempCounter] = Array($path,$file,$tempList);
00620                 }
00621             } else {
00622                 $output['error'][] = Array($path);
00623             }
00624         }
00625         return $output;
00626     }
00627 
00628     /**
00629      * Depends on selectNonEmpty.... to be executed first!!
00630      *
00631      * @param   array       Table with key/value pairs being table names and arrays with uid numbers
00632      * @return  string      HTML Error message
00633      */
00634     function testDBRefs($theArray)  {
00635         global $TCA;
00636         reset($theArray);
00637         while(list($table,$dbArr)=each($theArray))  {
00638             if ($TCA[$table])   {
00639                 $idlist = Array();
00640                 while(list($id,)=each($dbArr))  {
00641                     $idlist[]=$id;
00642                 }
00643                 $theList = implode(',',$idlist);
00644                 if ($theList)   {
00645                     $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$theList.')'.t3lib_BEfunc::deleteClause($table));
00646                     while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))  {
00647                         if (isset($dbArr[$row['uid']])) {
00648                             unset ($dbArr[$row['uid']]);
00649                         } else {
00650                             $result.='Strange Error. ...<br />';
00651                         }
00652                     }
00653                     reset($dbArr);
00654                     while (list($theId,$theC)=each($dbArr)) {
00655                         $result.='There are '.$theC.' records pointing to this missing or deleted record; ['.$table.']['.$theId.']<br />';
00656                     }
00657                 }
00658             } else {
00659                 $result.='Codeerror. Table is not a table...<br />';
00660             }
00661         }
00662         return $result;
00663     }
00664 
00665     /**
00666      * Finding all references to record based on table/uid
00667      *
00668      * @param   string      Table name
00669      * @param   integer     Uid of database record
00670      * @return  array       Array with other arrays containing information about where references was found
00671      */
00672     function whereIsRecordReferenced($searchTable,$id)  {
00673         global $TCA;
00674         $fileFields = $this->getDBFields($searchTable); // Gets tables / Fields that reference to files...
00675         $theRecordList=Array();
00676         while (list(,$info)=each($fileFields))  {
00677             $table=$info[0];    $field=$info[1];
00678             t3lib_div::loadTCA($table);
00679             $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00680                             'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00681                             $table,
00682                             $field.' LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($id, $table).'%\''
00683                         );
00684             while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00685                     // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
00686                 $fieldConf = $TCA[$table]['columns'][$field]['config'];
00687                 $allowedTables = ($fieldConf['type']=='group') ? $fieldConf['allowed'] : $fieldConf['foreign_table'];
00688 
00689                 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
00690                 $dbAnalysis->start($row[$field],$allowedTables,$fieldConf['MM'],$row['uid'], $table, $fieldConf);
00691                 reset($dbAnalysis->itemArray);
00692                 while (list(,$tempArr)=each($dbAnalysis->itemArray))    {
00693                     if ($tempArr['table']==$searchTable && $tempArr['id']==$id) {
00694                         $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00695                     }
00696                 }
00697             }
00698         }
00699         return $theRecordList;
00700     }
00701 
00702     /**
00703      * Finding all references to file based on uploadfolder / filename
00704      *
00705      * @param   string      Upload folder where file is found
00706      * @param   string      Filename to search for
00707      * @return  array       Array with other arrays containing information about where references was found
00708      */
00709     function whereIsFileReferenced($uploadfolder,$filename) {
00710         global $TCA;
00711         $fileFields = $this->getFileFields($uploadfolder);  // Gets tables / Fields that reference to files...
00712         $theRecordList=Array();
00713         while (list(,$info)=each($fileFields))  {
00714             $table=$info[0];    $field=$info[1];
00715             $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00716                             'uid,pid,'.$TCA[$table]['ctrl']['label'].','.$field,
00717                             $table,
00718                             $field.' LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($filename, $table).'%\''
00719                         );
00720             while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres)) {
00721                 // Now this is the field, where the reference COULD come from. But we're not garanteed, so we must carefully examine the data.
00722                 $tempArr = explode(',',trim($row[$field]));
00723                 while (list(,$file)=each($tempArr)) {
00724                     $file = trim($file);
00725                     if ($file==$filename)   {
00726                         $theRecordList[]=Array('table'=>$table,'uid'=>$row['uid'],'field'=>$field,'pid'=>$row['pid']);
00727                     }
00728                 }
00729             }
00730         }
00731         return $theRecordList;
00732     }
00733 }
00734 
00735 
00736 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php']) {
00737     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_admin.php']);
00738 }
00739 ?>

Generated on Sat Jan 3 04:23:26 2009 for TYPO3 API by  doxygen 1.4.7