2008-12-11 14:35:33 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Build file cache for content pages
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-09 16:06:18 +00:00
|
|
|
* @file
|
2008-12-11 14:35:33 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2024-05-03 19:28:04 +00:00
|
|
|
use MediaWiki\Debug\MWDebug;
|
2022-04-27 15:42:24 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-01-26 17:46:06 +00:00
|
|
|
use MediaWiki\Settings\SettingsBuilder;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-01-05 01:31:58 +00:00
|
|
|
use Wikimedia\AtEase\AtEase;
|
2023-07-18 22:56:37 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2018-08-05 17:58:51 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2008-12-11 14:35:33 +00:00
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
2020-07-11 11:54:58 +00:00
|
|
|
* Maintenance script that builds the file cache.
|
2012-08-09 16:06:18 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class RebuildFileCache extends Maintenance {
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var bool */
|
2016-09-14 20:36:34 +00:00
|
|
|
private $enabled = true;
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2020-07-11 11:54:58 +00:00
|
|
|
$this->addDescription( 'Build the file cache' );
|
2011-10-02 17:19:19 +00:00
|
|
|
$this->addOption( 'start', 'Page_id to start from', false, true );
|
|
|
|
|
$this->addOption( 'end', 'Page_id to end on', false, true );
|
|
|
|
|
$this->addOption( 'overwrite', 'Refresh page cache' );
|
2020-07-11 11:54:58 +00:00
|
|
|
$this->addOption( 'all', 'Build the file cache for pages in all namespaces, not just content pages' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->setBatchSize( 100 );
|
|
|
|
|
}
|
2008-12-11 14:35:33 +00:00
|
|
|
|
2024-01-02 14:12:21 +00:00
|
|
|
public function finalSetup( SettingsBuilder $settingsBuilder ) {
|
2022-04-27 15:42:24 +00:00
|
|
|
$this->enabled = $settingsBuilder->getConfig()->get( MainConfigNames::UseFileCache );
|
2022-01-26 17:24:31 +00:00
|
|
|
// Script will handle capturing output and saving it itself
|
2022-04-27 15:42:24 +00:00
|
|
|
$settingsBuilder->putConfigValue( MainConfigNames::UseFileCache, false );
|
2022-01-26 17:46:06 +00:00
|
|
|
|
2020-05-10 00:09:19 +00:00
|
|
|
// Avoid DB writes (like enotif/counters)
|
2023-08-31 09:21:12 +00:00
|
|
|
$this->getServiceContainer()->getReadOnlyMode()
|
2017-04-10 05:34:30 +00:00
|
|
|
->setReason( 'Building cache' );
|
2016-09-14 20:36:34 +00:00
|
|
|
|
2019-08-31 22:43:23 +00:00
|
|
|
// Ensure no debug-specific logic ends up in the cache (must be after Setup.php)
|
|
|
|
|
MWDebug::deinit();
|
|
|
|
|
|
2022-01-26 17:46:06 +00:00
|
|
|
parent::finalSetup( $settingsBuilder );
|
2012-02-07 09:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2016-09-14 20:36:34 +00:00
|
|
|
if ( !$this->enabled ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2012-02-06 17:17:51 +00:00
|
|
|
|
2011-10-02 17:19:19 +00:00
|
|
|
$start = $this->getOption( 'start', "0" );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !ctype_digit( $start ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid value for start parameter." );
|
2009-08-13 01:39:15 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
$start = intval( $start );
|
2011-10-02 17:19:19 +00:00
|
|
|
|
|
|
|
|
$end = $this->getOption( 'end', "0" );
|
|
|
|
|
if ( !ctype_digit( $end ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid value for end parameter." );
|
2011-10-02 17:19:19 +00:00
|
|
|
}
|
|
|
|
|
$end = intval( $end );
|
|
|
|
|
|
2020-07-11 11:54:58 +00:00
|
|
|
$this->output( "Building page file cache from page_id {$start}!\n" );
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
2017-11-08 03:35:11 +00:00
|
|
|
$batchSize = $this->getBatchSize();
|
2017-07-19 19:51:30 +00:00
|
|
|
$overwrite = $this->hasOption( 'overwrite' );
|
2011-10-02 17:19:19 +00:00
|
|
|
$start = ( $start > 0 )
|
|
|
|
|
? $start
|
2023-07-18 22:56:37 +00:00
|
|
|
: $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( 'MIN(page_id)' )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
2011-10-02 17:19:19 +00:00
|
|
|
$end = ( $end > 0 )
|
|
|
|
|
? $end
|
2023-07-18 22:56:37 +00:00
|
|
|
: $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( 'MAX(page_id)' )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$start ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Nothing to do." );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2020-07-11 11:54:58 +00:00
|
|
|
$where = [];
|
|
|
|
|
if ( !$this->getOption( 'all' ) ) {
|
|
|
|
|
// If 'all' isn't passed as an option, just fall back to previous behaviour
|
|
|
|
|
// of using content namespaces
|
|
|
|
|
$where['page_namespace'] =
|
2023-08-31 09:21:12 +00:00
|
|
|
$this->getServiceContainer()->getNamespaceInfo()->getContentNamespaces();
|
2020-07-11 11:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 10:33:37 +00:00
|
|
|
// Mock request (hack, no real client)
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Do remaining chunk
|
2017-11-08 03:35:11 +00:00
|
|
|
$end += $batchSize - 1;
|
2009-08-02 19:35:17 +00:00
|
|
|
$blockStart = $start;
|
2017-11-08 03:35:11 +00:00
|
|
|
$blockEnd = $start + $batchSize - 1;
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2009-08-02 19:35:17 +00:00
|
|
|
// Go through each page and save the output
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $blockEnd <= $end ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
// Get the pages
|
2023-07-18 22:56:37 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'page_namespace', 'page_title', 'page_id' ] )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->useIndex( 'PRIMARY' )
|
|
|
|
|
->where( $where )
|
2024-04-21 12:24:21 +00:00
|
|
|
->andWhere( [
|
|
|
|
|
$dbr->expr( 'page_id', '>=', (int)$blockStart ),
|
|
|
|
|
$dbr->expr( 'page_id', '<=', (int)$blockEnd ),
|
|
|
|
|
] )
|
2023-07-18 22:56:37 +00:00
|
|
|
->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2011-10-02 17:19:19 +00:00
|
|
|
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $dbw, __METHOD__ ); // for any changes
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$rebuilt = false;
|
2012-08-09 16:06:18 +00:00
|
|
|
|
2014-03-13 01:27:58 +00:00
|
|
|
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $title === null ) {
|
2009-08-17 02:46:18 +00:00
|
|
|
$this->output( "Page {$row->page_id} has bad title\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
continue; // broken title?
|
|
|
|
|
}
|
2012-01-17 12:38:23 +00:00
|
|
|
|
2016-09-14 20:36:34 +00:00
|
|
|
$context = new RequestContext();
|
2014-03-13 01:27:58 +00:00
|
|
|
$context->setTitle( $title );
|
|
|
|
|
$article = Article::newFromTitle( $title, $context );
|
2012-01-17 12:38:23 +00:00
|
|
|
$context->setWikiPage( $article->getPage() );
|
|
|
|
|
|
2017-01-18 06:10:03 +00:00
|
|
|
// Some extensions like FlaggedRevs while error out if this is unset
|
|
|
|
|
RequestContext::getMain()->setTitle( $title );
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
// If the article is cacheable, then load it
|
2016-09-14 20:36:34 +00:00
|
|
|
if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
|
|
|
|
|
$viewCache = new HTMLFileCache( $title, 'view' );
|
|
|
|
|
$historyCache = new HTMLFileCache( $title, 'history' );
|
|
|
|
|
if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $overwrite ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$rebuilt = true;
|
|
|
|
|
} else {
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
continue; // done already!
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-14 20:36:34 +00:00
|
|
|
|
2022-01-05 01:31:58 +00:00
|
|
|
AtEase::suppressWarnings(); // header notices
|
2016-08-12 10:33:37 +00:00
|
|
|
|
|
|
|
|
// 1. Cache ?action=view
|
|
|
|
|
// Be sure to reset the mocked request time (T24852)
|
|
|
|
|
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
|
2016-09-14 20:36:34 +00:00
|
|
|
ob_start();
|
|
|
|
|
$article->view();
|
|
|
|
|
$context->getOutput()->output();
|
|
|
|
|
$context->getOutput()->clearHTML();
|
|
|
|
|
$viewHtml = ob_get_clean();
|
|
|
|
|
$viewCache->saveToFileCache( $viewHtml );
|
2016-08-12 10:33:37 +00:00
|
|
|
|
|
|
|
|
// 2. Cache ?action=history
|
|
|
|
|
// Be sure to reset the mocked request time (T24852)
|
|
|
|
|
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
|
2016-09-14 20:36:34 +00:00
|
|
|
ob_start();
|
|
|
|
|
Action::factory( 'history', $article, $context )->show();
|
|
|
|
|
$context->getOutput()->output();
|
|
|
|
|
$context->getOutput()->clearHTML();
|
|
|
|
|
$historyHtml = ob_get_clean();
|
|
|
|
|
$historyCache->saveToFileCache( $historyHtml );
|
2016-08-12 10:33:37 +00:00
|
|
|
|
2022-01-05 01:31:58 +00:00
|
|
|
AtEase::restoreWarnings();
|
2016-09-14 20:36:34 +00:00
|
|
|
|
2011-10-02 17:19:19 +00:00
|
|
|
if ( $rebuilt ) {
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->output( "Re-cached page '$title' (id {$row->page_id})..." );
|
2011-10-02 17:19:19 +00:00
|
|
|
} else {
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->output( "Cached page '$title' (id {$row->page_id})..." );
|
2011-10-02 17:19:19 +00:00
|
|
|
}
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
|
|
|
|
|
"history: " . strlen( $historyHtml ) . " bytes]\n" );
|
2008-12-11 18:59:11 +00:00
|
|
|
} else {
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
|
2008-12-11 18:59:11 +00:00
|
|
|
}
|
2008-12-11 14:35:33 +00:00
|
|
|
}
|
2021-11-19 23:19:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); // commit any changes
|
2011-10-02 17:19:19 +00:00
|
|
|
|
2017-11-08 03:35:11 +00:00
|
|
|
$blockStart += $batchSize;
|
|
|
|
|
$blockEnd += $batchSize;
|
2008-12-11 14:35:33 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Done!\n" );
|
2008-12-11 14:35:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = RebuildFileCache::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|