MediaWiki master
compareParserCache.php
Go to the documentation of this file.
1<?php
22// @codeCoverageIgnoreStart
23require_once __DIR__ . '/Maintenance.php';
24// @codeCoverageIgnoreEnd
25
30
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 ( $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
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
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.