(bug 40829) Show cascading protection info on action=info
[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 © 2011 Platonides
10 * http://www.mediawiki.org/
11 *
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.
16 *
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.
21 *
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
26 *
27 * @file
28 * @ingroup Maintenance
29 */
30
31 require_once( __DIR__ . '/dumpIterator.php' );
32
33 /**
34 * Maintenance script to take page text out of an XML dump file and render
35 * basic HTML out to files.
36 *
37 * @ingroup Maintenance
38 */
39 class CompareParsers extends DumpIterator {
40
41 private $count = 0;
42
43 public function __construct() {
44 parent::__construct();
45 $this->saveFailed = false;
46 $this->mDescription = "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 );
50 $this->addOption( 'save-failed', 'Folder in which articles which differ will be stored.', false, true );
51 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
52 $this->addOption( 'diff-bin', 'Binary to use for diffing (can also be provided by DIFF env var).', false, false );
53 $this->addOption( 'strip-parameters', 'Remove parameters of html tags to increase readability.', false, false );
54 $this->addOption( 'show-parsed-output', 'Show the parsed html if both Parsers give the same output.', false, false );
55 }
56
57 public function checkOptions() {
58 if ( $this->hasOption('save-failed') ) {
59 $this->saveFailed = $this->getOption('save-failed');
60 }
61
62 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
63 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
64
65 $this->showDiff = $this->hasOption( 'show-diff' );
66 if ( $this->showDiff ) {
67 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
68 if ( $bin != '' ) {
69 global $wgDiff;
70 $wgDiff = $bin;
71 }
72 }
73
74 $user = new User();
75 $this->options = ParserOptions::newFromUser( $user );
76
77 if ( $this->hasOption( 'tidy' ) ) {
78 global $wgUseTidy;
79 if ( !$wgUseTidy ) {
80 $this->error( 'Tidy was requested but $wgUseTidy is not set in LocalSettings.php', true );
81 }
82 $this->options->setTidy( true );
83 }
84
85 $this->failed = 0;
86 }
87
88 public function conclusions() {
89 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
90 if ($this->count > 0)
91 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
92 }
93
94 function stripParameters( $text ) {
95 if ( !$this->stripParametersEnabled ) {
96 return $text;
97 }
98 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
99 }
100
101 /**
102 * Callback function for each revision, parse with both parsers and compare
103 * @param $rev Revision
104 */
105 public function processRevision( $rev ) {
106 $title = $rev->getTitle();
107
108 $parser1Name = $this->getOption( 'parser1' );
109 $parser2Name = $this->getOption( 'parser2' );
110
111 self::checkParserLocally( $parser1Name );
112 self::checkParserLocally( $parser2Name );
113
114 $parser1 = new $parser1Name();
115 $parser2 = new $parser2Name();
116
117 $content = $rev->getContent();
118
119 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
120 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext but {$content->getModel()}\n" );
121 return;
122 }
123
124 $text = strval( $content->getNativeData() );
125
126 $output1 = $parser1->parse( $text, $title, $this->options );
127 $output2 = $parser2->parse( $text, $title, $this->options );
128
129 if ( $output1->getText() != $output2->getText() ) {
130 $this->failed++;
131 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
132
133 if ( $this->saveFailed ) {
134 file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $text );
135 }
136 if ( $this->showDiff ) {
137 $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
138 }
139 } else {
140 $this->output( $title->getPrefixedText() . "\tOK\n" );
141 if ( $this->showParsedOutput ) {
142 $this->output( $this->stripParameters( $output1->getText() ) );
143 }
144 }
145 }
146
147 private static function checkParserLocally( $parserName ) {
148 /* Look for the parser in a file appropiately named in the current folder */
149 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
150 global $wgAutoloadClasses;
151 $wgAutoloadClasses[ $parserName ] = realpath( '.' ) . "/$parserName.php";
152 }
153 }
154
155 }
156
157 $maintClass = "CompareParsers";
158 require_once( RUN_MAINTENANCE_IF_MAIN );