From 41a09896c1fea6edc6716f1882708fbd68358ebe Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 4 Oct 2011 08:03:43 +0000 Subject: [PATCH] * Added a script to prune old file cache entries. * Use default .cache extension for ResourceFileCache. No need for js/css extension, and makes extension sanity check in prune script simpler. * Removed redundant setting of mExt in ObjectFileCache --- includes/cache/ResourceFileCache.php | 4 +- maintenance/pruneFileCache.php | 102 +++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 maintenance/pruneFileCache.php diff --git a/includes/cache/ResourceFileCache.php b/includes/cache/ResourceFileCache.php index bdb9e4ccbf..e73fc2d795 100644 --- a/includes/cache/ResourceFileCache.php +++ b/includes/cache/ResourceFileCache.php @@ -19,9 +19,9 @@ class ResourceFileCache extends FileCacheBase { $cache = new self(); if ( $context->getOnly() === 'styles' ) { - $cache->mType = $cache->mExt = 'css'; + $cache->mType = 'css'; } else { - $cache->mType = $cache->mExt = 'js'; + $cache->mType = 'js'; } $modules = array_unique( $context->getModules() ); // remove duplicates sort( $modules ); // normalize the order (permutation => combination) diff --git a/maintenance/pruneFileCache.php b/maintenance/pruneFileCache.php new file mode 100644 index 0000000000..11b6f997e2 --- /dev/null +++ b/maintenance/pruneFileCache.php @@ -0,0 +1,102 @@ +mDescription = "Build file cache for content pages"; + $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true ); + $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true ); + } + + public function execute() { + global $wgUseFileCache, $wgFileCacheDirectory; + + if ( !$wgUseFileCache ) { + $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true ); + } + + $age = $this->getOption( 'agedays' ); + if ( !ctype_digit( $age ) ) { + $this->error( "Non-integer 'age' parameter given.", true ); + } + // Delete items with a TS older than this + $this->minSurviveTimestamp = time() - ( 86400 * $age ); + + $dir = $wgFileCacheDirectory; + if ( !is_dir( $dir ) ) { + $this->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true ); + } + + $subDir = $this->getOption( 'subdir' ); + if ( $subDir !== null ) { + if ( !is_dir( "$dir/$subDir" ) ) { + $this->error( "The specified subdirectory `$subDir` does not exist.", true ); + } + $this->output( "Pruning `$dir/$subDir` directory...\n" ); + $this->prune_directory( "$dir/$subDir", 'report' ); + $this->output( "Done pruning `$dir/$subDir` directory\n" ); + } else { + $this->output( "Pruning `$dir` directory...\n" ); + // Note: don't prune things like .cdb files on the top level! + $this->prune_directory( $dir, 'report' ); + $this->output( "Done pruning `$dir` directory\n" ); + } + } + + /** + * @param $dir string + * @param $report string|bool Use 'report' to report the directories being scanned + */ + protected function prune_directory( $dir, $report = false ) { + $tsNow = time(); + $dirHandle = opendir( $dir ); + while ( false !== ( $file = readdir( $dirHandle ) ) ) { + // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess" + if ( $file[0] != "." ) { + $path = $dir . '/' . $file; // absolute + if ( is_dir( $path ) ) { + if ( $report === 'report' ) { + $this->output( "Scanning `$path`...\n" ); + } + $this->prune_directory( $path ); + } else { + $mts = filemtime( $path ); + // Sanity check the file extension against known cache types + if ( $mts < $this->minSurviveTimestamp + && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file ) + && unlink( $path ) ) + { + $daysOld = round( ( $tsNow - $mts ) / 86400, 2 ); + $this->output( "Deleted `$path` [days=$daysOld]\n" ); + } + } + } + } + closedir( $dirHandle ); + } +} + +$maintClass = "PruneFileCache"; +require_once( RUN_MAINTENANCE_IF_MAIN ); -- 2.20.1