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