3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup Maintenance
22 require_once __DIR__
. '/Maintenance.php';
24 use MediaWiki\MediaWikiServices
;
27 * @ingroup Maintenance
29 class CompareParserCache
extends Maintenance
{
30 public function __construct() {
31 parent
::__construct();
32 $this->addDescription( 'Parse random pages and compare output to cache.' );
33 $this->addOption( 'namespace', 'Page namespace number', true, true );
34 $this->addOption( 'maxpages', 'Number of pages to try', true, true );
37 public function execute() {
38 $pages = $this->getOption( 'maxpages' );
40 $dbr = $this->getDB( DB_REPLICA
);
46 $parserCache = MediaWikiServices
::getInstance()->getParserCache();
47 $renderer = MediaWikiServices
::getInstance()->getRevisionRenderer();
48 while ( $pages-- > 0 ) {
49 $row = $dbr->selectRow( 'page',
50 // @todo Title::selectFields() or Title::getQueryInfo() or something
52 'page_namespace', 'page_title', 'page_id',
53 'page_len', 'page_is_redirect', 'page_latest',
56 'page_namespace' => $this->getOption( 'namespace' ),
57 'page_is_redirect' => 0,
58 'page_random >= ' . wfRandom()
62 'ORDER BY' => 'page_random',
71 $title = Title
::newFromRow( $row );
72 $page = WikiPage
::factory( $title );
73 $revision = $page->getRevision()->getRevisionRecord();
74 $parserOptions = $page->makeParserOptions( 'canonical' );
76 $parserOutputOld = $parserCache->get( $page, $parserOptions );
78 if ( $parserOutputOld ) {
79 $t1 = microtime( true );
80 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
81 ->getRevisionParserOutput();
83 $sec = microtime( true ) - $t1;
86 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
88 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
89 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
90 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
91 $diff = wfDiff( $oldHtml, $newHtml );
92 if ( strlen( $diff ) ) {
93 $this->output( "differences found:\n\n$diff\n\n" );
96 $this->output( "No differences found.\n" );
100 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
104 $ave = $totalsec ?
$totalsec / $scanned : 0;
105 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
106 $this->output( "Pages with differences found: $withdiff\n" );
107 $this->output( "Average parse time: $ave sec\n" );
111 $maintClass = CompareParserCache
::class;
112 require_once RUN_MAINTENANCE_IF_MAIN
;