MediaWiki
REL1_24
|
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 $djVuSupport; 00071 00075 private $tidySupport; 00076 00077 private $maxFuzzTestLength = 300; 00078 private $fuzzSeed = 0; 00079 private $memoryLimit = 50; 00080 private $uploadDir = null; 00081 00082 public $regex = ""; 00083 private $savedGlobals = array(); 00084 00090 public function __construct( $options = array() ) { 00091 # Only colorize output if stdout is a terminal. 00092 $this->color = !wfIsWindows() && Maintenance::posix_isatty( 1 ); 00093 00094 if ( isset( $options['color'] ) ) { 00095 switch ( $options['color'] ) { 00096 case 'no': 00097 $this->color = false; 00098 break; 00099 case 'yes': 00100 default: 00101 $this->color = true; 00102 break; 00103 } 00104 } 00105 00106 $this->term = $this->color 00107 ? new AnsiTermColorer() 00108 : new DummyTermColorer(); 00109 00110 $this->showDiffs = !isset( $options['quick'] ); 00111 $this->showProgress = !isset( $options['quiet'] ); 00112 $this->showFailure = !( 00113 isset( $options['quiet'] ) 00114 && ( isset( $options['record'] ) 00115 || isset( $options['compare'] ) ) ); // redundant output 00116 00117 $this->showOutput = isset( $options['show-output'] ); 00118 00119 if ( isset( $options['filter'] ) ) { 00120 $options['regex'] = $options['filter']; 00121 } 00122 00123 if ( isset( $options['regex'] ) ) { 00124 if ( isset( $options['record'] ) ) { 00125 echo "Warning: --record cannot be used with --regex, disabling --record\n"; 00126 unset( $options['record'] ); 00127 } 00128 $this->regex = $options['regex']; 00129 } else { 00130 # Matches anything 00131 $this->regex = ''; 00132 } 00133 00134 $this->setupRecorder( $options ); 00135 $this->keepUploads = isset( $options['keep-uploads'] ); 00136 00137 if ( isset( $options['seed'] ) ) { 00138 $this->fuzzSeed = intval( $options['seed'] ) - 1; 00139 } 00140 00141 $this->runDisabled = isset( $options['run-disabled'] ); 00142 $this->runParsoid = isset( $options['run-parsoid'] ); 00143 00144 $this->djVuSupport = new DjVuSupport(); 00145 $this->tidySupport = new TidySupport(); 00146 if ( !$this->tidySupport->isEnabled() ) { 00147 echo "Warning: tidy is not installed, skipping some tests\n"; 00148 } 00149 00150 $this->hooks = array(); 00151 $this->functionHooks = array(); 00152 $this->transparentHooks = array(); 00153 self::setUp(); 00154 } 00155 00156 static function setUp() { 00157 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, 00158 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache, 00159 $wgExtraNamespaces, $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo, 00160 $wgExtraInterlanguageLinkPrefixes, $wgLocalInterwikis, 00161 $parserMemc, $wgThumbnailScriptPath, $wgScriptPath, 00162 $wgArticlePath, $wgScript, $wgStylePath, $wgExtensionAssetsPath, 00163 $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgLockManagers; 00164 00165 $wgScript = '/index.php'; 00166 $wgScriptPath = '/'; 00167 $wgArticlePath = '/wiki/$1'; 00168 $wgStylePath = '/skins'; 00169 $wgExtensionAssetsPath = '/extensions'; 00170 $wgThumbnailScriptPath = false; 00171 $wgLockManagers = array( array( 00172 'name' => 'fsLockManager', 00173 'class' => 'FSLockManager', 00174 'lockDirectory' => wfTempDir() . '/test-repo/lockdir', 00175 ), array( 00176 'name' => 'nullLockManager', 00177 'class' => 'NullLockManager', 00178 ) ); 00179 $wgLocalFileRepo = array( 00180 'class' => 'LocalRepo', 00181 'name' => 'local', 00182 'url' => 'http://example.com/images', 00183 'hashLevels' => 2, 00184 'transformVia404' => false, 00185 'backend' => new FSFileBackend( array( 00186 'name' => 'local-backend', 00187 'wikiId' => wfWikiId(), 00188 'containerPaths' => array( 00189 'local-public' => wfTempDir() . '/test-repo/public', 00190 'local-thumb' => wfTempDir() . '/test-repo/thumb', 00191 'local-temp' => wfTempDir() . '/test-repo/temp', 00192 'local-deleted' => wfTempDir() . '/test-repo/deleted', 00193 ) 00194 ) ) 00195 ); 00196 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface'; 00197 $wgNamespaceAliases['Image'] = NS_FILE; 00198 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK; 00199 # add a namespace shadowing a interwiki link, to test 00200 # proper precedence when resolving links. (bug 51680) 00201 $wgExtraNamespaces[100] = 'MemoryAlpha'; 00202 00203 // XXX: tests won't run without this (for CACHE_DB) 00204 if ( $wgMainCacheType === CACHE_DB ) { 00205 $wgMainCacheType = CACHE_NONE; 00206 } 00207 if ( $wgMessageCacheType === CACHE_DB ) { 00208 $wgMessageCacheType = CACHE_NONE; 00209 } 00210 if ( $wgParserCacheType === CACHE_DB ) { 00211 $wgParserCacheType = CACHE_NONE; 00212 } 00213 00214 $wgEnableParserCache = false; 00215 DeferredUpdates::clearPendingUpdates(); 00216 $wgMemc = wfGetMainCache(); // checks $wgMainCacheType 00217 $messageMemc = wfGetMessageCacheStorage(); 00218 $parserMemc = wfGetParserCacheStorage(); 00219 00220 $wgUser = new User; 00221 $context = new RequestContext; 00222 $wgLang = $context->getLanguage(); 00223 $wgOut = $context->getOutput(); 00224 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) ); 00225 $wgRequest = $context->getRequest(); 00226 00227 if ( $wgStyleDirectory === false ) { 00228 $wgStyleDirectory = "$IP/skins"; 00229 } 00230 00231 self::setupInterwikis(); 00232 $wgLocalInterwikis = array( 'local', 'mi' ); 00233 // "extra language links" 00234 // see https://gerrit.wikimedia.org/r/111390 00235 array_push( $wgExtraInterlanguageLinkPrefixes, 'mul' ); 00236 } 00237 00247 public static function setupInterwikis() { 00248 # Hack: insert a few Wikipedia in-project interwiki prefixes, 00249 # for testing inter-language links 00250 Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) { 00251 static $testInterwikis = array( 00252 'local' => array( 00253 'iw_url' => 'http://doesnt.matter.org/$1', 00254 'iw_api' => '', 00255 'iw_wikiid' => '', 00256 'iw_local' => 0 ), 00257 'wikipedia' => array( 00258 'iw_url' => 'http://en.wikipedia.org/wiki/$1', 00259 'iw_api' => '', 00260 'iw_wikiid' => '', 00261 'iw_local' => 0 ), 00262 'meatball' => array( 00263 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1', 00264 'iw_api' => '', 00265 'iw_wikiid' => '', 00266 'iw_local' => 0 ), 00267 'memoryalpha' => array( 00268 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1', 00269 'iw_api' => '', 00270 'iw_wikiid' => '', 00271 'iw_local' => 0 ), 00272 'zh' => array( 00273 'iw_url' => 'http://zh.wikipedia.org/wiki/$1', 00274 'iw_api' => '', 00275 'iw_wikiid' => '', 00276 'iw_local' => 1 ), 00277 'es' => array( 00278 'iw_url' => 'http://es.wikipedia.org/wiki/$1', 00279 'iw_api' => '', 00280 'iw_wikiid' => '', 00281 'iw_local' => 1 ), 00282 'fr' => array( 00283 'iw_url' => 'http://fr.wikipedia.org/wiki/$1', 00284 'iw_api' => '', 00285 'iw_wikiid' => '', 00286 'iw_local' => 1 ), 00287 'ru' => array( 00288 'iw_url' => 'http://ru.wikipedia.org/wiki/$1', 00289 'iw_api' => '', 00290 'iw_wikiid' => '', 00291 'iw_local' => 1 ), 00292 'mi' => array( 00293 'iw_url' => 'http://mi.wikipedia.org/wiki/$1', 00294 'iw_api' => '', 00295 'iw_wikiid' => '', 00296 'iw_local' => 1 ), 00297 'mul' => array( 00298 'iw_url' => 'http://wikisource.org/wiki/$1', 00299 'iw_api' => '', 00300 'iw_wikiid' => '', 00301 'iw_local' => 1 ), 00302 ); 00303 if ( array_key_exists( $prefix, $testInterwikis ) ) { 00304 $iwData = $testInterwikis[$prefix]; 00305 } 00306 00307 // We only want to rely on the above fixtures 00308 return false; 00309 } );// hooks::register 00310 } 00311 00315 public static function tearDownInterwikis() { 00316 Hooks::clear( 'InterwikiLoadPrefix' ); 00317 } 00318 00319 public function setupRecorder( $options ) { 00320 if ( isset( $options['record'] ) ) { 00321 $this->recorder = new DbTestRecorder( $this ); 00322 $this->recorder->version = isset( $options['setversion'] ) ? 00323 $options['setversion'] : SpecialVersion::getVersion(); 00324 } elseif ( isset( $options['compare'] ) ) { 00325 $this->recorder = new DbTestPreviewer( $this ); 00326 } else { 00327 $this->recorder = new TestRecorder( $this ); 00328 } 00329 } 00330 00337 public static function chomp( $s ) { 00338 if ( substr( $s, -1 ) === "\n" ) { 00339 return substr( $s, 0, -1 ); 00340 } else { 00341 return $s; 00342 } 00343 } 00344 00350 function fuzzTest( $filenames ) { 00351 $GLOBALS['wgContLang'] = Language::factory( 'en' ); 00352 $dict = $this->getFuzzInput( $filenames ); 00353 $dictSize = strlen( $dict ); 00354 $logMaxLength = log( $this->maxFuzzTestLength ); 00355 $this->setupDatabase(); 00356 ini_set( 'memory_limit', $this->memoryLimit * 1048576 ); 00357 00358 $numTotal = 0; 00359 $numSuccess = 0; 00360 $user = new User; 00361 $opts = ParserOptions::newFromUser( $user ); 00362 $title = Title::makeTitle( NS_MAIN, 'Parser_test' ); 00363 00364 while ( true ) { 00365 // Generate test input 00366 mt_srand( ++$this->fuzzSeed ); 00367 $totalLength = mt_rand( 1, $this->maxFuzzTestLength ); 00368 $input = ''; 00369 00370 while ( strlen( $input ) < $totalLength ) { 00371 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength; 00372 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize ); 00373 $offset = mt_rand( 0, $dictSize - $hairLength ); 00374 $input .= substr( $dict, $offset, $hairLength ); 00375 } 00376 00377 $this->setupGlobals(); 00378 $parser = $this->getParser(); 00379 00380 // Run the test 00381 try { 00382 $parser->parse( $input, $title, $opts ); 00383 $fail = false; 00384 } catch ( Exception $exception ) { 00385 $fail = true; 00386 } 00387 00388 if ( $fail ) { 00389 echo "Test failed with seed {$this->fuzzSeed}\n"; 00390 echo "Input:\n"; 00391 printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input ); 00392 echo "$exception\n"; 00393 } else { 00394 $numSuccess++; 00395 } 00396 00397 $numTotal++; 00398 $this->teardownGlobals(); 00399 $parser->__destruct(); 00400 00401 if ( $numTotal % 100 == 0 ) { 00402 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 ); 00403 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n"; 00404 if ( $usage > 90 ) { 00405 echo "Out of memory:\n"; 00406 $memStats = $this->getMemoryBreakdown(); 00407 00408 foreach ( $memStats as $name => $usage ) { 00409 echo "$name: $usage\n"; 00410 } 00411 $this->abort(); 00412 } 00413 } 00414 } 00415 } 00416 00422 function getFuzzInput( $filenames ) { 00423 $dict = ''; 00424 00425 foreach ( $filenames as $filename ) { 00426 $contents = file_get_contents( $filename ); 00427 preg_match_all( 00428 '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s', 00429 $contents, 00430 $matches 00431 ); 00432 00433 foreach ( $matches[1] as $match ) { 00434 $dict .= $match . "\n"; 00435 } 00436 } 00437 00438 return $dict; 00439 } 00440 00445 function getMemoryBreakdown() { 00446 $memStats = array(); 00447 00448 foreach ( $GLOBALS as $name => $value ) { 00449 $memStats['$' . $name] = strlen( serialize( $value ) ); 00450 } 00451 00452 $classes = get_declared_classes(); 00453 00454 foreach ( $classes as $class ) { 00455 $rc = new ReflectionClass( $class ); 00456 $props = $rc->getStaticProperties(); 00457 $memStats[$class] = strlen( serialize( $props ) ); 00458 $methods = $rc->getMethods(); 00459 00460 foreach ( $methods as $method ) { 00461 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) ); 00462 } 00463 } 00464 00465 $functions = get_defined_functions(); 00466 00467 foreach ( $functions['user'] as $function ) { 00468 $rf = new ReflectionFunction( $function ); 00469 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) ); 00470 } 00471 00472 asort( $memStats ); 00473 00474 return $memStats; 00475 } 00476 00477 function abort() { 00478 $this->abort(); 00479 } 00480 00492 public function runTestsFromFiles( $filenames ) { 00493 $ok = false; 00494 00495 // be sure, ParserTest::addArticle has correct language set, 00496 // so that system messages gets into the right language cache 00497 $GLOBALS['wgLanguageCode'] = 'en'; 00498 $GLOBALS['wgContLang'] = Language::factory( 'en' ); 00499 00500 $this->recorder->start(); 00501 try { 00502 $this->setupDatabase(); 00503 $ok = true; 00504 00505 foreach ( $filenames as $filename ) { 00506 $tests = new TestFileIterator( $filename, $this ); 00507 $ok = $this->runTests( $tests ) && $ok; 00508 } 00509 00510 $this->teardownDatabase(); 00511 $this->recorder->report(); 00512 } catch ( DBError $e ) { 00513 echo $e->getMessage(); 00514 } 00515 $this->recorder->end(); 00516 00517 return $ok; 00518 } 00519 00520 function runTests( $tests ) { 00521 $ok = true; 00522 00523 foreach ( $tests as $t ) { 00524 $result = 00525 $this->runTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] ); 00526 $ok = $ok && $result; 00527 $this->recorder->record( $t['test'], $result ); 00528 } 00529 00530 if ( $this->showProgress ) { 00531 print "\n"; 00532 } 00533 00534 return $ok; 00535 } 00536 00543 function getParser( $preprocessor = null ) { 00544 global $wgParserConf; 00545 00546 $class = $wgParserConf['class']; 00547 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf ); 00548 00549 foreach ( $this->hooks as $tag => $callback ) { 00550 $parser->setHook( $tag, $callback ); 00551 } 00552 00553 foreach ( $this->functionHooks as $tag => $bits ) { 00554 list( $callback, $flags ) = $bits; 00555 $parser->setFunctionHook( $tag, $callback, $flags ); 00556 } 00557 00558 foreach ( $this->transparentHooks as $tag => $callback ) { 00559 $parser->setTransparentTagHook( $tag, $callback ); 00560 } 00561 00562 wfRunHooks( 'ParserTestParser', array( &$parser ) ); 00563 00564 return $parser; 00565 } 00566 00579 public function runTest( $desc, $input, $result, $opts, $config ) { 00580 if ( $this->showProgress ) { 00581 $this->showTesting( $desc ); 00582 } 00583 00584 $opts = $this->parseOptions( $opts ); 00585 $context = $this->setupGlobals( $opts, $config ); 00586 00587 $user = $context->getUser(); 00588 $options = ParserOptions::newFromContext( $context ); 00589 00590 if ( isset( $opts['djvu'] ) ) { 00591 if ( !$this->djVuSupport->isEnabled() ) { 00592 return $this->showSkipped(); 00593 } 00594 } 00595 00596 if ( isset( $opts['title'] ) ) { 00597 $titleText = $opts['title']; 00598 } else { 00599 $titleText = 'Parser test'; 00600 } 00601 00602 $local = isset( $opts['local'] ); 00603 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null; 00604 $parser = $this->getParser( $preprocessor ); 00605 $title = Title::newFromText( $titleText ); 00606 00607 if ( isset( $opts['pst'] ) ) { 00608 $out = $parser->preSaveTransform( $input, $title, $user, $options ); 00609 } elseif ( isset( $opts['msg'] ) ) { 00610 $out = $parser->transformMsg( $input, $options, $title ); 00611 } elseif ( isset( $opts['section'] ) ) { 00612 $section = $opts['section']; 00613 $out = $parser->getSection( $input, $section ); 00614 } elseif ( isset( $opts['replace'] ) ) { 00615 $section = $opts['replace'][0]; 00616 $replace = $opts['replace'][1]; 00617 $out = $parser->replaceSection( $input, $section, $replace ); 00618 } elseif ( isset( $opts['comment'] ) ) { 00619 $out = Linker::formatComment( $input, $title, $local ); 00620 } elseif ( isset( $opts['preload'] ) ) { 00621 $out = $parser->getPreloadText( $input, $title, $options ); 00622 } else { 00623 $output = $parser->parse( $input, $title, $options, true, true, 1337 ); 00624 $output->setTOCEnabled( !isset( $opts['notoc'] ) ); 00625 $out = $output->getText(); 00626 if ( isset( $opts['tidy'] ) ) { 00627 if ( !$this->tidySupport->isEnabled() ) { 00628 return $this->showSkipped(); 00629 } 00630 $out = MWTidy::tidy( $out ); 00631 $out = preg_replace( '/\s+$/', '', $out ); 00632 } 00633 00634 if ( isset( $opts['showtitle'] ) ) { 00635 if ( $output->getTitleText() ) { 00636 $title = $output->getTitleText(); 00637 } 00638 00639 $out = "$title\n$out"; 00640 } 00641 00642 if ( isset( $opts['ill'] ) ) { 00643 $out = implode( ' ', $output->getLanguageLinks() ); 00644 } elseif ( isset( $opts['cat'] ) ) { 00645 $outputPage = $context->getOutput(); 00646 $outputPage->addCategoryLinks( $output->getCategories() ); 00647 $cats = $outputPage->getCategoryLinks(); 00648 00649 if ( isset( $cats['normal'] ) ) { 00650 $out = implode( ' ', $cats['normal'] ); 00651 } else { 00652 $out = ''; 00653 } 00654 } 00655 } 00656 00657 $this->teardownGlobals(); 00658 00659 $testResult = new ParserTestResult( $desc ); 00660 $testResult->expected = $result; 00661 $testResult->actual = $out; 00662 00663 return $this->showTestResult( $testResult ); 00664 } 00665 00671 function showTestResult( ParserTestResult $testResult ) { 00672 if ( $testResult->isSuccess() ) { 00673 $this->showSuccess( $testResult ); 00674 return true; 00675 } else { 00676 $this->showFailure( $testResult ); 00677 return false; 00678 } 00679 } 00680 00688 private static function getOptionValue( $key, $opts, $default ) { 00689 $key = strtolower( $key ); 00690 00691 if ( isset( $opts[$key] ) ) { 00692 return $opts[$key]; 00693 } else { 00694 return $default; 00695 } 00696 } 00697 00698 private function parseOptions( $instring ) { 00699 $opts = array(); 00700 // foo 00701 // foo=bar 00702 // foo="bar baz" 00703 // foo=[[bar baz]] 00704 // foo=bar,"baz quux" 00705 // foo={...json...} 00706 $defs = '(?(DEFINE) 00707 (?<qstr> # Quoted string 00708 " 00709 (?:[^\\\\"] | \\\\.)* 00710 " 00711 ) 00712 (?<json> 00713 \{ # Open bracket 00714 (?: 00715 [^"{}] | # Not a quoted string or object, or 00716 (?&qstr) | # A quoted string, or 00717 (?&json) # A json object (recursively) 00718 )* 00719 \} # Close bracket 00720 ) 00721 (?<value> 00722 (?: 00723 (?&qstr) # Quoted val 00724 | 00725 \[\[ 00726 [^]]* # Link target 00727 \]\] 00728 | 00729 [\w-]+ # Plain word 00730 | 00731 (?&json) # JSON object 00732 ) 00733 ) 00734 )'; 00735 $regex = '/' . $defs . '\b 00736 (?<k>[\w-]+) # Key 00737 \b 00738 (?:\s* 00739 = # First sub-value 00740 \s* 00741 (?<v> 00742 (?&value) 00743 (?:\s* 00744 , # Sub-vals 1..N 00745 \s* 00746 (?&value) 00747 )* 00748 ) 00749 )? 00750 /x'; 00751 $valueregex = '/' . $defs . '(?&value)/x'; 00752 00753 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) { 00754 foreach ( $matches as $bits ) { 00755 $key = strtolower( $bits['k'] ); 00756 if ( !isset( $bits['v'] ) ) { 00757 $opts[$key] = true; 00758 } else { 00759 preg_match_all( $valueregex, $bits['v'], $vmatches ); 00760 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $vmatches[0] ); 00761 if ( count( $opts[$key] ) == 1 ) { 00762 $opts[$key] = $opts[$key][0]; 00763 } 00764 } 00765 } 00766 } 00767 return $opts; 00768 } 00769 00770 private function cleanupOption( $opt ) { 00771 if ( substr( $opt, 0, 1 ) == '"' ) { 00772 return stripcslashes( substr( $opt, 1, -1 ) ); 00773 } 00774 00775 if ( substr( $opt, 0, 2 ) == '[[' ) { 00776 return substr( $opt, 2, -2 ); 00777 } 00778 00779 if ( substr( $opt, 0, 1 ) == '{' ) { 00780 return FormatJson::decode( $opt, true ); 00781 } 00782 return $opt; 00783 } 00784 00792 private function setupGlobals( $opts = '', $config = '' ) { 00793 global $IP; 00794 00795 # Find out values for some special options. 00796 $lang = 00797 self::getOptionValue( 'language', $opts, 'en' ); 00798 $variant = 00799 self::getOptionValue( 'variant', $opts, false ); 00800 $maxtoclevel = 00801 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 ); 00802 $linkHolderBatchSize = 00803 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 ); 00804 00805 $settings = array( 00806 'wgServer' => 'http://example.org', 00807 'wgServerName' => 'example.org', 00808 'wgScript' => '/index.php', 00809 'wgScriptPath' => '/', 00810 'wgArticlePath' => '/wiki/$1', 00811 'wgActionPaths' => array(), 00812 'wgLockManagers' => array( array( 00813 'name' => 'fsLockManager', 00814 'class' => 'FSLockManager', 00815 'lockDirectory' => $this->uploadDir . '/lockdir', 00816 ), array( 00817 'name' => 'nullLockManager', 00818 'class' => 'NullLockManager', 00819 ) ), 00820 'wgLocalFileRepo' => array( 00821 'class' => 'LocalRepo', 00822 'name' => 'local', 00823 'url' => 'http://example.com/images', 00824 'hashLevels' => 2, 00825 'transformVia404' => false, 00826 'backend' => new FSFileBackend( array( 00827 'name' => 'local-backend', 00828 'wikiId' => wfWikiId(), 00829 'containerPaths' => array( 00830 'local-public' => $this->uploadDir, 00831 'local-thumb' => $this->uploadDir . '/thumb', 00832 'local-temp' => $this->uploadDir . '/temp', 00833 'local-deleted' => $this->uploadDir . '/delete', 00834 ) 00835 ) ) 00836 ), 00837 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ), 00838 'wgUploadNavigationUrl' => false, 00839 'wgStylePath' => '/skins', 00840 'wgSitename' => 'MediaWiki', 00841 'wgLanguageCode' => $lang, 00842 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'parsertest_' : 'pt_', 00843 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ), 00844 'wgLang' => null, 00845 'wgContLang' => null, 00846 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ), 00847 'wgMaxTocLevel' => $maxtoclevel, 00848 'wgCapitalLinks' => true, 00849 'wgNoFollowLinks' => true, 00850 'wgNoFollowDomainExceptions' => array(), 00851 'wgThumbnailScriptPath' => false, 00852 'wgUseImageResize' => true, 00853 'wgSVGConverter' => 'null', 00854 'wgSVGConverters' => array( 'null' => 'echo "1">$output' ), 00855 'wgLocaltimezone' => 'UTC', 00856 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ), 00857 'wgThumbLimits' => array( self::getOptionValue( 'thumbsize', $opts, 180 ) ), 00858 'wgDefaultLanguageVariant' => $variant, 00859 'wgVariantArticlePath' => false, 00860 'wgGroupPermissions' => array( '*' => array( 00861 'createaccount' => true, 00862 'read' => true, 00863 'edit' => true, 00864 'createpage' => true, 00865 'createtalk' => true, 00866 ) ), 00867 'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ), 00868 'wgDefaultExternalStore' => array(), 00869 'wgForeignFileRepos' => array(), 00870 'wgLinkHolderBatchSize' => $linkHolderBatchSize, 00871 'wgExperimentalHtmlIds' => false, 00872 'wgExternalLinkTarget' => false, 00873 'wgHtml5' => true, 00874 'wgWellFormedXml' => true, 00875 'wgAllowMicrodataAttributes' => true, 00876 'wgAdaptiveMessageCache' => true, 00877 'wgDisableLangConversion' => false, 00878 'wgDisableTitleConversion' => false, 00879 // Tidy options. 00880 // We always set 'wgUseTidy' to false when parsing, but certain 00881 // test-running modes still use tidy if available, so ensure 00882 // that the tidy-related options are all set to their defaults. 00883 'wgUseTidy' => false, 00884 'wgAlwaysUseTidy' => false, 00885 'wgDebugTidy' => false, 00886 'wgTidyConf' => $IP . '/includes/tidy.conf', 00887 'wgTidyOpts' => '', 00888 'wgTidyInternal' => $this->tidySupport->isInternal(), 00889 ); 00890 00891 if ( $config ) { 00892 $configLines = explode( "\n", $config ); 00893 00894 foreach ( $configLines as $line ) { 00895 list( $var, $value ) = explode( '=', $line, 2 ); 00896 00897 $settings[$var] = eval( "return $value;" ); 00898 } 00899 } 00900 00901 $this->savedGlobals = array(); 00902 00904 wfRunHooks( 'ParserTestGlobals', array( &$settings ) ); 00905 00906 foreach ( $settings as $var => $val ) { 00907 if ( array_key_exists( $var, $GLOBALS ) ) { 00908 $this->savedGlobals[$var] = $GLOBALS[$var]; 00909 } 00910 00911 $GLOBALS[$var] = $val; 00912 } 00913 00914 $GLOBALS['wgContLang'] = Language::factory( $lang ); 00915 $GLOBALS['wgMemc'] = new EmptyBagOStuff; 00916 00917 $context = new RequestContext(); 00918 $GLOBALS['wgLang'] = $context->getLanguage(); 00919 $GLOBALS['wgOut'] = $context->getOutput(); 00920 $GLOBALS['wgUser'] = $context->getUser(); 00921 00922 // We (re)set $wgThumbLimits to a single-element array above. 00923 $context->getUser()->setOption( 'thumbsize', 0 ); 00924 00925 global $wgHooks; 00926 00927 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup'; 00928 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp'; 00929 00930 MagicWord::clearCache(); 00931 00932 return $context; 00933 } 00934 00940 private function listTables() { 00941 $tables = array( 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions', 00942 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks', 00943 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks', 00944 'site_stats', 'hitcounter', 'ipblocks', 'image', 'oldimage', 00945 'recentchanges', 'watchlist', 'interwiki', 'logging', 00946 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo', 00947 'archive', 'user_groups', 'page_props', 'category', 'msg_resource', 'msg_resource_links' 00948 ); 00949 00950 if ( in_array( $this->db->getType(), array( 'mysql', 'sqlite', 'oracle' ) ) ) { 00951 array_push( $tables, 'searchindex' ); 00952 } 00953 00954 // Allow extensions to add to the list of tables to duplicate; 00955 // may be necessary if they hook into page save or other code 00956 // which will require them while running tests. 00957 wfRunHooks( 'ParserTestTables', array( &$tables ) ); 00958 00959 return $tables; 00960 } 00961 00967 public function setupDatabase() { 00968 global $wgDBprefix; 00969 00970 if ( $this->databaseSetupDone ) { 00971 return; 00972 } 00973 00974 $this->db = wfGetDB( DB_MASTER ); 00975 $dbType = $this->db->getType(); 00976 00977 if ( $wgDBprefix === 'parsertest_' || ( $dbType == 'oracle' && $wgDBprefix === 'pt_' ) ) { 00978 throw new MWException( 'setupDatabase should be called before setupGlobals' ); 00979 } 00980 00981 $this->databaseSetupDone = true; 00982 00983 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892). 00984 # It seems to have been fixed since (r55079?), but regressed at some point before r85701. 00985 # This works around it for now... 00986 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff; 00987 00988 # CREATE TEMPORARY TABLE breaks if there is more than one server 00989 if ( wfGetLB()->getServerCount() != 1 ) { 00990 $this->useTemporaryTables = false; 00991 } 00992 00993 $temporary = $this->useTemporaryTables || $dbType == 'postgres'; 00994 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_'; 00995 00996 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix ); 00997 $this->dbClone->useTemporaryTables( $temporary ); 00998 $this->dbClone->cloneTableStructure(); 00999 01000 if ( $dbType == 'oracle' ) { 01001 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); 01002 # Insert 0 user to prevent FK violations 01003 01004 # Anonymous user 01005 $this->db->insert( 'user', array( 01006 'user_id' => 0, 01007 'user_name' => 'Anonymous' ) ); 01008 } 01009 01010 # Update certain things in site_stats 01011 $this->db->insert( 'site_stats', 01012 array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) ); 01013 01014 # Reinitialise the LocalisationCache to match the database state 01015 Language::getLocalisationCache()->unloadAll(); 01016 01017 # Clear the message cache 01018 MessageCache::singleton()->clear(); 01019 01020 // Remember to update newParserTests.php after changing the below 01021 // (and it uses a slightly different syntax just for teh lulz) 01022 $this->uploadDir = $this->setupUploadDir(); 01023 $user = User::createNew( 'WikiSysop' ); 01024 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) ); 01025 # note that the size/width/height/bits/etc of the file 01026 # are actually set by inspecting the file itself; the arguments 01027 # to recordUpload2 have no effect. That said, we try to make things 01028 # match up so it is less confusing to readers of the code & tests. 01029 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array( 01030 'size' => 7881, 01031 'width' => 1941, 01032 'height' => 220, 01033 'bits' => 8, 01034 'media_type' => MEDIATYPE_BITMAP, 01035 'mime' => 'image/jpeg', 01036 'metadata' => serialize( array() ), 01037 'sha1' => wfBaseConvert( '1', 16, 36, 31 ), 01038 'fileExists' => true 01039 ), $this->db->timestamp( '20010115123500' ), $user ); 01040 01041 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) ); 01042 # again, note that size/width/height below are ignored; see above. 01043 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', array( 01044 'size' => 22589, 01045 'width' => 135, 01046 'height' => 135, 01047 'bits' => 8, 01048 'media_type' => MEDIATYPE_BITMAP, 01049 'mime' => 'image/png', 01050 'metadata' => serialize( array() ), 01051 'sha1' => wfBaseConvert( '2', 16, 36, 31 ), 01052 'fileExists' => true 01053 ), $this->db->timestamp( '20130225203040' ), $user ); 01054 01055 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) ); 01056 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', array( 01057 'size' => 12345, 01058 'width' => 240, 01059 'height' => 180, 01060 'bits' => 0, 01061 'media_type' => MEDIATYPE_DRAWING, 01062 'mime' => 'image/svg+xml', 01063 'metadata' => serialize( array() ), 01064 'sha1' => wfBaseConvert( '', 16, 36, 31 ), 01065 'fileExists' => true 01066 ), $this->db->timestamp( '20010115123500' ), $user ); 01067 01068 # This image will be blacklisted in [[MediaWiki:Bad image list]] 01069 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) ); 01070 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', array( 01071 'size' => 12345, 01072 'width' => 320, 01073 'height' => 240, 01074 'bits' => 24, 01075 'media_type' => MEDIATYPE_BITMAP, 01076 'mime' => 'image/jpeg', 01077 'metadata' => serialize( array() ), 01078 'sha1' => wfBaseConvert( '3', 16, 36, 31 ), 01079 'fileExists' => true 01080 ), $this->db->timestamp( '20010115123500' ), $user ); 01081 01082 # A DjVu file 01083 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) ); 01084 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', array( 01085 'size' => 3249, 01086 'width' => 2480, 01087 'height' => 3508, 01088 'bits' => 0, 01089 'media_type' => MEDIATYPE_BITMAP, 01090 'mime' => 'image/vnd.djvu', 01091 'metadata' => '<?xml version="1.0" ?> 01092 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd"> 01093 <DjVuXML> 01094 <HEAD></HEAD> 01095 <BODY><OBJECT height="3508" width="2480"> 01096 <PARAM name="DPI" value="300" /> 01097 <PARAM name="GAMMA" value="2.2" /> 01098 </OBJECT> 01099 <OBJECT height="3508" width="2480"> 01100 <PARAM name="DPI" value="300" /> 01101 <PARAM name="GAMMA" value="2.2" /> 01102 </OBJECT> 01103 <OBJECT height="3508" width="2480"> 01104 <PARAM name="DPI" value="300" /> 01105 <PARAM name="GAMMA" value="2.2" /> 01106 </OBJECT> 01107 <OBJECT height="3508" width="2480"> 01108 <PARAM name="DPI" value="300" /> 01109 <PARAM name="GAMMA" value="2.2" /> 01110 </OBJECT> 01111 <OBJECT height="3508" width="2480"> 01112 <PARAM name="DPI" value="300" /> 01113 <PARAM name="GAMMA" value="2.2" /> 01114 </OBJECT> 01115 </BODY> 01116 </DjVuXML>', 01117 'sha1' => wfBaseConvert( '', 16, 36, 31 ), 01118 'fileExists' => true 01119 ), $this->db->timestamp( '20010115123600' ), $user ); 01120 } 01121 01122 public function teardownDatabase() { 01123 if ( !$this->databaseSetupDone ) { 01124 $this->teardownGlobals(); 01125 return; 01126 } 01127 $this->teardownUploadDir( $this->uploadDir ); 01128 01129 $this->dbClone->destroy(); 01130 $this->databaseSetupDone = false; 01131 01132 if ( $this->useTemporaryTables ) { 01133 if ( $this->db->getType() == 'sqlite' ) { 01134 # Under SQLite the searchindex table is virtual and need 01135 # to be explicitly destroyed. See bug 29912 01136 # See also MediaWikiTestCase::destroyDB() 01137 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" ); 01138 $this->db->query( "DROP TABLE `parsertest_searchindex`" ); 01139 } 01140 # Don't need to do anything 01141 $this->teardownGlobals(); 01142 return; 01143 } 01144 01145 $tables = $this->listTables(); 01146 01147 foreach ( $tables as $table ) { 01148 if ( $this->db->getType() == 'oracle' ) { 01149 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" ); 01150 } else { 01151 $this->db->query( "DROP TABLE `parsertest_$table`" ); 01152 } 01153 } 01154 01155 if ( $this->db->getType() == 'oracle' ) { 01156 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' ); 01157 } 01158 01159 $this->teardownGlobals(); 01160 } 01161 01168 private function setupUploadDir() { 01169 global $IP; 01170 01171 if ( $this->keepUploads ) { 01172 $dir = wfTempDir() . '/mwParser-images'; 01173 01174 if ( is_dir( $dir ) ) { 01175 return $dir; 01176 } 01177 } else { 01178 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images"; 01179 } 01180 01181 // wfDebug( "Creating upload directory $dir\n" ); 01182 if ( file_exists( $dir ) ) { 01183 wfDebug( "Already exists!\n" ); 01184 return $dir; 01185 } 01186 01187 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ ); 01188 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/3/3a/Foobar.jpg" ); 01189 wfMkdirParents( $dir . '/e/ea', null, __METHOD__ ); 01190 copy( "$IP/tests/phpunit/data/parser/wiki.png", "$dir/e/ea/Thumb.png" ); 01191 wfMkdirParents( $dir . '/0/09', null, __METHOD__ ); 01192 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/0/09/Bad.jpg" ); 01193 wfMkdirParents( $dir . '/f/ff', null, __METHOD__ ); 01194 file_put_contents( "$dir/f/ff/Foobar.svg", 01195 '<?xml version="1.0" encoding="utf-8"?>' . 01196 '<svg xmlns="http://www.w3.org/2000/svg"' . 01197 ' version="1.1" width="240" height="180"/>' ); 01198 wfMkdirParents( $dir . '/5/5f', null, __METHOD__ ); 01199 copy( "$IP/tests/phpunit/data/parser/LoremIpsum.djvu", "$dir/5/5f/LoremIpsum.djvu" ); 01200 01201 return $dir; 01202 } 01203 01208 private function teardownGlobals() { 01209 RepoGroup::destroySingleton(); 01210 FileBackendGroup::destroySingleton(); 01211 LockManagerGroup::destroySingletons(); 01212 LinkCache::singleton()->clear(); 01213 01214 foreach ( $this->savedGlobals as $var => $val ) { 01215 $GLOBALS[$var] = $val; 01216 } 01217 } 01218 01223 private function teardownUploadDir( $dir ) { 01224 if ( $this->keepUploads ) { 01225 return; 01226 } 01227 01228 // delete the files first, then the dirs. 01229 self::deleteFiles( 01230 array( 01231 "$dir/3/3a/Foobar.jpg", 01232 "$dir/thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg", 01233 "$dir/thumb/3/3a/Foobar.jpg/100px-Foobar.jpg", 01234 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg", 01235 "$dir/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg", 01236 "$dir/thumb/3/3a/Foobar.jpg/137px-Foobar.jpg", 01237 "$dir/thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg", 01238 "$dir/thumb/3/3a/Foobar.jpg/177px-Foobar.jpg", 01239 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg", 01240 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg", 01241 "$dir/thumb/3/3a/Foobar.jpg/206px-Foobar.jpg", 01242 "$dir/thumb/3/3a/Foobar.jpg/20px-Foobar.jpg", 01243 "$dir/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg", 01244 "$dir/thumb/3/3a/Foobar.jpg/265px-Foobar.jpg", 01245 "$dir/thumb/3/3a/Foobar.jpg/270px-Foobar.jpg", 01246 "$dir/thumb/3/3a/Foobar.jpg/274px-Foobar.jpg", 01247 "$dir/thumb/3/3a/Foobar.jpg/300px-Foobar.jpg", 01248 "$dir/thumb/3/3a/Foobar.jpg/30px-Foobar.jpg", 01249 "$dir/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg", 01250 "$dir/thumb/3/3a/Foobar.jpg/353px-Foobar.jpg", 01251 "$dir/thumb/3/3a/Foobar.jpg/360px-Foobar.jpg", 01252 "$dir/thumb/3/3a/Foobar.jpg/400px-Foobar.jpg", 01253 "$dir/thumb/3/3a/Foobar.jpg/40px-Foobar.jpg", 01254 "$dir/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg", 01255 "$dir/thumb/3/3a/Foobar.jpg/442px-Foobar.jpg", 01256 "$dir/thumb/3/3a/Foobar.jpg/450px-Foobar.jpg", 01257 "$dir/thumb/3/3a/Foobar.jpg/50px-Foobar.jpg", 01258 "$dir/thumb/3/3a/Foobar.jpg/600px-Foobar.jpg", 01259 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg", 01260 "$dir/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg", 01261 "$dir/thumb/3/3a/Foobar.jpg/75px-Foobar.jpg", 01262 "$dir/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg", 01263 01264 "$dir/e/ea/Thumb.png", 01265 01266 "$dir/0/09/Bad.jpg", 01267 01268 "$dir/5/5f/LoremIpsum.djvu", 01269 "$dir/thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg", 01270 "$dir/thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg", 01271 "$dir/thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg", 01272 01273 "$dir/f/ff/Foobar.svg", 01274 "$dir/thumb/f/ff/Foobar.svg/180px-Foobar.svg.png", 01275 "$dir/thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png", 01276 "$dir/thumb/f/ff/Foobar.svg/270px-Foobar.svg.png", 01277 "$dir/thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png", 01278 "$dir/thumb/f/ff/Foobar.svg/360px-Foobar.svg.png", 01279 "$dir/thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png", 01280 "$dir/thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png", 01281 "$dir/thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png", 01282 "$dir/thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png", 01283 01284 "$dir/math/f/a/5/fa50b8b616463173474302ca3e63586b.png", 01285 ) 01286 ); 01287 01288 self::deleteDirs( 01289 array( 01290 "$dir/3/3a", 01291 "$dir/3", 01292 "$dir/thumb/3/3a/Foobar.jpg", 01293 "$dir/thumb/3/3a", 01294 "$dir/thumb/3", 01295 "$dir/e/ea", 01296 "$dir/e", 01297 "$dir/f/ff/", 01298 "$dir/f/", 01299 "$dir/thumb/f/ff/Foobar.svg", 01300 "$dir/thumb/f/ff/", 01301 "$dir/thumb/f/", 01302 "$dir/0/09/", 01303 "$dir/0/", 01304 "$dir/5/5f", 01305 "$dir/5", 01306 "$dir/thumb/5/5f/LoremIpsum.djvu", 01307 "$dir/thumb/5/5f", 01308 "$dir/thumb/5", 01309 "$dir/thumb", 01310 "$dir/math/f/a/5", 01311 "$dir/math/f/a", 01312 "$dir/math/f", 01313 "$dir/math", 01314 "$dir", 01315 ) 01316 ); 01317 } 01318 01323 private static function deleteFiles( $files ) { 01324 foreach ( $files as $file ) { 01325 if ( file_exists( $file ) ) { 01326 unlink( $file ); 01327 } 01328 } 01329 } 01330 01335 private static function deleteDirs( $dirs ) { 01336 foreach ( $dirs as $dir ) { 01337 if ( is_dir( $dir ) ) { 01338 rmdir( $dir ); 01339 } 01340 } 01341 } 01342 01347 protected function showTesting( $desc ) { 01348 print "Running test $desc... "; 01349 } 01350 01359 protected function showSuccess( ParserTestResult $testResult ) { 01360 if ( $this->showProgress ) { 01361 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n"; 01362 } 01363 01364 return true; 01365 } 01366 01376 protected function showFailure( ParserTestResult $testResult ) { 01377 if ( $this->showFailure ) { 01378 if ( !$this->showProgress ) { 01379 # In quiet mode we didn't show the 'Testing' message before the 01380 # test, in case it succeeded. Show it now: 01381 $this->showTesting( $testResult->description ); 01382 } 01383 01384 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n"; 01385 01386 if ( $this->showOutput ) { 01387 print "--- Expected ---\n{$testResult->expected}\n"; 01388 print "--- Actual ---\n{$testResult->actual}\n"; 01389 } 01390 01391 if ( $this->showDiffs ) { 01392 print $this->quickDiff( $testResult->expected, $testResult->actual ); 01393 if ( !$this->wellFormed( $testResult->actual ) ) { 01394 print "XML error: $this->mXmlError\n"; 01395 } 01396 } 01397 } 01398 01399 return false; 01400 } 01401 01407 protected function showSkipped() { 01408 if ( $this->showProgress ) { 01409 print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n"; 01410 } 01411 01412 return true; 01413 } 01414 01425 protected function quickDiff( $input, $output, 01426 $inFileTail = 'expected', $outFileTail = 'actual' 01427 ) { 01428 # Windows, or at least the fc utility, is retarded 01429 $slash = wfIsWindows() ? '\\' : '/'; 01430 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand(); 01431 01432 $infile = "$prefix-$inFileTail"; 01433 $this->dumpToFile( $input, $infile ); 01434 01435 $outfile = "$prefix-$outFileTail"; 01436 $this->dumpToFile( $output, $outfile ); 01437 01438 $shellInfile = wfEscapeShellArg( $infile ); 01439 $shellOutfile = wfEscapeShellArg( $outfile ); 01440 01441 global $wgDiff3; 01442 // we assume that people with diff3 also have usual diff 01443 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au'; 01444 01445 $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" ); 01446 01447 unlink( $infile ); 01448 unlink( $outfile ); 01449 01450 return $this->colorDiff( $diff ); 01451 } 01452 01459 private function dumpToFile( $data, $filename ) { 01460 $file = fopen( $filename, "wt" ); 01461 fwrite( $file, $data . "\n" ); 01462 fclose( $file ); 01463 } 01464 01472 protected function colorDiff( $text ) { 01473 return preg_replace( 01474 array( '/^(-.*)$/m', '/^(\+.*)$/m' ), 01475 array( $this->term->color( 34 ) . '$1' . $this->term->reset(), 01476 $this->term->color( 31 ) . '$1' . $this->term->reset() ), 01477 $text ); 01478 } 01479 01485 public function showRunFile( $path ) { 01486 print $this->term->color( 1 ) . 01487 "Reading tests from \"$path\"..." . 01488 $this->term->reset() . 01489 "\n"; 01490 } 01491 01499 public static function addArticle( $name, $text, $line = 'unknown', $ignoreDuplicate = '' ) { 01500 global $wgCapitalLinks; 01501 01502 $oldCapitalLinks = $wgCapitalLinks; 01503 $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637 01504 01505 $text = self::chomp( $text ); 01506 $name = self::chomp( $name ); 01507 01508 $title = Title::newFromText( $name ); 01509 01510 if ( is_null( $title ) ) { 01511 throw new MWException( "invalid title '$name' at line $line\n" ); 01512 } 01513 01514 $page = WikiPage::factory( $title ); 01515 $page->loadPageData( 'fromdbmaster' ); 01516 01517 if ( $page->exists() ) { 01518 if ( $ignoreDuplicate == 'ignoreduplicate' ) { 01519 return; 01520 } else { 01521 throw new MWException( "duplicate article '$name' at line $line\n" ); 01522 } 01523 } 01524 01525 $page->doEditContent( ContentHandler::makeContent( $text, $title ), '', EDIT_NEW ); 01526 01527 $wgCapitalLinks = $oldCapitalLinks; 01528 } 01529 01538 public function requireHook( $name ) { 01539 global $wgParser; 01540 01541 $wgParser->firstCallInit(); // make sure hooks are loaded. 01542 01543 if ( isset( $wgParser->mTagHooks[$name] ) ) { 01544 $this->hooks[$name] = $wgParser->mTagHooks[$name]; 01545 } else { 01546 echo " This test suite requires the '$name' hook extension, skipping.\n"; 01547 return false; 01548 } 01549 01550 return true; 01551 } 01552 01561 public function requireFunctionHook( $name ) { 01562 global $wgParser; 01563 01564 $wgParser->firstCallInit(); // make sure hooks are loaded. 01565 01566 if ( isset( $wgParser->mFunctionHooks[$name] ) ) { 01567 $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name]; 01568 } else { 01569 echo " This test suite requires the '$name' function hook extension, skipping.\n"; 01570 return false; 01571 } 01572 01573 return true; 01574 } 01575 01584 public function requireTransparentHook( $name ) { 01585 global $wgParser; 01586 01587 $wgParser->firstCallInit(); // make sure hooks are loaded. 01588 01589 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) { 01590 $this->transparentHooks[$name] = $wgParser->mTransparentTagHooks[$name]; 01591 } else { 01592 echo " This test suite requires the '$name' transparent hook extension, skipping.\n"; 01593 return false; 01594 } 01595 01596 return true; 01597 } 01598 01599 private function wellFormed( $text ) { 01600 $html = 01601 Sanitizer::hackDocType() . 01602 '<html>' . 01603 $text . 01604 '</html>'; 01605 01606 $parser = xml_parser_create( "UTF-8" ); 01607 01608 # case folding violates XML standard, turn it off 01609 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); 01610 01611 if ( !xml_parse( $parser, $html, true ) ) { 01612 $err = xml_error_string( xml_get_error_code( $parser ) ); 01613 $position = xml_get_current_byte_index( $parser ); 01614 $fragment = $this->extractFragment( $html, $position ); 01615 $this->mXmlError = "$err at byte $position:\n$fragment"; 01616 xml_parser_free( $parser ); 01617 01618 return false; 01619 } 01620 01621 xml_parser_free( $parser ); 01622 01623 return true; 01624 } 01625 01626 private function extractFragment( $text, $position ) { 01627 $start = max( 0, $position - 10 ); 01628 $before = $position - $start; 01629 $fragment = '...' . 01630 $this->term->color( 34 ) . 01631 substr( $text, $start, $before ) . 01632 $this->term->color( 0 ) . 01633 $this->term->color( 31 ) . 01634 $this->term->color( 1 ) . 01635 substr( $text, $position, 1 ) . 01636 $this->term->color( 0 ) . 01637 $this->term->color( 34 ) . 01638 substr( $text, $position + 1, 9 ) . 01639 $this->term->color( 0 ) . 01640 '...'; 01641 $display = str_replace( "\n", ' ', $fragment ); 01642 $caret = ' ' . 01643 str_repeat( ' ', $before ) . 01644 $this->term->color( 31 ) . 01645 '^' . 01646 $this->term->color( 0 ); 01647 01648 return "$display\n$caret"; 01649 } 01650 01651 static function getFakeTimestamp( &$parser, &$ts ) { 01652 $ts = 123; //parsed as '1970-01-01T00:02:03Z' 01653 return true; 01654 } 01655 }