Tweak tables used in parserTests for the 2 new redirect tables
[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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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', 'show-output' );
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/includes/Hooks.php" );
35 require_once( "$IP/maintenance/parserTestsParserHook.php" );
36 require_once( "$IP/maintenance/parserTestsStaticParserHook.php" );
37 require_once( "$IP/maintenance/parserTestsParserTime.php" );
38
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;
49
50 /**
51 * boolean $lightcolor whereas output should use light colors
52 * @private
53 */
54 var $lightcolor;
55
56 /**
57 * boolean $showOutput Show test output
58 */
59 var $showOutput;
60
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;
69
70 # Only colorize output if stdout is a terminal.
71 $this->lightcolor = false;
72 $this->color = !wfIsWindows() && posix_isatty(1);
73
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 }
88
89 $this->showDiffs = !isset( $options['quick'] );
90 $this->quiet = isset( $options['quiet'] );
91 $this->showOutput = isset( $options['show-output'] );
92
93
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 }
103
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 }
116
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 }
134
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 }
229
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 }
243
244 $this->setupGlobals($opts);
245
246 $user = new User();
247 $options = ParserOptions::newFromUser( $user );
248
249 if (preg_match('/\\bmath\\b/i', $opts)) {
250 # XXX this should probably be done by the ParserOptions
251 $options->setUseTex(true);
252 }
253
254 if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) {
255 $titleText = $m[1];
256 }
257 else {
258 $titleText = 'Parser test';
259 }
260
261 $noxml = (bool)preg_match( '~\\b noxml \\b~x', $opts );
262
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 );
270
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();
285
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 }
293
294 $result = $this->tidy($result);
295 }
296
297 $this->teardownGlobals();
298
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 }
305
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 }
328
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_',
343
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;
364
365 $GLOBALS['wgLoadBalancer']->loadMasterPos();
366 //$GLOBALS['wgMessageCache'] = new MessageCache( new BagOStuff(), false, 0, $GLOBALS['wgDBname'] );
367 $this->setupDatabase();
368
369 global $wgUser;
370 $wgUser = new User();
371 }
372
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', 'redirect',
385 'querycachetwo'
386 );
387
388 // FIXME manually adding additional table for the tasks extension
389 // we probably need a better software wide system to register new
390 // tables.
391 global $wgExtensionFunctions;
392 if( in_array('wfTasksExtension' , $wgExtensionFunctions ) ) {
393 $tables[] = 'tasks';
394 }
395
396 return $tables;
397 }
398
399 /**
400 * Set up a temporary set of wiki tables to work with for the tests.
401 * Currently this will only be done once per run, and any changes to
402 * the db will be visible to later tests in the run.
403 *
404 * @private
405 */
406 function setupDatabase() {
407 static $setupDB = false;
408 global $wgDBprefix;
409
410 # Make sure we don't mess with the live DB
411 if (!$setupDB && $wgDBprefix === 'parsertest_') {
412 # oh teh horror
413 $GLOBALS['wgLoadBalancer'] = LoadBalancer::newFromParams( $GLOBALS['wgDBservers'] );
414 $db =& wfGetDB( DB_MASTER );
415
416 $tables = $this->listTables();
417
418 if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) {
419 # Database that supports CREATE TABLE ... LIKE
420 global $wgDBtype;
421 if( $wgDBtype == 'postgres' ) {
422 $def = 'INCLUDING DEFAULTS';
423 } else {
424 $def = '';
425 }
426 foreach ($tables as $tbl) {
427 $newTableName = $db->tableName( $tbl );
428 $tableName = $this->oldTableNames[$tbl];
429 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName $def)");
430 }
431 } else {
432 # Hack for MySQL versions < 4.1, which don't support
433 # "CREATE TABLE ... LIKE". Note that
434 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
435 # would not create the indexes we need....
436 foreach ($tables as $tbl) {
437 $res = $db->query("SHOW CREATE TABLE {$this->oldTableNames[$tbl]}");
438 $row = $db->fetchRow($res);
439 $create = $row[1];
440 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
441 . $wgDBprefix . $tbl .'`', $create);
442 if ($create === $create_tmp) {
443 # Couldn't do replacement
444 wfDie("could not create temporary table $tbl");
445 }
446 $db->query($create_tmp);
447 }
448
449 }
450
451 # Hack: insert a few Wikipedia in-project interwiki prefixes,
452 # for testing inter-language links
453 $db->insert( 'interwiki', array(
454 array( 'iw_prefix' => 'Wikipedia',
455 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
456 'iw_local' => 0 ),
457 array( 'iw_prefix' => 'MeatBall',
458 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
459 'iw_local' => 0 ),
460 array( 'iw_prefix' => 'zh',
461 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
462 'iw_local' => 1 ),
463 array( 'iw_prefix' => 'es',
464 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
465 'iw_local' => 1 ),
466 array( 'iw_prefix' => 'fr',
467 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
468 'iw_local' => 1 ),
469 array( 'iw_prefix' => 'ru',
470 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
471 'iw_local' => 1 ),
472 ) );
473
474 # Hack: Insert an image to work with
475 $db->insert( 'image', array(
476 'img_name' => 'Foobar.jpg',
477 'img_size' => 12345,
478 'img_description' => 'Some lame file',
479 'img_user' => 1,
480 'img_user_text' => 'WikiSysop',
481 'img_timestamp' => $db->timestamp( '20010115123500' ),
482 'img_width' => 1941,
483 'img_height' => 220,
484 'img_bits' => 24,
485 'img_media_type' => MEDIATYPE_BITMAP,
486 'img_major_mime' => "image",
487 'img_minor_mime' => "jpeg",
488 ) );
489
490 # Update certain things in site_stats
491 $db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 1, 'ss_good_articles' => 1 ) );
492
493 $setupDB = true;
494 }
495 }
496
497 /**
498 * Create a dummy uploads directory which will contain a couple
499 * of files in order to pass existence tests.
500 * @return string The directory
501 * @private
502 */
503 function setupUploadDir() {
504 global $IP;
505
506 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
507 mkdir( $dir );
508 mkdir( $dir . '/3' );
509 mkdir( $dir . '/3/3a' );
510
511 $img = "$IP/skins/monobook/headbg.jpg";
512 $h = fopen($img, 'r');
513 $c = fread($h, filesize($img));
514 fclose($h);
515
516 $f = fopen( $dir . '/3/3a/Foobar.jpg', 'wb' );
517 fwrite( $f, $c );
518 fclose( $f );
519 return $dir;
520 }
521
522 /**
523 * Restore default values and perform any necessary clean-up
524 * after each test runs.
525 *
526 * @private
527 */
528 function teardownGlobals() {
529 foreach( $this->savedGlobals as $var => $val ) {
530 $GLOBALS[$var] = $val;
531 }
532 if( isset( $this->uploadDir ) ) {
533 $this->teardownUploadDir( $this->uploadDir );
534 unset( $this->uploadDir );
535 }
536 }
537
538 /**
539 * Remove the dummy uploads directory
540 * @private
541 */
542 function teardownUploadDir( $dir ) {
543 unlink( "$dir/3/3a/Foobar.jpg" );
544 rmdir( "$dir/3/3a" );
545 rmdir( "$dir/3" );
546 @rmdir( "$dir/thumb/6/65" );
547 @rmdir( "$dir/thumb/6" );
548
549 @unlink( "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg" );
550 @rmdir( "$dir/thumb/3/3a/Foobar.jpg" );
551 @rmdir( "$dir/thumb/3/3a" );
552 @rmdir( "$dir/thumb/3/39" ); # wtf?
553 @rmdir( "$dir/thumb/3" );
554 @rmdir( "$dir/thumb" );
555 @rmdir( "$dir" );
556 }
557
558 /**
559 * "Running test $desc..."
560 * @private
561 */
562 function showTesting( $desc ) {
563 print "Running test $desc... ";
564 }
565
566 /**
567 * Print a happy success message.
568 *
569 * @param string $desc The test name
570 * @return bool
571 * @private
572 */
573 function showSuccess( $desc ) {
574 if( !$this->quiet ) {
575 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
576 }
577 return true;
578 }
579
580 /**
581 * Print a failure message and provide some explanatory output
582 * about what went wrong if so configured.
583 *
584 * @param string $desc The test name
585 * @param string $result Expected HTML output
586 * @param string $html Actual HTML output
587 * @return bool
588 * @private
589 */
590 function showFailure( $desc, $result, $html ) {
591 if( $this->quiet ) {
592 # In quiet mode we didn't show the 'Testing' message before the
593 # test, in case it succeeded. Show it now:
594 $this->showTesting( $desc );
595 }
596 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
597 if ( $this->showOutput ) {
598 print "--- Expected ---\n$result\n--- Actual ---\n$html\n";
599 }
600 if( $this->showDiffs ) {
601 print $this->quickDiff( $result, $html );
602 if( !$this->wellFormed( $html ) ) {
603 print "XML error: $this->mXmlError\n";
604 }
605 }
606 return false;
607 }
608
609 /**
610 * Run given strings through a diff and return the (colorized) output.
611 * Requires writable /tmp directory and a 'diff' command in the PATH.
612 *
613 * @param string $input
614 * @param string $output
615 * @param string $inFileTail Tailing for the input file name
616 * @param string $outFileTail Tailing for the output file name
617 * @return string
618 * @private
619 */
620 function quickDiff( $input, $output, $inFileTail='expected', $outFileTail='actual' ) {
621 $prefix = wfTempDir() . "/mwParser-" . mt_rand();
622
623 $infile = "$prefix-$inFileTail";
624 $this->dumpToFile( $input, $infile );
625
626 $outfile = "$prefix-$outFileTail";
627 $this->dumpToFile( $output, $outfile );
628
629 $diff = `diff -au $infile $outfile`;
630 unlink( $infile );
631 unlink( $outfile );
632
633 return $this->colorDiff( $diff );
634 }
635
636 /**
637 * Write the given string to a file, adding a final newline.
638 *
639 * @param string $data
640 * @param string $filename
641 * @private
642 */
643 function dumpToFile( $data, $filename ) {
644 $file = fopen( $filename, "wt" );
645 fwrite( $file, $data . "\n" );
646 fclose( $file );
647 }
648
649 /**
650 * Return ANSI terminal escape code for changing text attribs/color,
651 * or empty string if color output is disabled.
652 *
653 * @param string $color Semicolon-separated list of attribute/color codes
654 * @return string
655 * @private
656 */
657 function termColor( $color ) {
658 if($this->lightcolor) {
659 return $this->color ? "\x1b[1;{$color}m" : '';
660 } else {
661 return $this->color ? "\x1b[{$color}m" : '';
662 }
663 }
664
665 /**
666 * Return ANSI terminal escape code for restoring default text attributes,
667 * or empty string if color output is disabled.
668 *
669 * @return string
670 * @private
671 */
672 function termReset() {
673 return $this->color ? "\x1b[0m" : '';
674 }
675
676 /**
677 * Colorize unified diff output if set for ANSI color output.
678 * Subtractions are colored blue, additions red.
679 *
680 * @param string $text
681 * @return string
682 * @private
683 */
684 function colorDiff( $text ) {
685 return preg_replace(
686 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
687 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
688 $this->termColor( 31 ) . '$1' . $this->termReset() ),
689 $text );
690 }
691
692 /**
693 * Insert a temporary test article
694 * @param string $name the title, including any prefix
695 * @param string $text the article text
696 * @param int $line the input line number, for reporting errors
697 * @private
698 */
699 function addArticle($name, $text, $line) {
700 $this->setupGlobals();
701 $title = Title::newFromText( $name );
702 if ( is_null($title) ) {
703 wfDie( "invalid title at line $line\n" );
704 }
705
706 $aid = $title->getArticleID( GAID_FOR_UPDATE );
707 if ($aid != 0) {
708 wfDie( "duplicate article at line $line\n" );
709 }
710
711 $art = new Article($title);
712 $art->insertNewArticle($text, '', false, false );
713 $this->teardownGlobals();
714 }
715
716 /**
717 * Steal a callback function from the primary parser, save it for
718 * application to our scary parser. If the hook is not installed,
719 * die a painful dead to warn the others.
720 * @param string $name
721 */
722 private function requireHook( $name ) {
723 global $wgParser;
724 if( isset( $wgParser->mTagHooks[$name] ) ) {
725 $this->hooks[$name] = $wgParser->mTagHooks[$name];
726 } else {
727 wfDie( "This test suite requires the '$name' hook extension.\n" );
728 }
729 }
730
731 /*
732 * Run the "tidy" command on text if the $wgUseTidy
733 * global is true
734 *
735 * @param string $text the text to tidy
736 * @return string
737 * @static
738 * @private
739 */
740 function tidy( $text ) {
741 global $wgUseTidy;
742 if ($wgUseTidy) {
743 $text = Parser::tidy($text);
744 }
745 return $text;
746 }
747
748 function wellFormed( $text ) {
749 $html =
750 Sanitizer::hackDocType() .
751 '<html>' .
752 $text .
753 '</html>';
754
755 $parser = xml_parser_create( "UTF-8" );
756
757 # case folding violates XML standard, turn it off
758 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
759
760 if( !xml_parse( $parser, $html, true ) ) {
761 $err = xml_error_string( xml_get_error_code( $parser ) );
762 $position = xml_get_current_byte_index( $parser );
763 $fragment = $this->extractFragment( $html, $position );
764 $this->mXmlError = "$err at byte $position:\n$fragment";
765 xml_parser_free( $parser );
766 return false;
767 }
768 xml_parser_free( $parser );
769 return true;
770 }
771
772 function extractFragment( $text, $position ) {
773 $start = max( 0, $position - 10 );
774 $before = $position - $start;
775 $fragment = '...' .
776 $this->termColor( 34 ) .
777 substr( $text, $start, $before ) .
778 $this->termColor( 0 ) .
779 $this->termColor( 31 ) .
780 $this->termColor( 1 ) .
781 substr( $text, $position, 1 ) .
782 $this->termColor( 0 ) .
783 $this->termColor( 34 ) .
784 substr( $text, $position + 1, 9 ) .
785 $this->termColor( 0 ) .
786 '...';
787 $display = str_replace( "\n", ' ', $fragment );
788 $caret = ' ' .
789 str_repeat( ' ', $before ) .
790 $this->termColor( 31 ) .
791 '^' .
792 $this->termColor( 0 );
793 return "$display\n$caret";
794 }
795
796 }
797
798 ?>