Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompareParserCache
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
42
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
26use MediaWiki\Maintenance\Maintenance;
27use MediaWiki\Title\Title;
28use Wikimedia\Diff\Diff;
29use Wikimedia\Diff\UnifiedDiffFormatter;
30
31/**
32 * @ingroup Maintenance
33 */
34class CompareParserCache extends Maintenance {
35    public function __construct() {
36        parent::__construct();
37        $this->addDescription( 'Parse random pages and compare output to cache.' );
38        $this->addOption( 'namespace', 'Page namespace number', true, true );
39        $this->addOption( 'maxpages', 'Number of pages to try', true, true );
40    }
41
42    public function execute() {
43        $pages = $this->getOption( 'maxpages' );
44
45        $dbr = $this->getReplicaDB();
46
47        $totalsec = 0.0;
48        $scanned = 0;
49        $withcache = 0;
50        $withdiff = 0;
51        $services = $this->getServiceContainer();
52        $parserCache = $services->getParserCache();
53        $renderer = $services->getRevisionRenderer();
54        $wikiPageFactory = $services->getWikiPageFactory();
55        while ( $pages-- > 0 ) {
56            $row = $dbr->newSelectQueryBuilder()
57                // @todo Title::selectFields() or Title::getQueryInfo() or something
58                ->select( [
59                    'page_namespace',
60                    'page_title',
61                    'page_id',
62                    'page_len',
63                    'page_is_redirect',
64                    'page_latest',
65                ] )
66                ->from( 'page' )
67                ->where( [
68                    'page_namespace' => $this->getOption( 'namespace' ),
69                    'page_is_redirect' => 0,
70                    $dbr->expr( 'page_random', '>=', wfRandom() ),
71                ] )
72                ->orderBy( 'page_random' )
73                ->caller( __METHOD__ )->fetchRow();
74
75            if ( !$row ) {
76                continue;
77            }
78            ++$scanned;
79
80            $title = Title::newFromRow( $row );
81            $page = $wikiPageFactory->newFromTitle( $title );
82            $revision = $page->getRevisionRecord();
83            $parserOptions = $page->makeParserOptions( 'canonical' );
84            $parserOutputOld = $parserCache->get( $page, $parserOptions );
85
86            if ( $parserOutputOld ) {
87                $t1 = microtime( true );
88                $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
89                    ->getRevisionParserOutput();
90
91                $sec = microtime( true ) - $t1;
92                $totalsec += $sec;
93
94                $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
95
96                $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
97
98                $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '',
99                    $parserOutputOld->getRawText() ) );
100                $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '',
101                    $parserOutputNew->getRawText() ) );
102                $diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
103                $formatter = new UnifiedDiffFormatter();
104                $unifiedDiff = $formatter->format( $diffs );
105
106                if ( strlen( $unifiedDiff ) ) {
107                    $this->output( "differences found:\n\n$unifiedDiff\n\n" );
108                    ++$withdiff;
109                } else {
110                    $this->output( "No differences found.\n" );
111                }
112                ++$withcache;
113            } else {
114                $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
115            }
116        }
117
118        $ave = $totalsec ? $totalsec / $scanned : 0;
119        $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
120        $this->output( "Pages with differences found: $withdiff\n" );
121        $this->output( "Average parse time: $ave sec\n" );
122    }
123}
124
125// @codeCoverageIgnoreStart
126$maintClass = CompareParserCache::class;
127require_once RUN_MAINTENANCE_IF_MAIN;
128// @codeCoverageIgnoreEnd