3 * Build file cache for content pages
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that builds file cache for content pages.
31 * @ingroup Maintenance
33 class RebuildFileCache
extends Maintenance
{
34 private $enabled = true;
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Build file cache for content pages' );
39 $this->addOption( 'start', 'Page_id to start from', false, true );
40 $this->addOption( 'end', 'Page_id to end on', false, true );
41 $this->addOption( 'overwrite', 'Refresh page cache' );
42 $this->setBatchSize( 100 );
45 public function finalSetup() {
46 global $wgDebugToolbar, $wgUseFileCache;
48 $this->enabled
= $wgUseFileCache;
49 // Script will handle capturing output and saving it itself
50 $wgUseFileCache = false;
51 // Debug toolbar makes content uncacheable so we disable it.
52 // Has to be done before Setup.php initialize MWDebug
53 $wgDebugToolbar = false;
54 // Avoid DB writes (like enotif/counters)
55 MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode()
56 ->setReason( 'Building cache' );
61 public function execute() {
62 if ( !$this->enabled
) {
63 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
66 $start = $this->getOption( 'start', "0" );
67 if ( !ctype_digit( $start ) ) {
68 $this->fatalError( "Invalid value for start parameter." );
70 $start = intval( $start );
72 $end = $this->getOption( 'end', "0" );
73 if ( !ctype_digit( $end ) ) {
74 $this->fatalError( "Invalid value for end parameter." );
76 $end = intval( $end );
78 $this->output( "Building content page file cache from page {$start}!\n" );
80 $dbr = $this->getDB( DB_REPLICA
);
81 $batchSize = $this->getBatchSize();
82 $overwrite = $this->hasOption( 'overwrite' );
83 $start = ( $start > 0 )
85 : $dbr->selectField( 'page', 'MIN(page_id)', '', __METHOD__
);
88 : $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__
);
90 $this->fatalError( "Nothing to do." );
93 // Mock request (hack, no real client)
94 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
97 $end +
= $batchSize - 1;
99 $blockEnd = $start +
$batchSize - 1;
101 $dbw = $this->getDB( DB_MASTER
);
102 // Go through each page and save the output
103 while ( $blockEnd <= $end ) {
105 $res = $dbr->select( 'page',
106 [ 'page_namespace', 'page_title', 'page_id' ],
107 [ 'page_namespace' => MediaWikiServices
::getInstance()->getNamespaceInfo()->
108 getContentNamespaces(),
109 "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
111 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
114 $this->beginTransaction( $dbw, __METHOD__
); // for any changes
115 foreach ( $res as $row ) {
118 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
119 if ( $title === null ) {
120 $this->output( "Page {$row->page_id} has bad title\n" );
121 continue; // broken title?
124 $context = new RequestContext();
125 $context->setTitle( $title );
126 $article = Article
::newFromTitle( $title, $context );
127 $context->setWikiPage( $article->getPage() );
129 // Some extensions like FlaggedRevs while error out if this is unset
130 RequestContext
::getMain()->setTitle( $title );
132 // If the article is cacheable, then load it
133 if ( $article->isFileCacheable( HTMLFileCache
::MODE_REBUILD
) ) {
134 $viewCache = new HTMLFileCache( $title, 'view' );
135 $historyCache = new HTMLFileCache( $title, 'history' );
136 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
140 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
141 continue; // done already!
145 Wikimedia\
suppressWarnings(); // header notices
147 // 1. Cache ?action=view
148 // Be sure to reset the mocked request time (T24852)
149 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
152 $context->getOutput()->output();
153 $context->getOutput()->clearHTML();
154 $viewHtml = ob_get_clean();
155 $viewCache->saveToFileCache( $viewHtml );
157 // 2. Cache ?action=history
158 // Be sure to reset the mocked request time (T24852)
159 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
161 Action
::factory( 'history', $article, $context )->show();
162 $context->getOutput()->output();
163 $context->getOutput()->clearHTML();
164 $historyHtml = ob_get_clean();
165 $historyCache->saveToFileCache( $historyHtml );
167 Wikimedia\restoreWarnings
();
170 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
172 $this->output( "Cached page '$title' (id {$row->page_id})..." );
174 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
175 "history: " . strlen( $historyHtml ) . " bytes]\n" );
177 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
180 $this->commitTransaction( $dbw, __METHOD__
); // commit any changes (just for sanity)
182 $blockStart +
= $batchSize;
183 $blockEnd +
= $batchSize;
185 $this->output( "Done!\n" );
189 $maintClass = RebuildFileCache
::class;
190 require_once RUN_MAINTENANCE_IF_MAIN
;