MediaWiki  REL1_19
rebuildFileCache.php
Go to the documentation of this file.
00001 <?php
00023 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00024 
00025 class RebuildFileCache extends Maintenance {
00026         public function __construct() {
00027                 parent::__construct();
00028                 $this->mDescription = "Build file cache for content pages";
00029                 $this->addOption( 'start', 'Page_id to start from', false, true );
00030                 $this->addOption( 'end', 'Page_id to end on', false, true );
00031                 $this->addOption( 'overwrite', 'Refresh page cache' );
00032                 $this->setBatchSize( 100 );
00033         }
00034 
00035         public function finalSetup() {
00036                 global $wgDebugToolbar;
00037 
00038                 // Debug toolbar makes content uncacheable so we disable it.
00039                 // Has to be done before Setup.php initialize MWDebug
00040                 $wgDebugToolbar = false;
00041                 parent::finalSetup();
00042         }
00043 
00044         public function execute() {
00045                 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
00046                 global $wgTitle, $wgOut;
00047                 if ( !$wgUseFileCache ) {
00048                         $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
00049                 }
00050 
00051                 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
00052 
00053                 $start = $this->getOption( 'start', "0" );
00054                 if ( !ctype_digit( $start ) ) {
00055                         $this->error( "Invalid value for start parameter.", true );
00056                 }
00057                 $start = intval( $start );
00058 
00059                 $end = $this->getOption( 'end', "0" );
00060                 if ( !ctype_digit( $end ) ) {
00061                         $this->error( "Invalid value for end parameter.", true );
00062                 }
00063                 $end = intval( $end );
00064 
00065                 $this->output( "Building content page file cache from page {$start}!\n" );
00066 
00067                 $dbr = wfGetDB( DB_SLAVE );
00068                 $overwrite = $this->getOption( 'overwrite', false );
00069                 $start = ( $start > 0 )
00070                         ? $start
00071                         : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
00072                 $end = ( $end > 0 )
00073                         ? $end
00074                         : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
00075                 if ( !$start ) {
00076                         $this->error( "Nothing to do.", true );
00077                 }
00078 
00079                 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
00080 
00081                 # Do remaining chunk
00082                 $end += $this->mBatchSize - 1;
00083                 $blockStart = $start;
00084                 $blockEnd = $start + $this->mBatchSize - 1;
00085 
00086                 $dbw = wfGetDB( DB_MASTER );
00087                 // Go through each page and save the output
00088                 while ( $blockEnd <= $end ) {
00089                         // Get the pages
00090                         $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
00091                                 array( 'page_namespace' => $wgContentNamespaces,
00092                                         "page_id BETWEEN $blockStart AND $blockEnd" ),
00093                                 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
00094                         );
00095 
00096                         $dbw->begin(); // for any changes
00097                         foreach ( $res as $row ) {
00098                                 $rebuilt = false;
00099                                 $wgRequestTime = microtime( true ); # bug 22852
00100                                 
00101                                 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
00102                                 if ( null == $wgTitle ) {
00103                                         $this->output( "Page {$row->page_id} has bad title\n" );
00104                                         continue; // broken title?
00105                                 }
00106 
00107                                 $context = new RequestContext;
00108                                 $context->setTitle( $wgTitle );
00109                                 $article = Article::newFromTitle( $wgTitle, $context );
00110                                 $context->setWikiPage( $article->getPage() );
00111 
00112                                 $wgOut = $context->getOutput(); // set display title
00113 
00114                                 // If the article is cacheable, then load it
00115                                 if ( $article->isFileCacheable() ) {
00116                                         $cache = HTMLFileCache::newFromTitle( $wgTitle, 'view' );
00117                                         if ( $cache->isCacheGood() ) {
00118                                                 if ( $overwrite ) {
00119                                                         $rebuilt = true;
00120                                                 } else {
00121                                                         $this->output( "Page {$row->page_id} already cached\n" );
00122                                                         continue; // done already!
00123                                                 }
00124                                         }
00125                                         ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
00126                                         $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
00127                                         $article->view();
00128                                         wfSuppressWarnings(); // header notices
00129                                         $wgOut->output();
00130                                         wfRestoreWarnings();
00131                                         $wgUseFileCache = true;
00132                                         ob_end_clean(); // clear buffer
00133                                         if ( $rebuilt ) {
00134                                                 $this->output( "Re-cached page {$row->page_id}\n" );
00135                                         } else {
00136                                                 $this->output( "Cached page {$row->page_id}\n" );
00137                                         }
00138                                 } else {
00139                                         $this->output( "Page {$row->page_id} not cacheable\n" );
00140                                 }
00141                         }
00142                         $dbw->commit(); // commit any changes (just for sanity)
00143 
00144                         $blockStart += $this->mBatchSize;
00145                         $blockEnd += $this->mBatchSize;
00146                 }
00147                 $this->output( "Done!\n" );
00148 
00149                 // Remove these to be safe
00150                 if ( isset( $wgTitle ) )
00151                         unset( $wgTitle );
00152         }
00153 }
00154 
00155 $maintClass = "RebuildFileCache";
00156 require_once( RUN_MAINTENANCE_IF_MAIN );