28use Wikimedia\AtEase\AtEase;
30require_once __DIR__ .
'/Maintenance.php';
38 private $enabled =
true;
41 parent::__construct();
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' );
51 $this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache );
53 $settingsBuilder->putConfigValue( MainConfigNames::UseFileCache,
false );
56 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
57 ->setReason(
'Building cache' );
62 parent::finalSetup( $settingsBuilder );
66 if ( !$this->enabled ) {
67 $this->
fatalError(
"Nothing to do -- \$wgUseFileCache is disabled." );
70 $start = $this->
getOption(
'start',
"0" );
71 if ( !ctype_digit( $start ) ) {
72 $this->
fatalError(
"Invalid value for start parameter." );
74 $start = intval( $start );
77 if ( !ctype_digit( $end ) ) {
78 $this->
fatalError(
"Invalid value for end parameter." );
80 $end = intval( $end );
82 $this->
output(
"Building page file cache from page_id {$start}!\n" );
86 $overwrite = $this->
hasOption(
'overwrite' );
87 $start = ( $start > 0 )
89 :
$dbr->selectField(
'page',
'MIN(page_id)',
'', __METHOD__ );
92 :
$dbr->selectField(
'page',
'MAX(page_id)',
'', __METHOD__ );
101 $where[
'page_namespace'] =
102 MediaWikiServices::getInstance()->getNamespaceInfo()->getContentNamespaces();
106 $_SERVER[
'HTTP_ACCEPT_ENCODING'] =
'bgzip';
109 $end += $batchSize - 1;
110 $blockStart = $start;
111 $blockEnd = $start + $batchSize - 1;
115 while ( $blockEnd <= $end ) {
118 [
'page_namespace',
'page_title',
'page_id' ],
119 $where + [
"page_id BETWEEN " . (
int)$blockStart .
" AND " . (
int)$blockEnd ],
121 [
'ORDER BY' =>
'page_id ASC',
'USE INDEX' =>
'PRIMARY' ]
125 foreach (
$res as $row ) {
128 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
130 $this->
output(
"Page {$row->page_id} has bad title\n" );
135 $context->setTitle(
$title );
136 $article = Article::newFromTitle(
$title, $context );
137 $context->setWikiPage( $article->getPage() );
140 RequestContext::getMain()->setTitle(
$title );
146 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
150 $this->
output(
"Page '$title' (id {$row->page_id}) already cached\n" );
155 AtEase::suppressWarnings();
159 $_SERVER[
'REQUEST_TIME_FLOAT'] = microtime(
true );
162 $context->getOutput()->output();
163 $context->getOutput()->clearHTML();
164 $viewHtml = ob_get_clean();
165 $viewCache->saveToFileCache( $viewHtml );
169 $_SERVER[
'REQUEST_TIME_FLOAT'] = microtime(
true );
171 Action::factory(
'history', $article, $context )->show();
172 $context->getOutput()->output();
173 $context->getOutput()->clearHTML();
174 $historyHtml = ob_get_clean();
175 $historyCache->saveToFileCache( $historyHtml );
177 AtEase::restoreWarnings();
180 $this->
output(
"Re-cached page '$title' (id {$row->page_id})..." );
182 $this->
output(
"Cached page '$title' (id {$row->page_id})..." );
184 $this->
output(
"[view: " . strlen( $viewHtml ) .
" bytes; " .
185 "history: " . strlen( $historyHtml ) .
" bytes]\n" );
187 $this->
output(
"Page '$title' (id {$row->page_id}) not cacheable\n" );
192 $blockStart += $batchSize;
193 $blockEnd += $batchSize;
195 $this->
output(
"Done!\n" );
200require_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.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
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.