Merge "Fix comment ResourceLoader::tryRespondFromFileCache()."
[lhc/web/wiklou.git] / maintenance / purgeParserCache.php
1 <?php
2 /**
3 * @ingroup Maintenance
4 * @file
5 */
6
7 require( dirname( __FILE__ ) . '/Maintenance.php' );
8
9 class PurgeParserCache extends Maintenance {
10 var $lastProgress;
11
12 function __construct() {
13 parent::__construct();
14 $this->addDescription( "Remove old objects from the parser cache. " .
15 "This only works when the parser cache is in an SQL database." );
16 $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
17 $this->addOption( 'age',
18 'Delete objects created more than this many seconds ago, assuming $wgParserCacheExpireTime '.
19 'has been consistent.',
20 false, true );
21 }
22
23 function execute() {
24 $inputDate = $this->getOption( 'expiredate' );
25 $inputAge = $this->getOption( 'age' );
26 if ( $inputDate !== null ) {
27 $date = wfTimestamp( TS_MW, strtotime( $inputDate ) );
28 } elseif ( $inputAge !== null ) {
29 global $wgParserCacheExpireTime;
30 $date = wfTimestamp( TS_MW, time() + $wgParserCacheExpireTime - intval( $inputAge ) );
31 } else {
32 echo "Must specify either --expiredate or --age\n";
33 exit( 1 );
34 }
35
36 $english = Language::factory( 'en' );
37 echo "Deleting objects expiring before " . $english->timeanddate( $date ) . "\n";
38
39 $pc = wfGetParserCacheStorage();
40 $success = $pc->deleteObjectsExpiringBefore( $date, array( $this, 'showProgress' ) );
41 if ( !$success ) {
42 echo "\nCannot purge this kind of parser cache.\n";
43 exit( 1 );
44 }
45 $this->showProgress( 100 );
46 echo "\nDone\n";
47 }
48
49 function showProgress( $percent ) {
50 $percentString = sprintf( "%.2f", $percent );
51 if ( $percentString === $this->lastProgress ) {
52 return;
53 }
54 $this->lastProgress = $percentString;
55
56 $stars = floor( $percent / 2 );
57 echo '[' . str_repeat( '*', $stars ), str_repeat( '.', 50 - $stars ) . '] ' .
58 "$percentString%\r";
59
60 }
61 }
62 $maintClass = 'PurgeParserCache';
63 require_once( RUN_MAINTENANCE_IF_MAIN );