MediaWiki  master
pruneFileCache.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class PruneFileCache extends Maintenance {
32 
34 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Delete file cache files older than "agedays"' );
38  $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
39  $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
40  }
41 
42  public function execute() {
44 
45  if ( !$wgUseFileCache ) {
46  $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
47  }
48 
49  $age = $this->getOption( 'agedays' );
50  if ( !ctype_digit( $age ) ) {
51  $this->fatalError( "Non-integer 'age' parameter given." );
52  }
53  // Delete items with a TS older than this
54  $this->minSurviveTimestamp = time() - ( 86400 * $age );
55 
56  $dir = $wgFileCacheDirectory;
57  if ( !is_dir( $dir ) ) {
58  $this->fatalError( "Nothing to do -- \$wgFileCacheDirectory directory not found." );
59  }
60 
61  $subDir = $this->getOption( 'subdir' );
62  if ( $subDir !== null ) {
63  if ( !is_dir( "$dir/$subDir" ) ) {
64  $this->fatalError( "The specified subdirectory `$subDir` does not exist." );
65  }
66  $this->output( "Pruning `$dir/$subDir` directory...\n" );
67  $this->prune_directory( "$dir/$subDir", 'report' );
68  $this->output( "Done pruning `$dir/$subDir` directory\n" );
69  } else {
70  $this->output( "Pruning `$dir` directory...\n" );
71  // Note: don't prune things like .cdb files on the top level!
72  $this->prune_directory( $dir, 'report' );
73  $this->output( "Done pruning `$dir` directory\n" );
74  }
75  }
76 
81  protected function prune_directory( $dir, $report = false ) {
82  $tsNow = time();
83  $dirHandle = opendir( $dir );
84  while ( ( $file = readdir( $dirHandle ) ) !== false ) {
85  // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
86  if ( $file[0] != "." ) {
87  // absolute
88  $path = $dir . '/' . $file;
89  if ( is_dir( $path ) ) {
90  if ( $report === 'report' ) {
91  $this->output( "Scanning `$path`...\n" );
92  }
93  $this->prune_directory( $path );
94  } else {
95  $mts = filemtime( $path );
96  // Sanity check the file extension against known cache types
97  if ( $mts < $this->minSurviveTimestamp
98  && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
99  && unlink( $path )
100  ) {
101  $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
102  $this->output( "Deleted `$path` [days=$daysOld]\n" );
103  }
104  }
105  }
106  }
107  closedir( $dirHandle );
108  }
109 }
110 
111 $maintClass = PruneFileCache::class;
112 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
PruneFileCache\prune_directory
prune_directory( $dir, $report=false)
Definition: pruneFileCache.php:81
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
PruneFileCache\execute
execute()
Do the actual work.
Definition: pruneFileCache.php:42
PruneFileCache\$minSurviveTimestamp
$minSurviveTimestamp
Definition: pruneFileCache.php:33
$wgUseFileCache
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
Definition: DefaultSettings.php:2791
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$maintClass
$maintClass
Definition: pruneFileCache.php:111
PruneFileCache\__construct
__construct()
Default constructor.
Definition: pruneFileCache.php:35
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
$wgFileCacheDirectory
$wgFileCacheDirectory
Directory where the cached page will be saved.
Definition: DefaultSettings.php:298
PruneFileCache
Maintenance script that prunes file cache for pages, objects, resources, etc.
Definition: pruneFileCache.php:31