stupid PostgreSQL insists on parentheses :)
[lhc/web/wiklou.git] / maintenance / parserTests.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * @todo Make this more independent of the configuration (and if possible the database)
22 * @todo document
23 * @package MediaWiki
24 * @subpackage Maintenance
25 */
26
27 /** */
28 $optionsWithArgs = array('regex');
29
30 require_once( 'commandLine.inc' );
31 require_once( 'languages/LanguageUtf8.php' );
32
33 class ParserTest {
34
35 /**
36 * Sets terminal colorization and diff/quick modes depending on OS and
37 * command-line options (--color and --quick).
38 *
39 * @access public
40 */
41 function ParserTest() {
42 global $options;
43 if( isset( $_SERVER['argv'] ) && in_array( '--color', $_SERVER['argv'] ) ) {
44 $this->color = true;
45 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=yes', $_SERVER['argv'] ) ) {
46 $this->color = true;
47 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=no', $_SERVER['argv'] ) ) {
48 $this->color = false;
49 } elseif( wfIsWindows() ) {
50 $this->color = false;
51 } else {
52 # Only colorize output if stdout is a terminal.
53 $this->color = posix_isatty(1);
54 }
55
56 if( isset( $_SERVER['argv'] ) && in_array( '--quick', $_SERVER['argv'] ) ) {
57 $this->showDiffs = false;
58 } else {
59 $this->showDiffs = true;
60 }
61
62 if (isset($options['regex'])) {
63 $this->regex = $options['regex'];
64 }
65 else {
66 # Matches anything
67 $this->regex = '';
68 }
69 }
70
71 /**
72 * Remove last character if it is a newline
73 * @access private
74 */
75 function chomp($s) {
76 if (substr($s, -1) === "\n") {
77 return substr($s, 0, -1);
78 }
79 else {
80 return $s;
81 }
82 }
83
84 /**
85 * Run a series of tests listed in the given text file.
86 * Each test consists of a brief description, wikitext input,
87 * and the expected HTML output.
88 *
89 * Prints status updates on stdout and counts up the total
90 * number and percentage of passed tests.
91 *
92 * @param string $filename
93 * @return bool True if passed all tests, false if any tests failed.
94 * @access public
95 */
96 function runTestsFromFile( $filename ) {
97 $infile = fopen( $filename, 'rt' );
98 if( !$infile ) {
99 die( "Couldn't open parserTests.txt\n" );
100 }
101
102 $data = array();
103 $section = null;
104 $success = 0;
105 $total = 0;
106 $n = 0;
107 while( false !== ($line = fgets( $infile ) ) ) {
108 $n++;
109 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
110 $section = strtolower( $matches[1] );
111 if( $section == 'endarticle') {
112 if( !isset( $data['text'] ) ) {
113 die( "'endarticle' without 'text' at line $n\n" );
114 }
115 if( !isset( $data['article'] ) ) {
116 die( "'endarticle' without 'article' at line $n\n" );
117 }
118 $this->addArticle($this->chomp($data['article']), $this->chomp($data['text']), $n);
119 $data = array();
120 $section = null;
121 continue;
122 }
123 if( $section == 'end' ) {
124 if( !isset( $data['test'] ) ) {
125 die( "'end' without 'test' at line $n\n" );
126 }
127 if( !isset( $data['input'] ) ) {
128 die( "'end' without 'input' at line $n\n" );
129 }
130 if( !isset( $data['result'] ) ) {
131 die( "'end' without 'result' at line $n\n" );
132 }
133 if( !isset( $data['options'] ) ) {
134 $data['options'] = '';
135 }
136 else {
137 $data['options'] = $this->chomp( $data['options'] );
138 }
139 if (preg_match('/\\bdisabled\\b/i', $data['options'])
140 || !preg_match("/{$this->regex}/i", $data['test'])) {
141 # disabled test
142 $data = array();
143 $section = null;
144 continue;
145 }
146 if( $this->runTest(
147 $this->chomp( $data['test'] ),
148 $this->chomp( $data['input'] ),
149 $this->chomp( $data['result'] ),
150 $this->chomp( $data['options'] ) ) ) {
151 $success++;
152 }
153 $total++;
154 $data = array();
155 $section = null;
156 continue;
157 }
158 if ( isset ($data[$section] ) ) {
159 die ( "duplicate section '$section' at line $n\n" );
160 }
161 $data[$section] = '';
162 continue;
163 }
164 if( $section ) {
165 $data[$section] .= $line;
166 }
167 }
168 if( $total > 0 ) {
169 $ratio = IntVal( 100.0 * $success / $total );
170 print $this->termColor( 1 ) . "\nPassed $success of $total tests ($ratio%) ";
171 if( $success == $total ) {
172 print $this->termColor( 32 ) . "PASSED!";
173 } else {
174 print $this->termColor( 31 ) . "FAILED!";
175 }
176 print $this->termReset() . "\n";
177 return ($success == $total);
178 } else {
179 die( "No tests found.\n" );
180 }
181 }
182
183 /**
184 * Run a given wikitext input through a freshly-constructed wiki parser,
185 * and compare the output against the expected results.
186 * Prints status and explanatory messages to stdout.
187 *
188 * @param string $input Wikitext to try rendering
189 * @param string $result Result to output
190 * @return bool
191 */
192 function runTest( $desc, $input, $result, $opts ) {
193 print "Running test $desc... ";
194
195 $this->setupGlobals($opts);
196
197 $user =& new User();
198 $options =& ParserOptions::newFromUser( $user );
199
200 if (preg_match('/\\bmath\\b/i', $opts)) {
201 # XXX this should probably be done by the ParserOptions
202 require_once('Math.php');
203
204 $options->setUseTex(true);
205 }
206
207 if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) {
208 $titleText = $m[1];
209 }
210 else {
211 $titleText = 'Parser test';
212 }
213
214 $parser =& new Parser();
215 $title =& Title::makeTitle( NS_MAIN, $titleText );
216
217 if (preg_match('/\\bpst\\b/i', $opts)) {
218 $out = $parser->preSaveTransform( $input, $title, $user, $options );
219 }
220 else if (preg_match('/\\bmsg\\b/i', $opts)) {
221 $out = $parser->transformMsg( $input, $options );
222 }
223 else {
224 $output =& $parser->parse( $input, $title, $options );
225 $out = $output->getText();
226
227 $op = new OutputPage();
228 $op->replaceLinkHolders($out);
229
230 if (preg_match('/\\bill\\b/i', $opts)) {
231 $out .= implode( ' ', $output->getLanguageLinks() );
232 }
233 if (preg_match('/\\bcat\\b/i', $opts)) {
234 $out .= implode( ' ', $output->getCategoryLinks() );
235 }
236
237 if ($GLOBALS['wgUseTidy']) {
238 $out = Parser::tidy($out);
239 $result = Parser::tidy($result);
240 }
241 }
242
243 $this->teardownGlobals();
244
245 if( $result === $out ) {
246 return $this->showSuccess( $desc );
247 } else {
248 return $this->showFailure( $desc, $result, $out );
249 }
250 }
251
252 /**
253 * Set up the global variables for a consistent environment for each test.
254 * Ideally this should replace the global configuration entirely.
255 *
256 * @access private
257 */
258 function setupGlobals($opts = '') {
259 $settings = array(
260 'wgServer' => 'http://localhost',
261 'wgScript' => '/index.php',
262 'wgScriptPath' => '/',
263 'wgArticlePath' => '/wiki/$1',
264 'wgUploadPath' => '/images',
265 'wgSitename' => 'MediaWiki',
266 'wgLanguageCode' => 'en',
267 'wgUseLatin1' => false,
268 'wgDBprefix' => 'parsertest',
269
270 'wgLoadBalancer' => LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] ),
271 'wgLang' => new LanguageUtf8(),
272 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
273 );
274 $this->savedGlobals = array();
275 foreach( $settings as $var => $val ) {
276 $this->savedGlobals[$var] = $GLOBALS[$var];
277 $GLOBALS[$var] = $val;
278 }
279 $GLOBALS['wgLoadBalancer']->loadMasterPos();
280 $this->setupDatabase();
281 }
282
283 /**
284 * Set up a temporary set of wiki tables to work with for the tests.
285 * Currently this will only be done once per run, and any changes to
286 * the db will be visible to later tests in the run.
287 *
288 * @access private
289 */
290 function setupDatabase() {
291 static $setupDB = false;
292 global $wgDBprefix;
293
294 # Make sure we don't mess with the live DB
295 if (!$setupDB && $wgDBprefix === 'parsertest') {
296 $db =& wfGetDB( DB_MASTER );
297
298 # List of temporary tables to create, without prefix
299 # Some of these probably aren't necessary
300 $tables = array('user', 'cur', 'old', 'links',
301 'brokenlinks', 'imagelinks', 'categorylinks',
302 'linkscc', 'site_stats', 'hitcounter',
303 'ipblocks', 'image', 'oldimage',
304 'recentchanges',
305 'watchlist', 'math', 'searchindex',
306 'interwiki', 'querycache',
307 'objectcache', 'blobs', 'validate'
308 );
309
310 # List of tables whose contents we need to copy
311 $copy_tables = array('interwiki');
312
313 if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) {
314 # Database that supports CREATE TABLE ... LIKE
315 foreach ($tables as $tbl) {
316 $db->query("CREATE TEMPORARY TABLE $wgDBprefix$tbl (LIKE $tbl)");
317 }
318 }
319 else {
320 # Hack for MySQL versions < 4.1, which don't support
321 # "CREATE TABLE ... LIKE". Note that
322 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
323 # would not create the indexes we need....
324 foreach ($tables as $tbl) {
325 $res = $db->query("SHOW CREATE TABLE $tbl");
326 $row = $db->fetchRow($res);
327 $create = $row[1];
328 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
329 . $wgDBprefix . '\\1`', $create);
330 if ($create === $create_tmp) {
331 # Couldn't do replacement
332 die("could not create temporary table $tbl");
333 }
334 $db->query($create_tmp);
335 }
336
337 }
338
339 foreach ($copy_tables as $tbl) {
340 $db->query("INSERT INTO $wgDBprefix$tbl SELECT * FROM $tbl");
341 }
342
343 # Hack: insert a few Wikipedia in-project interwiki prefixes,
344 # for testing inter-language links
345 $db->query("INSERT INTO ${wgDBprefix}interwiki
346 (iw_prefix,iw_url,iw_local) VALUES
347 ('zh','http://zh.wikipedia.org/wiki/$1',1),
348 ('es','http://es.wikipedia.org/wiki/$1',1),
349 ('fr','http://fr.wikipedia.org/wiki/$1',1)");
350
351 $setupDB = true;
352 }
353 }
354
355 /**
356 * Restore default values and perform any necessary clean-up
357 * after each test runs.
358 *
359 * @access private
360 */
361 function teardownGlobals() {
362 foreach( $this->savedGlobals as $var => $val ) {
363 $GLOBALS[$var] = $val;
364 }
365 }
366
367 /**
368 * Print a happy success message.
369 *
370 * @param string $desc The test name
371 * @return bool
372 * @access private
373 */
374 function showSuccess( $desc ) {
375 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
376 return true;
377 }
378
379 /**
380 * Print a failure message and provide some explanatory output
381 * about what went wrong if so configured.
382 *
383 * @param string $desc The test name
384 * @param string $result Expected HTML output
385 * @param string $html Actual HTML output
386 * @return bool
387 * @access private
388 */
389 function showFailure( $desc, $result, $html ) {
390 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
391 if( $this->showDiffs ) {
392 print $this->quickDiff( $result, $html );
393 }
394 return false;
395 }
396
397 /**
398 * Run given strings through a diff and return the (colorized) output.
399 * Requires writable /tmp directory and a 'diff' command in the PATH.
400 *
401 * @param string $input
402 * @param string $output
403 * @return string
404 * @access private
405 */
406 function quickDiff( $input, $output ) {
407 $prefix = "/tmp/mwParser-" . mt_rand();
408
409 $infile = "$prefix-expected";
410 $this->dumpToFile( $input, $infile );
411
412 $outfile = "$prefix-actual";
413 $this->dumpToFile( $output, $outfile );
414
415 $diff = `diff -u $infile $outfile`;
416 unlink( $infile );
417 unlink( $outfile );
418
419 return $this->colorDiff( $diff );
420 }
421
422 /**
423 * Write the given string to a file, adding a final newline.
424 *
425 * @param string $data
426 * @param string $filename
427 * @access private
428 */
429 function dumpToFile( $data, $filename ) {
430 $file = fopen( $filename, "wt" );
431 fwrite( $file, $data . "\n" );
432 fclose( $file );
433 }
434
435 /**
436 * Return ANSI terminal escape code for changing text attribs/color,
437 * or empty string if color output is disabled.
438 *
439 * @param string $color Semicolon-separated list of attribute/color codes
440 * @return string
441 * @access private
442 */
443 function termColor( $color ) {
444 return $this->color ? "\x1b[{$color}m" : '';
445 }
446
447 /**
448 * Return ANSI terminal escape code for restoring default text attributes,
449 * or empty string if color output is disabled.
450 *
451 * @return string
452 * @access private
453 */
454 function termReset() {
455 return $this->color ? "\x1b[0m" : '';
456 }
457
458 /**
459 * Colorize unified diff output if set for ANSI color output.
460 * Subtractions are colored blue, additions red.
461 *
462 * @param string $text
463 * @return string
464 * @access private
465 */
466 function colorDiff( $text ) {
467 return preg_replace(
468 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
469 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
470 $this->termColor( 31 ) . '$1' . $this->termReset() ),
471 $text );
472 }
473
474 /**
475 * Insert a temporary test article
476 * @param $name string the title, including any prefix
477 * @param $text string the article text
478 * @param $line int the input line number, for reporting errors
479 * @static
480 * @access private
481 */
482 function addArticle($name, $text, $line) {
483 $this->setupGlobals();
484 $title = Title::newFromText( $name );
485 if ( is_null($title) ) {
486 die( "invalid title at line $line\n" );
487 }
488
489 $aid = $title->getArticleID( GAID_FOR_UPDATE );
490 if ($aid != 0) {
491 die( "duplicate article at line $line\n" );
492 }
493
494 $art = new Article($title);
495 $art->insertNewArticle($text, '', false, false );
496 $this->teardownGlobals();
497 }
498 }
499
500 $wgTitle = Title::newFromText( 'Parser test script' );
501 $tester =& new ParserTest();
502
503 # Note: the command line setup changes the current working directory
504 # to the parent, which is why we have to put the subdir here:
505 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
506
507 exit ($ok ? 0 : -1);
508
509 ?>