MediaWiki master
pruneFileCache.php
Go to the documentation of this file.
1<?php
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
22
25
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( 'Delete file cache files older than "agedays"' );
29 $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
30 $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
31 }
32
33 public function execute() {
35
36 if ( !$wgUseFileCache ) {
37 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
38 }
39
40 $age = $this->getOption( 'agedays' );
41 if ( !ctype_digit( $age ) ) {
42 $this->fatalError( "Non-integer 'age' parameter given." );
43 }
44 // Delete items with a TS older than this
45 $this->minSurviveTimestamp = time() - ( 86400 * $age );
46
48 if ( !is_dir( $dir ) ) {
49 $this->fatalError( "Nothing to do -- \$wgFileCacheDirectory directory not found." );
50 }
51
52 $subDir = $this->getOption( 'subdir' );
53 if ( $subDir !== null ) {
54 if ( !is_dir( "$dir/$subDir" ) ) {
55 $this->fatalError( "The specified subdirectory `$subDir` does not exist." );
56 }
57 $this->output( "Pruning `$dir/$subDir` directory...\n" );
58 $this->prune_directory( "$dir/$subDir", 'report' );
59 $this->output( "Done pruning `$dir/$subDir` directory\n" );
60 } else {
61 $this->output( "Pruning `$dir` directory...\n" );
62 // Note: don't prune things like .cdb files on the top level!
63 $this->prune_directory( $dir, 'report' );
64 $this->output( "Done pruning `$dir` directory\n" );
65 }
66 }
67
72 protected function prune_directory( $dir, $report = false ) {
73 $tsNow = time();
74 $dirHandle = opendir( $dir );
75 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
76 while ( ( $file = readdir( $dirHandle ) ) !== false ) {
77 // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
78 if ( $file[0] != "." ) {
79 // absolute
80 $path = $dir . '/' . $file;
81 if ( is_dir( $path ) ) {
82 if ( $report === 'report' ) {
83 $this->output( "Scanning `$path`...\n" );
84 }
85 $this->prune_directory( $path );
86 } else {
87 $mts = filemtime( $path );
88 // Check the file extension against known cache types
89 if ( $mts < $this->minSurviveTimestamp
90 && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
91 && unlink( $path )
92 ) {
93 $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
94 $this->output( "Deleted `$path` [days=$daysOld]\n" );
95 }
96 }
97 }
98 }
99 closedir( $dirHandle );
100 }
101}
102
103// @codeCoverageIgnoreStart
104$maintClass = PruneFileCache::class;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Maintenance script that prunes file cache for pages, objects, resources, etc.
__construct()
Default constructor.
prune_directory( $dir, $report=false)
execute()
Do the actual work.
$wgFileCacheDirectory
Config variable stub for the FileCacheDirectory setting, for use by phpdoc and IDEs.
$wgUseFileCache
Config variable stub for the UseFileCache setting, for use by phpdoc and IDEs.
$maintClass