MediaWiki master
compareParserCache.php
Go to the documentation of this file.
1<?php
8// @codeCoverageIgnoreStart
9require_once __DIR__ . '/Maintenance.php';
10// @codeCoverageIgnoreEnd
11
16
21 public function __construct() {
22 parent::__construct();
23 $this->addDescription( 'Parse random pages and compare output to cache.' );
24 $this->addOption( 'namespace', 'Page namespace number', true, true );
25 $this->addOption( 'maxpages', 'Number of pages to try', true, true );
26 }
27
28 public function execute() {
29 $pages = $this->getOption( 'maxpages' );
30
31 $dbr = $this->getReplicaDB();
32
33 $totalsec = 0.0;
34 $scanned = 0;
35 $withcache = 0;
36 $withdiff = 0;
37 $services = $this->getServiceContainer();
38 $parserCache = $services->getParserCache();
39 $renderer = $services->getRevisionRenderer();
40 $wikiPageFactory = $services->getWikiPageFactory();
41 while ( $pages-- > 0 ) {
42 $row = $dbr->newSelectQueryBuilder()
43 // @todo Title::selectFields() or Title::getQueryInfo() or something
44 ->select( [
45 'page_namespace',
46 'page_title',
47 'page_id',
48 'page_len',
49 'page_is_redirect',
50 'page_latest',
51 ] )
52 ->from( 'page' )
53 ->where( [
54 'page_namespace' => $this->getOption( 'namespace' ),
55 'page_is_redirect' => 0,
56 $dbr->expr( 'page_random', '>=', wfRandom() ),
57 ] )
58 ->orderBy( 'page_random' )
59 ->caller( __METHOD__ )->fetchRow();
60
61 if ( !$row ) {
62 continue;
63 }
64 ++$scanned;
65
66 $title = Title::newFromRow( $row );
67 $page = $wikiPageFactory->newFromTitle( $title );
68 $revision = $page->getRevisionRecord();
69 $parserOptions = $page->makeParserOptions( 'canonical' );
70 $parserOutputOld = $parserCache->get( $page, $parserOptions );
71
72 if ( $parserOutputOld ) {
73 $t1 = microtime( true );
74 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
75 ->getRevisionParserOutput();
76
77 $sec = microtime( true ) - $t1;
78 $totalsec += $sec;
79
80 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
81
82 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
83
84 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '',
85 $parserOutputOld->getRawText() ) );
86 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '',
87 $parserOutputNew->getRawText() ) );
88 $diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
89 $formatter = new UnifiedDiffFormatter();
90 $unifiedDiff = $formatter->format( $diffs );
91
92 if ( $unifiedDiff !== '' ) {
93 $this->output( "differences found:\n\n$unifiedDiff\n\n" );
94 ++$withdiff;
95 } else {
96 $this->output( "No differences found.\n" );
97 }
98 ++$withcache;
99 } else {
100 $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
101 }
102 }
103
104 $ave = $totalsec ? $totalsec / $scanned : 0;
105 $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
106 $this->output( "Pages with differences found: $withdiff\n" );
107 $this->output( "Average parse time: $ave sec\n" );
108 }
109}
110
111// @codeCoverageIgnoreStart
112$maintClass = CompareParserCache::class;
113require_once RUN_MAINTENANCE_IF_MAIN;
114// @codeCoverageIgnoreEnd
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:20
A formatter that outputs unified diffs.