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
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2008-12-11 14:35:33 +00:00
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that builds file cache for content pages.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class RebuildFileCache extends Maintenance {
|
2016-09-14 20:36:34 +00:00
|
|
|
private $enabled = true;
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Build file cache for content pages' );
|
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' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->setBatchSize( 100 );
|
|
|
|
|
}
|
2008-12-11 14:35:33 +00:00
|
|
|
|
2012-02-07 09:37:59 +00:00
|
|
|
public function finalSetup() {
|
2017-04-10 05:34:30 +00:00
|
|
|
global $wgDebugToolbar, $wgUseFileCache;
|
2012-02-07 09:37:59 +00:00
|
|
|
|
2016-09-14 20:36:34 +00:00
|
|
|
$this->enabled = $wgUseFileCache;
|
|
|
|
|
// Script will handle capturing output and saving it itself
|
|
|
|
|
$wgUseFileCache = false;
|
2012-02-07 09:37:59 +00:00
|
|
|
// Debug toolbar makes content uncacheable so we disable it.
|
|
|
|
|
// Has to be done before Setup.php initialize MWDebug
|
|
|
|
|
$wgDebugToolbar = false;
|
2016-09-14 20:36:34 +00:00
|
|
|
// Avoid DB writes (like enotif/counters)
|
2017-04-10 05:34:30 +00:00
|
|
|
MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
|
|
|
|
|
->setReason( 'Building cache' );
|
2016-09-14 20:36:34 +00:00
|
|
|
|
2012-02-07 09:37:59 +00:00
|
|
|
parent::finalSetup();
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2016-09-14 20:36:34 +00:00
|
|
|
global $wgRequestTime;
|
|
|
|
|
|
|
|
|
|
if ( !$this->enabled ) {
|
2009-08-02 21:55:10 +00:00
|
|
|
$this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
|
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 ) ) {
|
2009-08-13 01:39:15 +00:00
|
|
|
$this->error( "Invalid value for start parameter.", true );
|
|
|
|
|
}
|
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 ) ) {
|
|
|
|
|
$this->error( "Invalid value for end parameter.", true );
|
|
|
|
|
}
|
|
|
|
|
$end = intval( $end );
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Building content page file cache from page {$start}!\n" );
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
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
|
2016-09-14 19:37:36 +00:00
|
|
|
: $dbr->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
|
2011-10-02 17:19:19 +00:00
|
|
|
$end = ( $end > 0 )
|
|
|
|
|
? $end
|
2016-09-14 19:37:36 +00:00
|
|
|
: $dbr->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$start ) {
|
2009-08-02 21:55:10 +00:00
|
|
|
$this->error( "Nothing to do.", true );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
|
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
|
|
|
|
2015-12-31 00:07:37 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
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
|
2016-09-14 19:37:36 +00:00
|
|
|
$res = $dbr->select( 'page',
|
|
|
|
|
[ 'page_namespace', 'page_title', 'page_id' ],
|
2016-05-27 17:21:27 +00:00
|
|
|
[ 'page_namespace' => MWNamespace::getContentNamespaces(),
|
2016-02-17 09:09:32 +00:00
|
|
|
"page_id BETWEEN $blockStart AND $blockEnd" ],
|
2016-09-14 19:37:36 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
|
2009-08-02 19:35:17 +00:00
|
|
|
);
|
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 );
|
|
|
|
|
if ( null == $title ) {
|
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
|
|
|
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings(); // header notices
|
2016-09-14 20:36:34 +00:00
|
|
|
// Cache ?action=view
|
2017-02-20 22:48:21 +00:00
|
|
|
$wgRequestTime = microtime( true ); # T24852
|
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 );
|
|
|
|
|
// Cache ?action=history
|
2017-02-20 22:48:21 +00:00
|
|
|
$wgRequestTime = microtime( true ); # T24852
|
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 );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\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
|
|
|
}
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
|
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
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$maintClass = "RebuildFileCache";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|