Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PruneFileCache
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
 prune_directory
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Prune file cache for pages, objects, resources, etc.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10use MediaWiki\Maintenance\Maintenance;
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
16/**
17 * Maintenance script that prunes file cache for pages, objects, resources, etc.
18 *
19 * @ingroup Maintenance
20 */
21class PruneFileCache extends Maintenance {
22
23    /** @var int */
24    protected $minSurviveTimestamp;
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() {
34        global $wgUseFileCache, $wgFileCacheDirectory;
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
47        $dir = $wgFileCacheDirectory;
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
68    /**
69     * @param string $dir
70     * @param string|bool $report Use 'report' to report the directories being scanned
71     */
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