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