| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class PruneFileCache extends Maintenance { |
| 34 |
|
| 35 |
|
| 36 |
protected $minSurviveTimestamp; |
| 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() { |
| 46 |
global $wgUseFileCache, $wgFileCacheDirectory; |
| 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 |
|
| 57 |
$this->minSurviveTimestamp = time() - ( 86400 * $age ); |
| 58 |
|
| 59 |
$dir = $wgFileCacheDirectory; |
| 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 |
|
| 75 |
$this->prune_directory( $dir, 'report' ); |
| 76 |
$this->output( "Done pruning `$dir` directory\n" ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected function prune_directory( $dir, $report = false ) { |
| 85 |
$tsNow = time(); |
| 86 |
$dirHandle = opendir( $dir ); |
| 87 |
|
| 88 |
while ( ( $file = readdir( $dirHandle ) ) !== false ) { |
| 89 |
|
| 90 |
if ( $file[0] != "." ) { |
| 91 |
|
| 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 |
|
| 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 |
|
| 116 |
$maintClass = PruneFileCache::class; |
| 117 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 118 |
|