| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
require_once __DIR__ . '/Maintenance.php'; |
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use Wikimedia\Diff\Diff; |
| 28 |
use Wikimedia\Diff\UnifiedDiffFormatter; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class CompareParserCache extends Maintenance { |
| 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 |
|
| 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 |
|
| 125 |
$maintClass = CompareParserCache::class; |
| 126 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 127 |
|