composer.json: Add a phan script command
[mediawiki.git] / maintenance / rebuildFileCache.php
blobee7afe9a55e235b24d0f4e8b7bca7b178fe61604
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\MediaWikiServices;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that builds the file cache.
31 * @ingroup Maintenance
33 class RebuildFileCache extends Maintenance {
34 private $enabled = true;
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Build the file cache' );
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->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' );
43 $this->setBatchSize( 100 );
46 public function finalSetup() {
47 global $wgUseFileCache;
49 $this->enabled = $wgUseFileCache;
50 // Script will handle capturing output and saving it itself
51 $wgUseFileCache = false;
52 // Avoid DB writes (like enotif/counters)
53 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
54 ->setReason( 'Building cache' );
56 // Ensure no debug-specific logic ends up in the cache (must be after Setup.php)
57 MWDebug::deinit();
59 parent::finalSetup();
62 public function execute() {
63 if ( !$this->enabled ) {
64 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
67 $start = $this->getOption( 'start', "0" );
68 if ( !ctype_digit( $start ) ) {
69 $this->fatalError( "Invalid value for start parameter." );
71 $start = intval( $start );
73 $end = $this->getOption( 'end', "0" );
74 if ( !ctype_digit( $end ) ) {
75 $this->fatalError( "Invalid value for end parameter." );
77 $end = intval( $end );
79 $this->output( "Building page file cache from page_id {$start}!\n" );
81 $dbr = $this->getDB( DB_REPLICA );
82 $batchSize = $this->getBatchSize();
83 $overwrite = $this->hasOption( 'overwrite' );
84 $start = ( $start > 0 )
85 ? $start
86 : $dbr->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
87 $end = ( $end > 0 )
88 ? $end
89 : $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
90 if ( !$start ) {
91 $this->fatalError( "Nothing to do." );
94 $where = [];
95 if ( !$this->getOption( 'all' ) ) {
96 // If 'all' isn't passed as an option, just fall back to previous behaviour
97 // of using content namespaces
98 $where['page_namespace'] =
99 MediaWikiServices::getInstance()->getNamespaceInfo()->getContentNamespaces();
102 // Mock request (hack, no real client)
103 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
105 # Do remaining chunk
106 $end += $batchSize - 1;
107 $blockStart = $start;
108 $blockEnd = $start + $batchSize - 1;
110 $dbw = $this->getDB( DB_MASTER );
111 // Go through each page and save the output
112 while ( $blockEnd <= $end ) {
113 // Get the pages
114 $res = $dbr->select( 'page',
115 [ 'page_namespace', 'page_title', 'page_id' ],
116 $where + [ "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
117 __METHOD__,
118 [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
121 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
122 foreach ( $res as $row ) {
123 $rebuilt = false;
125 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
126 if ( $title === null ) {
127 $this->output( "Page {$row->page_id} has bad title\n" );
128 continue; // broken title?
131 $context = new RequestContext();
132 $context->setTitle( $title );
133 $article = Article::newFromTitle( $title, $context );
134 $context->setWikiPage( $article->getPage() );
136 // Some extensions like FlaggedRevs while error out if this is unset
137 RequestContext::getMain()->setTitle( $title );
139 // If the article is cacheable, then load it
140 if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
141 $viewCache = new HTMLFileCache( $title, 'view' );
142 $historyCache = new HTMLFileCache( $title, 'history' );
143 if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
144 if ( $overwrite ) {
145 $rebuilt = true;
146 } else {
147 $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
148 continue; // done already!
152 Wikimedia\suppressWarnings(); // header notices
154 // 1. Cache ?action=view
155 // Be sure to reset the mocked request time (T24852)
156 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
157 ob_start();
158 $article->view();
159 $context->getOutput()->output();
160 $context->getOutput()->clearHTML();
161 $viewHtml = ob_get_clean();
162 $viewCache->saveToFileCache( $viewHtml );
164 // 2. Cache ?action=history
165 // Be sure to reset the mocked request time (T24852)
166 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
167 ob_start();
168 Action::factory( 'history', $article, $context )->show();
169 $context->getOutput()->output();
170 $context->getOutput()->clearHTML();
171 $historyHtml = ob_get_clean();
172 $historyCache->saveToFileCache( $historyHtml );
174 Wikimedia\restoreWarnings();
176 if ( $rebuilt ) {
177 $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
178 } else {
179 $this->output( "Cached page '$title' (id {$row->page_id})..." );
181 $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
182 "history: " . strlen( $historyHtml ) . " bytes]\n" );
183 } else {
184 $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
187 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
189 $blockStart += $batchSize;
190 $blockEnd += $batchSize;
192 $this->output( "Done!\n" );
196 $maintClass = RebuildFileCache::class;
197 require_once RUN_MAINTENANCE_IF_MAIN;