MediaWiki REL1_34
compareParserCache.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Maintenance.php';
23
25
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Parse random pages and compare output to cache.' );
33 $this->addOption( 'namespace', 'Page namespace number', true, true );
34 $this->addOption( 'maxpages', 'Number of pages to try', true, true );
35 }
36
37 public function execute() {
38 $pages = $this->getOption( 'maxpages' );
39
40 $dbr = $this->getDB( DB_REPLICA );
41
42 $totalsec = 0.0;
43 $scanned = 0;
44 $withcache = 0;
45 $withdiff = 0;
46 $parserCache = MediaWikiServices::getInstance()->getParserCache();
47 $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
48 while ( $pages-- > 0 ) {
49 $row = $dbr->selectRow( 'page',
50 // @todo Title::selectFields() or Title::getQueryInfo() or something
51 [
52 'page_namespace', 'page_title', 'page_id',
53 'page_len', 'page_is_redirect', 'page_latest',
54 ],
55 [
56 'page_namespace' => $this->getOption( 'namespace' ),
57 'page_is_redirect' => 0,
58 'page_random >= ' . wfRandom()
59 ],
60 __METHOD__,
61 [
62 'ORDER BY' => 'page_random',
63 ]
64 );
65
66 if ( !$row ) {
67 continue;
68 }
69 ++$scanned;
70
71 $title = Title::newFromRow( $row );
72 $page = WikiPage::factory( $title );
73 $revision = $page->getRevision()->getRevisionRecord();
74 $parserOptions = $page->makeParserOptions( 'canonical' );
75
76 $parserOutputOld = $parserCache->get( $page, $parserOptions );
77
78 if ( $parserOutputOld ) {
79 $t1 = microtime( true );
80 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
81 ->getRevisionParserOutput();
82
83 $sec = microtime( true ) - $t1;
84 $totalsec += $sec;
85
86 $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
87
88 $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
89 $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
90 $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
91 $diff = wfDiff( $oldHtml, $newHtml );
92 if ( strlen( $diff ) ) {
93 $this->output( "differences found:\n\n$diff\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$maintClass = CompareParserCache::class;
112require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
wfDiff( $before, $after, $params='-u')
Returns unified plain-text diff of two texts.
const RUN_MAINTENANCE_IF_MAIN
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.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
const DB_REPLICA
Definition defines.php:25