Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / rebuildFileCache.php
blobf207a595ab85b247c0022ab29e0736171f63a6e9
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Context\RequestContext;
25 use MediaWiki\Debug\MWDebug;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\Maintenance\Maintenance;
28 use MediaWiki\Settings\SettingsBuilder;
29 use MediaWiki\Title\Title;
30 use Wikimedia\AtEase\AtEase;
31 use Wikimedia\Rdbms\SelectQueryBuilder;
33 // @codeCoverageIgnoreStart
34 require_once __DIR__ . '/Maintenance.php';
35 // @codeCoverageIgnoreEnd
37 /**
38 * Maintenance script that builds the file cache.
40 * @ingroup Maintenance
42 class RebuildFileCache extends Maintenance {
43 /** @var bool */
44 private $enabled = true;
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Build the file cache' );
49 $this->addOption( 'start', 'Page_id to start from', false, true );
50 $this->addOption( 'end', 'Page_id to end on', false, true );
51 $this->addOption( 'overwrite', 'Refresh page cache' );
52 $this->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' );
53 $this->setBatchSize( 100 );
56 public function finalSetup( SettingsBuilder $settingsBuilder ) {
57 $this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache );
58 // Script will handle capturing output and saving it itself
59 $settingsBuilder->putConfigValue( MainConfigNames::UseFileCache, false );
61 // Avoid DB writes (like enotif/counters)
62 $this->getServiceContainer()->getReadOnlyMode()
63 ->setReason( 'Building cache' );
65 // Ensure no debug-specific logic ends up in the cache (must be after Setup.php)
66 MWDebug::deinit();
68 parent::finalSetup( $settingsBuilder );
71 public function execute() {
72 if ( !$this->enabled ) {
73 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
76 $start = $this->getOption( 'start', "0" );
77 if ( !ctype_digit( $start ) ) {
78 $this->fatalError( "Invalid value for start parameter." );
80 $start = intval( $start );
82 $end = $this->getOption( 'end', "0" );
83 if ( !ctype_digit( $end ) ) {
84 $this->fatalError( "Invalid value for end parameter." );
86 $end = intval( $end );
88 $this->output( "Building page file cache from page_id {$start}!\n" );
90 $dbr = $this->getReplicaDB();
91 $batchSize = $this->getBatchSize();
92 $overwrite = $this->hasOption( 'overwrite' );
93 $start = ( $start > 0 )
94 ? $start
95 : $dbr->newSelectQueryBuilder()
96 ->select( 'MIN(page_id)' )
97 ->from( 'page' )
98 ->caller( __METHOD__ )->fetchField();
99 $end = ( $end > 0 )
100 ? $end
101 : $dbr->newSelectQueryBuilder()
102 ->select( 'MAX(page_id)' )
103 ->from( 'page' )
104 ->caller( __METHOD__ )->fetchField();
105 if ( !$start ) {
106 $this->fatalError( "Nothing to do." );
109 $where = [];
110 if ( !$this->getOption( 'all' ) ) {
111 // If 'all' isn't passed as an option, just fall back to previous behaviour
112 // of using content namespaces
113 $where['page_namespace'] =
114 $this->getServiceContainer()->getNamespaceInfo()->getContentNamespaces();
117 // Mock request (hack, no real client)
118 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
120 # Do remaining chunk
121 $end += $batchSize - 1;
122 $blockStart = $start;
123 $blockEnd = $start + $batchSize - 1;
125 $dbw = $this->getPrimaryDB();
126 // Go through each page and save the output
127 while ( $blockEnd <= $end ) {
128 // Get the pages
129 $res = $dbr->newSelectQueryBuilder()
130 ->select( [ 'page_namespace', 'page_title', 'page_id' ] )
131 ->from( 'page' )
132 ->useIndex( 'PRIMARY' )
133 ->where( $where )
134 ->andWhere( [
135 $dbr->expr( 'page_id', '>=', (int)$blockStart ),
136 $dbr->expr( 'page_id', '<=', (int)$blockEnd ),
138 ->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC )
139 ->caller( __METHOD__ )->fetchResultSet();
141 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
142 foreach ( $res as $row ) {
143 $rebuilt = false;
145 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
146 if ( $title === null ) {
147 $this->output( "Page {$row->page_id} has bad title\n" );
148 continue; // broken title?
151 $context = new RequestContext();
152 $context->setTitle( $title );
153 $article = Article::newFromTitle( $title, $context );
154 $context->setWikiPage( $article->getPage() );
156 // Some extensions like FlaggedRevs while error out if this is unset
157 RequestContext::getMain()->setTitle( $title );
159 // If the article is cacheable, then load it
160 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
161 $viewCache = new HTMLFileCache( $title, 'view' );
162 $historyCache = new HTMLFileCache( $title, 'history' );
163 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
164 if ( $overwrite ) {
165 $rebuilt = true;
166 } else {
167 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
168 continue; // done already!
172 AtEase::suppressWarnings(); // header notices
174 // 1. Cache ?action=view
175 // Be sure to reset the mocked request time (T24852)
176 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
177 ob_start();
178 $article->view();
179 $context->getOutput()->output();
180 $context->getOutput()->clearHTML();
181 $viewHtml = ob_get_clean();
182 $viewCache->saveToFileCache( $viewHtml );
184 // 2. Cache ?action=history
185 // Be sure to reset the mocked request time (T24852)
186 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
187 ob_start();
188 Action::factory( 'history', $article, $context )->show();
189 $context->getOutput()->output();
190 $context->getOutput()->clearHTML();
191 $historyHtml = ob_get_clean();
192 $historyCache->saveToFileCache( $historyHtml );
194 AtEase::restoreWarnings();
196 if ( $rebuilt ) {
197 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
198 } else {
199 $this->output( "Cached page '$title' (id {$row->page_id})..." );
201 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
202 "history: " . strlen( $historyHtml ) . " bytes]\n" );
203 } else {
204 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
207 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes
209 $blockStart += $batchSize;
210 $blockEnd += $batchSize;
212 $this->output( "Done!\n" );
216 // @codeCoverageIgnoreStart
217 $maintClass = RebuildFileCache::class;
218 require_once RUN_MAINTENANCE_IF_MAIN;
219 // @codeCoverageIgnoreEnd