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 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that builds file cache for content pages.
29 * @ingroup Maintenance
31 class RebuildFileCache
extends Maintenance
{
32 private $enabled = true;
34 public function __construct() {
35 parent
::__construct();
36 $this->addDescription( 'Build file cache for content pages' );
37 $this->addOption( 'start', 'Page_id to start from', false, true );
38 $this->addOption( 'end', 'Page_id to end on', false, true );
39 $this->addOption( 'overwrite', 'Refresh page cache' );
40 $this->setBatchSize( 100 );
43 public function finalSetup() {
44 global $wgDebugToolbar, $wgUseFileCache;
46 $this->enabled
= $wgUseFileCache;
47 // Script will handle capturing output and saving it itself
48 $wgUseFileCache = false;
49 // Debug toolbar makes content uncacheable so we disable it.
50 // Has to be done before Setup.php initialize MWDebug
51 $wgDebugToolbar = false;
52 // Avoid DB writes (like enotif/counters)
53 MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode()
54 ->setReason( 'Building cache' );
59 public function execute() {
60 if ( !$this->enabled
) {
61 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
64 $start = $this->getOption( 'start', "0" );
65 if ( !ctype_digit( $start ) ) {
66 $this->fatalError( "Invalid value for start parameter." );
68 $start = intval( $start );
70 $end = $this->getOption( 'end', "0" );
71 if ( !ctype_digit( $end ) ) {
72 $this->fatalError( "Invalid value for end parameter." );
74 $end = intval( $end );
76 $this->output( "Building content page file cache from page {$start}!\n" );
78 $dbr = $this->getDB( DB_REPLICA
);
79 $batchSize = $this->getBatchSize();
80 $overwrite = $this->hasOption( 'overwrite' );
81 $start = ( $start > 0 )
83 : $dbr->selectField( 'page', 'MIN(page_id)', '', __METHOD__
);
86 : $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__
);
88 $this->fatalError( "Nothing to do." );
91 // Mock request (hack, no real client)
92 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
95 $end +
= $batchSize - 1;
97 $blockEnd = $start +
$batchSize - 1;
99 $dbw = $this->getDB( DB_MASTER
);
100 // Go through each page and save the output
101 while ( $blockEnd <= $end ) {
103 $res = $dbr->select( 'page',
104 [ 'page_namespace', 'page_title', 'page_id' ],
105 [ 'page_namespace' => MWNamespace
::getContentNamespaces(),
106 "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
108 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
111 $this->beginTransaction( $dbw, __METHOD__
); // for any changes
112 foreach ( $res as $row ) {
115 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
116 if ( null == $title ) {
117 $this->output( "Page {$row->page_id} has bad title\n" );
118 continue; // broken title?
121 $context = new RequestContext();
122 $context->setTitle( $title );
123 $article = Article
::newFromTitle( $title, $context );
124 $context->setWikiPage( $article->getPage() );
126 // Some extensions like FlaggedRevs while error out if this is unset
127 RequestContext
::getMain()->setTitle( $title );
129 // If the article is cacheable, then load it
130 if ( $article->isFileCacheable( HTMLFileCache
::MODE_REBUILD
) ) {
131 $viewCache = new HTMLFileCache( $title, 'view' );
132 $historyCache = new HTMLFileCache( $title, 'history' );
133 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
137 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
138 continue; // done already!
142 Wikimedia\
suppressWarnings(); // header notices
144 // 1. Cache ?action=view
145 // Be sure to reset the mocked request time (T24852)
146 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
149 $context->getOutput()->output();
150 $context->getOutput()->clearHTML();
151 $viewHtml = ob_get_clean();
152 $viewCache->saveToFileCache( $viewHtml );
154 // 2. Cache ?action=history
155 // Be sure to reset the mocked request time (T24852)
156 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
158 Action
::factory( 'history', $article, $context )->show();
159 $context->getOutput()->output();
160 $context->getOutput()->clearHTML();
161 $historyHtml = ob_get_clean();
162 $historyCache->saveToFileCache( $historyHtml );
164 Wikimedia\restoreWarnings
();
167 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
169 $this->output( "Cached page '$title' (id {$row->page_id})..." );
171 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
172 "history: " . strlen( $historyHtml ) . " bytes]\n" );
174 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
177 $this->commitTransaction( $dbw, __METHOD__
); // commit any changes (just for sanity)
179 $blockStart +
= $batchSize;
180 $blockEnd +
= $batchSize;
182 $this->output( "Done!\n" );
186 $maintClass = RebuildFileCache
::class;
187 require_once RUN_MAINTENANCE_IF_MAIN
;