MediaWiki master
pruneFileCache.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
36
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Delete file cache files older than "agedays"' );
43 $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
44 $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
45 }
46
47 public function execute() {
49
50 if ( !$wgUseFileCache ) {
51 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
52 }
53
54 $age = $this->getOption( 'agedays' );
55 if ( !ctype_digit( $age ) ) {
56 $this->fatalError( "Non-integer 'age' parameter given." );
57 }
58 // Delete items with a TS older than this
59 $this->minSurviveTimestamp = time() - ( 86400 * $age );
60
62 if ( !is_dir( $dir ) ) {
63 $this->fatalError( "Nothing to do -- \$wgFileCacheDirectory directory not found." );
64 }
65
66 $subDir = $this->getOption( 'subdir' );
67 if ( $subDir !== null ) {
68 if ( !is_dir( "$dir/$subDir" ) ) {
69 $this->fatalError( "The specified subdirectory `$subDir` does not exist." );
70 }
71 $this->output( "Pruning `$dir/$subDir` directory...\n" );
72 $this->prune_directory( "$dir/$subDir", 'report' );
73 $this->output( "Done pruning `$dir/$subDir` directory\n" );
74 } else {
75 $this->output( "Pruning `$dir` directory...\n" );
76 // Note: don't prune things like .cdb files on the top level!
77 $this->prune_directory( $dir, 'report' );
78 $this->output( "Done pruning `$dir` directory\n" );
79 }
80 }
81
86 protected function prune_directory( $dir, $report = false ) {
87 $tsNow = time();
88 $dirHandle = opendir( $dir );
89 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
90 while ( ( $file = readdir( $dirHandle ) ) !== false ) {
91 // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
92 if ( $file[0] != "." ) {
93 // absolute
94 $path = $dir . '/' . $file;
95 if ( is_dir( $path ) ) {
96 if ( $report === 'report' ) {
97 $this->output( "Scanning `$path`...\n" );
98 }
99 $this->prune_directory( $path );
100 } else {
101 $mts = filemtime( $path );
102 // Check the file extension against known cache types
103 if ( $mts < $this->minSurviveTimestamp
104 && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
105 && unlink( $path )
106 ) {
107 $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
108 $this->output( "Deleted `$path` [days=$daysOld]\n" );
109 }
110 }
111 }
112 }
113 closedir( $dirHandle );
114 }
115}
116
117// @codeCoverageIgnoreStart
118$maintClass = PruneFileCache::class;
119require_once RUN_MAINTENANCE_IF_MAIN;
120// @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