MediaWiki  REL1_21
purgeList.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/Maintenance.php' );
00025 
00031 class PurgeList extends Maintenance {
00032         public function __construct() {
00033                 parent::__construct();
00034                 $this->mDescription = "Send purge requests for listed pages to squid";
00035                 $this->addOption( 'purge', 'Whether to update page_touched.', false, false );
00036                 $this->addOption( 'namespace', 'Namespace number', false, true );
00037                 $this->addOption( 'all', 'Purge all pages', false, false );
00038                 $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
00039                 $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
00040                 $this->setBatchSize( 100 );
00041         }
00042 
00043         public function execute() {
00044                 if ( $this->hasOption( 'all' ) ) {
00045                         $this->purgeNamespace( false );
00046                 } elseif ( $this->hasOption( 'namespace' ) ) {
00047                         $this->purgeNamespace( intval( $this->getOption( 'namespace') ) );
00048                 } else {
00049                         $this->doPurge();
00050                 }
00051                 $this->output( "Done!\n" );
00052         }
00053 
00055         private function doPurge() {
00056                 $stdin = $this->getStdin();
00057                 $urls = array();
00058 
00059                 while ( !feof( $stdin ) ) {
00060                         $page = trim( fgets( $stdin ) );
00061                         if ( preg_match( '%^https?://%', $page ) ) {
00062                                 $urls[] = $page;
00063                         } elseif ( $page !== '' ) {
00064                                 $title = Title::newFromText( $page );
00065                                 if ( $title ) {
00066                                         $url = $title->getInternalUrl();
00067                                         $this->output( "$url\n" );
00068                                         $urls[] = $url;
00069                                         if ( $this->getOption( 'purge' ) ) {
00070                                                 $title->invalidateCache();
00071                                         }
00072                                 } else {
00073                                         $this->output( "(Invalid title '$page')\n" );
00074                                 }
00075                         }
00076                 }
00077                 $this->output( "Purging " . count( $urls ). " urls\n" );
00078                 $this->sendPurgeRequest( $urls );
00079         }
00080 
00082         private function purgeNamespace( $namespace = false ) {
00083                 $dbr = wfGetDB( DB_SLAVE );
00084                 $startId = 0;
00085                 if ( $namespace === false ) {
00086                         $conds = array();
00087                 } else {
00088                         $conds = array( 'page_namespace' => $namespace );
00089                 }
00090                 while ( true ) {
00091                         $res = $dbr->select( 'page', 
00092                                 array( 'page_id', 'page_namespace', 'page_title' ),
00093                                 $conds + array( 'page_id > ' . $dbr->addQuotes( $startId ) ),
00094                                 __METHOD__,
00095                                 array(
00096                                         'LIMIT' => $this->mBatchSize,
00097                                         'ORDER BY' => 'page_id'
00098 
00099                                 )
00100                         );
00101                         if ( !$res->numRows() ) {
00102                                 break;
00103                         }
00104                         $urls = array();
00105                         foreach ( $res as $row ) {
00106                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00107                                 $url = $title->getInternalUrl();
00108                                 $urls[] = $url;
00109                                 $startId = $row->page_id;
00110                         }
00111                         $this->sendPurgeRequest( $urls );
00112                 }
00113         }
00114 
00119         private function sendPurgeRequest( $urls ) {
00120                 if ( $this->hasOption( 'delay' ) ) {
00121                         $delay = floatval( $this->getOption( 'delay' ) );
00122                         foreach ( $urls as $url ) {
00123                                 if ( $this->hasOption( 'verbose' ) ) {
00124                                         $this->output( $url . "\n" );
00125                                 }
00126                                 $u = new SquidUpdate( array( $url ) );
00127                                 $u->doUpdate();
00128                                 usleep( $delay * 1e6 );
00129                         }
00130                 } else {
00131                         if ( $this->hasOption( 'verbose' ) ) {
00132                                 $this->output( implode( "\n", $urls ) . "\n"  );
00133                         }
00134                         $u = new SquidUpdate( $urls );
00135                         $u->doUpdate();
00136                 }
00137         }
00138 
00139 }
00140 
00141 $maintClass = "PurgeList";
00142 require_once( RUN_MAINTENANCE_IF_MAIN );