Apply jdanni's patch from from 2+ years ago for Bug 15218 - LinkSearch results should...
[lhc/web/wiklou.git] / maintenance / pruneFileCache.php
1 <?php
2 /**
3 * Prune file cache for pages, objects, resources, ect...
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 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class PruneFileCache extends Maintenance {
26
27 protected $minSurviveTimestamp;
28
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "Build file cache for content pages";
32 $this->addOption( 'agedays', 'How many days old files must be in order to delete', true, true );
33 $this->addOption( 'subdir', 'Prune one $wgFileCacheDirectory subdirectory name', false, true );
34 }
35
36 public function execute() {
37 global $wgUseFileCache, $wgFileCacheDirectory;
38
39 if ( !$wgUseFileCache ) {
40 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
41 }
42
43 $age = $this->getOption( 'agedays' );
44 if ( !ctype_digit( $age ) ) {
45 $this->error( "Non-integer 'age' parameter given.", true );
46 }
47 // Delete items with a TS older than this
48 $this->minSurviveTimestamp = time() - ( 86400 * $age );
49
50 $dir = $wgFileCacheDirectory;
51 if ( !is_dir( $dir ) ) {
52 $this->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true );
53 }
54
55 $subDir = $this->getOption( 'subdir' );
56 if ( $subDir !== null ) {
57 if ( !is_dir( "$dir/$subDir" ) ) {
58 $this->error( "The specified subdirectory `$subDir` does not exist.", true );
59 }
60 $this->output( "Pruning `$dir/$subDir` directory...\n" );
61 $this->prune_directory( "$dir/$subDir", 'report' );
62 $this->output( "Done pruning `$dir/$subDir` directory\n" );
63 } else {
64 $this->output( "Pruning `$dir` directory...\n" );
65 // Note: don't prune things like .cdb files on the top level!
66 $this->prune_directory( $dir, 'report' );
67 $this->output( "Done pruning `$dir` directory\n" );
68 }
69 }
70
71 /**
72 * @param $dir string
73 * @param $report string|bool Use 'report' to report the directories being scanned
74 */
75 protected function prune_directory( $dir, $report = false ) {
76 $tsNow = time();
77 $dirHandle = opendir( $dir );
78 while ( false !== ( $file = readdir( $dirHandle ) ) ) {
79 // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
80 if ( $file[0] != "." ) {
81 $path = $dir . '/' . $file; // absolute
82 if ( is_dir( $path ) ) {
83 if ( $report === 'report' ) {
84 $this->output( "Scanning `$path`...\n" );
85 }
86 $this->prune_directory( $path );
87 } else {
88 $mts = filemtime( $path );
89 // Sanity check the file extension against known cache types
90 if ( $mts < $this->minSurviveTimestamp
91 && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
92 && unlink( $path ) )
93 {
94 $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
95 $this->output( "Deleted `$path` [days=$daysOld]\n" );
96 }
97 }
98 }
99 }
100 closedir( $dirHandle );
101 }
102 }
103
104 $maintClass = "PruneFileCache";
105 require_once( RUN_MAINTENANCE_IF_MAIN );