Update git submodules
[mediawiki.git] / maintenance / compareParserCache.php
blob5da351946fb27e3f262f36ad476ac8dde0b7e6bc
1 <?php
2 /**
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
18 * @file
19 * @ingroup Maintenance
22 require_once __DIR__ . '/Maintenance.php';
24 use MediaWiki\Title\Title;
25 use Wikimedia\Diff\Diff;
26 use Wikimedia\Diff\UnifiedDiffFormatter;
28 /**
29 * @ingroup Maintenance
31 class CompareParserCache extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Parse random pages and compare output to cache.' );
35 $this->addOption( 'namespace', 'Page namespace number', true, true );
36 $this->addOption( 'maxpages', 'Number of pages to try', true, true );
39 public function execute() {
40 $pages = $this->getOption( 'maxpages' );
42 $dbr = $this->getDB( DB_REPLICA );
44 $totalsec = 0.0;
45 $scanned = 0;
46 $withcache = 0;
47 $withdiff = 0;
48 $services = $this->getServiceContainer();
49 $parserCache = $services->getParserCache();
50 $renderer = $services->getRevisionRenderer();
51 $wikiPageFactory = $services->getWikiPageFactory();
52 while ( $pages-- > 0 ) {
53 $row = $dbr->newSelectQueryBuilder()
54 // @todo Title::selectFields() or Title::getQueryInfo() or something
55 ->select( [
56 'page_namespace',
57 'page_title',
58 'page_id',
59 'page_len',
60 'page_is_redirect',
61 'page_latest',
62 ] )
63 ->from( 'page' )
64 ->where( [
65 'page_namespace' => $this->getOption( 'namespace' ),
66 'page_is_redirect' => 0,
67 'page_random >= ' . wfRandom()
68 ] )
69 ->orderBy( 'page_random' )
70 ->caller( __METHOD__ )->fetchRow();
72 if ( !$row ) {
73 continue;
75 ++$scanned;
77 $title = Title::newFromRow( $row );
78 $page = $wikiPageFactory->newFromTitle( $title );
79 $revision = $page->getRevisionRecord();
80 $parserOptions = $page->makeParserOptions( 'canonical' );
82 $parserOutputOld = $parserCache->get( $page, $parserOptions );
84 if ( $parserOutputOld ) {
85 $t1 = microtime( true );
86 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
87 ->getRevisionParserOutput();
89 $sec = microtime( true ) - $t1;
90 $totalsec += $sec;
92 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
94 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
96 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
97 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
98 $diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
99 $formatter = new UnifiedDiffFormatter();
100 $unifiedDiff = $formatter->format( $diffs );
102 if ( strlen( $unifiedDiff ) ) {
103 $this->output( "differences found:\n\n$unifiedDiff\n\n" );
104 ++$withdiff;
105 } else {
106 $this->output( "No differences found.\n" );
108 ++$withcache;
109 } else {
110 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
114 $ave = $totalsec ? $totalsec / $scanned : 0;
115 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
116 $this->output( "Pages with differences found: $withdiff\n" );
117 $this->output( "Average parse time: $ave sec\n" );
121 $maintClass = CompareParserCache::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;