MediaWiki
REL1_23
|
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 00086 private function purgeNamespace( $namespace = false ) { 00087 $dbr = wfGetDB( DB_SLAVE ); 00088 $startId = 0; 00089 if ( $namespace === false ) { 00090 $conds = array(); 00091 } else { 00092 $conds = array( 'page_namespace' => $namespace ); 00093 } 00094 while ( true ) { 00095 $res = $dbr->select( 'page', 00096 array( 'page_id', 'page_namespace', 'page_title' ), 00097 $conds + array( 'page_id > ' . $dbr->addQuotes( $startId ) ), 00098 __METHOD__, 00099 array( 00100 'LIMIT' => $this->mBatchSize, 00101 'ORDER BY' => 'page_id' 00102 00103 ) 00104 ); 00105 if ( !$res->numRows() ) { 00106 break; 00107 } 00108 $urls = array(); 00109 foreach ( $res as $row ) { 00110 $title = Title::makeTitle( $row->page_namespace, $row->page_title ); 00111 $url = $title->getInternalURL(); 00112 $urls[] = $url; 00113 $startId = $row->page_id; 00114 } 00115 $this->sendPurgeRequest( $urls ); 00116 } 00117 } 00118 00123 private function sendPurgeRequest( $urls ) { 00124 if ( $this->hasOption( 'delay' ) ) { 00125 $delay = floatval( $this->getOption( 'delay' ) ); 00126 foreach ( $urls as $url ) { 00127 if ( $this->hasOption( 'verbose' ) ) { 00128 $this->output( $url . "\n" ); 00129 } 00130 $u = new SquidUpdate( array( $url ) ); 00131 $u->doUpdate(); 00132 usleep( $delay * 1e6 ); 00133 } 00134 } else { 00135 if ( $this->hasOption( 'verbose' ) ) { 00136 $this->output( implode( "\n", $urls ) . "\n" ); 00137 } 00138 $u = new SquidUpdate( $urls ); 00139 $u->doUpdate(); 00140 } 00141 } 00142 00143 } 00144 00145 $maintClass = "PurgeList"; 00146 require_once RUN_MAINTENANCE_IF_MAIN;