MediaWiki  REL1_22
pruneFileCache.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00031 class PruneFileCache extends Maintenance {
00032 
00033     protected $minSurviveTimestamp;
00034 
00035     public function __construct() {
00036         parent::__construct();
00037         $this->mDescription = "Build file cache for content pages";
00038         $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
00039         $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
00040     }
00041 
00042     public function execute() {
00043         global $wgUseFileCache, $wgFileCacheDirectory;
00044 
00045         if ( !$wgUseFileCache ) {
00046             $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
00047         }
00048 
00049         $age = $this->getOption( 'agedays' );
00050         if ( !ctype_digit( $age ) ) {
00051             $this->error( "Non-integer 'age' parameter given.", true );
00052         }
00053         // Delete items with a TS older than this
00054         $this->minSurviveTimestamp = time() - ( 86400 * $age );
00055 
00056         $dir = $wgFileCacheDirectory;
00057         if ( !is_dir( $dir ) ) {
00058             $this->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true );
00059         }
00060 
00061         $subDir = $this->getOption( 'subdir' );
00062         if ( $subDir !== null ) {
00063             if ( !is_dir( "$dir/$subDir" ) ) {
00064                 $this->error( "The specified subdirectory `$subDir` does not exist.", true );
00065             }
00066             $this->output( "Pruning `$dir/$subDir` directory...\n" );
00067             $this->prune_directory( "$dir/$subDir", 'report' );
00068             $this->output( "Done pruning `$dir/$subDir` directory\n" );
00069         } else {
00070             $this->output( "Pruning `$dir` directory...\n" );
00071             // Note: don't prune things like .cdb files on the top level!
00072             $this->prune_directory( $dir, 'report' );
00073             $this->output( "Done pruning `$dir` directory\n" );
00074         }
00075     }
00076 
00081     protected function prune_directory( $dir, $report = false ) {
00082         $tsNow = time();
00083         $dirHandle = opendir( $dir );
00084         while ( false !== ( $file = readdir( $dirHandle ) ) ) {
00085             // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
00086             if ( $file[0] != "." ) {
00087                 $path = $dir . '/' . $file; // absolute
00088                 if ( is_dir( $path ) ) {
00089                     if ( $report === 'report' ) {
00090                         $this->output( "Scanning `$path`...\n" );
00091                     }
00092                     $this->prune_directory( $path );
00093                 } else {
00094                     $mts = filemtime( $path );
00095                     // Sanity check the file extension against known cache types
00096                     if ( $mts < $this->minSurviveTimestamp
00097                         && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
00098                         && unlink( $path ) )
00099                     {
00100                         $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
00101                         $this->output( "Deleted `$path` [days=$daysOld]\n" );
00102                     }
00103                 }
00104             }
00105         }
00106         closedir( $dirHandle );
00107     }
00108 }
00109 
00110 $maintClass = "PruneFileCache";
00111 require_once RUN_MAINTENANCE_IF_MAIN;