MediaWiki  1.30.0
pruneFileCache.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class PruneFileCache extends Maintenance {
32 
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() {
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 
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 
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;
PruneFileCache\prune_directory
prune_directory( $dir, $report=false)
Definition: pruneFileCache.php:81
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
PruneFileCache\execute
execute()
Do the actual work.
Definition: pruneFileCache.php:42
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
PruneFileCache\$minSurviveTimestamp
$minSurviveTimestamp
Definition: pruneFileCache.php:33
$wgUseFileCache
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
Definition: DefaultSettings.php:2554
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$dir
$dir
Definition: Autoload.php:8
$maintClass
$maintClass
Definition: pruneFileCache.php:110
PruneFileCache\__construct
__construct()
Default constructor.
Definition: pruneFileCache.php:35
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$path
$path
Definition: NoLocalSettings.php:26
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
$wgFileCacheDirectory
$wgFileCacheDirectory
Directory where the cached page will be saved.
Definition: DefaultSettings.php:272
PruneFileCache
Maintenance script that prunes file cache for pages, objects, resources, etc.
Definition: pruneFileCache.php:31