* Added specific page header when showing "search deleted pages" form
[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 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Build file cache for content pages";
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->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
38 }
39
40 $age = $this->getOption( 'agedays' );
41 if ( !ctype_digit( $age ) ) {
42 $this->error( "Non-integer 'age' parameter given.", true );
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->error( "Nothing to do -- \$wgFileCacheDirectory directory not found.", true );
50 }
51
52 $subDir = $this->getOption( 'subdir' );
53 if ( $subDir !== null ) {
54 if ( !is_dir( "$dir/$subDir" ) ) {
55 $this->error( "The specified subdirectory `$subDir` does not exist.", true );
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
68 /**
69 * @param $dir string
70 * @param $report string|bool Use 'report' to report the directories being scanned
71 */
72 protected function prune_directory( $dir, $report = false ) {
73 $tsNow = time();
74 $dirHandle = opendir( $dir );
75 while ( false !== ( $file = readdir( $dirHandle ) ) ) {
76 // Skip ".", "..", and also any dirs or files like ".svn" or ".htaccess"
77 if ( $file[0] != "." ) {
78 $path = $dir . '/' . $file; // absolute
79 if ( is_dir( $path ) ) {
80 if ( $report === 'report' ) {
81 $this->output( "Scanning `$path`...\n" );
82 }
83 $this->prune_directory( $path );
84 } else {
85 $mts = filemtime( $path );
86 // Sanity check the file extension against known cache types
87 if ( $mts < $this->minSurviveTimestamp
88 && preg_match( '/\.(?:html|cache)(?:\.gz)?$/', $file )
89 && unlink( $path ) )
90 {
91 $daysOld = round( ( $tsNow - $mts ) / 86400, 2 );
92 $this->output( "Deleted `$path` [days=$daysOld]\n" );
93 }
94 }
95 }
96 }
97 closedir( $dirHandle );
98 }
99 }
100
101 $maintClass = "PruneFileCache";
102 require_once( RUN_MAINTENANCE_IF_MAIN );