2014-05-02 18:13:26 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
|
|
|
|
|
2017-09-09 00:19:17 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-06-20 04:02:04 +00:00
|
|
|
use Wikimedia\Diff\Diff;
|
|
|
|
|
use Wikimedia\Diff\UnifiedDiffFormatter;
|
2017-09-09 00:19:17 +00:00
|
|
|
|
2014-05-02 18:13:26 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
class CompareParserCache extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Parse random pages and compare output to cache.' );
|
2014-05-02 18:13:26 +00:00
|
|
|
$this->addOption( 'namespace', 'Page namespace number', true, true );
|
|
|
|
|
$this->addOption( 'maxpages', 'Number of pages to try', true, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$pages = $this->getOption( 'maxpages' );
|
|
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2014-05-02 18:13:26 +00:00
|
|
|
|
|
|
|
|
$totalsec = 0.0;
|
|
|
|
|
$scanned = 0;
|
|
|
|
|
$withcache = 0;
|
2014-05-15 15:39:21 +00:00
|
|
|
$withdiff = 0;
|
2020-11-12 20:36:35 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$parserCache = $services->getParserCache();
|
|
|
|
|
$renderer = $services->getRevisionRenderer();
|
|
|
|
|
$wikiPageFactory = $services->getWikiPageFactory();
|
2014-05-02 18:13:26 +00:00
|
|
|
while ( $pages-- > 0 ) {
|
2023-07-18 22:56:37 +00:00
|
|
|
$row = $dbr->newSelectQueryBuilder()
|
2017-10-12 18:34:15 +00:00
|
|
|
// @todo Title::selectFields() or Title::getQueryInfo() or something
|
2023-07-18 22:56:37 +00:00
|
|
|
->select( [
|
|
|
|
|
'page_namespace',
|
|
|
|
|
'page_title',
|
|
|
|
|
'page_id',
|
|
|
|
|
'page_len',
|
|
|
|
|
'page_is_redirect',
|
|
|
|
|
'page_latest',
|
|
|
|
|
] )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->where( [
|
2014-05-02 18:13:26 +00:00
|
|
|
'page_namespace' => $this->getOption( 'namespace' ),
|
|
|
|
|
'page_is_redirect' => 0,
|
|
|
|
|
'page_random >= ' . wfRandom()
|
2023-07-18 22:56:37 +00:00
|
|
|
] )
|
|
|
|
|
->orderBy( 'page_random' )
|
|
|
|
|
->caller( __METHOD__ )->fetchRow();
|
2014-05-02 18:13:26 +00:00
|
|
|
|
|
|
|
|
if ( !$row ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
++$scanned;
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromRow( $row );
|
2020-11-12 20:36:35 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( $title );
|
2020-04-18 02:39:58 +00:00
|
|
|
$revision = $page->getRevisionRecord();
|
2014-05-02 18:13:26 +00:00
|
|
|
$parserOptions = $page->makeParserOptions( 'canonical' );
|
|
|
|
|
|
2017-09-09 00:19:17 +00:00
|
|
|
$parserOutputOld = $parserCache->get( $page, $parserOptions );
|
2014-05-02 18:13:26 +00:00
|
|
|
|
2014-05-15 15:39:21 +00:00
|
|
|
if ( $parserOutputOld ) {
|
|
|
|
|
$t1 = microtime( true );
|
2018-08-14 16:37:30 +00:00
|
|
|
$parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
|
|
|
|
|
->getRevisionParserOutput();
|
|
|
|
|
|
2014-05-15 15:39:21 +00:00
|
|
|
$sec = microtime( true ) - $t1;
|
|
|
|
|
$totalsec += $sec;
|
2014-05-02 18:13:26 +00:00
|
|
|
|
2014-05-15 15:39:21 +00:00
|
|
|
$this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
|
2014-05-02 18:13:26 +00:00
|
|
|
|
|
|
|
|
$this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
|
2020-03-31 22:16:42 +00:00
|
|
|
|
2014-05-02 18:13:26 +00:00
|
|
|
$oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
|
2014-07-19 21:12:10 +00:00
|
|
|
$newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
|
2020-03-31 22:16:42 +00:00
|
|
|
$diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
|
|
|
|
|
$formatter = new UnifiedDiffFormatter();
|
|
|
|
|
$unifiedDiff = $formatter->format( $diffs );
|
|
|
|
|
|
|
|
|
|
if ( strlen( $unifiedDiff ) ) {
|
|
|
|
|
$this->output( "differences found:\n\n$unifiedDiff\n\n" );
|
2014-05-15 15:39:21 +00:00
|
|
|
++$withdiff;
|
2014-05-02 18:13:26 +00:00
|
|
|
} else {
|
|
|
|
|
$this->output( "No differences found.\n" );
|
|
|
|
|
}
|
|
|
|
|
++$withcache;
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 15:39:21 +00:00
|
|
|
$ave = $totalsec ? $totalsec / $scanned : 0;
|
2014-05-02 18:13:26 +00:00
|
|
|
$this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
|
2014-05-15 15:39:21 +00:00
|
|
|
$this->output( "Pages with differences found: $withdiff\n" );
|
2014-05-02 18:13:26 +00:00
|
|
|
$this->output( "Average parse time: $ave sec\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = CompareParserCache::class;
|
2014-05-02 18:13:26 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|