* Added specific page header when showing "search deleted pages" form
[lhc/web/wiklou.git] / maintenance / compareParsers.php
1 <?php
2 /**
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.
6 *
7 * Templates etc are pulled from the local wiki database, not from the dump.
8 *
9 * Copyright (C) 2011 Platonides - http://www.mediawiki.org/
10 *
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.
15 *
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.
20 *
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
25 *
26 * @file
27 * @ingroup Maintenance
28 */
29
30 require_once( dirname( __FILE__ ) . '/dumpIterator.php' );
31
32 class CompareParsers extends DumpIterator {
33
34 private $count = 0;
35
36 public function __construct() {
37 parent::__construct();
38 $this->saveFailed = false;
39 $this->mDescription = "Run a file or dump with several parsers";
40 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
41 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
42 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
43 $this->addOption( 'save-failed', 'Folder in which articles which differ will be stored.', false, true );
44 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
45 $this->addOption( 'diff-bin', 'Binary to use for diffing (can also be provided by DIFF env var).', false, false );
46 $this->addOption( 'strip-parameters', 'Remove parameters of html tags to increase readability.', false, false );
47 $this->addOption( 'show-parsed-output', 'Show the parsed html if both Parsers give the same output.', false, false );
48 }
49
50 public function checkOptions() {
51 if ( $this->hasOption('save-failed') ) {
52 $this->saveFailed = $this->getOption('save-failed');
53 }
54
55 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
56 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
57
58 $this->showDiff = $this->hasOption( 'show-diff' );
59 if ( $this->showDiff ) {
60 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
61 if ( $bin != '' ) {
62 global $wgDiff;
63 $wgDiff = $bin;
64 }
65 }
66
67 $user = new User();
68 $this->options = ParserOptions::newFromUser( $user );
69
70 if ( $this->hasOption( 'tidy' ) ) {
71 global $wgUseTidy;
72 if ( !$wgUseTidy ) {
73 $this->error( 'Tidy was requested but $wgUseTidy is not set in LocalSettings.php', true );
74 }
75 $this->options->setTidy( true );
76 }
77
78 $this->failed = 0;
79 }
80
81 public function conclusions() {
82 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
83 if ($this->count > 0)
84 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
85 }
86
87 function stripParameters( $text ) {
88 if ( !$this->stripParametersEnabled ) {
89 return $text;
90 }
91 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
92 }
93
94 /**
95 * Callback function for each revision, parse with both parsers and compare
96 * @param $rev Revision
97 */
98 public function processRevision( $rev ) {
99 $title = $rev->getTitle();
100
101 $parser1Name = $this->getOption( 'parser1' );
102 $parser2Name = $this->getOption( 'parser2' );
103
104 self::checkParserLocally( $parser1Name );
105 self::checkParserLocally( $parser2Name );
106
107 $parser1 = new $parser1Name();
108 $parser2 = new $parser2Name();
109
110 $output1 = $parser1->parse( $rev->getText(), $title, $this->options );
111 $output2 = $parser2->parse( $rev->getText(), $title, $this->options );
112
113 if ( $output1->getText() != $output2->getText() ) {
114 $this->failed++;
115 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
116
117 if ( $this->saveFailed ) {
118 file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $rev->getText());
119 }
120 if ( $this->showDiff ) {
121 $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
122 }
123 } else {
124 $this->output( $title->getPrefixedText() . "\tOK\n" );
125 if ( $this->showParsedOutput ) {
126 $this->output( $this->stripParameters( $output1->getText() ) );
127 }
128 }
129 }
130
131 private static function checkParserLocally( $parserName ) {
132 /* Look for the parser in a file appropiately named in the current folder */
133 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
134 global $wgAutoloadClasses;
135 $wgAutoloadClasses[ $parserName ] = realpath( '.' ) . "/$parserName.php";
136 }
137 }
138
139 }
140
141 $maintClass = "CompareParsers";
142 require_once( RUN_MAINTENANCE_IF_MAIN );