MediaWiki  REL1_23
parserTest.inc
Go to the documentation of this file.
00001 <?php
00034 class ParserTest {
00038     private $color;
00039 
00043     private $showOutput;
00044 
00048     private $useTemporaryTables = true;
00049 
00053     private $databaseSetupDone = false;
00054 
00059     private $db;
00060 
00065     private $dbClone;
00066 
00070     private $oldTablePrefix;
00071 
00072     private $maxFuzzTestLength = 300;
00073     private $fuzzSeed = 0;
00074     private $memoryLimit = 50;
00075     private $uploadDir = null;
00076 
00077     public $regex = "";
00078     private $savedGlobals = array();
00079 
00084     public function __construct( $options = array() ) {
00085         # Only colorize output if stdout is a terminal.
00086         $this->color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
00087 
00088         if ( isset( $options['color'] ) ) {
00089             switch ( $options['color'] ) {
00090                 case 'no':
00091                     $this->color = false;
00092                     break;
00093                 case 'yes':
00094                 default:
00095                     $this->color = true;
00096                     break;
00097             }
00098         }
00099 
00100         $this->term = $this->color
00101             ? new AnsiTermColorer()
00102             : new DummyTermColorer();
00103 
00104         $this->showDiffs = !isset( $options['quick'] );
00105         $this->showProgress = !isset( $options['quiet'] );
00106         $this->showFailure = !(
00107             isset( $options['quiet'] )
00108                 && ( isset( $options['record'] )
00109                 || isset( $options['compare'] ) ) ); // redundant output
00110 
00111         $this->showOutput = isset( $options['show-output'] );
00112 
00113         if ( isset( $options['filter'] ) ) {
00114             $options['regex'] = $options['filter'];
00115         }
00116 
00117         if ( isset( $options['regex'] ) ) {
00118             if ( isset( $options['record'] ) ) {
00119                 echo "Warning: --record cannot be used with --regex, disabling --record\n";
00120                 unset( $options['record'] );
00121             }
00122             $this->regex = $options['regex'];
00123         } else {
00124             # Matches anything
00125             $this->regex = '';
00126         }
00127 
00128         $this->setupRecorder( $options );
00129         $this->keepUploads = isset( $options['keep-uploads'] );
00130 
00131         if ( isset( $options['seed'] ) ) {
00132             $this->fuzzSeed = intval( $options['seed'] ) - 1;
00133         }
00134 
00135         $this->runDisabled = isset( $options['run-disabled'] );
00136         $this->runParsoid = isset( $options['run-parsoid'] );
00137 
00138         $this->hooks = array();
00139         $this->functionHooks = array();
00140         self::setUp();
00141     }
00142 
00143     static function setUp() {
00144         global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc,
00145             $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
00146             $wgExtraNamespaces, $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
00147             $parserMemc, $wgThumbnailScriptPath, $wgScriptPath,
00148             $wgArticlePath, $wgScript, $wgStylePath, $wgExtensionAssetsPath,
00149             $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgLockManagers;
00150 
00151         $wgScript = '/index.php';
00152         $wgScriptPath = '/';
00153         $wgArticlePath = '/wiki/$1';
00154         $wgStylePath = '/skins';
00155         $wgExtensionAssetsPath = '/extensions';
00156         $wgThumbnailScriptPath = false;
00157         $wgLockManagers = array( array(
00158             'name' => 'fsLockManager',
00159             'class' => 'FSLockManager',
00160             'lockDirectory' => wfTempDir() . '/test-repo/lockdir',
00161         ), array(
00162             'name' => 'nullLockManager',
00163             'class' => 'NullLockManager',
00164         ) );
00165         $wgLocalFileRepo = array(
00166             'class' => 'LocalRepo',
00167             'name' => 'local',
00168             'url' => 'http://example.com/images',
00169             'hashLevels' => 2,
00170             'transformVia404' => false,
00171             'backend' => new FSFileBackend( array(
00172                 'name' => 'local-backend',
00173                 'wikiId' => wfWikiId(),
00174                 'containerPaths' => array(
00175                     'local-public' => wfTempDir() . '/test-repo/public',
00176                     'local-thumb' => wfTempDir() . '/test-repo/thumb',
00177                     'local-temp' => wfTempDir() . '/test-repo/temp',
00178                     'local-deleted' => wfTempDir() . '/test-repo/deleted',
00179                 )
00180             ) )
00181         );
00182         $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
00183         $wgNamespaceAliases['Image'] = NS_FILE;
00184         $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
00185         # add a namespace shadowing a interwiki link, to test
00186         # proper precedence when resolving links. (bug 51680)
00187         $wgExtraNamespaces[100] = 'MemoryAlpha';
00188 
00189         // XXX: tests won't run without this (for CACHE_DB)
00190         if ( $wgMainCacheType === CACHE_DB ) {
00191             $wgMainCacheType = CACHE_NONE;
00192         }
00193         if ( $wgMessageCacheType === CACHE_DB ) {
00194             $wgMessageCacheType = CACHE_NONE;
00195         }
00196         if ( $wgParserCacheType === CACHE_DB ) {
00197             $wgParserCacheType = CACHE_NONE;
00198         }
00199 
00200         $wgEnableParserCache = false;
00201         DeferredUpdates::clearPendingUpdates();
00202         $wgMemc = wfGetMainCache(); // checks $wgMainCacheType
00203         $messageMemc = wfGetMessageCacheStorage();
00204         $parserMemc = wfGetParserCacheStorage();
00205 
00206         // $wgContLang = new StubContLang;
00207         $wgUser = new User;
00208         $context = new RequestContext;
00209         $wgLang = $context->getLanguage();
00210         $wgOut = $context->getOutput();
00211         $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
00212         $wgRequest = $context->getRequest();
00213 
00214         if ( $wgStyleDirectory === false ) {
00215             $wgStyleDirectory = "$IP/skins";
00216         }
00217 
00218         self::setupInterwikis();
00219     }
00220 
00230     public static function setupInterwikis() {
00231         # Hack: insert a few Wikipedia in-project interwiki prefixes,
00232         # for testing inter-language links
00233         Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
00234             static $testInterwikis = array(
00235                 'wikipedia' => array(
00236                     'iw_url' => 'http://en.wikipedia.org/wiki/$1',
00237                     'iw_api' => '',
00238                     'iw_wikiid' => '',
00239                     'iw_local' => 0 ),
00240                 'meatball' => array(
00241                     'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
00242                     'iw_api' => '',
00243                     'iw_wikiid' => '',
00244                     'iw_local' => 0 ),
00245                 'memoryalpha' => array(
00246                     'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
00247                     'iw_api' => '',
00248                     'iw_wikiid' => '',
00249                     'iw_local' => 0 ),
00250                 'zh' => array(
00251                     'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
00252                     'iw_api' => '',
00253                     'iw_wikiid' => '',
00254                     'iw_local' => 1 ),
00255                 'es' => array(
00256                     'iw_url' => 'http://es.wikipedia.org/wiki/$1',
00257                     'iw_api' => '',
00258                     'iw_wikiid' => '',
00259                     'iw_local' => 1 ),
00260                 'fr' => array(
00261                     'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
00262                     'iw_api' => '',
00263                     'iw_wikiid' => '',
00264                     'iw_local' => 1 ),
00265                 'ru' => array(
00266                     'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
00267                     'iw_api' => '',
00268                     'iw_wikiid' => '',
00269                     'iw_local' => 1 ),
00270             );
00271             if ( array_key_exists( $prefix, $testInterwikis ) ) {
00272                 $iwData = $testInterwikis[$prefix];
00273             }
00274 
00275             // We only want to rely on the above fixtures
00276             return false;
00277         } );// hooks::register
00278     }
00279 
00283     public static function tearDownInterwikis() {
00284         Hooks::clear( 'InterwikiLoadPrefix' );
00285     }
00286 
00287     public function setupRecorder( $options ) {
00288         if ( isset( $options['record'] ) ) {
00289             $this->recorder = new DbTestRecorder( $this );
00290             $this->recorder->version = isset( $options['setversion'] ) ?
00291                 $options['setversion'] : SpecialVersion::getVersion();
00292         } elseif ( isset( $options['compare'] ) ) {
00293             $this->recorder = new DbTestPreviewer( $this );
00294         } else {
00295             $this->recorder = new TestRecorder( $this );
00296         }
00297     }
00298 
00303     public static function chomp( $s ) {
00304         if ( substr( $s, -1 ) === "\n" ) {
00305             return substr( $s, 0, -1 );
00306         } else {
00307             return $s;
00308         }
00309     }
00310 
00315     function fuzzTest( $filenames ) {
00316         $GLOBALS['wgContLang'] = Language::factory( 'en' );
00317         $dict = $this->getFuzzInput( $filenames );
00318         $dictSize = strlen( $dict );
00319         $logMaxLength = log( $this->maxFuzzTestLength );
00320         $this->setupDatabase();
00321         ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
00322 
00323         $numTotal = 0;
00324         $numSuccess = 0;
00325         $user = new User;
00326         $opts = ParserOptions::newFromUser( $user );
00327         $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
00328 
00329         while ( true ) {
00330             // Generate test input
00331             mt_srand( ++$this->fuzzSeed );
00332             $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
00333             $input = '';
00334 
00335             while ( strlen( $input ) < $totalLength ) {
00336                 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
00337                 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
00338                 $offset = mt_rand( 0, $dictSize - $hairLength );
00339                 $input .= substr( $dict, $offset, $hairLength );
00340             }
00341 
00342             $this->setupGlobals();
00343             $parser = $this->getParser();
00344 
00345             // Run the test
00346             try {
00347                 $parser->parse( $input, $title, $opts );
00348                 $fail = false;
00349             } catch ( Exception $exception ) {
00350                 $fail = true;
00351             }
00352 
00353             if ( $fail ) {
00354                 echo "Test failed with seed {$this->fuzzSeed}\n";
00355                 echo "Input:\n";
00356                 printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input );
00357                 echo "$exception\n";
00358             } else {
00359                 $numSuccess++;
00360             }
00361 
00362             $numTotal++;
00363             $this->teardownGlobals();
00364             $parser->__destruct();
00365 
00366             if ( $numTotal % 100 == 0 ) {
00367                 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
00368                 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
00369                 if ( $usage > 90 ) {
00370                     echo "Out of memory:\n";
00371                     $memStats = $this->getMemoryBreakdown();
00372 
00373                     foreach ( $memStats as $name => $usage ) {
00374                         echo "$name: $usage\n";
00375                     }
00376                     $this->abort();
00377                 }
00378             }
00379         }
00380     }
00381 
00385     function getFuzzInput( $filenames ) {
00386         $dict = '';
00387 
00388         foreach ( $filenames as $filename ) {
00389             $contents = file_get_contents( $filename );
00390             preg_match_all( '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s', $contents, $matches );
00391 
00392             foreach ( $matches[1] as $match ) {
00393                 $dict .= $match . "\n";
00394             }
00395         }
00396 
00397         return $dict;
00398     }
00399 
00403     function getMemoryBreakdown() {
00404         $memStats = array();
00405 
00406         foreach ( $GLOBALS as $name => $value ) {
00407             $memStats['$' . $name] = strlen( serialize( $value ) );
00408         }
00409 
00410         $classes = get_declared_classes();
00411 
00412         foreach ( $classes as $class ) {
00413             $rc = new ReflectionClass( $class );
00414             $props = $rc->getStaticProperties();
00415             $memStats[$class] = strlen( serialize( $props ) );
00416             $methods = $rc->getMethods();
00417 
00418             foreach ( $methods as $method ) {
00419                 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
00420             }
00421         }
00422 
00423         $functions = get_defined_functions();
00424 
00425         foreach ( $functions['user'] as $function ) {
00426             $rf = new ReflectionFunction( $function );
00427             $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
00428         }
00429 
00430         asort( $memStats );
00431 
00432         return $memStats;
00433     }
00434 
00435     function abort() {
00436         $this->abort();
00437     }
00438 
00450     public function runTestsFromFiles( $filenames ) {
00451         $ok = false;
00452 
00453         // be sure, ParserTest::addArticle has correct language set,
00454         // so that system messages gets into the right language cache
00455         $GLOBALS['wgLanguageCode'] = 'en';
00456         $GLOBALS['wgContLang'] = Language::factory( 'en' );
00457 
00458         $this->recorder->start();
00459         try {
00460             $this->setupDatabase();
00461             $ok = true;
00462 
00463             foreach ( $filenames as $filename ) {
00464                 $tests = new TestFileIterator( $filename, $this );
00465                 $ok = $this->runTests( $tests ) && $ok;
00466             }
00467 
00468             $this->teardownDatabase();
00469             $this->recorder->report();
00470         } catch ( DBError $e ) {
00471             echo $e->getMessage();
00472         }
00473         $this->recorder->end();
00474 
00475         return $ok;
00476     }
00477 
00478     function runTests( $tests ) {
00479         $ok = true;
00480 
00481         foreach ( $tests as $t ) {
00482             $result =
00483                 $this->runTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] );
00484             $ok = $ok && $result;
00485             $this->recorder->record( $t['test'], $result );
00486         }
00487 
00488         if ( $this->showProgress ) {
00489             print "\n";
00490         }
00491 
00492         return $ok;
00493     }
00494 
00501     function getParser( $preprocessor = null ) {
00502         global $wgParserConf;
00503 
00504         $class = $wgParserConf['class'];
00505         $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
00506 
00507         foreach ( $this->hooks as $tag => $callback ) {
00508             $parser->setHook( $tag, $callback );
00509         }
00510 
00511         foreach ( $this->functionHooks as $tag => $bits ) {
00512             list( $callback, $flags ) = $bits;
00513             $parser->setFunctionHook( $tag, $callback, $flags );
00514         }
00515 
00516         wfRunHooks( 'ParserTestParser', array( &$parser ) );
00517 
00518         return $parser;
00519     }
00520 
00533     public function runTest( $desc, $input, $result, $opts, $config ) {
00534         if ( $this->showProgress ) {
00535             $this->showTesting( $desc );
00536         }
00537 
00538         $opts = $this->parseOptions( $opts );
00539         $context = $this->setupGlobals( $opts, $config );
00540 
00541         $user = $context->getUser();
00542         $options = ParserOptions::newFromContext( $context );
00543 
00544         if ( isset( $opts['title'] ) ) {
00545             $titleText = $opts['title'];
00546         } else {
00547             $titleText = 'Parser test';
00548         }
00549 
00550         $local = isset( $opts['local'] );
00551         $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
00552         $parser = $this->getParser( $preprocessor );
00553         $title = Title::newFromText( $titleText );
00554 
00555         if ( isset( $opts['pst'] ) ) {
00556             $out = $parser->preSaveTransform( $input, $title, $user, $options );
00557         } elseif ( isset( $opts['msg'] ) ) {
00558             $out = $parser->transformMsg( $input, $options, $title );
00559         } elseif ( isset( $opts['section'] ) ) {
00560             $section = $opts['section'];
00561             $out = $parser->getSection( $input, $section );
00562         } elseif ( isset( $opts['replace'] ) ) {
00563             $section = $opts['replace'][0];
00564             $replace = $opts['replace'][1];
00565             $out = $parser->replaceSection( $input, $section, $replace );
00566         } elseif ( isset( $opts['comment'] ) ) {
00567             $out = Linker::formatComment( $input, $title, $local );
00568         } elseif ( isset( $opts['preload'] ) ) {
00569             $out = $parser->getPreloadText( $input, $title, $options );
00570         } else {
00571             $output = $parser->parse( $input, $title, $options, true, true, 1337 );
00572             $output->setTOCEnabled( !isset( $opts['notoc'] ) );
00573             $out = $output->getText();
00574 
00575             if ( isset( $opts['showtitle'] ) ) {
00576                 if ( $output->getTitleText() ) {
00577                     $title = $output->getTitleText();
00578                 }
00579 
00580                 $out = "$title\n$out";
00581             }
00582 
00583             if ( isset( $opts['ill'] ) ) {
00584                 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
00585             } elseif ( isset( $opts['cat'] ) ) {
00586                 $outputPage = $context->getOutput();
00587                 $outputPage->addCategoryLinks( $output->getCategories() );
00588                 $cats = $outputPage->getCategoryLinks();
00589 
00590                 if ( isset( $cats['normal'] ) ) {
00591                     $out = $this->tidy( implode( ' ', $cats['normal'] ) );
00592                 } else {
00593                     $out = '';
00594                 }
00595             }
00596 
00597             $result = $this->tidy( $result );
00598         }
00599 
00600         $this->teardownGlobals();
00601 
00602         $testResult = new ParserTestResult( $desc );
00603         $testResult->expected = $result;
00604         $testResult->actual = $out;
00605 
00606         return $this->showTestResult( $testResult );
00607     }
00608 
00612     function showTestResult( ParserTestResult $testResult ) {
00613         if ( $testResult->isSuccess() ) {
00614             $this->showSuccess( $testResult );
00615             return true;
00616         } else {
00617             $this->showFailure( $testResult );
00618             return false;
00619         }
00620     }
00621 
00628     private static function getOptionValue( $key, $opts, $default ) {
00629         $key = strtolower( $key );
00630 
00631         if ( isset( $opts[$key] ) ) {
00632             return $opts[$key];
00633         } else {
00634             return $default;
00635         }
00636     }
00637 
00638     private function parseOptions( $instring ) {
00639         $opts = array();
00640         // foo
00641         // foo=bar
00642         // foo="bar baz"
00643         // foo=[[bar baz]]
00644         // foo=bar,"baz quux"
00645         // foo={...json...}
00646         $defs = '(?(DEFINE)
00647             (?<qstr>                    # Quoted string
00648                 "
00649                 (?:[^\\\\"] | \\\\.)*
00650                 "
00651             )
00652             (?<json>
00653                 \{      # Open bracket
00654                 (?:
00655                     [^"{}] |                # Not a quoted string or object, or
00656                     (?&qstr) |              # A quoted string, or
00657                     (?&json)                # A json object (recursively)
00658                 )*
00659                 \}      # Close bracket
00660             )
00661             (?<value>
00662                 (?:
00663                     (?&qstr)            # Quoted val
00664                 |
00665                     \[\[
00666                         [^]]*           # Link target
00667                     \]\]
00668                 |
00669                     [\w-]+              # Plain word
00670                 |
00671                     (?&json)            # JSON object
00672                 )
00673             )
00674         )';
00675         $regex = '/' . $defs . '\b
00676             (?<k>[\w-]+)                # Key
00677             \b
00678             (?:\s*
00679                 =                       # First sub-value
00680                 \s*
00681                 (?<v>
00682                     (?&value)
00683                     (?:\s*
00684                         ,               # Sub-vals 1..N
00685                         \s*
00686                         (?&value)
00687                     )*
00688                 )
00689             )?
00690             /x';
00691         $valueregex = '/' . $defs . '(?&value)/x';
00692 
00693         if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
00694             foreach ( $matches as $bits ) {
00695                 $key = strtolower( $bits[ 'k' ] );
00696                 if ( !isset( $bits[ 'v' ] ) ) {
00697                     $opts[$key] = true;
00698                 } else {
00699                     preg_match_all( $valueregex, $bits[ 'v' ], $vmatches );
00700                     $opts[$key] = array_map( array( $this, 'cleanupOption' ), $vmatches[0] );
00701                     if ( count( $opts[$key] ) == 1 ) {
00702                         $opts[$key] = $opts[$key][0];
00703                     }
00704                 }
00705             }
00706         }
00707         return $opts;
00708     }
00709 
00710     private function cleanupOption( $opt ) {
00711         if ( substr( $opt, 0, 1 ) == '"' ) {
00712             return stripcslashes( substr( $opt, 1, -1 ) );
00713         }
00714 
00715         if ( substr( $opt, 0, 2 ) == '[[' ) {
00716             return substr( $opt, 2, -2 );
00717         }
00718 
00719         if ( substr( $opt, 0, 1 ) == '{' ) {
00720             return FormatJson::decode( $opt, true );
00721         }
00722         return $opt;
00723     }
00724 
00729     private function setupGlobals( $opts = '', $config = '' ) {
00730         # Find out values for some special options.
00731         $lang =
00732             self::getOptionValue( 'language', $opts, 'en' );
00733         $variant =
00734             self::getOptionValue( 'variant', $opts, false );
00735         $maxtoclevel =
00736             self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
00737         $linkHolderBatchSize =
00738             self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
00739 
00740         $settings = array(
00741             'wgServer' => 'http://example.org',
00742             'wgScript' => '/index.php',
00743             'wgScriptPath' => '/',
00744             'wgArticlePath' => '/wiki/$1',
00745             'wgActionPaths' => array(),
00746             'wgLockManagers' => array( array(
00747                 'name' => 'fsLockManager',
00748                 'class' => 'FSLockManager',
00749                 'lockDirectory' => $this->uploadDir . '/lockdir',
00750             ), array(
00751                 'name' => 'nullLockManager',
00752                 'class' => 'NullLockManager',
00753             ) ),
00754             'wgLocalFileRepo' => array(
00755                 'class' => 'LocalRepo',
00756                 'name' => 'local',
00757                 'url' => 'http://example.com/images',
00758                 'hashLevels' => 2,
00759                 'transformVia404' => false,
00760                 'backend' => new FSFileBackend( array(
00761                     'name' => 'local-backend',
00762                     'wikiId' => wfWikiId(),
00763                     'containerPaths' => array(
00764                         'local-public' => $this->uploadDir,
00765                         'local-thumb' => $this->uploadDir . '/thumb',
00766                         'local-temp' => $this->uploadDir . '/temp',
00767                         'local-deleted' => $this->uploadDir . '/delete',
00768                     )
00769                 ) )
00770             ),
00771             'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
00772             'wgStylePath' => '/skins',
00773             'wgSitename' => 'MediaWiki',
00774             'wgLanguageCode' => $lang,
00775             'wgDBprefix' => $this->db->getType() != 'oracle' ? 'parsertest_' : 'pt_',
00776             'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
00777             'wgLang' => null,
00778             'wgContLang' => null,
00779             'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
00780             'wgMaxTocLevel' => $maxtoclevel,
00781             'wgCapitalLinks' => true,
00782             'wgNoFollowLinks' => true,
00783             'wgNoFollowDomainExceptions' => array(),
00784             'wgThumbnailScriptPath' => false,
00785             'wgUseImageResize' => true,
00786             'wgSVGConverter' => 'null',
00787             'wgSVGConverters' => array( 'null' => 'echo "1">$output' ),
00788             'wgLocaltimezone' => 'UTC',
00789             'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
00790             'wgThumbLimits' => array( self::getOptionValue( 'thumbsize', $opts, 180 ) ),
00791             'wgUseTidy' => false,
00792             'wgDefaultLanguageVariant' => $variant,
00793             'wgVariantArticlePath' => false,
00794             'wgGroupPermissions' => array( '*' => array(
00795                 'createaccount' => true,
00796                 'read' => true,
00797                 'edit' => true,
00798                 'createpage' => true,
00799                 'createtalk' => true,
00800             ) ),
00801             'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ),
00802             'wgDefaultExternalStore' => array(),
00803             'wgForeignFileRepos' => array(),
00804             'wgLinkHolderBatchSize' => $linkHolderBatchSize,
00805             'wgExperimentalHtmlIds' => false,
00806             'wgExternalLinkTarget' => false,
00807             'wgAlwaysUseTidy' => false,
00808             'wgHtml5' => true,
00809             'wgWellFormedXml' => true,
00810             'wgAllowMicrodataAttributes' => true,
00811             'wgAdaptiveMessageCache' => true,
00812             'wgDisableLangConversion' => false,
00813             'wgDisableTitleConversion' => false,
00814         );
00815 
00816         if ( $config ) {
00817             $configLines = explode( "\n", $config );
00818 
00819             foreach ( $configLines as $line ) {
00820                 list( $var, $value ) = explode( '=', $line, 2 );
00821 
00822                 $settings[$var] = eval( "return $value;" );
00823             }
00824         }
00825 
00826         $this->savedGlobals = array();
00827 
00829         wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
00830 
00831         foreach ( $settings as $var => $val ) {
00832             if ( array_key_exists( $var, $GLOBALS ) ) {
00833                 $this->savedGlobals[$var] = $GLOBALS[$var];
00834             }
00835 
00836             $GLOBALS[$var] = $val;
00837         }
00838 
00839         $GLOBALS['wgContLang'] = Language::factory( $lang );
00840         $GLOBALS['wgMemc'] = new EmptyBagOStuff;
00841 
00842         $context = new RequestContext();
00843         $GLOBALS['wgLang'] = $context->getLanguage();
00844         $GLOBALS['wgOut'] = $context->getOutput();
00845         $GLOBALS['wgUser'] = $context->getUser();
00846 
00847         // We (re)set $wgThumbLimits to a single-element array above.
00848         $context->getUser()->setOption( 'thumbsize', 0 );
00849 
00850         global $wgHooks;
00851 
00852         $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
00853         $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
00854 
00855         MagicWord::clearCache();
00856 
00857         return $context;
00858     }
00859 
00864     private function listTables() {
00865         $tables = array( 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
00866             'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
00867             'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
00868             'site_stats', 'hitcounter', 'ipblocks', 'image', 'oldimage',
00869             'recentchanges', 'watchlist', 'interwiki', 'logging',
00870             'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
00871             'archive', 'user_groups', 'page_props', 'category', 'msg_resource', 'msg_resource_links'
00872         );
00873 
00874         if ( in_array( $this->db->getType(), array( 'mysql', 'sqlite', 'oracle' ) ) ) {
00875             array_push( $tables, 'searchindex' );
00876         }
00877 
00878         // Allow extensions to add to the list of tables to duplicate;
00879         // may be necessary if they hook into page save or other code
00880         // which will require them while running tests.
00881         wfRunHooks( 'ParserTestTables', array( &$tables ) );
00882 
00883         return $tables;
00884     }
00885 
00891     public function setupDatabase() {
00892         global $wgDBprefix;
00893 
00894         if ( $this->databaseSetupDone ) {
00895             return;
00896         }
00897 
00898         $this->db = wfGetDB( DB_MASTER );
00899         $dbType = $this->db->getType();
00900 
00901         if ( $wgDBprefix === 'parsertest_' || ( $dbType == 'oracle' && $wgDBprefix === 'pt_' ) ) {
00902             throw new MWException( 'setupDatabase should be called before setupGlobals' );
00903         }
00904 
00905         $this->databaseSetupDone = true;
00906         $this->oldTablePrefix = $wgDBprefix;
00907 
00908         # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
00909         # It seems to have been fixed since (r55079?), but regressed at some point before r85701.
00910         # This works around it for now...
00911         ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
00912 
00913         # CREATE TEMPORARY TABLE breaks if there is more than one server
00914         if ( wfGetLB()->getServerCount() != 1 ) {
00915             $this->useTemporaryTables = false;
00916         }
00917 
00918         $temporary = $this->useTemporaryTables || $dbType == 'postgres';
00919         $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
00920 
00921         $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
00922         $this->dbClone->useTemporaryTables( $temporary );
00923         $this->dbClone->cloneTableStructure();
00924 
00925         if ( $dbType == 'oracle' ) {
00926             $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
00927             # Insert 0 user to prevent FK violations
00928 
00929             # Anonymous user
00930             $this->db->insert( 'user', array(
00931                 'user_id' => 0,
00932                 'user_name' => 'Anonymous' ) );
00933         }
00934 
00935         # Update certain things in site_stats
00936         $this->db->insert( 'site_stats',
00937             array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
00938 
00939         # Reinitialise the LocalisationCache to match the database state
00940         Language::getLocalisationCache()->unloadAll();
00941 
00942         # Clear the message cache
00943         MessageCache::singleton()->clear();
00944 
00945         // Remember to update newParserTests.php after changing the below
00946         // (and it uses a slightly different syntax just for teh lulz)
00947         $this->uploadDir = $this->setupUploadDir();
00948         $user = User::createNew( 'WikiSysop' );
00949         $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
00950         # note that the size/width/height/bits/etc of the file
00951         # are actually set by inspecting the file itself; the arguments
00952         # to recordUpload2 have no effect.  That said, we try to make things
00953         # match up so it is less confusing to readers of the code & tests.
00954         $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
00955             'size' => 7881,
00956             'width' => 1941,
00957             'height' => 220,
00958             'bits' => 8,
00959             'media_type' => MEDIATYPE_BITMAP,
00960             'mime' => 'image/jpeg',
00961             'metadata' => serialize( array() ),
00962             'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
00963             'fileExists' => true
00964         ), $this->db->timestamp( '20010115123500' ), $user );
00965 
00966         $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
00967         # again, note that size/width/height below are ignored; see above.
00968         $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', array(
00969             'size' => 22589,
00970             'width' => 135,
00971             'height' => 135,
00972             'bits' => 8,
00973             'media_type' => MEDIATYPE_BITMAP,
00974             'mime' => 'image/png',
00975             'metadata' => serialize( array() ),
00976             'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
00977             'fileExists' => true
00978         ), $this->db->timestamp( '20130225203040' ), $user );
00979 
00980         $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
00981         $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', array(
00982                 'size'        => 12345,
00983                 'width'       => 240,
00984                 'height'      => 180,
00985                 'bits'        => 24,
00986                 'media_type'  => MEDIATYPE_DRAWING,
00987                 'mime'        => 'image/svg+xml',
00988                 'metadata'    => serialize( array() ),
00989                 'sha1'        => wfBaseConvert( '', 16, 36, 31 ),
00990                 'fileExists'  => true
00991         ), $this->db->timestamp( '20010115123500' ), $user );
00992 
00993         # This image will be blacklisted in [[MediaWiki:Bad image list]]
00994         $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
00995         $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array(
00996             'size' => 12345,
00997             'width' => 320,
00998             'height' => 240,
00999             'bits' => 24,
01000             'media_type' => MEDIATYPE_BITMAP,
01001             'mime' => 'image/jpeg',
01002             'metadata' => serialize( array() ),
01003             'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
01004             'fileExists' => true
01005         ), $this->db->timestamp( '20010115123500' ), $user );
01006     }
01007 
01008     public function teardownDatabase() {
01009         if ( !$this->databaseSetupDone ) {
01010             $this->teardownGlobals();
01011             return;
01012         }
01013         $this->teardownUploadDir( $this->uploadDir );
01014 
01015         $this->dbClone->destroy();
01016         $this->databaseSetupDone = false;
01017 
01018         if ( $this->useTemporaryTables ) {
01019             if ( $this->db->getType() == 'sqlite' ) {
01020                 # Under SQLite the searchindex table is virtual and need
01021                 # to be explicitly destroyed. See bug 29912
01022                 # See also MediaWikiTestCase::destroyDB()
01023                 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
01024                 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
01025             }
01026             # Don't need to do anything
01027             $this->teardownGlobals();
01028             return;
01029         }
01030 
01031         $tables = $this->listTables();
01032 
01033         foreach ( $tables as $table ) {
01034             if ( $this->db->getType() == 'oracle' ) {
01035                 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
01036             } else {
01037                 $this->db->query( "DROP TABLE `parsertest_$table`" );
01038             }
01039         }
01040 
01041         if ( $this->db->getType() == 'oracle' ) {
01042             $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
01043         }
01044 
01045         $this->teardownGlobals();
01046     }
01047 
01054     private function setupUploadDir() {
01055         global $IP;
01056 
01057         if ( $this->keepUploads ) {
01058             $dir = wfTempDir() . '/mwParser-images';
01059 
01060             if ( is_dir( $dir ) ) {
01061                 return $dir;
01062             }
01063         } else {
01064             $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
01065         }
01066 
01067         // wfDebug( "Creating upload directory $dir\n" );
01068         if ( file_exists( $dir ) ) {
01069             wfDebug( "Already exists!\n" );
01070             return $dir;
01071         }
01072 
01073         wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
01074         copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
01075         wfMkdirParents( $dir . '/e/ea', null, __METHOD__ );
01076         copy( "$IP/skins/monobook/wiki.png", "$dir/e/ea/Thumb.png" );
01077         wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
01078         copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
01079         wfMkdirParents( $dir . '/f/ff', null, __METHOD__ );
01080         file_put_contents( "$dir/f/ff/Foobar.svg",
01081             '<?xml version="1.0" encoding="utf-8"?>' .
01082             '<svg xmlns="http://www.w3.org/2000/svg"' .
01083             ' version="1.1" width="240" height="180"/>' );
01084         return $dir;
01085     }
01086 
01091     private function teardownGlobals() {
01092         RepoGroup::destroySingleton();
01093         FileBackendGroup::destroySingleton();
01094         LockManagerGroup::destroySingletons();
01095         LinkCache::singleton()->clear();
01096 
01097         foreach ( $this->savedGlobals as $var => $val ) {
01098             $GLOBALS[$var] = $val;
01099         }
01100     }
01101 
01105     private function teardownUploadDir( $dir ) {
01106         if ( $this->keepUploads ) {
01107             return;
01108         }
01109 
01110         // delete the files first, then the dirs.
01111         self::deleteFiles(
01112             array(
01113                 "$dir/3/3a/Foobar.jpg",
01114                 "$dir/thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
01115                 "$dir/thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
01116                 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
01117                 "$dir/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
01118                 "$dir/thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
01119                 "$dir/thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
01120                 "$dir/thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
01121                 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
01122                 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
01123                 "$dir/thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
01124                 "$dir/thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
01125                 "$dir/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
01126                 "$dir/thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
01127                 "$dir/thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
01128                 "$dir/thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
01129                 "$dir/thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
01130                 "$dir/thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
01131                 "$dir/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
01132                 "$dir/thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
01133                 "$dir/thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
01134                 "$dir/thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
01135                 "$dir/thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
01136                 "$dir/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
01137                 "$dir/thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
01138                 "$dir/thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
01139                 "$dir/thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
01140                 "$dir/thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
01141                 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
01142                 "$dir/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
01143                 "$dir/thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
01144                 "$dir/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
01145 
01146                 "$dir/e/ea/Thumb.png",
01147 
01148                 "$dir/0/09/Bad.jpg",
01149 
01150                 "$dir/f/ff/Foobar.svg",
01151                 "$dir/thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
01152                 "$dir/thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
01153                 "$dir/thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
01154                 "$dir/thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
01155                 "$dir/thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
01156                 "$dir/thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
01157                 "$dir/thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
01158                 "$dir/thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
01159                 "$dir/thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
01160 
01161                 "$dir/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
01162             )
01163         );
01164 
01165         self::deleteDirs(
01166             array(
01167                 "$dir/3/3a",
01168                 "$dir/3",
01169                 "$dir/thumb/3/3a/Foobar.jpg",
01170                 "$dir/thumb/3/3a",
01171                 "$dir/thumb/3",
01172                 "$dir/e/ea",
01173                 "$dir/e",
01174                 "$dir/f/ff/",
01175                 "$dir/f/",
01176                 "$dir/thumb/f/ff/Foobar.svg",
01177                 "$dir/thumb/f/ff/",
01178                 "$dir/thumb/f/",
01179                 "$dir/0/09/",
01180                 "$dir/0/",
01181                 "$dir/thumb",
01182                 "$dir/math/f/a/5",
01183                 "$dir/math/f/a",
01184                 "$dir/math/f",
01185                 "$dir/math",
01186                 "$dir",
01187             )
01188         );
01189     }
01190 
01195     private static function deleteFiles( $files ) {
01196         foreach ( $files as $file ) {
01197             if ( file_exists( $file ) ) {
01198                 unlink( $file );
01199             }
01200         }
01201     }
01202 
01207     private static function deleteDirs( $dirs ) {
01208         foreach ( $dirs as $dir ) {
01209             if ( is_dir( $dir ) ) {
01210                 rmdir( $dir );
01211             }
01212         }
01213     }
01214 
01218     protected function showTesting( $desc ) {
01219         print "Running test $desc... ";
01220     }
01221 
01230     protected function showSuccess( ParserTestResult $testResult ) {
01231         if ( $this->showProgress ) {
01232             print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
01233         }
01234 
01235         return true;
01236     }
01237 
01247     protected function showFailure( ParserTestResult $testResult ) {
01248         if ( $this->showFailure ) {
01249             if ( !$this->showProgress ) {
01250                 # In quiet mode we didn't show the 'Testing' message before the
01251                 # test, in case it succeeded. Show it now:
01252                 $this->showTesting( $testResult->description );
01253             }
01254 
01255             print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
01256 
01257             if ( $this->showOutput ) {
01258                 print "--- Expected ---\n{$testResult->expected}\n";
01259                 print "--- Actual ---\n{$testResult->actual}\n";
01260             }
01261 
01262             if ( $this->showDiffs ) {
01263                 print $this->quickDiff( $testResult->expected, $testResult->actual );
01264                 if ( !$this->wellFormed( $testResult->actual ) ) {
01265                     print "XML error: $this->mXmlError\n";
01266                 }
01267             }
01268         }
01269 
01270         return false;
01271     }
01272 
01283     protected function quickDiff( $input, $output,
01284         $inFileTail = 'expected', $outFileTail = 'actual'
01285     ) {
01286         # Windows, or at least the fc utility, is retarded
01287         $slash = wfIsWindows() ? '\\' : '/';
01288         $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
01289 
01290         $infile = "$prefix-$inFileTail";
01291         $this->dumpToFile( $input, $infile );
01292 
01293         $outfile = "$prefix-$outFileTail";
01294         $this->dumpToFile( $output, $outfile );
01295 
01296         $shellInfile = wfEscapeShellArg( $infile );
01297         $shellOutfile = wfEscapeShellArg( $outfile );
01298 
01299         global $wgDiff3;
01300         // we assume that people with diff3 also have usual diff
01301         $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
01302 
01303         $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" );
01304 
01305         unlink( $infile );
01306         unlink( $outfile );
01307 
01308         return $this->colorDiff( $diff );
01309     }
01310 
01317     private function dumpToFile( $data, $filename ) {
01318         $file = fopen( $filename, "wt" );
01319         fwrite( $file, $data . "\n" );
01320         fclose( $file );
01321     }
01322 
01330     protected function colorDiff( $text ) {
01331         return preg_replace(
01332             array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
01333             array( $this->term->color( 34 ) . '$1' . $this->term->reset(),
01334                 $this->term->color( 31 ) . '$1' . $this->term->reset() ),
01335             $text );
01336     }
01337 
01343     public function showRunFile( $path ) {
01344         print $this->term->color( 1 ) .
01345             "Reading tests from \"$path\"..." .
01346             $this->term->reset() .
01347             "\n";
01348     }
01349 
01357     public static function addArticle( $name, $text, $line = 'unknown', $ignoreDuplicate = '' ) {
01358         global $wgCapitalLinks;
01359 
01360         $oldCapitalLinks = $wgCapitalLinks;
01361         $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
01362 
01363         $text = self::chomp( $text );
01364         $name = self::chomp( $name );
01365 
01366         $title = Title::newFromText( $name );
01367 
01368         if ( is_null( $title ) ) {
01369             throw new MWException( "invalid title '$name' at line $line\n" );
01370         }
01371 
01372         $page = WikiPage::factory( $title );
01373         $page->loadPageData( 'fromdbmaster' );
01374 
01375         if ( $page->exists() ) {
01376             if ( $ignoreDuplicate == 'ignoreduplicate' ) {
01377                 return;
01378             } else {
01379                 throw new MWException( "duplicate article '$name' at line $line\n" );
01380             }
01381         }
01382 
01383         $page->doEditContent( ContentHandler::makeContent( $text, $title ), '', EDIT_NEW );
01384 
01385         $wgCapitalLinks = $oldCapitalLinks;
01386     }
01387 
01396     public function requireHook( $name ) {
01397         global $wgParser;
01398 
01399         $wgParser->firstCallInit(); // make sure hooks are loaded.
01400 
01401         if ( isset( $wgParser->mTagHooks[$name] ) ) {
01402             $this->hooks[$name] = $wgParser->mTagHooks[$name];
01403         } else {
01404             echo "   This test suite requires the '$name' hook extension, skipping.\n";
01405             return false;
01406         }
01407 
01408         return true;
01409     }
01410 
01419     public function requireFunctionHook( $name ) {
01420         global $wgParser;
01421 
01422         $wgParser->firstCallInit(); // make sure hooks are loaded.
01423 
01424         if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
01425             $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
01426         } else {
01427             echo "   This test suite requires the '$name' function hook extension, skipping.\n";
01428             return false;
01429         }
01430 
01431         return true;
01432     }
01433 
01441     private function tidy( $text ) {
01442         global $wgUseTidy;
01443 
01444         if ( $wgUseTidy ) {
01445             $text = MWTidy::tidy( $text );
01446         }
01447 
01448         return $text;
01449     }
01450 
01451     private function wellFormed( $text ) {
01452         $html =
01453             Sanitizer::hackDocType() .
01454                 '<html>' .
01455                 $text .
01456                 '</html>';
01457 
01458         $parser = xml_parser_create( "UTF-8" );
01459 
01460         # case folding violates XML standard, turn it off
01461         xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
01462 
01463         if ( !xml_parse( $parser, $html, true ) ) {
01464             $err = xml_error_string( xml_get_error_code( $parser ) );
01465             $position = xml_get_current_byte_index( $parser );
01466             $fragment = $this->extractFragment( $html, $position );
01467             $this->mXmlError = "$err at byte $position:\n$fragment";
01468             xml_parser_free( $parser );
01469 
01470             return false;
01471         }
01472 
01473         xml_parser_free( $parser );
01474 
01475         return true;
01476     }
01477 
01478     private function extractFragment( $text, $position ) {
01479         $start = max( 0, $position - 10 );
01480         $before = $position - $start;
01481         $fragment = '...' .
01482             $this->term->color( 34 ) .
01483             substr( $text, $start, $before ) .
01484             $this->term->color( 0 ) .
01485             $this->term->color( 31 ) .
01486             $this->term->color( 1 ) .
01487             substr( $text, $position, 1 ) .
01488             $this->term->color( 0 ) .
01489             $this->term->color( 34 ) .
01490             substr( $text, $position + 1, 9 ) .
01491             $this->term->color( 0 ) .
01492             '...';
01493         $display = str_replace( "\n", ' ', $fragment );
01494         $caret = '   ' .
01495             str_repeat( ' ', $before ) .
01496             $this->term->color( 31 ) .
01497             '^' .
01498             $this->term->color( 0 );
01499 
01500         return "$display\n$caret";
01501     }
01502 
01503     static function getFakeTimestamp( &$parser, &$ts ) {
01504         $ts = 123; //parsed as '1970-01-01T00:02:03Z'
01505         return true;
01506     }
01507 }