3 * Helper code for the MediaWiki parser test suite. Some code is duplicated
4 * in PHPUnit's NewParserTests.php, so you'll probably want to update both
7 * Copyright © 2004, 2010 Brion Vibber <brion@pobox.com>
8 * https://www.mediawiki.org/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @todo Make this more independent of the configuration (and if possible the database)
30 use MediaWiki\MediaWikiServices;
37 * @var bool $color whereas output should be colorized
42 * @var bool $showOutput Show test output
47 * @var bool $useTemporaryTables Use temporary tables for the temporary database
49 private $useTemporaryTables = true;
52 * @var bool $databaseSetupDone True if the database has been set up
54 private $databaseSetupDone = false;
57 * Our connection to the database
63 * Database clone helper
78 private $maxFuzzTestLength = 300;
79 private $fuzzSeed = 0;
80 private $memoryLimit = 50;
81 private $uploadDir = null;
84 private $savedGlobals = [];
85 private $useDwdiff = false;
86 private $markWhitespace = false;
87 private $normalizationFunctions = [];
90 * Sets terminal colorization and diff/quick modes depending on OS and
91 * command-line options (--color and --quick).
92 * @param array $options
94 public function __construct( $options = [] ) {
95 # Only colorize output if stdout is a terminal.
96 $this->color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
98 if ( isset( $options['color'] ) ) {
99 switch ( $options['color'] ) {
101 $this->color = false;
110 $this->term = $this->color
111 ? new AnsiTermColorer()
112 : new DummyTermColorer();
114 $this->showDiffs = !isset( $options['quick'] );
115 $this->showProgress = !isset( $options['quiet'] );
116 $this->showFailure = !(
117 isset( $options['quiet'] )
118 && ( isset( $options['record'] )
119 || isset( $options['compare'] ) ) ); // redundant output
121 $this->showOutput = isset( $options['show-output'] );
122 $this->useDwdiff = isset( $options['dwdiff'] );
123 $this->markWhitespace = isset( $options['mark-ws'] );
125 if ( isset( $options['norm'] ) ) {
126 foreach ( explode( ',', $options['norm'] ) as $func ) {
127 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
128 $this->normalizationFunctions[] = $func;
130 echo "Warning: unknown normalization option \"$func\"\n";
135 if ( isset( $options['filter'] ) ) {
136 $options['regex'] = $options['filter'];
139 if ( isset( $options['regex'] ) ) {
140 if ( isset( $options['record'] ) ) {
141 echo "Warning: --record cannot be used with --regex, disabling --record\n";
142 unset( $options['record'] );
144 $this->regex = $options['regex'];
150 $this->setupRecorder( $options );
151 $this->keepUploads = isset( $options['keep-uploads'] );
153 if ( $this->keepUploads ) {
154 $this->uploadDir = wfTempDir() . '/mwParser-images';
156 $this->uploadDir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
159 if ( isset( $options['seed'] ) ) {
160 $this->fuzzSeed = intval( $options['seed'] ) - 1;
163 $this->runDisabled = isset( $options['run-disabled'] );
164 $this->runParsoid = isset( $options['run-parsoid'] );
166 $this->djVuSupport = new DjVuSupport();
167 $this->tidySupport = new TidySupport();
168 if ( !$this->tidySupport->isEnabled() ) {
169 echo "Warning: tidy is not installed, skipping some tests\n";
172 if ( !extension_loaded( 'gd' ) ) {
173 echo "Warning: GD extension is not present, thumbnailing tests will probably fail\n";
177 $this->functionHooks = [];
178 $this->transparentHooks = [];
183 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc,
184 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
185 $wgExtraNamespaces, $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
186 $wgExtraInterlanguageLinkPrefixes, $wgLocalInterwikis,
187 $parserMemc, $wgThumbnailScriptPath, $wgScriptPath, $wgResourceBasePath,
188 $wgArticlePath, $wgScript, $wgStylePath, $wgExtensionAssetsPath,
189 $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgLockManagers;
192 $wgScript = '/index.php';
193 $wgStylePath = '/skins';
194 $wgResourceBasePath = '';
195 $wgExtensionAssetsPath = '/extensions';
196 $wgArticlePath = '/wiki/$1';
197 $wgThumbnailScriptPath = false;
198 $wgLockManagers = [ [
199 'name' => 'fsLockManager',
200 'class' => 'FSLockManager',
201 'lockDirectory' => $this->uploadDir . '/lockdir',
203 'name' => 'nullLockManager',
204 'class' => 'NullLockManager',
207 'class' => 'LocalRepo',
209 'url' => 'http://example.com/images',
211 'transformVia404' => false,
212 'backend' => new FSFileBackend( [
213 'name' => 'local-backend',
214 'wikiId' => wfWikiID(),
215 'containerPaths' => [
216 'local-public' => $this->uploadDir . '/public',
217 'local-thumb' => $this->uploadDir . '/thumb',
218 'local-temp' => $this->uploadDir . '/temp',
219 'local-deleted' => $this->uploadDir . '/deleted',
223 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
224 $wgNamespaceAliases['Image'] = NS_FILE;
225 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
226 # add a namespace shadowing a interwiki link, to test
227 # proper precedence when resolving links. (bug 51680)
228 $wgExtraNamespaces[100] = 'MemoryAlpha';
229 $wgExtraNamespaces[101] = 'MemoryAlpha talk';
231 // XXX: tests won't run without this (for CACHE_DB)
232 if ( $wgMainCacheType === CACHE_DB ) {
233 $wgMainCacheType = CACHE_NONE;
235 if ( $wgMessageCacheType === CACHE_DB ) {
236 $wgMessageCacheType = CACHE_NONE;
238 if ( $wgParserCacheType === CACHE_DB ) {
239 $wgParserCacheType = CACHE_NONE;
242 DeferredUpdates::clearPendingUpdates();
243 $wgMemc = wfGetMainCache(); // checks $wgMainCacheType
244 $messageMemc = wfGetMessageCacheStorage();
245 $parserMemc = wfGetParserCacheStorage();
247 RequestContext::resetMain();
248 $context = new RequestContext;
250 $wgLang = $context->getLanguage();
251 $wgOut = $context->getOutput();
252 $wgRequest = $context->getRequest();
253 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], [ $wgParserConf ] );
255 if ( $wgStyleDirectory === false ) {
256 $wgStyleDirectory = "$IP/skins";
259 self::setupInterwikis();
260 $wgLocalInterwikis = [ 'local', 'mi' ];
261 // "extra language links"
262 // see https://gerrit.wikimedia.org/r/111390
263 array_push( $wgExtraInterlanguageLinkPrefixes, 'mul' );
265 // Reset namespace cache
266 MWNamespace::getCanonicalNamespaces( true );
267 Language::factory( 'en' )->resetNamespaces();
271 * Insert hardcoded interwiki in the lookup table.
273 * This function insert a set of well known interwikis that are used in
274 * the parser tests. They can be considered has fixtures are injected in
275 * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
276 * Since we are not interested in looking up interwikis in the database,
277 * the hook completely replace the existing mechanism (hook returns false).
279 public static function setupInterwikis() {
280 # Hack: insert a few Wikipedia in-project interwiki prefixes,
281 # for testing inter-language links
282 Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
283 static $testInterwikis = [
285 'iw_url' => 'http://doesnt.matter.org/$1',
290 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
295 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
300 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
305 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
310 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
315 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
320 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
325 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
330 'iw_url' => 'http://wikisource.org/wiki/$1',
335 if ( array_key_exists( $prefix, $testInterwikis ) ) {
336 $iwData = $testInterwikis[$prefix];
339 // We only want to rely on the above fixtures
341 } );// hooks::register
345 * Remove the hardcoded interwiki lookup table.
347 public static function tearDownInterwikis() {
348 Hooks::clear( 'InterwikiLoadPrefix' );
352 * Reset the Title-related services that need resetting
355 public static function resetTitleServices() {
356 $services = MediaWikiServices::getInstance();
357 $services->resetServiceForTesting( 'TitleFormatter' );
358 $services->resetServiceForTesting( 'TitleParser' );
359 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
360 $services->resetServiceForTesting( 'LinkRenderer' );
361 $services->resetServiceForTesting( 'LinkRendererFactory' );
364 public function setupRecorder( $options ) {
365 if ( isset( $options['record'] ) ) {
366 $this->recorder = new DbTestRecorder( $this );
367 $this->recorder->version = isset( $options['setversion'] ) ?
368 $options['setversion'] : SpecialVersion::getVersion();
369 } elseif ( isset( $options['compare'] ) ) {
370 $this->recorder = new DbTestPreviewer( $this );
372 $this->recorder = new TestRecorder( $this );
377 * Remove last character if it is a newline
382 public static function chomp( $s ) {
383 if ( substr( $s, -1 ) === "\n" ) {
384 return substr( $s, 0, -1 );
391 * Run a fuzz test series
392 * Draw input from a set of test files
393 * @param array $filenames
395 function fuzzTest( $filenames ) {
396 $GLOBALS['wgContLang'] = Language::factory( 'en' );
397 $dict = $this->getFuzzInput( $filenames );
398 $dictSize = strlen( $dict );
399 $logMaxLength = log( $this->maxFuzzTestLength );
400 $this->setupDatabase();
401 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
406 $opts = ParserOptions::newFromUser( $user );
407 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
410 // Generate test input
411 mt_srand( ++$this->fuzzSeed );
412 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
415 while ( strlen( $input ) < $totalLength ) {
416 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
417 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
418 $offset = mt_rand( 0, $dictSize - $hairLength );
419 $input .= substr( $dict, $offset, $hairLength );
422 $this->setupGlobals();
423 $parser = $this->getParser();
427 $parser->parse( $input, $title, $opts );
429 } catch ( Exception $exception ) {
434 echo "Test failed with seed {$this->fuzzSeed}\n";
436 printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input );
443 $this->teardownGlobals();
444 $parser->__destruct();
446 if ( $numTotal % 100 == 0 ) {
447 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
448 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
450 echo "Out of memory:\n";
451 $memStats = $this->getMemoryBreakdown();
453 foreach ( $memStats as $name => $usage ) {
454 echo "$name: $usage\n";
463 * Get an input dictionary from a set of parser test files
464 * @param array $filenames
467 function getFuzzInput( $filenames ) {
470 foreach ( $filenames as $filename ) {
471 $contents = file_get_contents( $filename );
473 '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s',
478 foreach ( $matches[1] as $match ) {
479 $dict .= $match . "\n";
487 * Get a memory usage breakdown
490 function getMemoryBreakdown() {
493 foreach ( $GLOBALS as $name => $value ) {
494 $memStats['$' . $name] = strlen( serialize( $value ) );
497 $classes = get_declared_classes();
499 foreach ( $classes as $class ) {
500 $rc = new ReflectionClass( $class );
501 $props = $rc->getStaticProperties();
502 $memStats[$class] = strlen( serialize( $props ) );
503 $methods = $rc->getMethods();
505 foreach ( $methods as $method ) {
506 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
510 $functions = get_defined_functions();
512 foreach ( $functions['user'] as $function ) {
513 $rf = new ReflectionFunction( $function );
514 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
527 * Run a series of tests listed in the given text files.
528 * Each test consists of a brief description, wikitext input,
529 * and the expected HTML output.
531 * Prints status updates on stdout and counts up the total
532 * number and percentage of passed tests.
534 * @param array $filenames Array of strings
535 * @return bool True if passed all tests, false if any tests failed.
537 public function runTestsFromFiles( $filenames ) {
540 // be sure, ParserTest::addArticle has correct language set,
541 // so that system messages gets into the right language cache
542 $GLOBALS['wgLanguageCode'] = 'en';
543 $GLOBALS['wgContLang'] = Language::factory( 'en' );
545 $this->recorder->start();
547 $this->setupDatabase();
550 foreach ( $filenames as $filename ) {
551 echo "Running parser tests from: $filename\n";
552 $tests = new TestFileIterator( $filename, $this );
553 $ok = $this->runTests( $tests ) && $ok;
556 $this->teardownDatabase();
557 $this->recorder->report();
558 } catch ( DBError $e ) {
559 echo $e->getMessage();
561 $this->recorder->end();
566 function runTests( $tests ) {
569 foreach ( $tests as $t ) {
571 $this->runTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] );
572 $ok = $ok && $result;
573 $this->recorder->record( $t['test'], $t['subtest'], $result );
576 if ( $this->showProgress ) {
584 * Get a Parser object
586 * @param string $preprocessor
589 function getParser( $preprocessor = null ) {
590 global $wgParserConf;
592 $class = $wgParserConf['class'];
593 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] + $wgParserConf );
595 foreach ( $this->hooks as $tag => $callback ) {
596 $parser->setHook( $tag, $callback );
599 foreach ( $this->functionHooks as $tag => $bits ) {
600 list( $callback, $flags ) = $bits;
601 $parser->setFunctionHook( $tag, $callback, $flags );
604 foreach ( $this->transparentHooks as $tag => $callback ) {
605 $parser->setTransparentTagHook( $tag, $callback );
608 Hooks::run( 'ParserTestParser', [ &$parser ] );
614 * Run a given wikitext input through a freshly-constructed wiki parser,
615 * and compare the output against the expected results.
616 * Prints status and explanatory messages to stdout.
618 * @param string $desc Test's description
619 * @param string $input Wikitext to try rendering
620 * @param string $result Result to output
621 * @param array $opts Test's options
622 * @param string $config Overrides for global variables, one per line
625 public function runTest( $desc, $input, $result, $opts, $config ) {
626 if ( $this->showProgress ) {
627 $this->showTesting( $desc );
630 $opts = $this->parseOptions( $opts );
631 $context = $this->setupGlobals( $opts, $config );
633 $user = $context->getUser();
634 $options = ParserOptions::newFromContext( $context );
636 if ( isset( $opts['djvu'] ) ) {
637 if ( !$this->djVuSupport->isEnabled() ) {
638 return $this->showSkipped();
642 if ( isset( $opts['tidy'] ) ) {
643 if ( !$this->tidySupport->isEnabled() ) {
644 return $this->showSkipped();
646 $options->setTidy( true );
650 if ( isset( $opts['title'] ) ) {
651 $titleText = $opts['title'];
653 $titleText = 'Parser test';
656 ObjectCache::getMainWANInstance()->clearProcessCache();
657 $local = isset( $opts['local'] );
658 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
659 $parser = $this->getParser( $preprocessor );
660 $title = Title::newFromText( $titleText );
662 if ( isset( $opts['pst'] ) ) {
663 $out = $parser->preSaveTransform( $input, $title, $user, $options );
664 } elseif ( isset( $opts['msg'] ) ) {
665 $out = $parser->transformMsg( $input, $options, $title );
666 } elseif ( isset( $opts['section'] ) ) {
667 $section = $opts['section'];
668 $out = $parser->getSection( $input, $section );
669 } elseif ( isset( $opts['replace'] ) ) {
670 $section = $opts['replace'][0];
671 $replace = $opts['replace'][1];
672 $out = $parser->replaceSection( $input, $section, $replace );
673 } elseif ( isset( $opts['comment'] ) ) {
674 $out = Linker::formatComment( $input, $title, $local );
675 } elseif ( isset( $opts['preload'] ) ) {
676 $out = $parser->getPreloadText( $input, $title, $options );
678 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
679 $output->setTOCEnabled( !isset( $opts['notoc'] ) );
680 $out = $output->getText();
681 if ( isset( $opts['tidy'] ) ) {
682 $out = preg_replace( '/\s+$/', '', $out );
685 if ( isset( $opts['showtitle'] ) ) {
686 if ( $output->getTitleText() ) {
687 $title = $output->getTitleText();
690 $out = "$title\n$out";
693 if ( isset( $opts['showindicators'] ) ) {
695 foreach ( $output->getIndicators() as $id => $content ) {
696 $indicators .= "$id=$content\n";
698 $out = $indicators . $out;
701 if ( isset( $opts['ill'] ) ) {
702 $out = implode( ' ', $output->getLanguageLinks() );
703 } elseif ( isset( $opts['cat'] ) ) {
704 $outputPage = $context->getOutput();
705 $outputPage->addCategoryLinks( $output->getCategories() );
706 $cats = $outputPage->getCategoryLinks();
708 if ( isset( $cats['normal'] ) ) {
709 $out = implode( ' ', $cats['normal'] );
716 $this->teardownGlobals();
718 if ( count( $this->normalizationFunctions ) ) {
719 $result = ParserTestResultNormalizer::normalize( $result, $this->normalizationFunctions );
720 $out = ParserTestResultNormalizer::normalize( $out, $this->normalizationFunctions );
723 $testResult = new ParserTestResult( $desc );
724 $testResult->expected = $result;
725 $testResult->actual = $out;
727 return $this->showTestResult( $testResult );
731 * Refactored in 1.22 to use ParserTestResult
732 * @param ParserTestResult $testResult
735 function showTestResult( ParserTestResult $testResult ) {
736 if ( $testResult->isSuccess() ) {
737 $this->showSuccess( $testResult );
740 $this->showFailure( $testResult );
746 * Use a regex to find out the value of an option
747 * @param string $key Name of option val to retrieve
748 * @param array $opts Options array to look in
749 * @param mixed $default Default value returned if not found
752 private static function getOptionValue( $key, $opts, $default ) {
753 $key = strtolower( $key );
755 if ( isset( $opts[$key] ) ) {
762 private function parseOptions( $instring ) {
768 // foo=bar,"baz quux"
771 (?<qstr> # Quoted string
773 (?:[^\\\\"] | \\\\.)*
779 [^"{}] | # Not a quoted string or object, or
780 (?&qstr) | # A quoted string, or
781 (?&json) # A json object (recursively)
787 (?&qstr) # Quoted val
795 (?&json) # JSON object
799 $regex = '/' . $defs . '\b
815 $valueregex = '/' . $defs . '(?&value)/x';
817 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
818 foreach ( $matches as $bits ) {
819 $key = strtolower( $bits['k'] );
820 if ( !isset( $bits['v'] ) ) {
823 preg_match_all( $valueregex, $bits['v'], $vmatches );
824 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
825 if ( count( $opts[$key] ) == 1 ) {
826 $opts[$key] = $opts[$key][0];
834 private function cleanupOption( $opt ) {
835 if ( substr( $opt, 0, 1 ) == '"' ) {
836 return stripcslashes( substr( $opt, 1, -1 ) );
839 if ( substr( $opt, 0, 2 ) == '[[' ) {
840 return substr( $opt, 2, -2 );
843 if ( substr( $opt, 0, 1 ) == '{' ) {
844 return FormatJson::decode( $opt, true );
850 * Set up the global variables for a consistent environment for each test.
851 * Ideally this should replace the global configuration entirely.
852 * @param string $opts
853 * @param string $config
854 * @return RequestContext
856 private function setupGlobals( $opts = '', $config = '' ) {
859 # Find out values for some special options.
861 self::getOptionValue( 'language', $opts, 'en' );
863 self::getOptionValue( 'variant', $opts, false );
865 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
866 $linkHolderBatchSize =
867 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
870 'wgServer' => 'http://example.org',
871 'wgServerName' => 'example.org',
872 'wgScript' => '/index.php',
873 'wgScriptPath' => '',
874 'wgArticlePath' => '/wiki/$1',
875 'wgActionPaths' => [],
876 'wgLockManagers' => [ [
877 'name' => 'fsLockManager',
878 'class' => 'FSLockManager',
879 'lockDirectory' => $this->uploadDir . '/lockdir',
881 'name' => 'nullLockManager',
882 'class' => 'NullLockManager',
884 'wgLocalFileRepo' => [
885 'class' => 'LocalRepo',
887 'url' => 'http://example.com/images',
889 'transformVia404' => false,
890 'backend' => new FSFileBackend( [
891 'name' => 'local-backend',
892 'wikiId' => wfWikiID(),
893 'containerPaths' => [
894 'local-public' => $this->uploadDir,
895 'local-thumb' => $this->uploadDir . '/thumb',
896 'local-temp' => $this->uploadDir . '/temp',
897 'local-deleted' => $this->uploadDir . '/delete',
901 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
902 'wgUploadNavigationUrl' => false,
903 'wgStylePath' => '/skins',
904 'wgSitename' => 'MediaWiki',
905 'wgLanguageCode' => $lang,
906 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'parsertest_' : 'pt_',
907 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
909 'wgContLang' => null,
910 'wgNamespacesWithSubpages' => [ 0 => isset( $opts['subpage'] ) ],
911 'wgMaxTocLevel' => $maxtoclevel,
912 'wgCapitalLinks' => true,
913 'wgNoFollowLinks' => true,
914 'wgNoFollowDomainExceptions' => [ 'no-nofollow.org' ],
915 'wgThumbnailScriptPath' => false,
916 'wgUseImageResize' => true,
917 'wgSVGConverter' => 'null',
918 'wgSVGConverters' => [ 'null' => 'echo "1">$output' ],
919 'wgLocaltimezone' => 'UTC',
920 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
921 'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
922 'wgDefaultLanguageVariant' => $variant,
923 'wgVariantArticlePath' => false,
924 'wgGroupPermissions' => [ '*' => [
925 'createaccount' => true,
928 'createpage' => true,
929 'createtalk' => true,
931 'wgNamespaceProtection' => [ NS_MEDIAWIKI => 'editinterface' ],
932 'wgDefaultExternalStore' => [],
933 'wgForeignFileRepos' => [],
934 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
935 'wgExperimentalHtmlIds' => false,
936 'wgExternalLinkTarget' => false,
938 'wgAdaptiveMessageCache' => true,
939 'wgDisableLangConversion' => false,
940 'wgDisableTitleConversion' => false,
942 'wgUseTidy' => isset( $opts['tidy'] ),
943 'wgTidyConfig' => null,
944 'wgDebugTidy' => false,
945 'wgTidyConf' => $IP . '/includes/tidy/tidy.conf',
947 'wgTidyInternal' => $this->tidySupport->isInternal(),
951 $configLines = explode( "\n", $config );
953 foreach ( $configLines as $line ) {
954 list( $var, $value ) = explode( '=', $line, 2 );
956 $settings[$var] = eval( "return $value;" );
960 $this->savedGlobals = [];
963 Hooks::run( 'ParserTestGlobals', [ &$settings ] );
965 foreach ( $settings as $var => $val ) {
966 if ( array_key_exists( $var, $GLOBALS ) ) {
967 $this->savedGlobals[$var] = $GLOBALS[$var];
970 $GLOBALS[$var] = $val;
973 // Must be set before $context as user language defaults to $wgContLang
974 $GLOBALS['wgContLang'] = Language::factory( $lang );
975 $GLOBALS['wgMemc'] = new EmptyBagOStuff;
977 RequestContext::resetMain();
978 $context = RequestContext::getMain();
979 $GLOBALS['wgLang'] = $context->getLanguage();
980 $GLOBALS['wgOut'] = $context->getOutput();
981 $GLOBALS['wgUser'] = $context->getUser();
983 // We (re)set $wgThumbLimits to a single-element array above.
984 $context->getUser()->setOption( 'thumbsize', 0 );
988 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
989 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
991 MagicWord::clearCache();
992 MWTidy::destroySingleton();
993 RepoGroup::destroySingleton();
995 self::resetTitleServices();
1001 * List of temporary tables to create, without prefix.
1002 * Some of these probably aren't necessary.
1005 private function listTables() {
1006 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1007 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
1008 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1009 'site_stats', 'ipblocks', 'image', 'oldimage',
1010 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1011 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1012 'archive', 'user_groups', 'page_props', 'category'
1015 if ( in_array( $this->db->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1016 array_push( $tables, 'searchindex' );
1019 // Allow extensions to add to the list of tables to duplicate;
1020 // may be necessary if they hook into page save or other code
1021 // which will require them while running tests.
1022 Hooks::run( 'ParserTestTables', [ &$tables ] );
1028 * Set up a temporary set of wiki tables to work with for the tests.
1029 * Currently this will only be done once per run, and any changes to
1030 * the db will be visible to later tests in the run.
1032 public function setupDatabase() {
1035 if ( $this->databaseSetupDone ) {
1039 $this->db = wfGetDB( DB_MASTER );
1040 $dbType = $this->db->getType();
1042 if ( $wgDBprefix === 'parsertest_' || ( $dbType == 'oracle' && $wgDBprefix === 'pt_' ) ) {
1043 throw new MWException( 'setupDatabase should be called before setupGlobals' );
1046 $this->databaseSetupDone = true;
1048 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
1049 # It seems to have been fixed since (r55079?), but regressed at some point before r85701.
1050 # This works around it for now...
1051 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
1053 # CREATE TEMPORARY TABLE breaks if there is more than one server
1054 if ( wfGetLB()->getServerCount() != 1 ) {
1055 $this->useTemporaryTables = false;
1058 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
1059 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
1061 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
1062 $this->dbClone->useTemporaryTables( $temporary );
1063 $this->dbClone->cloneTableStructure();
1065 if ( $dbType == 'oracle' ) {
1066 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1067 # Insert 0 user to prevent FK violations
1070 $this->db->insert( 'user', [
1072 'user_name' => 'Anonymous' ] );
1075 # Update certain things in site_stats
1076 $this->db->insert( 'site_stats',
1077 [ 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ] );
1079 # Reinitialise the LocalisationCache to match the database state
1080 Language::getLocalisationCache()->unloadAll();
1082 # Clear the message cache
1083 MessageCache::singleton()->clear();
1085 // Remember to update newParserTests.php after changing the below
1086 // (and it uses a slightly different syntax just for teh lulz)
1087 $this->setupUploadDir();
1088 $user = User::createNew( 'WikiSysop' );
1089 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
1090 # note that the size/width/height/bits/etc of the file
1091 # are actually set by inspecting the file itself; the arguments
1092 # to recordUpload2 have no effect. That said, we try to make things
1093 # match up so it is less confusing to readers of the code & tests.
1094 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1099 'media_type' => MEDIATYPE_BITMAP,
1100 'mime' => 'image/jpeg',
1101 'metadata' => serialize( [] ),
1102 'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
1103 'fileExists' => true
1104 ], $this->db->timestamp( '20010115123500' ), $user );
1106 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
1107 # again, note that size/width/height below are ignored; see above.
1108 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1113 'media_type' => MEDIATYPE_BITMAP,
1114 'mime' => 'image/png',
1115 'metadata' => serialize( [] ),
1116 'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
1117 'fileExists' => true
1118 ], $this->db->timestamp( '20130225203040' ), $user );
1120 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
1121 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1126 'media_type' => MEDIATYPE_DRAWING,
1127 'mime' => 'image/svg+xml',
1128 'metadata' => serialize( [] ),
1129 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1130 'fileExists' => true
1131 ], $this->db->timestamp( '20010115123500' ), $user );
1133 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1134 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
1135 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1140 'media_type' => MEDIATYPE_BITMAP,
1141 'mime' => 'image/jpeg',
1142 'metadata' => serialize( [] ),
1143 'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
1144 'fileExists' => true
1145 ], $this->db->timestamp( '20010115123500' ), $user );
1147 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
1148 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1153 'media_type' => MEDIATYPE_VIDEO,
1154 'mime' => 'application/ogg',
1155 'metadata' => serialize( [] ),
1156 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1157 'fileExists' => true
1158 ], $this->db->timestamp( '20010115123500' ), $user );
1160 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
1161 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1166 'media_type' => MEDIATYPE_AUDIO,
1167 'mime' => 'application/ogg',
1168 'metadata' => serialize( [] ),
1169 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1170 'fileExists' => true
1171 ], $this->db->timestamp( '20010115123500' ), $user );
1174 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
1175 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1180 'media_type' => MEDIATYPE_BITMAP,
1181 'mime' => 'image/vnd.djvu',
1182 'metadata' => '<?xml version="1.0" ?>
1183 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1186 <BODY><OBJECT height="3508" width="2480">
1187 <PARAM name="DPI" value="300" />
1188 <PARAM name="GAMMA" value="2.2" />
1190 <OBJECT height="3508" width="2480">
1191 <PARAM name="DPI" value="300" />
1192 <PARAM name="GAMMA" value="2.2" />
1194 <OBJECT height="3508" width="2480">
1195 <PARAM name="DPI" value="300" />
1196 <PARAM name="GAMMA" value="2.2" />
1198 <OBJECT height="3508" width="2480">
1199 <PARAM name="DPI" value="300" />
1200 <PARAM name="GAMMA" value="2.2" />
1202 <OBJECT height="3508" width="2480">
1203 <PARAM name="DPI" value="300" />
1204 <PARAM name="GAMMA" value="2.2" />
1208 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1209 'fileExists' => true
1210 ], $this->db->timestamp( '20010115123600' ), $user );
1213 public function teardownDatabase() {
1214 if ( !$this->databaseSetupDone ) {
1215 $this->teardownGlobals();
1218 $this->teardownUploadDir( $this->uploadDir );
1220 $this->dbClone->destroy();
1221 $this->databaseSetupDone = false;
1223 if ( $this->useTemporaryTables ) {
1224 if ( $this->db->getType() == 'sqlite' ) {
1225 # Under SQLite the searchindex table is virtual and need
1226 # to be explicitly destroyed. See bug 29912
1227 # See also MediaWikiTestCase::destroyDB()
1228 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1229 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
1231 # Don't need to do anything
1232 $this->teardownGlobals();
1236 $tables = $this->listTables();
1238 foreach ( $tables as $table ) {
1239 if ( $this->db->getType() == 'oracle' ) {
1240 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1242 $this->db->query( "DROP TABLE `parsertest_$table`" );
1246 if ( $this->db->getType() == 'oracle' ) {
1247 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1250 $this->teardownGlobals();
1254 * Create a dummy uploads directory which will contain a couple
1255 * of files in order to pass existence tests.
1257 * @return string The directory
1259 private function setupUploadDir() {
1262 $dir = $this->uploadDir;
1263 if ( $this->keepUploads && is_dir( $dir ) ) {
1267 // wfDebug( "Creating upload directory $dir\n" );
1268 if ( file_exists( $dir ) ) {
1269 wfDebug( "Already exists!\n" );
1273 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
1274 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
1275 wfMkdirParents( $dir . '/e/ea', null, __METHOD__ );
1276 copy( "$IP/tests/phpunit/data/parser/wiki.png", "$dir/e/ea/Thumb.png" );
1277 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
1278 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/0/09/Bad.jpg" );
1279 wfMkdirParents( $dir . '/f/ff', null, __METHOD__ );
1280 file_put_contents( "$dir/f/ff/Foobar.svg",
1281 '<?xml version="1.0" encoding="utf-8"?>' .
1282 '<svg xmlns="http://www.w3.org/2000/svg"' .
1283 ' version="1.1" width="240" height="180"/>' );
1284 wfMkdirParents( $dir . '/5/5f', null, __METHOD__ );
1285 copy( "$IP/tests/phpunit/data/parser/LoremIpsum.djvu", "$dir/5/5f/LoremIpsum.djvu" );
1286 wfMkdirParents( $dir . '/0/00', null, __METHOD__ );
1287 copy( "$IP/tests/phpunit/data/parser/320x240.ogv", "$dir/0/00/Video.ogv" );
1288 wfMkdirParents( $dir . '/4/41', null, __METHOD__ );
1289 copy( "$IP/tests/phpunit/data/media/say-test.ogg", "$dir/4/41/Audio.oga" );
1295 * Restore default values and perform any necessary clean-up
1296 * after each test runs.
1298 private function teardownGlobals() {
1299 RepoGroup::destroySingleton();
1300 FileBackendGroup::destroySingleton();
1301 LockManagerGroup::destroySingletons();
1302 LinkCache::singleton()->clear();
1303 MWTidy::destroySingleton();
1305 foreach ( $this->savedGlobals as $var => $val ) {
1306 $GLOBALS[$var] = $val;
1311 * Remove the dummy uploads directory
1312 * @param string $dir
1314 private function teardownUploadDir( $dir ) {
1315 if ( $this->keepUploads ) {
1319 // delete the files first, then the dirs.
1322 "$dir/3/3a/Foobar.jpg",
1323 "$dir/thumb/3/3a/Foobar.jpg/*.jpg",
1324 "$dir/e/ea/Thumb.png",
1325 "$dir/0/09/Bad.jpg",
1326 "$dir/5/5f/LoremIpsum.djvu",
1327 "$dir/thumb/5/5f/LoremIpsum.djvu/*-LoremIpsum.djvu.jpg",
1328 "$dir/f/ff/Foobar.svg",
1329 "$dir/thumb/f/ff/Foobar.svg/*-Foobar.svg.png",
1330 "$dir/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
1331 "$dir/0/00/Video.ogv",
1332 "$dir/thumb/0/00/Video.ogv/120px--Video.ogv.jpg",
1333 "$dir/thumb/0/00/Video.ogv/180px--Video.ogv.jpg",
1334 "$dir/thumb/0/00/Video.ogv/240px--Video.ogv.jpg",
1335 "$dir/thumb/0/00/Video.ogv/320px--Video.ogv.jpg",
1336 "$dir/thumb/0/00/Video.ogv/270px--Video.ogv.jpg",
1337 "$dir/thumb/0/00/Video.ogv/320px-seek=2-Video.ogv.jpg",
1338 "$dir/thumb/0/00/Video.ogv/320px-seek=3.3666666666667-Video.ogv.jpg",
1339 "$dir/4/41/Audio.oga",
1347 "$dir/thumb/3/3a/Foobar.jpg",
1354 "$dir/thumb/f/ff/Foobar.svg",
1362 "$dir/thumb/0/00/Video.ogv",
1365 "$dir/thumb/5/5f/LoremIpsum.djvu",
1382 * Delete the specified files, if they exist.
1383 * @param array $files Full paths to files to delete.
1385 private static function deleteFiles( $files ) {
1386 foreach ( $files as $pattern ) {
1387 foreach ( glob( $pattern ) as $file ) {
1388 if ( file_exists( $file ) ) {
1396 * Delete the specified directories, if they exist. Must be empty.
1397 * @param array $dirs Full paths to directories to delete.
1399 private static function deleteDirs( $dirs ) {
1400 foreach ( $dirs as $dir ) {
1401 if ( is_dir( $dir ) ) {
1408 * "Running test $desc..."
1409 * @param string $desc
1411 protected function showTesting( $desc ) {
1412 print "Running test $desc... ";
1416 * Print a happy success message.
1418 * Refactored in 1.22 to use ParserTestResult
1420 * @param ParserTestResult $testResult
1423 protected function showSuccess( ParserTestResult $testResult ) {
1424 if ( $this->showProgress ) {
1425 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
1432 * Print a failure message and provide some explanatory output
1433 * about what went wrong if so configured.
1435 * Refactored in 1.22 to use ParserTestResult
1437 * @param ParserTestResult $testResult
1440 protected function showFailure( ParserTestResult $testResult ) {
1441 if ( $this->showFailure ) {
1442 if ( !$this->showProgress ) {
1443 # In quiet mode we didn't show the 'Testing' message before the
1444 # test, in case it succeeded. Show it now:
1445 $this->showTesting( $testResult->description );
1448 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
1450 if ( $this->showOutput ) {
1451 print "--- Expected ---\n{$testResult->expected}\n";
1452 print "--- Actual ---\n{$testResult->actual}\n";
1455 if ( $this->showDiffs ) {
1456 print $this->quickDiff( $testResult->expected, $testResult->actual );
1457 if ( !$this->wellFormed( $testResult->actual ) ) {
1458 print "XML error: $this->mXmlError\n";
1467 * Print a skipped message.
1471 protected function showSkipped() {
1472 if ( $this->showProgress ) {
1473 print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
1480 * Run given strings through a diff and return the (colorized) output.
1481 * Requires writable /tmp directory and a 'diff' command in the PATH.
1483 * @param string $input
1484 * @param string $output
1485 * @param string $inFileTail Tailing for the input file name
1486 * @param string $outFileTail Tailing for the output file name
1489 protected function quickDiff( $input, $output,
1490 $inFileTail = 'expected', $outFileTail = 'actual'
1492 if ( $this->markWhitespace ) {
1498 $input = strtr( $input, $pairs );
1499 $output = strtr( $output, $pairs );
1502 # Windows, or at least the fc utility, is retarded
1503 $slash = wfIsWindows() ? '\\' : '/';
1504 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
1506 $infile = "$prefix-$inFileTail";
1507 $this->dumpToFile( $input, $infile );
1509 $outfile = "$prefix-$outFileTail";
1510 $this->dumpToFile( $output, $outfile );
1512 $shellInfile = wfEscapeShellArg( $infile );
1513 $shellOutfile = wfEscapeShellArg( $outfile );
1516 // we assume that people with diff3 also have usual diff
1517 if ( $this->useDwdiff ) {
1518 $shellCommand = 'dwdiff -Pc';
1520 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
1523 $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" );
1528 if ( $this->useDwdiff ) {
1531 return $this->colorDiff( $diff );
1536 * Write the given string to a file, adding a final newline.
1538 * @param string $data
1539 * @param string $filename
1541 private function dumpToFile( $data, $filename ) {
1542 $file = fopen( $filename, "wt" );
1543 fwrite( $file, $data . "\n" );
1548 * Colorize unified diff output if set for ANSI color output.
1549 * Subtractions are colored blue, additions red.
1551 * @param string $text
1554 protected function colorDiff( $text ) {
1555 return preg_replace(
1556 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
1557 [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
1558 $this->term->color( 31 ) . '$1' . $this->term->reset() ],
1563 * Show "Reading tests from ..."
1565 * @param string $path
1567 public function showRunFile( $path ) {
1568 print $this->term->color( 1 ) .
1569 "Reading tests from \"$path\"..." .
1570 $this->term->reset() .
1575 * Insert a temporary test article
1576 * @param string $name The title, including any prefix
1577 * @param string $text The article text
1578 * @param int|string $line The input line number, for reporting errors
1579 * @param bool|string $ignoreDuplicate Whether to silently ignore duplicate pages
1581 * @throws MWException
1583 public static function addArticle( $name, $text, $line = 'unknown', $ignoreDuplicate = '' ) {
1584 global $wgCapitalLinks;
1586 $oldCapitalLinks = $wgCapitalLinks;
1587 $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
1589 $text = self::chomp( $text );
1590 $name = self::chomp( $name );
1592 $title = Title::newFromText( $name );
1594 if ( is_null( $title ) ) {
1595 throw new MWException( "invalid title '$name' at line $line\n" );
1598 $page = WikiPage::factory( $title );
1599 $page->loadPageData( 'fromdbmaster' );
1601 if ( $page->exists() ) {
1602 if ( $ignoreDuplicate == 'ignoreduplicate' ) {
1605 throw new MWException( "duplicate article '$name' at line $line\n" );
1609 $page->doEditContent( ContentHandler::makeContent( $text, $title ), '', EDIT_NEW );
1611 $wgCapitalLinks = $oldCapitalLinks;
1615 * Steal a callback function from the primary parser, save it for
1616 * application to our scary parser. If the hook is not installed,
1617 * abort processing of this file.
1619 * @param string $name
1620 * @return bool True if tag hook is present
1622 public function requireHook( $name ) {
1625 $wgParser->firstCallInit(); // make sure hooks are loaded.
1627 if ( isset( $wgParser->mTagHooks[$name] ) ) {
1628 $this->hooks[$name] = $wgParser->mTagHooks[$name];
1630 echo " This test suite requires the '$name' hook extension, skipping.\n";
1638 * Steal a callback function from the primary parser, save it for
1639 * application to our scary parser. If the hook is not installed,
1640 * abort processing of this file.
1642 * @param string $name
1643 * @return bool True if function hook is present
1645 public function requireFunctionHook( $name ) {
1648 $wgParser->firstCallInit(); // make sure hooks are loaded.
1650 if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
1651 $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
1653 echo " This test suite requires the '$name' function hook extension, skipping.\n";
1661 * Steal a callback function from the primary parser, save it for
1662 * application to our scary parser. If the hook is not installed,
1663 * abort processing of this file.
1665 * @param string $name
1666 * @return bool True if function hook is present
1668 public function requireTransparentHook( $name ) {
1671 $wgParser->firstCallInit(); // make sure hooks are loaded.
1673 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
1674 $this->transparentHooks[$name] = $wgParser->mTransparentTagHooks[$name];
1676 echo " This test suite requires the '$name' transparent hook extension, skipping.\n";
1683 private function wellFormed( $text ) {
1685 Sanitizer::hackDocType() .
1690 $parser = xml_parser_create( "UTF-8" );
1692 # case folding violates XML standard, turn it off
1693 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
1695 if ( !xml_parse( $parser, $html, true ) ) {
1696 $err = xml_error_string( xml_get_error_code( $parser ) );
1697 $position = xml_get_current_byte_index( $parser );
1698 $fragment = $this->extractFragment( $html, $position );
1699 $this->mXmlError = "$err at byte $position:\n$fragment";
1700 xml_parser_free( $parser );
1705 xml_parser_free( $parser );
1710 private function extractFragment( $text, $position ) {
1711 $start = max( 0, $position - 10 );
1712 $before = $position - $start;
1714 $this->term->color( 34 ) .
1715 substr( $text, $start, $before ) .
1716 $this->term->color( 0 ) .
1717 $this->term->color( 31 ) .
1718 $this->term->color( 1 ) .
1719 substr( $text, $position, 1 ) .
1720 $this->term->color( 0 ) .
1721 $this->term->color( 34 ) .
1722 substr( $text, $position + 1, 9 ) .
1723 $this->term->color( 0 ) .
1725 $display = str_replace( "\n", ' ', $fragment );
1727 str_repeat( ' ', $before ) .
1728 $this->term->color( 31 ) .
1730 $this->term->color( 0 );
1732 return "$display\n$caret";
1735 static function getFakeTimestamp( &$parser, &$ts ) {
1736 $ts = 123; // parsed as '1970-01-01T00:02:03Z'
1741 class ParserTestResultNormalizer {
1742 protected $doc, $xpath, $invalid;
1744 public static function normalize( $text, $funcs ) {
1745 $norm = new self( $text );
1746 if ( $norm->invalid ) {
1749 foreach ( $funcs as $func ) {
1752 return $norm->serialize();
1755 protected function __construct( $text ) {
1756 $this->doc = new DOMDocument( '1.0', 'utf-8' );
1758 // Note: parsing a supposedly XHTML document with an XML parser is not
1759 // guaranteed to give accurate results. For example, it may introduce
1760 // differences in the number of line breaks in <pre> tags.
1762 MediaWiki\suppressWarnings();
1763 if ( !$this->doc->loadXML( '<html><body>' . $text . '</body></html>' ) ) {
1764 $this->invalid = true;
1766 MediaWiki\restoreWarnings();
1767 $this->xpath = new DOMXPath( $this->doc );
1768 $this->body = $this->xpath->query( '//body' )->item( 0 );
1771 protected function removeTbody() {
1772 foreach ( $this->xpath->query( '//tbody' ) as $tbody ) {
1773 while ( $tbody->firstChild ) {
1774 $child = $tbody->firstChild;
1775 $tbody->removeChild( $child );
1776 $tbody->parentNode->insertBefore( $child, $tbody );
1778 $tbody->parentNode->removeChild( $tbody );
1783 * The point of this function is to produce a normalized DOM in which
1784 * Tidy's output matches the output of html5depurate. Tidy both trims
1785 * and pretty-prints, so this requires fairly aggressive treatment.
1787 * In particular, note that Tidy converts <pre>x</pre> to <pre>\nx\n</pre>,
1788 * which theoretically affects display since the second line break is not
1789 * ignored by compliant HTML parsers.
1791 * This function also removes empty elements, as does Tidy.
1793 protected function trimWhitespace() {
1794 foreach ( $this->xpath->query( '//text()' ) as $child ) {
1795 if ( strtolower( $child->parentNode->nodeName ) === 'pre' ) {
1796 // Just trim one line break from the start and end
1797 if ( substr_compare( $child->data, "\n", 0 ) === 0 ) {
1798 $child->data = substr( $child->data, 1 );
1800 if ( substr_compare( $child->data, "\n", -1 ) === 0 ) {
1801 $child->data = substr( $child->data, 0, -1 );
1804 // Trim all whitespace
1805 $child->data = trim( $child->data );
1807 if ( $child->data === '' ) {
1808 $child->parentNode->removeChild( $child );
1814 * Serialize the XML DOM for comparison purposes. This does not generate HTML.
1816 protected function serialize() {
1817 return strtr( $this->doc->saveXML( $this->body ),
1818 [ '<body>' => '', '</body>' => '' ] );