3 * Take page text out of an XML dump file and render basic HTML out to files.
4 * This is *NOT* suitable for publishing or offline use; it's intended for
5 * running comparative tests of parsing behavior using real-world data.
7 * Templates etc are pulled from the local wiki database, not from the dump.
9 * Copyright © 2011 Platonides
10 * https://www.mediawiki.org/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
28 * @ingroup Maintenance
31 use MediaWiki\Content\WikitextContent
;
32 use MediaWiki\Parser\ParserOptions
;
33 use MediaWiki\User\User
;
34 use Wikimedia\Diff\Diff
;
35 use Wikimedia\Diff\UnifiedDiffFormatter
;
37 // @codeCoverageIgnoreStart
38 require_once __DIR__
. '/dumpIterator.php';
39 // @codeCoverageIgnoreEnd
42 * Maintenance script to take page text out of an XML dump file and render
43 * basic HTML out to files.
45 * @ingroup Maintenance
47 class CompareParsers
extends DumpIterator
{
51 /** @var string|false */
52 private $saveFailed = false;
54 private $stripParametersEnabled;
56 private $showParsedOutput;
59 /** @var ParserOptions */
64 public function __construct() {
65 parent
::__construct();
66 $this->addDescription( 'Run a file or dump with several parsers' );
67 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
68 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
71 'Folder in which articles which differ will be stored.',
75 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
78 'Binary to use for diffing (can also be provided by DIFF env var).',
84 'Remove parameters of html tags to increase readability.',
90 'Show the parsed html if both Parsers give the same output.',
96 public function checkOptions() {
97 if ( $this->hasOption( 'save-failed' ) ) {
98 $this->saveFailed
= $this->getOption( 'save-failed' );
101 $this->stripParametersEnabled
= $this->hasOption( 'strip-parameters' );
102 $this->showParsedOutput
= $this->hasOption( 'show-parsed-output' );
104 $this->showDiff
= $this->hasOption( 'show-diff' );
105 if ( $this->showDiff
) {
106 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
114 $this->options
= ParserOptions
::newFromUser( $user );
119 public function conclusions() {
120 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
121 if ( $this->count
> 0 ) {
122 $this->output( " (" . ( $this->failed
/ $this->count
) . "%)\n" );
126 private function stripParameters( $text ) {
127 if ( !$this->stripParametersEnabled
) {
131 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
135 * Callback function for each revision, parse with both parsers and compare
136 * @param WikiRevision $rev
138 public function processRevision( WikiRevision
$rev ) {
139 $title = $rev->getTitle();
141 $parser1Name = $this->getOption( 'parser1' );
142 $parser2Name = $this->getOption( 'parser2' );
144 self
::checkParserLocally( $parser1Name );
145 self
::checkParserLocally( $parser2Name );
147 $parser1 = new $parser1Name();
148 $parser2 = new $parser2Name();
150 $content = $rev->getContent();
152 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT
) {
153 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
154 . "but {$content->getModel()}\n" );
159 /** @var WikitextContent $content */
160 '@phan-var WikitextContent $content';
161 $text = strval( $content->getText() );
163 $output1 = $parser1->parse( $text, $title, $this->options
);
164 $output2 = $parser2->parse( $text, $title, $this->options
);
166 if ( $output1->getText() != $output2->getText() ) {
168 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
170 if ( $this->saveFailed
) {
172 $this->saveFailed
. '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
176 if ( $this->showDiff
) {
178 explode( "\n", $this->stripParameters( $output1->getText() ) ),
179 explode( "\n", $this->stripParameters( $output2->getText() ) )
181 $formatter = new UnifiedDiffFormatter();
182 $unifiedDiff = $formatter->format( $diffs );
184 $this->output( $unifiedDiff );
187 $this->output( $title->getPrefixedText() . "\tOK\n" );
189 if ( $this->showParsedOutput
) {
190 $this->output( $this->stripParameters( $output1->getText() ) );
195 private static function checkParserLocally( $parserName ) {
196 /* Look for the parser in a file appropriately named in the current folder */
197 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
198 global $wgAutoloadClasses;
199 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
204 // @codeCoverageIgnoreStart
205 $maintClass = CompareParsers
::class;
206 require_once RUN_MAINTENANCE_IF_MAIN
;
207 // @codeCoverageIgnoreEnd