| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Context\RequestContext; |
| 25 |
use MediaWiki\Debug\MWDebug; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Settings\SettingsBuilder; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\AtEase\AtEase; |
| 30 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 31 |
|
| 32 |
|
| 33 |
require_once __DIR__ . '/Maintenance.php'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class RebuildFileCache extends Maintenance { |
| 42 |
|
| 43 |
private $enabled = true; |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
parent::__construct(); |
| 47 |
$this->addDescription( 'Build the file cache' ); |
| 48 |
$this->addOption( 'start', 'Page_id to start from', false, true ); |
| 49 |
$this->addOption( 'end', 'Page_id to end on', false, true ); |
| 50 |
$this->addOption( 'overwrite', 'Refresh page cache' ); |
| 51 |
$this->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' ); |
| 52 |
$this->setBatchSize( 100 ); |
| 53 |
} |
| 54 |
|
| 55 |
public function finalSetup( SettingsBuilder $settingsBuilder ) { |
| 56 |
$this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache ); |
| 57 |
|
| 58 |
$settingsBuilder->putConfigValue( MainConfigNames::UseFileCache, false ); |
| 59 |
|
| 60 |
|
| 61 |
$this->getServiceContainer()->getReadOnlyMode() |
| 62 |
->setReason( 'Building cache' ); |
| 63 |
|
| 64 |
|
| 65 |
MWDebug::deinit(); |
| 66 |
|
| 67 |
parent::finalSetup( $settingsBuilder ); |
| 68 |
} |
| 69 |
|
| 70 |
public function execute() { |
| 71 |
if ( !$this->enabled ) { |
| 72 |
$this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." ); |
| 73 |
} |
| 74 |
|
| 75 |
$start = $this->getOption( 'start', "0" ); |
| 76 |
if ( !ctype_digit( $start ) ) { |
| 77 |
$this->fatalError( "Invalid value for start parameter." ); |
| 78 |
} |
| 79 |
$start = intval( $start ); |
| 80 |
|
| 81 |
$end = $this->getOption( 'end', "0" ); |
| 82 |
if ( !ctype_digit( $end ) ) { |
| 83 |
$this->fatalError( "Invalid value for end parameter." ); |
| 84 |
} |
| 85 |
$end = intval( $end ); |
| 86 |
|
| 87 |
$this->output( "Building page file cache from page_id {$start}!\n" ); |
| 88 |
|
| 89 |
$dbr = $this->getReplicaDB(); |
| 90 |
$batchSize = $this->getBatchSize(); |
| 91 |
$overwrite = $this->hasOption( 'overwrite' ); |
| 92 |
$start = ( $start > 0 ) |
| 93 |
? $start |
| 94 |
: $dbr->newSelectQueryBuilder() |
| 95 |
->select( 'MIN(page_id)' ) |
| 96 |
->from( 'page' ) |
| 97 |
->caller( __METHOD__ )->fetchField(); |
| 98 |
$end = ( $end > 0 ) |
| 99 |
? $end |
| 100 |
: $dbr->newSelectQueryBuilder() |
| 101 |
->select( 'MAX(page_id)' ) |
| 102 |
->from( 'page' ) |
| 103 |
->caller( __METHOD__ )->fetchField(); |
| 104 |
if ( !$start ) { |
| 105 |
$this->fatalError( "Nothing to do." ); |
| 106 |
} |
| 107 |
|
| 108 |
$where = []; |
| 109 |
if ( !$this->getOption( 'all' ) ) { |
| 110 |
|
| 111 |
|
| 112 |
$where['page_namespace'] = |
| 113 |
$this->getServiceContainer()->getNamespaceInfo()->getContentNamespaces(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; |
| 118 |
|
| 119 |
|
| 120 |
$end += $batchSize - 1; |
| 121 |
$blockStart = $start; |
| 122 |
$blockEnd = $start + $batchSize - 1; |
| 123 |
|
| 124 |
$dbw = $this->getPrimaryDB(); |
| 125 |
|
| 126 |
while ( $blockEnd <= $end ) { |
| 127 |
|
| 128 |
$res = $dbr->newSelectQueryBuilder() |
| 129 |
->select( [ 'page_namespace', 'page_title', 'page_id' ] ) |
| 130 |
->from( 'page' ) |
| 131 |
->useIndex( 'PRIMARY' ) |
| 132 |
->where( $where ) |
| 133 |
->andWhere( [ |
| 134 |
$dbr->expr( 'page_id', '>=', (int)$blockStart ), |
| 135 |
$dbr->expr( 'page_id', '<=', (int)$blockEnd ), |
| 136 |
] ) |
| 137 |
->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC ) |
| 138 |
->caller( __METHOD__ )->fetchResultSet(); |
| 139 |
|
| 140 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 141 |
foreach ( $res as $row ) { |
| 142 |
$rebuilt = false; |
| 143 |
|
| 144 |
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
| 145 |
if ( $title === null ) { |
| 146 |
$this->output( "Page {$row->page_id} has bad title\n" ); |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
$context = new RequestContext(); |
| 151 |
$context->setTitle( $title ); |
| 152 |
$article = Article::newFromTitle( $title, $context ); |
| 153 |
$context->setWikiPage( $article->getPage() ); |
| 154 |
|
| 155 |
|
| 156 |
RequestContext::getMain()->setTitle( $title ); |
| 157 |
|
| 158 |
|
| 159 |
if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) { |
| 160 |
$viewCache = new HTMLFileCache( $title, 'view' ); |
| 161 |
$historyCache = new HTMLFileCache( $title, 'history' ); |
| 162 |
if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) { |
| 163 |
if ( $overwrite ) { |
| 164 |
$rebuilt = true; |
| 165 |
} else { |
| 166 |
$this->output( "Page '$title' (id {$row->page_id}) already cached\n" ); |
| 167 |
continue; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
AtEase::suppressWarnings(); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ); |
| 176 |
ob_start(); |
| 177 |
$article->view(); |
| 178 |
$context->getOutput()->output(); |
| 179 |
$context->getOutput()->clearHTML(); |
| 180 |
$viewHtml = ob_get_clean(); |
| 181 |
$viewCache->saveToFileCache( $viewHtml ); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ); |
| 186 |
ob_start(); |
| 187 |
Action::factory( 'history', $article, $context )->show(); |
| 188 |
$context->getOutput()->output(); |
| 189 |
$context->getOutput()->clearHTML(); |
| 190 |
$historyHtml = ob_get_clean(); |
| 191 |
$historyCache->saveToFileCache( $historyHtml ); |
| 192 |
|
| 193 |
AtEase::restoreWarnings(); |
| 194 |
|
| 195 |
if ( $rebuilt ) { |
| 196 |
$this->output( "Re-cached page '$title' (id {$row->page_id})..." ); |
| 197 |
} else { |
| 198 |
$this->output( "Cached page '$title' (id {$row->page_id})..." ); |
| 199 |
} |
| 200 |
$this->output( "[view: " . strlen( $viewHtml ) . " bytes; " . |
| 201 |
"history: " . strlen( $historyHtml ) . " bytes]\n" ); |
| 202 |
} else { |
| 203 |
$this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" ); |
| 204 |
} |
| 205 |
} |
| 206 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 207 |
|
| 208 |
$blockStart += $batchSize; |
| 209 |
$blockEnd += $batchSize; |
| 210 |
} |
| 211 |
$this->output( "Done!\n" ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
$maintClass = RebuildFileCache::class; |
| 217 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 218 |
|