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