MediaWiki master
rebuildFileCache.php
Go to the documentation of this file.
1<?php
30use Wikimedia\AtEase\AtEase;
32
33// @codeCoverageIgnoreStart
34require_once __DIR__ . '/Maintenance.php';
35// @codeCoverageIgnoreEnd
36
44 private $enabled = true;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Build the file cache' );
49 $this->addOption( 'start', 'Page_id to start from', false, true );
50 $this->addOption( 'end', 'Page_id to end on', false, true );
51 $this->addOption( 'overwrite', 'Refresh page cache' );
52 $this->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' );
53 $this->setBatchSize( 100 );
54 }
55
56 public function finalSetup( SettingsBuilder $settingsBuilder ) {
57 $this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache );
58 // Script will handle capturing output and saving it itself
59 $settingsBuilder->putConfigValue( MainConfigNames::UseFileCache, false );
60
61 // Avoid DB writes (like enotif/counters)
62 $this->getServiceContainer()->getReadOnlyMode()
63 ->setReason( 'Building cache' );
64
65 // Ensure no debug-specific logic ends up in the cache (must be after Setup.php)
66 MWDebug::deinit();
67
68 parent::finalSetup( $settingsBuilder );
69 }
70
71 public function execute() {
72 if ( !$this->enabled ) {
73 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
74 }
75
76 $start = $this->getOption( 'start', "0" );
77 if ( !ctype_digit( $start ) ) {
78 $this->fatalError( "Invalid value for start parameter." );
79 }
80 $start = intval( $start );
81
82 $end = $this->getOption( 'end', "0" );
83 if ( !ctype_digit( $end ) ) {
84 $this->fatalError( "Invalid value for end parameter." );
85 }
86 $end = intval( $end );
87
88 $this->output( "Building page file cache from page_id {$start}!\n" );
89
90 $dbr = $this->getReplicaDB();
91 $batchSize = $this->getBatchSize();
92 $overwrite = $this->hasOption( 'overwrite' );
93 $start = ( $start > 0 )
94 ? $start
95 : $dbr->newSelectQueryBuilder()
96 ->select( 'MIN(page_id)' )
97 ->from( 'page' )
98 ->caller( __METHOD__ )->fetchField();
99 $end = ( $end > 0 )
100 ? $end
101 : $dbr->newSelectQueryBuilder()
102 ->select( 'MAX(page_id)' )
103 ->from( 'page' )
104 ->caller( __METHOD__ )->fetchField();
105 if ( !$start ) {
106 $this->fatalError( "Nothing to do." );
107 }
108
109 $where = [];
110 if ( !$this->getOption( 'all' ) ) {
111 // If 'all' isn't passed as an option, just fall back to previous behaviour
112 // of using content namespaces
113 $where['page_namespace'] =
114 $this->getServiceContainer()->getNamespaceInfo()->getContentNamespaces();
115 }
116
117 // Mock request (hack, no real client)
118 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
119
120 # Do remaining chunk
121 $end += $batchSize - 1;
122 $blockStart = $start;
123 $blockEnd = $start + $batchSize - 1;
124
125 $dbw = $this->getPrimaryDB();
126 // Go through each page and save the output
127 while ( $blockEnd <= $end ) {
128 // Get the pages
129 $res = $dbr->newSelectQueryBuilder()
130 ->select( [ 'page_namespace', 'page_title', 'page_id' ] )
131 ->from( 'page' )
132 ->useIndex( 'PRIMARY' )
133 ->where( $where )
134 ->andWhere( [
135 $dbr->expr( 'page_id', '>=', (int)$blockStart ),
136 $dbr->expr( 'page_id', '<=', (int)$blockEnd ),
137 ] )
138 ->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC )
139 ->caller( __METHOD__ )->fetchResultSet();
140
141 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
142 foreach ( $res as $row ) {
143 $rebuilt = false;
144
145 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
146 if ( $title === null ) {
147 $this->output( "Page {$row->page_id} has bad title\n" );
148 continue; // broken title?
149 }
150
151 $context = new RequestContext();
152 $context->setTitle( $title );
153 $article = Article::newFromTitle( $title, $context );
154 $context->setWikiPage( $article->getPage() );
155
156 // Some extensions like FlaggedRevs while error out if this is unset
157 RequestContext::getMain()->setTitle( $title );
158
159 // If the article is cacheable, then load it
160 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
161 $viewCache = new HTMLFileCache( $title, 'view' );
162 $historyCache = new HTMLFileCache( $title, 'history' );
163 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
164 if ( $overwrite ) {
165 $rebuilt = true;
166 } else {
167 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
168 continue; // done already!
169 }
170 }
171
172 AtEase::suppressWarnings(); // header notices
173
174 // 1. Cache ?action=view
175 // Be sure to reset the mocked request time (T24852)
176 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
177 ob_start();
178 $article->view();
179 $context->getOutput()->output();
180 $context->getOutput()->clearHTML();
181 $viewHtml = ob_get_clean();
182 $viewCache->saveToFileCache( $viewHtml );
183
184 // 2. Cache ?action=history
185 // Be sure to reset the mocked request time (T24852)
186 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
187 ob_start();
188 Action::factory( 'history', $article, $context )->show();
189 $context->getOutput()->output();
190 $context->getOutput()->clearHTML();
191 $historyHtml = ob_get_clean();
192 $historyCache->saveToFileCache( $historyHtml );
193
194 AtEase::restoreWarnings();
195
196 if ( $rebuilt ) {
197 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
198 } else {
199 $this->output( "Cached page '$title' (id {$row->page_id})..." );
200 }
201 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
202 "history: " . strlen( $historyHtml ) . " bytes]\n" );
203 } else {
204 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
205 }
206 }
207 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes
208
209 $blockStart += $batchSize;
210 $blockEnd += $batchSize;
211 }
212 $this->output( "Done!\n" );
213 }
214}
215
216// @codeCoverageIgnoreStart
217$maintClass = RebuildFileCache::class;
218require_once RUN_MAINTENANCE_IF_MAIN;
219// @codeCoverageIgnoreEnd
Page view caching in the file system.
Group all the pieces relevant to the context of a request into one instance.
Debug toolbar.
Definition MWDebug.php:49
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Builder class for constructing a Config object from a set of sources during bootstrap.
getConfig()
Returns the config loaded so far.
putConfigValue(string $key, $value)
Puts a value into a config variable.
Represents a title within MediaWiki.
Definition Title.php:78
Maintenance script that builds the file cache.
execute()
Do the actual work.
finalSetup(SettingsBuilder $settingsBuilder)
Handle some last-minute setup here.
__construct()
Default constructor.
Build SELECT queries with a fluent interface.