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