MediaWiki  REL1_24
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 
00057     private function doPurge() {
00058         $stdin = $this->getStdin();
00059         $urls = array();
00060 
00061         while ( !feof( $stdin ) ) {
00062             $page = trim( fgets( $stdin ) );
00063             if ( preg_match( '%^https?://%', $page ) ) {
00064                 $urls[] = $page;
00065             } elseif ( $page !== '' ) {
00066                 $title = Title::newFromText( $page );
00067                 if ( $title ) {
00068                     $url = $title->getInternalURL();
00069                     $this->output( "$url\n" );
00070                     $urls[] = $url;
00071                     if ( $this->getOption( 'purge' ) ) {
00072                         $title->invalidateCache();
00073                     }
00074                 } else {
00075                     $this->output( "(Invalid title '$page')\n" );
00076                 }
00077             }
00078         }
00079         $this->output( "Purging " . count( $urls ) . " urls\n" );
00080         $this->sendPurgeRequest( $urls );
00081     }
00082 
00088     private function purgeNamespace( $namespace = false ) {
00089         $dbr = wfGetDB( DB_SLAVE );
00090         $startId = 0;
00091         if ( $namespace === false ) {
00092             $conds = array();
00093         } else {
00094             $conds = array( 'page_namespace' => $namespace );
00095         }
00096         while ( true ) {
00097             $res = $dbr->select( 'page',
00098                 array( 'page_id', 'page_namespace', 'page_title' ),
00099                 $conds + array( 'page_id > ' . $dbr->addQuotes( $startId ) ),
00100                 __METHOD__,
00101                 array(
00102                     'LIMIT' => $this->mBatchSize,
00103                     'ORDER BY' => 'page_id'
00104 
00105                 )
00106             );
00107             if ( !$res->numRows() ) {
00108                 break;
00109             }
00110             $urls = array();
00111             foreach ( $res as $row ) {
00112                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00113                 $url = $title->getInternalURL();
00114                 $urls[] = $url;
00115                 $startId = $row->page_id;
00116             }
00117             $this->sendPurgeRequest( $urls );
00118         }
00119     }
00120 
00125     private function sendPurgeRequest( $urls ) {
00126         if ( $this->hasOption( 'delay' ) ) {
00127             $delay = floatval( $this->getOption( 'delay' ) );
00128             foreach ( $urls as $url ) {
00129                 if ( $this->hasOption( 'verbose' ) ) {
00130                     $this->output( $url . "\n" );
00131                 }
00132                 $u = new SquidUpdate( array( $url ) );
00133                 $u->doUpdate();
00134                 usleep( $delay * 1e6 );
00135             }
00136         } else {
00137             if ( $this->hasOption( 'verbose' ) ) {
00138                 $this->output( implode( "\n", $urls ) . "\n" );
00139             }
00140             $u = new SquidUpdate( $urls );
00141             $u->doUpdate();
00142         }
00143     }
00144 }
00145 
00146 $maintClass = "PurgeList";
00147 require_once RUN_MAINTENANCE_IF_MAIN;