Spacing
[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__ ) . '/Maintenance.php' );
31
32 class CompareParsers extends Maintenance {
33
34 private $count = 0;
35 private $outputDirectory, $startTime;
36
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( 'file', 'File with text to run.', false, true );
44 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
45 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
46 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
47 $this->addOption( 'save-failed', 'Folder in which articles which differ will be stored.', false, true );
48 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
49 $this->addOption( 'diff-bin', 'Binary to use for diffing (can also be provided by DIFF env var).', false, false );
50 $this->addOption( 'strip-parameters', 'Remove parameters of html tags to increase readability.', false, false );
51 $this->addOption( 'show-parsed-output', 'Show the parsed html if both Parsers give the same output.', false, false );
52 }
53
54 public function execute() {
55 if (! ( $this->hasOption('file') ^ $this->hasOption('dump') ) ) {
56 $this->error("You must provide file or dump", true);
57 }
58
59 if ( $this->hasOption('save-failed') ) {
60 $this->saveFailed = $this->getOption('save-failed');
61 }
62
63 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
64 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
65
66 $this->showDiff = $this->hasOption( 'show-diff' );
67 if ( $this->showDiff ) {
68 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
69 if ( $bin != '' ) {
70 global $wgDiff;
71 $wgDiff = $bin;
72 }
73 }
74
75 $user = new User();
76 $this->options = ParserOptions::newFromUser( $user );
77
78 if ( $this->hasOption( 'tidy' ) ) {
79 global $wgUseTidy;
80 if ( !$wgUseTidy ) {
81 $this->error( 'Tidy was requested but $wgUseTidy is not set in LocalSettings.php', true );
82 }
83 $this->options->setTidy( true );
84 }
85
86 if ( $this->hasOption('file') ) {
87 $revision = new WikiRevision;
88
89 $revision->setText( file_get_contents( $this->getOption('file') ) );
90 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption('file'), '.txt' ) ) ) );
91 $this->handleRevision( $revision );
92 return;
93 }
94
95 $this->startTime = wfTime();
96
97 if ( $this->getOption('dump') == '-' ) {
98 $source = new ImportStreamSource( $this->getStdin() );
99 } else {
100 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
101 }
102 $importer = new WikiImporter( $source );
103
104 $importer->setRevisionCallback(
105 array( &$this, 'handleRevision' ) );
106
107 $this->from = $this->getOption( 'from', null );
108 $this->count = 0;
109 $this->failed = 0;
110 $importer->doImport();
111
112 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
113 if ($this->count > 0)
114 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
115
116 $delta = wfTime() - $this->startTime;
117 $this->error( "Compared {$this->count} pages in " . round($delta, 2) . " seconds " );
118 if ($delta > 0)
119 $this->error( round($this->count / $delta, 2) . " pages/sec" );
120 $this->error( "\n" );
121 }
122
123 function stripParameters( $text ) {
124 if ( !$this->stripParametersEnabled ) {
125 return $text;
126 }
127 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
128 }
129
130 /**
131 * Callback function for each revision, parse with both parsers and compare
132 * @param $rev Revision
133 */
134 public function handleRevision( $rev ) {
135 $title = $rev->getTitle();
136 if ( !$title ) {
137 $this->error( "Got bogus revision with null title!" );
138 return;
139 }
140
141 $this->count++;
142 if ( isset( $this->from ) ) {
143 if ( $this->from != $title )
144 return;
145 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
146
147 $this->count = 1;
148 $this->from = null;
149 }
150
151
152
153 $parser1Name = $this->getOption( 'parser1' );
154 $parser2Name = $this->getOption( 'parser2' );
155
156 self::checkParserLocally( $parser1Name );
157 self::checkParserLocally( $parser2Name );
158
159 $parser1 = new $parser1Name();
160 $parser2 = new $parser2Name();
161
162 $output1 = $parser1->parse( $rev->getText(), $title, $this->options );
163 $output2 = $parser2->parse( $rev->getText(), $title, $this->options );
164
165 if ( $output1->getText() != $output2->getText() ) {
166 $this->failed++;
167 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
168
169 if ( $this->saveFailed ) {
170 file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $rev->getText());
171 }
172 if ( $this->showDiff ) {
173 $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
174 }
175 } else {
176 $this->output( $title->getPrefixedText() . "\tOK\n" );
177 if ( $this->showParsedOutput ) {
178 $this->output( $this->stripParameters( $output1->getText() ) );
179 }
180 }
181 }
182
183 private static function checkParserLocally( $parserName ) {
184 /* Look for the parser in a file appropiately named in the current folder */
185 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
186 global $wgAutoloadClasses;
187 $wgAutoloadClasses[ $parserName ] = realpath( '.' ) . "/$parserName.php";
188 }
189 }
190
191 }
192
193 $maintClass = "CompareParsers";
194 require_once( DO_MAINTENANCE );