TYPO3 API  SVNRelease
class.missing_files.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2011 Kasper Skårhøj (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  * Cleaner module: Missing files
00029  * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
00030  *
00031  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00032  */
00033 /**
00034  * [CLASS/FUNCTION INDEX of SCRIPT]
00035  *
00036  *
00037  *
00038  *   56: class tx_lowlevel_missing_files extends tx_lowlevel_cleaner_core
00039  *   65:     function tx_lowlevel_missing_files()
00040  *   98:     function main()
00041  *  154:     function main_autoFix($resultArray)
00042  *
00043  * TOTAL FUNCTIONS: 3
00044  * (This index is automatically created/updated by the extension "extdeveval")
00045  *
00046  */
00047 
00048 
00049 /**
00050  * Looking for missing files.
00051  *
00052  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00053  * @package TYPO3
00054  * @subpackage tx_lowlevel
00055  */
00056 class tx_lowlevel_missing_files extends tx_lowlevel_cleaner_core {
00057 
00058     var $checkRefIndex = TRUE;
00059 
00060     /**
00061      * Constructor
00062      *
00063      * @return  void
00064      */
00065     function tx_lowlevel_missing_files()    {
00066         parent::tx_lowlevel_cleaner_core();
00067 
00068             // Setting up help:
00069         $this->cli_help['name'] = 'missing_files -- Find all file references from records pointing to a missing (non-existing) file.';
00070         $this->cli_help['description'] = trim('
00071 Assumptions:
00072 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
00073 - relevant soft reference parsers applied everywhere file references are used inline
00074 
00075 Files may be missing for these reasons (except software bugs):
00076 - 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.
00077 - 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.
00078 
00079 Automatic Repair of Errors:
00080 - 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.
00081 - Soft References: Requires manual fix if you consider it an error.
00082 
00083 Manual repair suggestions:
00084 - 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.
00085 - 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.
00086 ');
00087 
00088         $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner missing_files -s -r
00089 This will show you missing files in the TYPO3 system and only report back if errors were found.';
00090     }
00091 
00092     /**
00093      * Find file references that points to non-existing files in system
00094      * Fix methods: API in t3lib_refindex that allows to change the value of a reference (or remove it)
00095      *
00096      * @return  array
00097      */
00098     function main() {
00099         global $TYPO3_DB;
00100 
00101             // Initialize result array:
00102         $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;
00103         $resultArray = array(
00104             'message' => $this->cli_help['name'].LF.LF.$this->cli_help['description'],
00105             'headers' => array(
00106                 'managedFilesMissing' => array('List of missing files managed by TCEmain', $listExplain, 3),
00107                 'softrefFilesMissing' => array('List of missing files registered as a soft reference', $listExplain, 2),
00108             ),
00109             'managedFilesMissing' => array(),
00110             'softrefFilesMissing' => array(),
00111         );
00112 
00113 
00114             // Select all files in the reference table
00115         $recs = $TYPO3_DB->exec_SELECTgetRows(
00116             '*',
00117             'sys_refindex',
00118             'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex'),
00119             '',
00120             'sorting DESC'
00121         );
00122 
00123             // Traverse the files and put into a large table:
00124         if (is_array($recs)) {
00125             foreach($recs as $rec)  {
00126 
00127                     // Compile info string for location of reference:
00128                 $infoString = $this->infoStr($rec);
00129 
00130                     // Handle missing file:
00131                 if (!@is_file(PATH_site.$rec['ref_string']))    {
00132 
00133                     if ((string)$rec['softref_key']=='')    {
00134                         $resultArrayIndex = 'managedFilesMissing';
00135                     } else {
00136                         $resultArrayIndex = 'softrefFilesMissing';
00137                     }
00138 
00139                     $resultArray[$resultArrayIndex][$rec['ref_string']][$rec['hash']] = $infoString;
00140                     ksort($resultArray[$resultArrayIndex][$rec['ref_string']]); // Sort by array key.
00141                 }
00142             }
00143         }
00144 
00145         ksort($resultArray['managedFilesMissing']);
00146         ksort($resultArray['softrefFilesMissing']);
00147 
00148         return $resultArray;
00149     }
00150 
00151     /**
00152      * Mandatory autofix function
00153      * Will run auto-fix on the result array. Echos status during processing.
00154      *
00155      * @param   array       Result array from main() function
00156      * @return  void
00157      */
00158     function main_autoFix($resultArray) {
00159         foreach($resultArray['managedFilesMissing'] as $key => $value)  {
00160             echo 'Processing file: '.$key.LF;
00161             $c=0;
00162             foreach($value as $hash => $recReference)   {
00163                 echo '  Removing reference in record "'.$recReference.'": ';
00164                 if ($bypass = $this->cli_noExecutionCheck($recReference))   {
00165                     echo $bypass;
00166                 } else {
00167                     $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
00168                     $error = $sysRefObj->setReferenceValue($hash,NULL);
00169                     if ($error) {
00170                         echo '      t3lib_refindex::setReferenceValue(): '.$error.LF;
00171                         echo 'missing_files: exit on error'.LF;
00172                         exit;
00173                     } else echo "DONE";
00174                 }
00175                 echo LF;
00176             }
00177         }
00178     }
00179 }
00180 
00181 ?>