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