MediaWiki
master
Toggle main menu visibility
pruneFileCache.php
Go to the documentation of this file.
1
<?php
9
10
use
MediaWiki\Maintenance\Maintenance
;
11
12
// @codeCoverageIgnoreStart
13
require_once __DIR__ .
'/Maintenance.php'
;
14
// @codeCoverageIgnoreEnd
15
21
class
PruneFileCache
extends
Maintenance
{
22
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
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;
105
require_once RUN_MAINTENANCE_IF_MAIN;
106
// @codeCoverageIgnoreEnd
$path
$path
Definition
NoLocalSettings.php:14
$maintClass
$maintClass
Definition
addChangeTag.php:56
PruneFileCache
Maintenance script that prunes file cache for pages, objects, resources, etc.
Definition
pruneFileCache.php:21
PruneFileCache\__construct
__construct()
Definition
pruneFileCache.php:26
PruneFileCache\prune_directory
prune_directory( $dir, $report=false)
Definition
pruneFileCache.php:72
PruneFileCache\execute
execute()
Definition
pruneFileCache.php:33
PruneFileCache\$minSurviveTimestamp
int $minSurviveTimestamp
Definition
pruneFileCache.php:24
$wgFileCacheDirectory
$wgFileCacheDirectory
Config variable stub for the FileCacheDirectory setting, for use by phpdoc and IDEs.
Definition
config-vars.php:151
$wgUseFileCache
$wgUseFileCache
Config variable stub for the UseFileCache setting, for use by phpdoc and IDEs.
Definition
config-vars.php:1665
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
pruneFileCache.php
Generated on
for MediaWiki by
1.18.0