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