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
|
|
|
require_once __DIR__ . '/Maintenance.php'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Maintenance script that prunes file cache for pages, objects, resources, etc. |
28
|
|
|
* |
29
|
|
|
* @ingroup Maintenance |
30
|
|
|
*/ |
31
|
|
|
class PruneFileCache extends Maintenance { |
32
|
|
|
|
33
|
|
|
protected $minSurviveTimestamp; |
34
|
|
|
|
35
|
|
|
public function __construct() { |
36
|
|
|
parent::__construct(); |
37
|
|
|
$this->addDescription( 'Build file cache for content pages' ); |
38
|
|
|
$this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true ); |
39
|
|
|
$this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function execute() { |
43
|
|
|
global $wgUseFileCache, $wgFileCacheDirectory; |
44
|
|
|
|
45
|
|
|
if ( !$wgUseFileCache ) { |
46
|
|
|
$this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$age = $this->getOption( 'agedays' ); |
50
|
|
|
if ( !ctype_digit( $age ) ) { |
51
|
|
|
$this->error( "Non-integer 'age' parameter given.", true ); |
52
|
|
|
} |
53
|
|
|
// Delete items with a TS older than this |
54
|
|
|
$this->minSurviveTimestamp = time() - ( 86400 * $age ); |
55
|
|
|
|
56
|
|
|
$dir = $wgFileCacheDirectory; |
57
|
|
|
if ( !is_dir( $dir ) ) { |
58
|
|
|
$this->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$subDir = $this->getOption( 'subdir' ); |
62
|
|
|
if ( $subDir !== null ) { |
63
|
|
|
if ( !is_dir( "$dir/$subDir" ) ) { |
64
|
|
|
$this->error( "The specified subdirectory `$subDir` does not exist.", true ); |
65
|
|
|
} |
66
|
|
|
$this->output( "Pruning `$dir/$subDir` directory...\n" ); |
67
|
|
|
$this->prune_directory( "$dir/$subDir", 'report' ); |
68
|
|
|
$this->output( "Done pruning `$dir/$subDir` directory\n" ); |
69
|
|
|
} else { |
70
|
|
|
$this->output( "Pruning `$dir` directory...\n" ); |
71
|
|
|
// Note: don't prune things like .cdb files on the top level! |
72
|
|
|
$this->prune_directory( $dir, 'report' ); |
73
|
|
|
$this->output( "Done pruning `$dir` directory\n" ); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $dir |
79
|
|
|
* @param string|bool $report Use 'report' to report the directories being scanned |
80
|
|
|
*/ |
81
|
|
|
protected function prune_directory( $dir, $report = false ) { |
82
|
|
|
$tsNow = time(); |
83
|
|
|
$dirHandle = opendir( $dir ); |
84
|
|
|
while ( false !== ( $file = readdir( $dirHandle ) ) ) { |
85
|
|
|
// Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess" |
86
|
|
|
if ( $file[0] != "." ) { |
87
|
|
|
$path = $dir . '/' . $file; // absolute |
88
|
|
|
if ( is_dir( $path ) ) { |
89
|
|
|
if ( $report === 'report' ) { |
90
|
|
|
$this->output( "Scanning `$path`...\n" ); |
91
|
|
|
} |
92
|
|
|
$this->prune_directory( $path ); |
93
|
|
|
} else { |
94
|
|
|
$mts = filemtime( $path ); |
95
|
|
|
// Sanity check the file extension against known cache types |
96
|
|
|
if ( $mts < $this->minSurviveTimestamp |
97
|
|
|
&& preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file ) |
98
|
|
|
&& unlink( $path ) |
99
|
|
|
) { |
100
|
|
|
$daysOld = round( ( $tsNow - $mts ) / 86400, 2 ); |
101
|
|
|
$this->output( "Deleted `$path` [days=$daysOld]\n" ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
closedir( $dirHandle ); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$maintClass = "PruneFileCache"; |
111
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN; |
112
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.