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 require_once __DIR__
. '/dumpIterator.php';
34 * Maintenance script to take page text out of an XML dump file and render
35 * basic HTML out to files.
37 * @ingroup Maintenance
39 class CompareParsers
extends DumpIterator
{
43 public function __construct() {
44 parent
::__construct();
45 $this->saveFailed
= false;
46 $this->addDescription( 'Run a file or dump with several parsers' );
47 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
48 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
49 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
52 'Folder in which articles which differ will be stored.',
56 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
59 'Binary to use for diffing (can also be provided by DIFF env var).',
65 'Remove parameters of html tags to increase readability.',
71 'Show the parsed html if both Parsers give the same output.',
77 public function checkOptions() {
78 if ( $this->hasOption( 'save-failed' ) ) {
79 $this->saveFailed
= $this->getOption( 'save-failed' );
82 $this->stripParametersEnabled
= $this->hasOption( 'strip-parameters' );
83 $this->showParsedOutput
= $this->hasOption( 'show-parsed-output' );
85 $this->showDiff
= $this->hasOption( 'show-diff' );
86 if ( $this->showDiff
) {
87 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
95 $this->options
= ParserOptions
::newFromUser( $user );
97 if ( $this->hasOption( 'tidy' ) ) {
98 if ( !MWTidy
::isEnabled() ) {
99 $this->fatalError( 'Tidy was requested but $wgTidyConfig is not set in LocalSettings.php' );
101 $this->options
->setTidy( true );
107 public function conclusions() {
108 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
109 if ( $this->count
> 0 ) {
110 $this->output( " (" . ( $this->failed
/ $this->count
) . "%)\n" );
114 function stripParameters( $text ) {
115 if ( !$this->stripParametersEnabled
) {
119 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
123 * Callback function for each revision, parse with both parsers and compare
124 * @param Revision $rev
126 public function processRevision( $rev ) {
127 $title = $rev->getTitle();
129 $parser1Name = $this->getOption( 'parser1' );
130 $parser2Name = $this->getOption( 'parser2' );
132 self
::checkParserLocally( $parser1Name );
133 self
::checkParserLocally( $parser2Name );
135 $parser1 = new $parser1Name();
136 $parser2 = new $parser2Name();
138 $content = $rev->getContent();
140 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT
) {
141 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
142 . "but {$content->getModel()}\n" );
147 $text = strval( $content->getText() );
149 $output1 = $parser1->parse( $text, $title, $this->options
);
150 $output2 = $parser2->parse( $text, $title, $this->options
);
152 if ( $output1->getText() != $output2->getText() ) {
154 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
156 if ( $this->saveFailed
) {
158 $this->saveFailed
. '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
162 if ( $this->showDiff
) {
163 $this->output( wfDiff(
164 $this->stripParameters( $output1->getText() ),
165 $this->stripParameters( $output2->getText() ),
170 $this->output( $title->getPrefixedText() . "\tOK\n" );
172 if ( $this->showParsedOutput
) {
173 $this->output( $this->stripParameters( $output1->getText() ) );
178 private static function checkParserLocally( $parserName ) {
179 /* Look for the parser in a file appropiately named in the current folder */
180 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
181 global $wgAutoloadClasses;
182 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
187 $maintClass = CompareParsers
::class;
188 require_once RUN_MAINTENANCE_IF_MAIN
;