Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
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 / 64
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 / 60
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
22require_once __DIR__ . '/Maintenance.php';
23
24use MediaWiki\Title\Title;
25use Wikimedia\Diff\Diff;
26use Wikimedia\Diff\UnifiedDiffFormatter;
27
28/**
29 * @ingroup Maintenance
30 */
31class 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 );
37    }
38
39    public function execute() {
40        $pages = $this->getOption( 'maxpages' );
41
42        $dbr = $this->getReplicaDB();
43
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();
71
72            if ( !$row ) {
73                continue;
74            }
75            ++$scanned;
76
77            $title = Title::newFromRow( $row );
78            $page = $wikiPageFactory->newFromTitle( $title );
79            $revision = $page->getRevisionRecord();
80            $parserOptions = $page->makeParserOptions( 'canonical' );
81
82            $parserOutputOld = $parserCache->get( $page, $parserOptions );
83
84            if ( $parserOutputOld ) {
85                $t1 = microtime( true );
86                $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
87                    ->getRevisionParserOutput();
88
89                $sec = microtime( true ) - $t1;
90                $totalsec += $sec;
91
92                $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
93
94                $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
95
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 );
101
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" );
107                }
108                ++$withcache;
109            } else {
110                $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
111            }
112        }
113
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" );
118    }
119}
120
121$maintClass = CompareParserCache::class;
122require_once RUN_MAINTENANCE_IF_MAIN;