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