MediaWiki  master
compareParserCache.php
Go to the documentation of this file.
1 <?php
22 require_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  $services = MediaWikiServices::getInstance();
47  $parserCache = $services->getParserCache();
48  $renderer = $services->getRevisionRenderer();
49  $wikiPageFactory = $services->getWikiPageFactory();
50  while ( $pages-- > 0 ) {
51  $row = $dbr->selectRow( 'page',
52  // @todo Title::selectFields() or Title::getQueryInfo() or something
53  [
54  'page_namespace', 'page_title', 'page_id',
55  'page_len', 'page_is_redirect', 'page_latest',
56  ],
57  [
58  'page_namespace' => $this->getOption( 'namespace' ),
59  'page_is_redirect' => 0,
60  'page_random >= ' . wfRandom()
61  ],
62  __METHOD__,
63  [
64  'ORDER BY' => 'page_random',
65  ]
66  );
67 
68  if ( !$row ) {
69  continue;
70  }
71  ++$scanned;
72 
73  $title = Title::newFromRow( $row );
74  $page = $wikiPageFactory->newFromTitle( $title );
75  $revision = $page->getRevisionRecord();
76  $parserOptions = $page->makeParserOptions( 'canonical' );
77 
78  $parserOutputOld = $parserCache->get( $page, $parserOptions );
79 
80  if ( $parserOutputOld ) {
81  $t1 = microtime( true );
82  $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
83  ->getRevisionParserOutput();
84 
85  $sec = microtime( true ) - $t1;
86  $totalsec += $sec;
87 
88  $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
89 
90  $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
91 
92  $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
93  $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
94  $diffs = new Diff( explode( "\n", $oldHtml ), explode( "\n", $newHtml ) );
95  $formatter = new UnifiedDiffFormatter();
96  $unifiedDiff = $formatter->format( $diffs );
97 
98  if ( strlen( $unifiedDiff ) ) {
99  $this->output( "differences found:\n\n$unifiedDiff\n\n" );
100  ++$withdiff;
101  } else {
102  $this->output( "No differences found.\n" );
103  }
104  ++$withcache;
105  } else {
106  $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
107  }
108  }
109 
110  $ave = $totalsec ? $totalsec / $scanned : 0;
111  $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
112  $this->output( "Pages with differences found: $withdiff\n" );
113  $this->output( "Average parse time: $ave sec\n" );
114  }
115 }
116 
117 $maintClass = CompareParserCache::class;
118 require_once RUN_MAINTENANCE_IF_MAIN;
CompareParserCache
Definition: compareParserCache.php:29
CompareParserCache\__construct
__construct()
Default constructor.
Definition: compareParserCache.php:30
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$dbr
$dbr
Definition: testCompression.php:54
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:592
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
UnifiedDiffFormatter
A formatter that outputs unified diffs @newable.
Definition: UnifiedDiffFormatter.php:32
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
CompareParserCache\execute
execute()
Do the actual work.
Definition: compareParserCache.php:37
wfRandom
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
Definition: GlobalFunctions.php:239
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
$maintClass
$maintClass
Definition: compareParserCache.php:117
Diff
Class representing a 'diff' between two sequences of strings.
Definition: Diff.php:32