22 require_once __DIR__ .
'/Maintenance.php';
33 parent::__construct();
34 $this->
addDescription(
'Parse random pages and compare output to cache.' );
35 $this->
addOption(
'namespace',
'Page namespace number',
true,
true );
36 $this->
addOption(
'maxpages',
'Number of pages to try',
true,
true );
49 $parserCache = $services->getParserCache();
50 $renderer = $services->getRevisionRenderer();
51 $wikiPageFactory = $services->getWikiPageFactory();
52 while ( $pages-- > 0 ) {
53 $row = $dbr->newSelectQueryBuilder()
65 'page_namespace' => $this->
getOption(
'namespace' ),
66 'page_is_redirect' => 0,
69 ->orderBy(
'page_random' )
70 ->caller( __METHOD__ )->fetchRow();
77 $title = Title::newFromRow( $row );
78 $page = $wikiPageFactory->newFromTitle( $title );
79 $revision = $page->getRevisionRecord();
80 $parserOptions = $page->makeParserOptions(
'canonical' );
82 $parserOutputOld = $parserCache->get( $page, $parserOptions );
84 if ( $parserOutputOld ) {
85 $t1 = microtime(
true );
86 $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
87 ->getRevisionParserOutput();
89 $sec = microtime(
true ) - $t1;
92 $this->
output(
"Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
94 $this->
output(
"Found cache entry found for '{$title->getPrefixedText()}'..." );
96 $oldHtml = trim( preg_replace(
'#<!-- .+-->#Us',
'', $parserOutputOld->getText() ) );
97 $newHtml = trim( preg_replace(
'#<!-- .+-->#Us',
'', $parserOutputNew->getText() ) );
98 $diffs =
new Diff( explode(
"\n", $oldHtml ), explode(
"\n", $newHtml ) );
100 $unifiedDiff = $formatter->format( $diffs );
102 if ( strlen( $unifiedDiff ) ) {
103 $this->
output(
"differences found:\n\n$unifiedDiff\n\n" );
106 $this->
output(
"No differences found.\n" );
110 $this->
output(
"No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
114 $ave = $totalsec ? $totalsec / $scanned : 0;
115 $this->
output(
"Checked $scanned pages; $withcache had prior cache entries.\n" );
116 $this->
output(
"Pages with differences found: $withdiff\n" );
117 $this->
output(
"Average parse time: $ave sec\n" );
122 require_once RUN_MAINTENANCE_IF_MAIN;
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...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.