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