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