MediaWiki REL1_40
rebuildFileCache.php
Go to the documentation of this file.
1<?php
28use 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 MediaWiki\MediaWikiServices::getInstance()->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->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
90 $end = ( $end > 0 )
91 ? $end
92 : $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
93 if ( !$start ) {
94 $this->fatalError( "Nothing to do." );
95 }
96
97 $where = [];
98 if ( !$this->getOption( 'all' ) ) {
99 // If 'all' isn't passed as an option, just fall back to previous behaviour
100 // of using content namespaces
101 $where['page_namespace'] =
102 MediaWikiServices::getInstance()->getNamespaceInfo()->getContentNamespaces();
103 }
104
105 // Mock request (hack, no real client)
106 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
107
108 # Do remaining chunk
109 $end += $batchSize - 1;
110 $blockStart = $start;
111 $blockEnd = $start + $batchSize - 1;
112
113 $dbw = $this->getDB( DB_PRIMARY );
114 // Go through each page and save the output
115 while ( $blockEnd <= $end ) {
116 // Get the pages
117 $res = $dbr->select( 'page',
118 [ 'page_namespace', 'page_title', 'page_id' ],
119 $where + [ "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
120 __METHOD__,
121 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
122 );
123
124 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
125 foreach ( $res as $row ) {
126 $rebuilt = false;
127
128 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
129 if ( $title === null ) {
130 $this->output( "Page {$row->page_id} has bad title\n" );
131 continue; // broken title?
132 }
133
134 $context = new RequestContext();
135 $context->setTitle( $title );
136 $article = Article::newFromTitle( $title, $context );
137 $context->setWikiPage( $article->getPage() );
138
139 // Some extensions like FlaggedRevs while error out if this is unset
140 RequestContext::getMain()->setTitle( $title );
141
142 // If the article is cacheable, then load it
143 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
144 $viewCache = new HTMLFileCache( $title, 'view' );
145 $historyCache = new HTMLFileCache( $title, 'history' );
146 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
147 if ( $overwrite ) {
148 $rebuilt = true;
149 } else {
150 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
151 continue; // done already!
152 }
153 }
154
155 AtEase::suppressWarnings(); // header notices
156
157 // 1. Cache ?action=view
158 // Be sure to reset the mocked request time (T24852)
159 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
160 ob_start();
161 $article->view();
162 $context->getOutput()->output();
163 $context->getOutput()->clearHTML();
164 $viewHtml = ob_get_clean();
165 $viewCache->saveToFileCache( $viewHtml );
166
167 // 2. Cache ?action=history
168 // Be sure to reset the mocked request time (T24852)
169 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
170 ob_start();
171 Action::factory( 'history', $article, $context )->show();
172 $context->getOutput()->output();
173 $context->getOutput()->clearHTML();
174 $historyHtml = ob_get_clean();
175 $historyCache->saveToFileCache( $historyHtml );
176
177 AtEase::restoreWarnings();
178
179 if ( $rebuilt ) {
180 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
181 } else {
182 $this->output( "Cached page '$title' (id {$row->page_id})..." );
183 }
184 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
185 "history: " . strlen( $historyHtml ) . " bytes]\n" );
186 } else {
187 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
188 }
189 }
190 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes
191
192 $blockStart += $batchSize;
193 $blockEnd += $batchSize;
194 }
195 $this->output( "Done!\n" );
196 }
197}
198
199$maintClass = RebuildFileCache::class;
200require_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.
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.
Service locator for MediaWiki core services.
Builder class for constructing a Config object from a set of sources during bootstrap.
Represents a title within MediaWiki.
Definition Title.php:82
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.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28