MediaWiki master
compareParsers.php
Go to the documentation of this file.
1<?php
23
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/dumpIterator.php';
26// @codeCoverageIgnoreEnd
27
35
37 private $count = 0;
39 private $saveFailed = false;
41 private $stripParametersEnabled;
43 private $showParsedOutput;
45 private $showDiff;
47 private $options;
49 private $failed;
50
51 public function __construct() {
52 parent::__construct();
53 $this->addDescription( 'Run a file or dump with several parsers' );
54 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
55 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
56 $this->addOption(
57 'save-failed',
58 'Folder in which articles which differ will be stored.',
59 false,
60 true
61 );
62 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
63 $this->addOption(
64 'diff-bin',
65 'Binary to use for diffing (can also be provided by DIFF env var).',
66 false,
67 false
68 );
69 $this->addOption(
70 'strip-parameters',
71 'Remove parameters of html tags to increase readability.',
72 false,
73 false
74 );
75 $this->addOption(
76 'show-parsed-output',
77 'Show the parsed html if both Parsers give the same output.',
78 false,
79 false
80 );
81 }
82
83 public function checkOptions() {
84 if ( $this->hasOption( 'save-failed' ) ) {
85 $this->saveFailed = $this->getOption( 'save-failed' );
86 }
87
88 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
89 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
90
91 $this->showDiff = $this->hasOption( 'show-diff' );
92 if ( $this->showDiff ) {
93 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
94 if ( $bin != '' ) {
95 global $wgDiff;
96 $wgDiff = $bin;
97 }
98 }
99
100 $user = new User();
101 $this->options = ParserOptions::newFromUser( $user );
102
103 $this->failed = 0;
104 }
105
106 public function conclusions() {
107 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
108 if ( $this->count > 0 ) {
109 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
110 }
111 }
112
113 private function stripParameters( string $text ): string {
114 if ( !$this->stripParametersEnabled ) {
115 return $text;
116 }
117
118 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
119 }
120
124 public function processRevision( WikiRevision $rev ) {
125 $title = $rev->getTitle();
126
127 $parser1Name = $this->getOption( 'parser1' );
128 $parser2Name = $this->getOption( 'parser2' );
129
130 self::checkParserLocally( $parser1Name );
131 self::checkParserLocally( $parser2Name );
132
133 $parser1 = new $parser1Name();
134 $parser2 = new $parser2Name();
135
136 $content = $rev->getContent();
137
138 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
139 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
140 . "but {$content->getModel()}\n" );
141
142 return;
143 }
144
146 '@phan-var WikitextContent $content';
147 $text = strval( $content->getText() );
148
149 $output1 = $parser1->parse( $text, $title, $this->options );
150 $output2 = $parser2->parse( $text, $title, $this->options );
151
152 if ( $output1->getText() != $output2->getText() ) {
153 $this->failed++;
154 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
155
156 if ( $this->saveFailed ) {
157 file_put_contents(
158 $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
159 $text
160 );
161 }
162 if ( $this->showDiff ) {
163 $diffs = new Diff(
164 explode( "\n", $this->stripParameters( $output1->getText() ) ),
165 explode( "\n", $this->stripParameters( $output2->getText() ) )
166 );
167 $formatter = new UnifiedDiffFormatter();
168 $unifiedDiff = $formatter->format( $diffs );
169
170 $this->output( $unifiedDiff );
171 }
172 } else {
173 $this->output( $title->getPrefixedText() . "\tOK\n" );
174
175 if ( $this->showParsedOutput ) {
176 $this->output( $this->stripParameters( $output1->getText() ) );
177 }
178 }
179 }
180
181 private static function checkParserLocally( string $parserName ) {
182 /* Look for the parser in a file appropriately named in the current folder */
183 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
184 global $wgAutoloadClasses;
185 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
186 }
187 }
188}
189
190// @codeCoverageIgnoreStart
191$maintClass = CompareParsers::class;
192require_once RUN_MAINTENANCE_IF_MAIN;
193// @codeCoverageIgnoreEnd
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
$wgAutoloadClasses
Definition Setup.php:139
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Maintenance script to take page text out of an XML dump file and render basic HTML out to files.
conclusions()
Stub function for giving data about what was computed.
processRevision(WikiRevision $rev)
Callback function for each revision, parse with both parsers and compare.
__construct()
Default constructor.
checkOptions()
Stub function for processing additional options.
Base class for iterating over a dump.
Content object for wiki text pages.
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
output( $out, $channel=null)
Throw some output to the user.
error( $err)
Throw an error to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Set options of the Parser.
User class for the MediaWiki software.
Definition User.php:129
Class representing a 'diff' between two sequences of strings.
Definition Diff.php:20
A formatter that outputs unified diffs.
$maintClass
$wgDiff
Config variable stub for the Diff setting, for use by phpdoc and IDEs.