MediaWiki  REL1_24
purgeParserCache.php
Go to the documentation of this file.
00001 <?php
00025 require __DIR__ . '/Maintenance.php';
00026 
00032 class PurgeParserCache extends Maintenance {
00033     public $lastProgress;
00034 
00035     function __construct() {
00036         parent::__construct();
00037         $this->addDescription( "Remove old objects from the parser cache. " .
00038             "This only works when the parser cache is in an SQL database." );
00039         $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
00040         $this->addOption(
00041             'age',
00042             'Delete objects created more than this many seconds ago, assuming $wgParserCacheExpireTime ' .
00043                 'has been consistent.',
00044             false, true );
00045     }
00046 
00047     function execute() {
00048         $inputDate = $this->getOption( 'expiredate' );
00049         $inputAge = $this->getOption( 'age' );
00050         if ( $inputDate !== null ) {
00051             $date = wfTimestamp( TS_MW, strtotime( $inputDate ) );
00052         } elseif ( $inputAge !== null ) {
00053             global $wgParserCacheExpireTime;
00054             $date = wfTimestamp( TS_MW, time() + $wgParserCacheExpireTime - intval( $inputAge ) );
00055         } else {
00056             $this->error( "Must specify either --expiredate or --age", 1 );
00057         }
00058 
00059         $english = Language::factory( 'en' );
00060         $this->output( "Deleting objects expiring before " . $english->timeanddate( $date ) . "\n" );
00061 
00062         $pc = wfGetParserCacheStorage();
00063         $success = $pc->deleteObjectsExpiringBefore( $date, array( $this, 'showProgress' ) );
00064         if ( !$success ) {
00065             $this->error( "\nCannot purge this kind of parser cache.", 1 );
00066         }
00067         $this->showProgress( 100 );
00068         $this->output( "\nDone\n" );
00069     }
00070 
00071     function showProgress( $percent ) {
00072         $percentString = sprintf( "%.2f", $percent );
00073         if ( $percentString === $this->lastProgress ) {
00074             return;
00075         }
00076         $this->lastProgress = $percentString;
00077 
00078         $stars = floor( $percent / 2 );
00079         $this->output( '[' . str_repeat( '*', $stars ) . str_repeat( '.', 50 - $stars ) . '] ' .
00080             "$percentString%\r" );
00081     }
00082 }
00083 
00084 $maintClass = 'PurgeParserCache';
00085 require_once RUN_MAINTENANCE_IF_MAIN;