MediaWiki master
compareParserCache.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
29
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
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
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.
Represents a title within MediaWiki.
Definition Title.php:78
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:34
A formatter that outputs unified diffs.