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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Maintenance\Maintenance;
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
30/**
31 * Maintenance script that prunes file cache for pages, objects, resources, etc.
32 *
33 * @ingroup Maintenance
34 */
35class PruneFileCache extends Maintenance {
36
37    /** @var int */
38    protected $minSurviveTimestamp;
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() {
48        global $wgUseFileCache, $wgFileCacheDirectory;
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
61        $dir = $wgFileCacheDirectory;
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
82    /**
83     * @param string $dir
84     * @param string|bool $report Use 'report' to report the directories being scanned
85     */
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