Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / compareParserCache.php
blob3b131b4ab34797e0adaa4f2fdc07280972a35ca8
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\MediaWikiServices;
26 /**
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 );
42 $totalsec = 0.0;
43 $scanned = 0;
44 $withcache = 0;
45 $withdiff = 0;
46 $services = MediaWikiServices::getInstance();
47 $parserCache = $services->getParserCache();
48 $renderer = $services->getRevisionRenderer();
49 $wikiPageFactory = $services->getWikiPageFactory();
50 while ( $pages-- > 0 ) {
51 $row = $dbr->selectRow( 'page',
52 // @todo Title::selectFields() or Title::getQueryInfo() or something
54 'page_namespace', 'page_title', 'page_id',
55 'page_len', 'page_is_redirect', 'page_latest',
58 'page_namespace' => $this->getOption( 'namespace' ),
59 'page_is_redirect' => 0,
60 'page_random >= ' . wfRandom()
62 __METHOD__,
64 'ORDER BY' => 'page_random',
68 if ( !$row ) {
69 continue;
71 ++$scanned;
73 $title = Title::newFromRow( $row );
74 $page = $wikiPageFactory->newFromTitle( $title );
75 $revision = $page->getRevisionRecord();
76 $parserOptions = $page->makeParserOptions( 'canonical' );
78 $parserOutputOld = $parserCache->get( $page, $parserOptions );
80 if ( $parserOutputOld ) {
81 $t1 = microtime( true );
82 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
83 ->getRevisionParserOutput();
85 $sec = microtime( true ) - $t1;
86 $totalsec += $sec;
88 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
90 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
92 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
93 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
94 $diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
95 $formatter = new UnifiedDiffFormatter();
96 $unifiedDiff = $formatter->format( $diffs );
98 if ( strlen( $unifiedDiff ) ) {
99 $this->output( "differences found:\n\n$unifiedDiff\n\n" );
100 ++$withdiff;
101 } else {
102 $this->output( "No differences found.\n" );
104 ++$withcache;
105 } else {
106 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
110 $ave = $totalsec ? $totalsec / $scanned : 0;
111 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
112 $this->output( "Pages with differences found: $withdiff\n" );
113 $this->output( "Average parse time: $ave sec\n" );
117 $maintClass = CompareParserCache::class;
118 require_once RUN_MAINTENANCE_IF_MAIN;