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 (C) 2011 Platonides - http://www.mediawiki.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 * @ingroup Maintenance
30 require_once( dirname( __FILE__
) . '/dumpIterator.php' );
32 class CompareParsers
extends DumpIterator
{
37 public function __construct() {
38 parent
::__construct();
39 $this->saveFailed
= false;
40 $this->mDescription
= "Run a file or dump with several parsers";
41 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
42 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
43 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
44 $this->addOption( 'save-failed', 'Folder in which articles which differ will be stored.', false, true );
45 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
46 $this->addOption( 'diff-bin', 'Binary to use for diffing (can also be provided by DIFF env var).', false, false );
47 $this->addOption( 'strip-parameters', 'Remove parameters of html tags to increase readability.', false, false );
48 $this->addOption( 'show-parsed-output', 'Show the parsed html if both Parsers give the same output.', false, false );
51 public function checkOptions() {
52 if ( $this->hasOption('save-failed') ) {
53 $this->saveFailed
= $this->getOption('save-failed');
56 $this->stripParametersEnabled
= $this->hasOption( 'strip-parameters' );
57 $this->showParsedOutput
= $this->hasOption( 'show-parsed-output' );
59 $this->showDiff
= $this->hasOption( 'show-diff' );
60 if ( $this->showDiff
) {
61 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
69 $this->options
= ParserOptions
::newFromUser( $user );
71 if ( $this->hasOption( 'tidy' ) ) {
74 $this->error( 'Tidy was requested but $wgUseTidy is not set in LocalSettings.php', true );
76 $this->options
->setTidy( true );
82 public function conclusions() {
83 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
85 $this->output( " (" . ( $this->failed
/ $this->count
) . "%)\n" );
88 function stripParameters( $text ) {
89 if ( !$this->stripParametersEnabled
) {
92 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
96 * Callback function for each revision, parse with both parsers and compare
97 * @param $rev Revision
99 public function processRevision( $rev ) {
100 $title = $rev->getTitle();
102 $parser1Name = $this->getOption( 'parser1' );
103 $parser2Name = $this->getOption( 'parser2' );
105 self
::checkParserLocally( $parser1Name );
106 self
::checkParserLocally( $parser2Name );
108 $parser1 = new $parser1Name();
109 $parser2 = new $parser2Name();
111 $output1 = $parser1->parse( $rev->getText(), $title, $this->options
);
112 $output2 = $parser2->parse( $rev->getText(), $title, $this->options
);
114 if ( $output1->getText() != $output2->getText() ) {
116 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
118 if ( $this->saveFailed
) {
119 file_put_contents( $this->saveFailed
. '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $rev->getText());
121 if ( $this->showDiff
) {
122 $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
125 $this->output( $title->getPrefixedText() . "\tOK\n" );
126 if ( $this->showParsedOutput
) {
127 $this->output( $this->stripParameters( $output1->getText() ) );
132 private static function checkParserLocally( $parserName ) {
133 /* Look for the parser in a file appropiately named in the current folder */
134 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
135 global $wgAutoloadClasses;
136 $wgAutoloadClasses[ $parserName ] = realpath( '.' ) . "/$parserName.php";
142 $maintClass = "CompareParsers";
143 require( RUN_MAINTENANCE_IF_MAIN
);