2abe8ae1baff35ef46ac21591a1a0ec4bb120f98
[lhc/web/wiklou.git] / maintenance / parserTests.inc
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 $options = array( 'quick', 'color', 'quiet', 'help' );
29 $optionsWithArgs = array( 'regex' );
30
31 require_once( 'commandLine.inc' );
32 require_once( "$IP/includes/ObjectCache.php" );
33 require_once( "$IP/includes/BagOStuff.php" );
34 require_once( "$IP/languages/LanguageUtf8.php" );
35 require_once( "$IP/includes/Hooks.php" );
36 require_once( "$IP/maintenance/parserTestsParserHook.php" );
37
38 /**
39 * @package MediaWiki
40 * @subpackage Maintenance
41 */
42 class ParserTest {
43 /**
44 * boolean $color whereas output should be colorized
45 * @access private
46 */
47 var $color;
48
49 /**
50 * boolean $lightcolor whereas output should use light colors
51 * @access private
52 */
53 var $lightcolor;
54
55 /**
56 * Sets terminal colorization and diff/quick modes depending on OS and
57 * command-line options (--color and --quick).
58 *
59 * @access public
60 */
61 function ParserTest() {
62 global $options;
63
64 # Only colorize output if stdout is a terminal.
65 $this->lightcolor = false;
66 $this->color = !wfIsWindows() && posix_isatty(1);
67
68 if( isset( $options['color'] ) ) {
69 switch( $options['color'] ) {
70 case 'no':
71 $this->color = false;
72 break;
73 case 'light':
74 $this->lightcolor = true;
75 # Fall through
76 case 'yes':
77 default:
78 $this->color = true;
79 break;
80 }
81 }
82
83 $this->showDiffs = !isset( $options['quick'] );
84
85 $this->quiet = isset( $options['quiet'] );
86
87 if (isset($options['regex'])) {
88 $this->regex = $options['regex'];
89 } else {
90 # Matches anything
91 $this->regex = '';
92 }
93 }
94
95 /**
96 * Remove last character if it is a newline
97 * @access private
98 */
99 function chomp($s) {
100 if (substr($s, -1) === "\n") {
101 return substr($s, 0, -1);
102 }
103 else {
104 return $s;
105 }
106 }
107
108 /**
109 * Run a series of tests listed in the given text file.
110 * Each test consists of a brief description, wikitext input,
111 * and the expected HTML output.
112 *
113 * Prints status updates on stdout and counts up the total
114 * number and percentage of passed tests.
115 *
116 * @param string $filename
117 * @return bool True if passed all tests, false if any tests failed.
118 * @access public
119 */
120 function runTestsFromFile( $filename ) {
121 $infile = fopen( $filename, 'rt' );
122 if( !$infile ) {
123 die( "Couldn't open parserTests.txt\n" );
124 }
125
126 $data = array();
127 $section = null;
128 $success = 0;
129 $total = 0;
130 $n = 0;
131 while( false !== ($line = fgets( $infile ) ) ) {
132 $n++;
133 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
134 $section = strtolower( $matches[1] );
135 if( $section == 'endarticle') {
136 if( !isset( $data['text'] ) ) {
137 die( "'endarticle' without 'text' at line $n\n" );
138 }
139 if( !isset( $data['article'] ) ) {
140 die( "'endarticle' without 'article' at line $n\n" );
141 }
142 $this->addArticle($this->chomp($data['article']), $this->chomp($data['text']), $n);
143 $data = array();
144 $section = null;
145 continue;
146 }
147 if( $section == 'end' ) {
148 if( !isset( $data['test'] ) ) {
149 die( "'end' without 'test' at line $n\n" );
150 }
151 if( !isset( $data['input'] ) ) {
152 die( "'end' without 'input' at line $n\n" );
153 }
154 if( !isset( $data['result'] ) ) {
155 die( "'end' without 'result' at line $n\n" );
156 }
157 if( !isset( $data['options'] ) ) {
158 $data['options'] = '';
159 }
160 else {
161 $data['options'] = $this->chomp( $data['options'] );
162 }
163 if (preg_match('/\\bdisabled\\b/i', $data['options'])
164 || !preg_match("/{$this->regex}/i", $data['test'])) {
165 # disabled test
166 $data = array();
167 $section = null;
168 continue;
169 }
170 if( $this->runTest(
171 $this->chomp( $data['test'] ),
172 $this->chomp( $data['input'] ),
173 $this->chomp( $data['result'] ),
174 $this->chomp( $data['options'] ) ) ) {
175 $success++;
176 }
177 $total++;
178 $data = array();
179 $section = null;
180 continue;
181 }
182 if ( isset ($data[$section] ) ) {
183 die ( "duplicate section '$section' at line $n\n" );
184 }
185 $data[$section] = '';
186 continue;
187 }
188 if( $section ) {
189 $data[$section] .= $line;
190 }
191 }
192 if( $total > 0 ) {
193 $ratio = wfPercent( 100 * $success / $total );
194 print $this->termColor( 1 ) . "\nPassed $success of $total tests ($ratio) ";
195 if( $success == $total ) {
196 print $this->termColor( 32 ) . "PASSED!";
197 } else {
198 print $this->termColor( 31 ) . "FAILED!";
199 }
200 print $this->termReset() . "\n";
201 return ($success == $total);
202 } else {
203 die( "No tests found.\n" );
204 }
205 }
206
207 /**
208 * Run a given wikitext input through a freshly-constructed wiki parser,
209 * and compare the output against the expected results.
210 * Prints status and explanatory messages to stdout.
211 *
212 * @param string $input Wikitext to try rendering
213 * @param string $result Result to output
214 * @return bool
215 */
216 function runTest( $desc, $input, $result, $opts ) {
217 if( !$this->quiet ) {
218 $this->showTesting( $desc );
219 }
220
221 $this->setupGlobals($opts);
222
223 $user =& new User();
224 $options = ParserOptions::newFromUser( $user );
225
226 if (preg_match('/\\bmath\\b/i', $opts)) {
227 # XXX this should probably be done by the ParserOptions
228 require_once('Math.php');
229
230 $options->setUseTex(true);
231 }
232
233 if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) {
234 $titleText = $m[1];
235 }
236 else {
237 $titleText = 'Parser test';
238 }
239
240 $noxml = (bool)preg_match( '~\\b noxml \\b~x', $opts );
241
242 $parser =& new Parser();
243 wfRunHooks( 'ParserTestParser', array( &$parser ) );
244 $title =& Title::makeTitle( NS_MAIN, $titleText );
245
246 if (preg_match('/\\bpst\\b/i', $opts)) {
247 $out = $parser->preSaveTransform( $input, $title, $user, $options );
248 } elseif (preg_match('/\\bmsg\\b/i', $opts)) {
249 $out = $parser->transformMsg( $input, $options );
250 } else {
251 $output = $parser->parse( $input, $title, $options );
252 $out = $output->getText();
253
254 if (preg_match('/\\bill\\b/i', $opts)) {
255 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
256 } else if (preg_match('/\\bcat\\b/i', $opts)) {
257 $out = $this->tidy ( implode( ' ', $output->getCategoryLinks() ) );
258 }
259
260 $result = $this->tidy($result);
261 }
262
263 $this->teardownGlobals();
264
265 if( $result === $out && ( $noxml === true || $this->wellFormed( $out ) ) ) {
266 return $this->showSuccess( $desc );
267 } else {
268 return $this->showFailure( $desc, $result, $out );
269 }
270 }
271
272 /**
273 * Set up the global variables for a consistent environment for each test.
274 * Ideally this should replace the global configuration entirely.
275 *
276 * @access private
277 */
278 function setupGlobals($opts = '') {
279 # Save the prefixed / quoted table names for later use when we make the temporaries.
280 $db =& wfGetDB( DB_READ );
281 $this->oldTableNames = array();
282 foreach( $this->listTables() as $table ) {
283 $this->oldTableNames[$table] = $db->tableName( $table );
284 }
285 if( !isset( $this->uploadDir ) ) {
286 $this->uploadDir = $this->setupUploadDir();
287 }
288
289 $settings = array(
290 'wgServer' => 'http://localhost',
291 'wgScript' => '/index.php',
292 'wgScriptPath' => '/',
293 'wgArticlePath' => '/wiki/$1',
294 'wgUploadPath' => 'http://example.com/images',
295 'wgUploadDirectory' => $this->uploadDir,
296 'wgStyleSheetPath' => '/skins',
297 'wgSitename' => 'MediaWiki',
298 'wgLanguageCode' => 'en',
299 'wgContLanguageCode' => 'en',
300 'wgDBprefix' => 'parsertest',
301 'wgDefaultUserOptions' => array(),
302
303 'wgLang' => new LanguageUtf8(),
304 'wgContLang' => new LanguageUtf8(),
305 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
306 'wgMaxTocLevel' => 999,
307 'wgCapitalLinks' => true,
308 'wgDefaultUserOptions' => array(),
309 'wgNoFollowLinks' => true,
310 'wgThumbnailScriptPath' => false,
311 'wgUseTeX' => false,
312 );
313 $this->savedGlobals = array();
314 foreach( $settings as $var => $val ) {
315 $this->savedGlobals[$var] = $GLOBALS[$var];
316 $GLOBALS[$var] = $val;
317 }
318 $GLOBALS['wgLoadBalancer']->loadMasterPos();
319 $GLOBALS['wgMessageCache']->initialise( new BagOStuff(), false, 0, $GLOBALS['wgDBname'] );
320 $this->setupDatabase();
321
322 global $wgUser;
323 $wgUser = new User();
324 }
325
326 # List of temporary tables to create, without prefix
327 # Some of these probably aren't necessary
328 function listTables() {
329 return array('user', 'page', 'revision', 'text',
330 'pagelinks', 'imagelinks', 'categorylinks', 'templatelinks',
331 'site_stats', 'hitcounter',
332 'ipblocks', 'image', 'oldimage',
333 'recentchanges',
334 'watchlist', 'math', 'searchindex',
335 'interwiki', 'querycache',
336 'objectcache'
337 );
338 }
339
340 /**
341 * Set up a temporary set of wiki tables to work with for the tests.
342 * Currently this will only be done once per run, and any changes to
343 * the db will be visible to later tests in the run.
344 *
345 * @access private
346 */
347 function setupDatabase() {
348 static $setupDB = false;
349 global $wgDBprefix;
350
351 # Make sure we don't mess with the live DB
352 if (!$setupDB && $wgDBprefix === 'parsertest') {
353 # oh teh horror
354 $GLOBALS['wgLoadBalancer'] = LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] );
355 $db =& wfGetDB( DB_MASTER );
356
357 $tables = $this->listTables();
358
359 if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) {
360 # Database that supports CREATE TABLE ... LIKE
361 global $wgDBtype;
362 if( $wgDBtype == 'PostgreSQL' ) {
363 $def = 'INCLUDING DEFAULTS';
364 } else {
365 $def = '';
366 }
367 foreach ($tables as $tbl) {
368 $newTableName = $db->tableName( $tbl );
369 $tableName = $this->oldTableNames[$tbl];
370 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName $def)");
371 }
372 } else {
373 # Hack for MySQL versions < 4.1, which don't support
374 # "CREATE TABLE ... LIKE". Note that
375 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
376 # would not create the indexes we need....
377 foreach ($tables as $tbl) {
378 $res = $db->query("SHOW CREATE TABLE {$this->oldTableNames[$tbl]}");
379 $row = $db->fetchRow($res);
380 $create = $row[1];
381 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
382 . $wgDBprefix . $tbl .'`', $create);
383 if ($create === $create_tmp) {
384 # Couldn't do replacement
385 die("could not create temporary table $tbl");
386 }
387 $db->query($create_tmp);
388 }
389
390 }
391
392 # Hack: insert a few Wikipedia in-project interwiki prefixes,
393 # for testing inter-language links
394 $db->insert( 'interwiki', array(
395 array( 'iw_prefix' => 'Wikipedia',
396 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
397 'iw_local' => 0 ),
398 array( 'iw_prefix' => 'MeatBall',
399 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
400 'iw_local' => 0 ),
401 array( 'iw_prefix' => 'zh',
402 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
403 'iw_local' => 1 ),
404 array( 'iw_prefix' => 'es',
405 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
406 'iw_local' => 1 ),
407 array( 'iw_prefix' => 'fr',
408 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
409 'iw_local' => 1 ),
410 array( 'iw_prefix' => 'ru',
411 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
412 'iw_local' => 1 ),
413 ) );
414
415 # Hack: Insert an image to work with
416 $db->insert( 'image', array(
417 'img_name' => 'Foobar.jpg',
418 'img_size' => 12345,
419 'img_description' => 'Some lame file',
420 'img_user' => 1,
421 'img_user_text' => 'WikiSysop',
422 'img_timestamp' => $db->timestamp( '20010115123500' ),
423 'img_width' => 1941,
424 'img_height' => 220,
425 'img_bits' => 24,
426 'img_media_type' => MEDIATYPE_BITMAP,
427 'img_major_mime' => "image",
428 'img_minor_mime' => "jpeg",
429 ) );
430
431 $setupDB = true;
432 }
433 }
434
435 /**
436 * Create a dummy uploads directory which will contain a couple
437 * of files in order to pass existence tests.
438 * @return string The directory
439 * @access private
440 */
441 function setupUploadDir() {
442 global $IP;
443
444 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
445 mkdir( $dir );
446 mkdir( $dir . '/3' );
447 mkdir( $dir . '/3/3a' );
448
449 $img = "$IP/skins/monobook/headbg.jpg";
450 $h = fopen($img, 'r');
451 $c = fread($h, filesize($img));
452 fclose($h);
453
454 $f = fopen( $dir . '/3/3a/Foobar.jpg', 'wb' );
455 fwrite( $f, $c );
456 fclose( $f );
457 return $dir;
458 }
459
460 /**
461 * Restore default values and perform any necessary clean-up
462 * after each test runs.
463 *
464 * @access private
465 */
466 function teardownGlobals() {
467 foreach( $this->savedGlobals as $var => $val ) {
468 $GLOBALS[$var] = $val;
469 }
470 if( isset( $this->uploadDir ) ) {
471 $this->teardownUploadDir( $this->uploadDir );
472 unset( $this->uploadDir );
473 }
474 }
475
476 /**
477 * Remove the dummy uploads directory
478 * @access private
479 */
480 function teardownUploadDir( $dir ) {
481 unlink( "$dir/3/3a/Foobar.jpg" );
482 rmdir( "$dir/3/3a" );
483 rmdir( "$dir/3" );
484
485 @unlink( "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg" );
486 @rmdir( "$dir/thumb/3/3a/Foobar.jpg" );
487 @rmdir( "$dir/thumb/3/3a" );
488 @rmdir( "$dir/thumb/3/39" ); # wtf?
489 @rmdir( "$dir/thumb/3" );
490 @rmdir( "$dir/thumb" );
491 rmdir( "$dir" );
492 }
493
494 /**
495 * "Running test $desc..."
496 * @access private
497 */
498 function showTesting( $desc ) {
499 print "Running test $desc... ";
500 }
501
502 /**
503 * Print a happy success message.
504 *
505 * @param string $desc The test name
506 * @return bool
507 * @access private
508 */
509 function showSuccess( $desc ) {
510 if( !$this->quiet ) {
511 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
512 }
513 return true;
514 }
515
516 /**
517 * Print a failure message and provide some explanatory output
518 * about what went wrong if so configured.
519 *
520 * @param string $desc The test name
521 * @param string $result Expected HTML output
522 * @param string $html Actual HTML output
523 * @return bool
524 * @access private
525 */
526 function showFailure( $desc, $result, $html ) {
527 if( $this->quiet ) {
528 # In quiet mode we didn't show the 'Testing' message before the
529 # test, in case it succeeded. Show it now:
530 $this->showTesting( $desc );
531 }
532 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
533 if( $this->showDiffs ) {
534 print $this->quickDiff( $result, $html );
535 }
536 if( !$this->wellFormed( $html ) ) {
537 print "XML error: $this->mXmlError\n";
538 }
539 return false;
540 }
541
542 /**
543 * Run given strings through a diff and return the (colorized) output.
544 * Requires writable /tmp directory and a 'diff' command in the PATH.
545 *
546 * @param string $input
547 * @param string $output
548 * @param string $inFileTail Tailing for the input file name
549 * @param string $outFileTail Tailing for the output file name
550 * @return string
551 * @access private
552 */
553 function quickDiff( $input, $output, $inFileTail='expected', $outFileTail='actual' ) {
554 $prefix = wfTempDir() . "/mwParser-" . mt_rand();
555
556 $infile = "$prefix-$inFileTail";
557 $this->dumpToFile( $input, $infile );
558
559 $outfile = "$prefix-$outFileTail";
560 $this->dumpToFile( $output, $outfile );
561
562 $diff = `diff -au $infile $outfile`;
563 unlink( $infile );
564 unlink( $outfile );
565
566 return $this->colorDiff( $diff );
567 }
568
569 /**
570 * Write the given string to a file, adding a final newline.
571 *
572 * @param string $data
573 * @param string $filename
574 * @access private
575 */
576 function dumpToFile( $data, $filename ) {
577 $file = fopen( $filename, "wt" );
578 fwrite( $file, $data . "\n" );
579 fclose( $file );
580 }
581
582 /**
583 * Return ANSI terminal escape code for changing text attribs/color,
584 * or empty string if color output is disabled.
585 *
586 * @param string $color Semicolon-separated list of attribute/color codes
587 * @return string
588 * @access private
589 */
590 function termColor( $color ) {
591 if($this->lightcolor) {
592 return $this->color ? "\x1b[1;{$color}m" : '';
593 } else {
594 return $this->color ? "\x1b[{$color}m" : '';
595 }
596 }
597
598 /**
599 * Return ANSI terminal escape code for restoring default text attributes,
600 * or empty string if color output is disabled.
601 *
602 * @return string
603 * @access private
604 */
605 function termReset() {
606 return $this->color ? "\x1b[0m" : '';
607 }
608
609 /**
610 * Colorize unified diff output if set for ANSI color output.
611 * Subtractions are colored blue, additions red.
612 *
613 * @param string $text
614 * @return string
615 * @access private
616 */
617 function colorDiff( $text ) {
618 return preg_replace(
619 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
620 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
621 $this->termColor( 31 ) . '$1' . $this->termReset() ),
622 $text );
623 }
624
625 /**
626 * Insert a temporary test article
627 * @param string $name the title, including any prefix
628 * @param string $text the article text
629 * @param int $line the input line number, for reporting errors
630 * @static
631 * @access private
632 */
633 function addArticle($name, $text, $line) {
634 $this->setupGlobals();
635 $title = Title::newFromText( $name );
636 if ( is_null($title) ) {
637 die( "invalid title at line $line\n" );
638 }
639
640 $aid = $title->getArticleID( GAID_FOR_UPDATE );
641 if ($aid != 0) {
642 die( "duplicate article at line $line\n" );
643 }
644
645 $art = new Article($title);
646 $art->insertNewArticle($text, '', false, false );
647 $this->teardownGlobals();
648 }
649
650 /*
651 * Run the "tidy" command on text if the $wgUseTidy
652 * global is true
653 *
654 * @param string $text the text to tidy
655 * @return string
656 * @static
657 * @access private
658 */
659 function tidy( $text ) {
660 global $wgUseTidy;
661 if ($wgUseTidy) {
662 $text = Parser::tidy($text);
663 }
664 return $text;
665 }
666
667 /**
668 * Hack up a private DOCTYPE with HTML's standard entity declarations.
669 * PHP 4 seemed to know these if you gave it an HTML doctype, but
670 * PHP 5.1 doesn't.
671 * @return string
672 * @access private
673 */
674 function hackDocType() {
675 global $wgHtmlEntities;
676 $out = "<!DOCTYPE html [\n";
677 foreach( $wgHtmlEntities as $entity => $codepoint ) {
678 $out .= "<!ENTITY $entity \"&#$codepoint;\">";
679 }
680 $out .= "]>\n";
681 return $out;
682 }
683
684 function wellFormed( $text ) {
685 $html =
686 $this->hackDocType() .
687 '<html>' .
688 $text .
689 '</html>';
690
691 $parser = xml_parser_create( "UTF-8" );
692
693 # case folding violates XML standard, turn it off
694 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
695
696 if( !xml_parse( $parser, $html, true ) ) {
697 $err = xml_error_string( xml_get_error_code( $parser ) );
698 $position = xml_get_current_byte_index( $parser );
699 $fragment = $this->extractFragment( $html, $position );
700 $this->mXmlError = "$err at byte $position:\n$fragment";
701 xml_parser_free( $parser );
702 return false;
703 }
704 xml_parser_free( $parser );
705 return true;
706 }
707
708 function extractFragment( $text, $position ) {
709 $start = max( 0, $position - 10 );
710 $before = $position - $start;
711 $fragment = '...' .
712 $this->termColor( 34 ) .
713 substr( $text, $start, $before ) .
714 $this->termColor( 0 ) .
715 $this->termColor( 31 ) .
716 $this->termColor( 1 ) .
717 substr( $text, $position, 1 ) .
718 $this->termColor( 0 ) .
719 $this->termColor( 34 ) .
720 substr( $text, $position + 1, 9 ) .
721 $this->termColor( 0 ) .
722 '...';
723 $display = str_replace( "\n", ' ', $fragment );
724 $caret = ' ' .
725 str_repeat( ' ', $before ) .
726 $this->termColor( 31 ) .
727 '^' .
728 $this->termColor( 0 );
729 return "$display\n$caret";
730 }
731
732 }
733
734 ?>