MediaWiki  REL1_24
updateSpecialPages.php
Go to the documentation of this file.
00001 <?php
00025 require_once __DIR__ . '/Maintenance.php';
00026 
00032 class UpdateSpecialPages extends Maintenance {
00033     public function __construct() {
00034         parent::__construct();
00035         $this->addOption( 'list', 'List special page names' );
00036         $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
00037             'check correct case by calling this script with --list. ' .
00038             'Ex: --only=BrokenRedirects', false, true );
00039         $this->addOption( 'override', 'Also update pages that have updates disabled' );
00040     }
00041 
00042     public function execute() {
00043         global $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
00044 
00045         $dbw = wfGetDB( DB_MASTER );
00046 
00047         $this->doSpecialPageCacheUpdates( $dbw );
00048 
00049         foreach ( QueryPage::getPages() as $page ) {
00050             list( $class, $special ) = $page;
00051             $limit = isset( $page[2] ) ? $page[2] : null;
00052 
00053             # --list : just show the name of pages
00054             if ( $this->hasOption( 'list' ) ) {
00055                 $this->output( "$special [QueryPage]\n" );
00056                 continue;
00057             }
00058 
00059             if ( !$this->hasOption( 'override' )
00060                 && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate )
00061             ) {
00062                 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
00063                 continue;
00064             }
00065 
00066             $specialObj = SpecialPageFactory::getPage( $special );
00067             if ( !$specialObj ) {
00068                 $this->output( "No such special page: $special\n" );
00069                 exit;
00070             }
00071             if ( $specialObj instanceof QueryPage ) {
00072                 $queryPage = $specialObj;
00073             } else {
00074                 if ( !class_exists( $class ) ) {
00075                     $file = $specialObj->getFile();
00076                     require_once $file;
00077                 }
00078                 $queryPage = new $class;
00079             }
00080 
00081             if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
00082                 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
00083                 if ( $queryPage->isExpensive() ) {
00084                     $t1 = explode( ' ', microtime() );
00085                     # Do the query
00086                     $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
00087                     $t2 = explode( ' ', microtime() );
00088                     if ( $num === false ) {
00089                         $this->output( "FAILED: database error\n" );
00090                     } else {
00091                         $this->output( "got $num rows in " );
00092 
00093                         $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
00094                         $hours = intval( $elapsed / 3600 );
00095                         $minutes = intval( $elapsed % 3600 / 60 );
00096                         $seconds = $elapsed - $hours * 3600 - $minutes * 60;
00097                         if ( $hours ) {
00098                             $this->output( $hours . 'h ' );
00099                         }
00100                         if ( $minutes ) {
00101                             $this->output( $minutes . 'm ' );
00102                         }
00103                         $this->output( sprintf( "%.2fs\n", $seconds ) );
00104                     }
00105                     # Reopen any connections that have closed
00106                     if ( !wfGetLB()->pingAll() ) {
00107                         $this->output( "\n" );
00108                         do {
00109                             $this->error( "Connection failed, reconnecting in 10 seconds..." );
00110                             sleep( 10 );
00111                         } while ( !wfGetLB()->pingAll() );
00112                         $this->output( "Reconnected\n\n" );
00113                     }
00114                     # Wait for the slave to catch up
00115                     wfWaitForSlaves();
00116                 } else {
00117                     $this->output( "cheap, skipped\n" );
00118                 }
00119                 if ( $this->hasOption( 'only' ) ) {
00120                     break;
00121                 }
00122             }
00123         }
00124     }
00125 
00126     public function doSpecialPageCacheUpdates( $dbw ) {
00127         global $wgSpecialPageCacheUpdates;
00128 
00129         foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
00130             # --list : just show the name of pages
00131             if ( $this->hasOption( 'list' ) ) {
00132                 $this->output( "$special [callback]\n" );
00133                 continue;
00134             }
00135 
00136             if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $special ) {
00137                 if ( !is_callable( $call ) ) {
00138                     $this->error( "Uncallable function $call!" );
00139                     continue;
00140                 }
00141                 $this->output( sprintf( '%-30s [callback] ', $special ) );
00142                 $t1 = explode( ' ', microtime() );
00143                 call_user_func( $call, $dbw );
00144                 $t2 = explode( ' ', microtime() );
00145 
00146                 $this->output( "completed in " );
00147                 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
00148                 $hours = intval( $elapsed / 3600 );
00149                 $minutes = intval( $elapsed % 3600 / 60 );
00150                 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
00151                 if ( $hours ) {
00152                     $this->output( $hours . 'h ' );
00153                 }
00154                 if ( $minutes ) {
00155                     $this->output( $minutes . 'm ' );
00156                 }
00157                 $this->output( sprintf( "%.2fs\n", $seconds ) );
00158                 # Wait for the slave to catch up
00159                 wfWaitForSlaves();
00160             }
00161         }
00162     }
00163 }
00164 
00165 $maintClass = "UpdateSpecialPages";
00166 require_once RUN_MAINTENANCE_IF_MAIN;