3 * Prune file cache for pages, objects, resources, etc.
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.
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.
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
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance
;
26 // @codeCoverageIgnoreStart
27 require_once __DIR__
. '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
31 * Maintenance script that prunes file cache for pages, objects, resources, etc.
33 * @ingroup Maintenance
35 class PruneFileCache
extends Maintenance
{
38 protected $minSurviveTimestamp;
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 );
47 public function execute() {
48 global $wgUseFileCache, $wgFileCacheDirectory;
50 if ( !$wgUseFileCache ) {
51 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
54 $age = $this->getOption( 'agedays' );
55 if ( !ctype_digit( $age ) ) {
56 $this->fatalError( "Non-integer 'age' parameter given." );
58 // Delete items with a TS older than this
59 $this->minSurviveTimestamp
= time() - ( 86400 * $age );
61 $dir = $wgFileCacheDirectory;
62 if ( !is_dir( $dir ) ) {
63 $this->fatalError( "Nothing to do -- \$wgFileCacheDirectory directory not found." );
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." );
71 $this->output( "Pruning `$dir/$subDir` directory...\n" );
72 $this->prune_directory( "$dir/$subDir", 'report' );
73 $this->output( "Done pruning `$dir/$subDir` directory\n" );
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" );
84 * @param string|bool $report Use 'report' to report the directories being scanned
86 protected function prune_directory( $dir, $report = false ) {
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] != "." ) {
94 $path = $dir . '/' . $file;
95 if ( is_dir( $path ) ) {
96 if ( $report === 'report' ) {
97 $this->output( "Scanning `$path`...\n" );
99 $this->prune_directory( $path );
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 )
107 $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
108 $this->output( "Deleted `$path` [days=$daysOld]\n" );
113 closedir( $dirHandle );
117 // @codeCoverageIgnoreStart
118 $maintClass = PruneFileCache
::class;
119 require_once RUN_MAINTENANCE_IF_MAIN
;
120 // @codeCoverageIgnoreEnd