build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[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 * https://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 /** @var bool */
43 private $saveFailed;
44 /** @var bool */
45 private $stripParametersEnabled;
46 /** @var bool */
47 private $showParsedOutput;
48 /** @var bool */
49 private $showDiff;
50 /** @var ParserOptions */
51 private $options;
52 /** @var int */
53 private $failed;
54
55 public function __construct() {
56 parent::__construct();
57 $this->saveFailed = false;
58 $this->addDescription( 'Run a file or dump with several parsers' );
59 $this->addOption( 'parser1', 'The first parser to compare.', true, true );
60 $this->addOption( 'parser2', 'The second parser to compare.', true, true );
61 $this->addOption( 'tidy', 'Run tidy on the articles.', false, false );
62 $this->addOption(
63 'save-failed',
64 'Folder in which articles which differ will be stored.',
65 false,
66 true
67 );
68 $this->addOption( 'show-diff', 'Show a diff of the two renderings.', false, false );
69 $this->addOption(
70 'diff-bin',
71 'Binary to use for diffing (can also be provided by DIFF env var).',
72 false,
73 false
74 );
75 $this->addOption(
76 'strip-parameters',
77 'Remove parameters of html tags to increase readability.',
78 false,
79 false
80 );
81 $this->addOption(
82 'show-parsed-output',
83 'Show the parsed html if both Parsers give the same output.',
84 false,
85 false
86 );
87 }
88
89 public function checkOptions() {
90 if ( $this->hasOption( 'save-failed' ) ) {
91 $this->saveFailed = $this->getOption( 'save-failed' );
92 }
93
94 $this->stripParametersEnabled = $this->hasOption( 'strip-parameters' );
95 $this->showParsedOutput = $this->hasOption( 'show-parsed-output' );
96
97 $this->showDiff = $this->hasOption( 'show-diff' );
98 if ( $this->showDiff ) {
99 $bin = $this->getOption( 'diff-bin', getenv( 'DIFF' ) );
100 if ( $bin != '' ) {
101 global $wgDiff;
102 $wgDiff = $bin;
103 }
104 }
105
106 $user = new User();
107 $this->options = ParserOptions::newFromUser( $user );
108
109 if ( $this->hasOption( 'tidy' ) ) {
110 if ( !MWTidy::isEnabled() ) {
111 $this->fatalError( 'Tidy was requested but $wgTidyConfig is not set in LocalSettings.php' );
112 }
113 $this->options->setTidy( true );
114 }
115
116 $this->failed = 0;
117 }
118
119 public function conclusions() {
120 $this->error( "{$this->failed} failed revisions out of {$this->count}" );
121 if ( $this->count > 0 ) {
122 $this->output( " (" . ( $this->failed / $this->count ) . "%)\n" );
123 }
124 }
125
126 function stripParameters( $text ) {
127 if ( !$this->stripParametersEnabled ) {
128 return $text;
129 }
130
131 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
132 }
133
134 /**
135 * Callback function for each revision, parse with both parsers and compare
136 * @param Revision $rev
137 */
138 public function processRevision( $rev ) {
139 $title = $rev->getTitle();
140
141 $parser1Name = $this->getOption( 'parser1' );
142 $parser2Name = $this->getOption( 'parser2' );
143
144 self::checkParserLocally( $parser1Name );
145 self::checkParserLocally( $parser2Name );
146
147 $parser1 = new $parser1Name();
148 $parser2 = new $parser2Name();
149
150 $content = $rev->getContent();
151
152 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
153 $this->error( "Page {$title->getPrefixedText()} does not contain wikitext "
154 . "but {$content->getModel()}\n" );
155
156 return;
157 }
158
159 /** @var WikitextContent $content */
160 '@phan-var WikitextContent $content';
161 $text = strval( $content->getText() );
162
163 $output1 = $parser1->parse( $text, $title, $this->options );
164 $output2 = $parser2->parse( $text, $title, $this->options );
165
166 if ( $output1->getText() != $output2->getText() ) {
167 $this->failed++;
168 $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
169
170 if ( $this->saveFailed ) {
171 file_put_contents(
172 $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt",
173 $text
174 );
175 }
176 if ( $this->showDiff ) {
177 $this->output( wfDiff(
178 $this->stripParameters( $output1->getText() ),
179 $this->stripParameters( $output2->getText() ),
180 ''
181 ) );
182 }
183 } else {
184 $this->output( $title->getPrefixedText() . "\tOK\n" );
185
186 if ( $this->showParsedOutput ) {
187 $this->output( $this->stripParameters( $output1->getText() ) );
188 }
189 }
190 }
191
192 private static function checkParserLocally( $parserName ) {
193 /* Look for the parser in a file appropiately named in the current folder */
194 if ( !class_exists( $parserName ) && file_exists( "$parserName.php" ) ) {
195 global $wgAutoloadClasses;
196 $wgAutoloadClasses[$parserName] = realpath( '.' ) . "/$parserName.php";
197 }
198 }
199 }
200
201 $maintClass = CompareParsers::class;
202 require_once RUN_MAINTENANCE_IF_MAIN;