Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PruneFileCache | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 | |||
prune_directory | |
0.00% |
0 / 16 |
|
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 | |
24 | // @codeCoverageIgnoreStart |
25 | require_once __DIR__ . '/Maintenance.php'; |
26 | // @codeCoverageIgnoreEnd |
27 | |
28 | /** |
29 | * Maintenance script that prunes file cache for pages, objects, resources, etc. |
30 | * |
31 | * @ingroup Maintenance |
32 | */ |
33 | class PruneFileCache extends Maintenance { |
34 | |
35 | /** @var int */ |
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 | // Delete items with a TS older than this |
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 | // 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 | |
80 | /** |
81 | * @param string $dir |
82 | * @param string|bool $report Use 'report' to report the directories being scanned |
83 | */ |
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; |
117 | require_once RUN_MAINTENANCE_IF_MAIN; |
118 | // @codeCoverageIgnoreEnd |