1<?php 2/** 3 * Build file cache for content pages 4 * 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. 9 * 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. 14 * 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 19 * 20 * @file 21 * @ingroup Maintenance 22 */ 23 24use MediaWiki\MediaWikiServices; 25 26require_once __DIR__ . '/Maintenance.php'; 27 28/** 29 * Maintenance script that builds the file cache. 30 * 31 * @ingroup Maintenance 32 */ 33class RebuildFileCache extends Maintenance { 34 private $enabled = true; 35 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 ); 44 } 45 46 public function finalSetup() { 47 global $wgUseFileCache; 48 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' ); 55 56 // Ensure no debug-specific logic ends up in the cache (must be after Setup.php) 57 MWDebug::deinit(); 58 59 parent::finalSetup(); 60 } 61 62 public function execute() { 63 if ( !$this->enabled ) { 64 $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." ); 65 } 66 67 $start = $this->getOption( 'start', "0" ); 68 if ( !ctype_digit( $start ) ) { 69 $this->fatalError( "Invalid value for start parameter." ); 70 } 71 $start = intval( $start ); 72 73 $end = $this->getOption( 'end', "0" ); 74 if ( !ctype_digit( $end ) ) { 75 $this->fatalError( "Invalid value for end parameter." ); 76 } 77 $end = intval( $end ); 78 79 $this->output( "Building page file cache from page_id {$start}!\n" ); 80 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." ); 92 } 93 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(); 100 } 101 102 // Mock request (hack, no real client) 103 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; 104 105 # Do remaining chunk 106 $end += $batchSize - 1; 107 $blockStart = $start; 108 $blockEnd = $start + $batchSize - 1; 109 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' ] 119 ); 120 121 $this->beginTransaction( $dbw, __METHOD__ ); // for any changes 122 foreach ( $res as $row ) { 123 $rebuilt = false; 124 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? 129 } 130 131 $context = new RequestContext(); 132 $context->setTitle( $title ); 133 $article = Article::newFromTitle( $title, $context ); 134 $context->setWikiPage( $article->getPage() ); 135 136 // Some extensions like FlaggedRevs while error out if this is unset 137 RequestContext::getMain()->setTitle( $title ); 138 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! 149 } 150 } 151 152 Wikimedia\suppressWarnings(); // header notices 153 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 ); 163 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 ); 173 174 Wikimedia\restoreWarnings(); 175 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})..." ); 180 } 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" ); 185 } 186 } 187 $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity) 188 189 $blockStart += $batchSize; 190 $blockEnd += $batchSize; 191 } 192 $this->output( "Done!\n" ); 193 } 194} 195 196$maintClass = RebuildFileCache::class; 197require_once RUN_MAINTENANCE_IF_MAIN; 198