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