27 use Wikimedia\AtEase\AtEase;
29 require_once __DIR__ .
'/Maintenance.php';
37 private $enabled =
true;
40 parent::__construct();
42 $this->
addOption(
'start',
'Page_id to start from',
false,
true );
43 $this->
addOption(
'end',
'Page_id to end on',
false,
true );
44 $this->
addOption(
'overwrite',
'Refresh page cache' );
45 $this->
addOption(
'all',
'Build the file cache for pages in all namespaces, not just content pages' );
50 $this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache );
52 $settingsBuilder->putConfigValue( MainConfigNames::UseFileCache,
false );
56 ->setReason(
'Building cache' );
61 parent::finalSetup( $settingsBuilder );
65 if ( !$this->enabled ) {
66 $this->
fatalError(
"Nothing to do -- \$wgUseFileCache is disabled." );
69 $start = $this->
getOption(
'start',
"0" );
70 if ( !ctype_digit( $start ) ) {
71 $this->
fatalError(
"Invalid value for start parameter." );
73 $start = intval( $start );
76 if ( !ctype_digit( $end ) ) {
77 $this->
fatalError(
"Invalid value for end parameter." );
79 $end = intval( $end );
81 $this->
output(
"Building page file cache from page_id {$start}!\n" );
85 $overwrite = $this->
hasOption(
'overwrite' );
86 $start = ( $start > 0 )
88 :
$dbr->selectField(
'page',
'MIN(page_id)',
'', __METHOD__ );
91 :
$dbr->selectField(
'page',
'MAX(page_id)',
'', __METHOD__ );
100 $where[
'page_namespace'] =
101 MediaWikiServices::getInstance()->getNamespaceInfo()->getContentNamespaces();
105 $_SERVER[
'HTTP_ACCEPT_ENCODING'] =
'bgzip';
108 $end += $batchSize - 1;
109 $blockStart = $start;
110 $blockEnd = $start + $batchSize - 1;
114 while ( $blockEnd <= $end ) {
117 [
'page_namespace',
'page_title',
'page_id' ],
118 $where + [
"page_id BETWEEN " . (
int)$blockStart .
" AND " . (
int)$blockEnd ],
120 [
'ORDER BY' =>
'page_id ASC',
'USE INDEX' =>
'PRIMARY' ]
124 foreach (
$res as $row ) {
129 $this->
output(
"Page {$row->page_id} has bad title\n" );
134 $context->setTitle(
$title );
136 $context->setWikiPage( $article->getPage() );
145 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
149 $this->
output(
"Page '$title' (id {$row->page_id}) already cached\n" );
154 AtEase::suppressWarnings();
158 $_SERVER[
'REQUEST_TIME_FLOAT'] = microtime(
true );
161 $context->getOutput()->output();
162 $context->getOutput()->clearHTML();
163 $viewHtml = ob_get_clean();
164 $viewCache->saveToFileCache( $viewHtml );
168 $_SERVER[
'REQUEST_TIME_FLOAT'] = microtime(
true );
171 $context->getOutput()->output();
172 $context->getOutput()->clearHTML();
173 $historyHtml = ob_get_clean();
174 $historyCache->saveToFileCache( $historyHtml );
176 AtEase::restoreWarnings();
179 $this->
output(
"Re-cached page '$title' (id {$row->page_id})..." );
181 $this->
output(
"Cached page '$title' (id {$row->page_id})..." );
183 $this->
output(
"[view: " . strlen( $viewHtml ) .
" bytes; " .
184 "history: " . strlen( $historyHtml ) .
" bytes]\n" );
186 $this->
output(
"Page '$title' (id {$row->page_id}) not cacheable\n" );
191 $blockStart += $batchSize;
192 $blockEnd += $batchSize;
194 $this->
output(
"Done!\n" );
199 require_once RUN_MAINTENANCE_IF_MAIN;
static factory(string $action, Article $article, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
static newFromTitle( $title, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Page view caching in the file system.
static deinit()
Disable the debugger.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
static getMain()
Get the RequestContext object associated with the main request.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.