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->addArg( 'start', 'Page_id to start from', true );
00030 $this->addArg( 'overwrite', 'Refresh page cache', false );
00031 $this->setBatchSize( 100 );
00032 }
00033
00034 public function execute() {
00035 global $wgUseFileCache, $wgDisableCounters, $wgContentNamespaces;
00036 global $wgTitle, $wgArticle, $wgOut, $wgUser;
00037 if( !$wgUseFileCache ) {
00038 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
00039 }
00040 $wgDisableCounters = false;
00041 $start = $this->getArg( 0, "0" );
00042 if( !ctype_digit($start) ) {
00043 $this->error( "Invalid value for start parameter.", true );
00044 }
00045 $start = intval($start);
00046 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
00047 $this->output( "Building content page file cache from page {$start}!\n" );
00048
00049 $dbr = wfGetDB( DB_SLAVE );
00050 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
00051 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
00052 if( !$start ) {
00053 $this->error( "Nothing to do.", true );
00054 }
00055
00056 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
00057 OutputPage::setEncodings(); # Not really used yet
00058
00059 # Do remaining chunk
00060 $end += $this->mBatchSize - 1;
00061 $blockStart = $start;
00062 $blockEnd = $start + $this->mBatchSize - 1;
00063
00064 $dbw = wfGetDB( DB_MASTER );
00065
00066 while( $blockEnd <= $end ) {
00067
00068 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
00069 array('page_namespace' => $wgContentNamespaces,
00070 "page_id BETWEEN $blockStart AND $blockEnd" ),
00071 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
00072 );
00073 foreach( $res as $row ) {
00074 $rebuilt = false;
00075 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
00076 if( null == $wgTitle ) {
00077 $this->output( "Page {$row->page_id} has bad title\n" );
00078 continue;
00079 }
00080 $wgOut->setTitle( $wgTitle );
00081 $wgUser->getSkin( $wgTitle );
00082 $wgArticle = new Article( $wgTitle );
00083
00084 if( $wgArticle->isFileCacheable() ) {
00085 $cache = new HTMLFileCache( $wgTitle );
00086 if( $cache->isFileCacheGood() ) {
00087 if( $overwrite ) {
00088 $rebuilt = true;
00089 } else {
00090 $this->output( "Page {$row->page_id} already cached\n" );
00091 continue;
00092 }
00093 }
00094 ob_start( array(&$cache, 'saveToFileCache' ) );
00095 $wgUseFileCache = false;
00096 $wgArticle->view();
00097 @$wgOut->output();
00098 $wgUseFileCache = true;
00099 ob_end_clean();
00100 $wgOut = new OutputPage();
00101 if( $rebuilt )
00102 $this->output( "Re-cached page {$row->page_id}\n" );
00103 else
00104 $this->output( "Cached page {$row->page_id}\n" );
00105 } else {
00106 $this->output( "Page {$row->page_id} not cacheable\n" );
00107 }
00108 $dbw->commit();
00109 }
00110 $blockStart += $this->mBatchSize;
00111 $blockEnd += $this->mBatchSize;
00112 wfWaitForSlaves( 5 );
00113 }
00114 $this->output( "Done!\n" );
00115
00116
00117 if( isset($wgTitle) )
00118 unset($wgTitle);
00119 if( isset($wgArticle) )
00120 unset($wgArticle);
00121 }
00122 }
00123
00124 $maintClass = "RebuildFileCache";
00125 require_once( DO_MAINTENANCE );