00001 <?php
00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00028 $options = array( 'quick', 'color', 'quiet', 'help', 'show-output', 'record' );
00029 $optionsWithArgs = array( 'regex', 'seed' );
00030
00031 require_once( 'commandLine.inc' );
00032 require_once( "$IP/maintenance/parserTestsParserHook.php" );
00033 require_once( "$IP/maintenance/parserTestsStaticParserHook.php" );
00034 require_once( "$IP/maintenance/parserTestsParserTime.php" );
00035
00039 class ParserTest {
00043 private $color;
00044
00048 private $showOutput;
00049
00053 private $useTemporaryTables = true;
00054
00058 private $databaseSetupDone = false;
00059
00063 private $oldTablePrefix;
00064
00065 private $maxFuzzTestLength = 300;
00066 private $fuzzSeed = 0;
00067 private $memoryLimit = 50;
00068
00073 public function ParserTest() {
00074 global $options;
00075
00076 # Only colorize output if stdout is a terminal.
00077 $this->color = !wfIsWindows() && posix_isatty(1);
00078
00079 if( isset( $options['color'] ) ) {
00080 switch( $options['color'] ) {
00081 case 'no':
00082 $this->color = false;
00083 break;
00084 case 'yes':
00085 default:
00086 $this->color = true;
00087 break;
00088 }
00089 }
00090 $this->term = $this->color
00091 ? new AnsiTermColorer()
00092 : new DummyTermColorer();
00093
00094 $this->showDiffs = !isset( $options['quick'] );
00095 $this->showProgress = !isset( $options['quiet'] );
00096 $this->showFailure = !(
00097 isset( $options['quiet'] )
00098 && ( isset( $options['record'] )
00099 || isset( $options['compare'] ) ) );
00100
00101 $this->showOutput = isset( $options['show-output'] );
00102
00103
00104 if (isset($options['regex'])) {
00105 if ( isset( $options['record'] ) ) {
00106 echo "Warning: --record cannot be used with --regex, disabling --record\n";
00107 unset( $options['record'] );
00108 }
00109 $this->regex = $options['regex'];
00110 } else {
00111 # Matches anything
00112 $this->regex = '';
00113 }
00114
00115 if( isset( $options['record'] ) ) {
00116 $this->recorder = new DbTestRecorder( $this );
00117 } elseif( isset( $options['compare'] ) ) {
00118 $this->recorder = new DbTestPreviewer( $this );
00119 } else {
00120 $this->recorder = new TestRecorder( $this );
00121 }
00122 $this->keepUploads = isset( $options['keep-uploads'] );
00123
00124 if ( isset( $options['seed'] ) ) {
00125 $this->fuzzSeed = intval( $options['seed'] ) - 1;
00126 }
00127
00128 $this->hooks = array();
00129 $this->functionHooks = array();
00130 }
00131
00135 private function chomp($s) {
00136 if (substr($s, -1) === "\n") {
00137 return substr($s, 0, -1);
00138 }
00139 else {
00140 return $s;
00141 }
00142 }
00143
00148 function fuzzTest( $filenames ) {
00149 $dict = $this->getFuzzInput( $filenames );
00150 $dictSize = strlen( $dict );
00151 $logMaxLength = log( $this->maxFuzzTestLength );
00152 $this->setupDatabase();
00153 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
00154
00155 $numTotal = 0;
00156 $numSuccess = 0;
00157 $user = new User;
00158 $opts = ParserOptions::newFromUser( $user );
00159 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
00160
00161 while ( true ) {
00162
00163 mt_srand( ++$this->fuzzSeed );
00164 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
00165 $input = '';
00166 while ( strlen( $input ) < $totalLength ) {
00167 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
00168 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
00169 $offset = mt_rand( 0, $dictSize - $hairLength );
00170 $input .= substr( $dict, $offset, $hairLength );
00171 }
00172
00173 $this->setupGlobals();
00174 $parser = $this->getParser();
00175
00176 try {
00177 $parser->parse( $input, $title, $opts );
00178 $fail = false;
00179 } catch ( Exception $exception ) {
00180 $fail = true;
00181 }
00182
00183 if ( $fail ) {
00184 echo "Test failed with seed {$this->fuzzSeed}\n";
00185 echo "Input:\n";
00186 var_dump( $input );
00187 echo "\n\n";
00188 echo "$exception\n";
00189 } else {
00190 $numSuccess++;
00191 }
00192 $numTotal++;
00193 $this->teardownGlobals();
00194 $parser->__destruct();
00195
00196 if ( $numTotal % 100 == 0 ) {
00197 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
00198 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
00199 if ( $usage > 90 ) {
00200 echo "Out of memory:\n";
00201 $memStats = $this->getMemoryBreakdown();
00202 foreach ( $memStats as $name => $usage ) {
00203 echo "$name: $usage\n";
00204 }
00205 $this->abort();
00206 }
00207 }
00208 }
00209 }
00210
00214 function getFuzzInput( $filenames ) {
00215 $dict = '';
00216 foreach( $filenames as $filename ) {
00217 $contents = file_get_contents( $filename );
00218 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
00219 foreach ( $matches[1] as $match ) {
00220 $dict .= $match . "\n";
00221 }
00222 }
00223 return $dict;
00224 }
00225
00229 function getMemoryBreakdown() {
00230 $memStats = array();
00231 foreach ( $GLOBALS as $name => $value ) {
00232 $memStats['$'.$name] = strlen( serialize( $value ) );
00233 }
00234 $classes = get_declared_classes();
00235 foreach ( $classes as $class ) {
00236 $rc = new ReflectionClass( $class );
00237 $props = $rc->getStaticProperties();
00238 $memStats[$class] = strlen( serialize( $props ) );
00239 $methods = $rc->getMethods();
00240 foreach ( $methods as $method ) {
00241 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
00242 }
00243 }
00244 $functions = get_defined_functions();
00245 foreach ( $functions['user'] as $function ) {
00246 $rf = new ReflectionFunction( $function );
00247 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
00248 }
00249 asort( $memStats );
00250 return $memStats;
00251 }
00252
00253 function abort() {
00254 $this->abort();
00255 }
00256
00268 public function runTestsFromFiles( $filenames ) {
00269 $this->recorder->start();
00270 $this->setupDatabase();
00271 $ok = true;
00272 foreach( $filenames as $filename ) {
00273 $ok = $this->runFile( $filename ) && $ok;
00274 }
00275 $this->teardownDatabase();
00276 $this->recorder->report();
00277 $this->recorder->end();
00278 return $ok;
00279 }
00280
00281 private function runFile( $filename ) {
00282 $infile = fopen( $filename, 'rt' );
00283 if( !$infile ) {
00284 wfDie( "Couldn't open $filename\n" );
00285 } else {
00286 global $IP;
00287 $relative = wfRelativePath( $filename, $IP );
00288 $this->showRunFile( $relative );
00289 }
00290
00291 $data = array();
00292 $section = null;
00293 $n = 0;
00294 $ok = true;
00295 while( false !== ($line = fgets( $infile ) ) ) {
00296 $n++;
00297 $matches = array();
00298 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
00299 $section = strtolower( $matches[1] );
00300 if( $section == 'endarticle') {
00301 if( !isset( $data['text'] ) ) {
00302 wfDie( "'endarticle' without 'text' at line $n of $filename\n" );
00303 }
00304 if( !isset( $data['article'] ) ) {
00305 wfDie( "'endarticle' without 'article' at line $n of $filename\n" );
00306 }
00307 $this->addArticle($this->chomp($data['article']), $this->chomp($data['text']), $n);
00308 $data = array();
00309 $section = null;
00310 continue;
00311 }
00312 if( $section == 'endhooks' ) {
00313 if( !isset( $data['hooks'] ) ) {
00314 wfDie( "'endhooks' without 'hooks' at line $n of $filename\n" );
00315 }
00316 foreach( explode( "\n", $data['hooks'] ) as $line ) {
00317 $line = trim( $line );
00318 if( $line ) {
00319 $this->requireHook( $line );
00320 }
00321 }
00322 $data = array();
00323 $section = null;
00324 continue;
00325 }
00326 if( $section == 'endfunctionhooks' ) {
00327 if( !isset( $data['functionhooks'] ) ) {
00328 wfDie( "'endfunctionhooks' without 'functionhooks' at line $n of $filename\n" );
00329 }
00330 foreach( explode( "\n", $data['functionhooks'] ) as $line ) {
00331 $line = trim( $line );
00332 if( $line ) {
00333 $this->requireFunctionHook( $line );
00334 }
00335 }
00336 $data = array();
00337 $section = null;
00338 continue;
00339 }
00340 if( $section == 'end' ) {
00341 if( !isset( $data['test'] ) ) {
00342 wfDie( "'end' without 'test' at line $n of $filename\n" );
00343 }
00344 if( !isset( $data['input'] ) ) {
00345 wfDie( "'end' without 'input' at line $n of $filename\n" );
00346 }
00347 if( !isset( $data['result'] ) ) {
00348 wfDie( "'end' without 'result' at line $n of $filename\n" );
00349 }
00350 if( !isset( $data['options'] ) ) {
00351 $data['options'] = '';
00352 }
00353 else {
00354 $data['options'] = $this->chomp( $data['options'] );
00355 }
00356 if (!isset( $data['config'] ) )
00357 $data['config'] = '';
00358
00359 if (preg_match('/\\bdisabled\\b/i', $data['options'])
00360 || !preg_match("/{$this->regex}/i", $data['test'])) {
00361 # disabled test
00362 $data = array();
00363 $section = null;
00364 continue;
00365 }
00366 $result = $this->runTest(
00367 $this->chomp( $data['test'] ),
00368 $this->chomp( $data['input'] ),
00369 $this->chomp( $data['result'] ),
00370 $this->chomp( $data['options'] ),
00371 $this->chomp( $data['config'] )
00372 );
00373 $ok = $ok && $result;
00374 $this->recorder->record( $this->chomp( $data['test'] ), $result );
00375 $data = array();
00376 $section = null;
00377 continue;
00378 }
00379 if ( isset ($data[$section] ) ) {
00380 wfDie( "duplicate section '$section' at line $n of $filename\n" );
00381 }
00382 $data[$section] = '';
00383 continue;
00384 }
00385 if( $section ) {
00386 $data[$section] .= $line;
00387 }
00388 }
00389 if ( $this->showProgress ) {
00390 print "\n";
00391 }
00392 return $ok;
00393 }
00394
00398 function getParser() {
00399 global $wgParserConf;
00400 $class = $wgParserConf['class'];
00401 $parser = new $class( $wgParserConf );
00402 foreach( $this->hooks as $tag => $callback ) {
00403 $parser->setHook( $tag, $callback );
00404 }
00405 foreach( $this->functionHooks as $tag => $bits ) {
00406 list( $callback, $flags ) = $bits;
00407 $parser->setFunctionHook( $tag, $callback, $flags );
00408 }
00409 wfRunHooks( 'ParserTestParser', array( &$parser ) );
00410 return $parser;
00411 }
00412
00422 private function runTest( $desc, $input, $result, $opts, $config ) {
00423 if( $this->showProgress ) {
00424 $this->showTesting( $desc );
00425 }
00426
00427 $this->setupGlobals($opts, $config);
00428
00429 $user = new User();
00430 $options = ParserOptions::newFromUser( $user );
00431
00432 if (preg_match('/\\bmath\\b/i', $opts)) {
00433 # XXX this should probably be done by the ParserOptions
00434 $options->setUseTex(true);
00435 }
00436
00437 $m = array();
00438 if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) {
00439 $titleText = $m[1];
00440 }
00441 else {
00442 $titleText = 'Parser test';
00443 }
00444
00445 $noxml = (bool)preg_match( '~\\b noxml \\b~x', $opts );
00446 $parser = $this->getParser();
00447 $title =& Title::makeTitle( NS_MAIN, $titleText );
00448
00449 $matches = array();
00450 if (preg_match('/\\bpst\\b/i', $opts)) {
00451 $out = $parser->preSaveTransform( $input, $title, $user, $options );
00452 } elseif (preg_match('/\\bmsg\\b/i', $opts)) {
00453 $out = $parser->transformMsg( $input, $options );
00454 } elseif( preg_match( '/\\bsection=([\w-]+)\b/i', $opts, $matches ) ) {
00455 $section = $matches[1];
00456 $out = $parser->getSection( $input, $section );
00457 } elseif( preg_match( '/\\breplace=([\w-]+),"(.*?)"/i', $opts, $matches ) ) {
00458 $section = $matches[1];
00459 $replace = $matches[2];
00460 $out = $parser->replaceSection( $input, $section, $replace );
00461 } else {
00462 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
00463 $out = $output->getText();
00464
00465 if (preg_match('/\\bill\\b/i', $opts)) {
00466 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
00467 } else if (preg_match('/\\bcat\\b/i', $opts)) {
00468 global $wgOut;
00469 $wgOut->addCategoryLinks($output->getCategories());
00470 $cats = $wgOut->getCategoryLinks();
00471 if ( isset( $cats['normal'] ) ) {
00472 $out = $this->tidy( implode( ' ', $cats['normal'] ) );
00473 } else {
00474 $out = '';
00475 }
00476 }
00477
00478 $result = $this->tidy($result);
00479 }
00480
00481 $this->teardownGlobals();
00482
00483 if( $result === $out && ( $noxml === true || $this->wellFormed( $out ) ) ) {
00484 return $this->showSuccess( $desc );
00485 } else {
00486 return $this->showFailure( $desc, $result, $out );
00487 }
00488 }
00489
00490
00497 private static function getOptionValue( $regex, $opts, $default ) {
00498 $m = array();
00499 if( preg_match( $regex, $opts, $m ) ) {
00500 return $m[1];
00501 } else {
00502 return $default;
00503 }
00504 }
00505
00510 private function setupGlobals($opts = '', $config = '') {
00511 if( !isset( $this->uploadDir ) ) {
00512 $this->uploadDir = $this->setupUploadDir();
00513 }
00514
00515 # Find out values for some special options.
00516 $lang =
00517 self::getOptionValue( '/language=([a-z]+(?:_[a-z]+)?)/', $opts, 'en' );
00518 $variant =
00519 self::getOptionValue( '/variant=([a-z]+(?:-[a-z]+)?)/', $opts, false );
00520 $maxtoclevel =
00521 self::getOptionValue( '/wgMaxTocLevel=(\d+)/', $opts, 999 );
00522 $linkHolderBatchSize =
00523 self::getOptionValue( '/wgLinkHolderBatchSize=(\d+)/', $opts, 1000 );
00524
00525 $settings = array(
00526 'wgServer' => 'http://localhost',
00527 'wgScript' => '/index.php',
00528 'wgScriptPath' => '/',
00529 'wgArticlePath' => '/wiki/$1',
00530 'wgActionPaths' => array(),
00531 'wgLocalFileRepo' => array(
00532 'class' => 'LocalRepo',
00533 'name' => 'local',
00534 'directory' => $this->uploadDir,
00535 'url' => 'http://example.com/images',
00536 'hashLevels' => 2,
00537 'transformVia404' => false,
00538 ),
00539 'wgEnableUploads' => true,
00540 'wgStyleSheetPath' => '/skins',
00541 'wgSitename' => 'MediaWiki',
00542 'wgServerName' => 'Britney Spears',
00543 'wgLanguageCode' => $lang,
00544 'wgContLanguageCode' => $lang,
00545 'wgDBprefix' => 'parsertest_',
00546 'wgRawHtml' => preg_match('/\\brawhtml\\b/i', $opts),
00547 'wgLang' => null,
00548 'wgContLang' => null,
00549 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
00550 'wgMaxTocLevel' => $maxtoclevel,
00551 'wgCapitalLinks' => true,
00552 'wgNoFollowLinks' => true,
00553 'wgNoFollowDomainExceptions' => array(),
00554 'wgThumbnailScriptPath' => false,
00555 'wgUseTeX' => false,
00556 'wgLocaltimezone' => 'UTC',
00557 'wgAllowExternalImages' => true,
00558 'wgUseTidy' => false,
00559 'wgDefaultLanguageVariant' => $variant,
00560 'wgVariantArticlePath' => false,
00561 'wgGroupPermissions' => array( '*' => array(
00562 'createaccount' => true,
00563 'read' => true,
00564 'edit' => true,
00565 'createpage' => true,
00566 'createtalk' => true,
00567 ) ),
00568 'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ),
00569 'wgDefaultExternalStore' => array(),
00570 'wgForeignFileRepos' => array(),
00571 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
00572 'wgEnforceHtmlIds' => true,
00573 'wgExternalLinkTarget' => false,
00574 'wgAlwaysUseTidy' => false,
00575 );
00576
00577 if ($config) {
00578 $configLines = explode( "\n", $config );
00579
00580 foreach( $configLines as $line ) {
00581 list( $var, $value ) = explode( '=', $line, 2 );
00582
00583 $settings[$var] = eval("return $value;" );
00584 }
00585 }
00586
00587 $this->savedGlobals = array();
00588 foreach( $settings as $var => $val ) {
00589 $this->savedGlobals[$var] = $GLOBALS[$var];
00590 $GLOBALS[$var] = $val;
00591 }
00592 $langObj = Language::factory( $lang );
00593 $GLOBALS['wgLang'] = $langObj;
00594 $GLOBALS['wgContLang'] = $langObj;
00595 $GLOBALS['wgMemc'] = new FakeMemCachedClient;
00596
00597
00598
00599 global $wgUser;
00600 $wgUser = new User();
00601 }
00602
00607 private function listTables() {
00608 global $wgDBtype;
00609 $tables = array('user', 'page', 'page_restrictions',
00610 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
00611 'categorylinks', 'templatelinks', 'externallinks', 'langlinks',
00612 'site_stats', 'hitcounter', 'ipblocks', 'image', 'oldimage',
00613 'recentchanges', 'watchlist', 'math', 'interwiki',
00614 'querycache', 'objectcache', 'job', 'redirect', 'querycachetwo',
00615 'archive', 'user_groups', 'page_props', 'category'
00616 );
00617
00618 if ($wgDBtype === 'mysql')
00619 array_push( $tables, 'searchindex' );
00620
00621
00622
00623
00624 wfRunHooks( 'ParserTestTables', array( &$tables ) );
00625
00626 return $tables;
00627 }
00628
00634 private function setupDatabase() {
00635 global $wgDBprefix, $wgDBtype;
00636 if ( $this->databaseSetupDone ) {
00637 return;
00638 }
00639 if ( $wgDBprefix === 'parsertest_' ) {
00640 throw new MWException( 'setupDatabase should be called before setupGlobals' );
00641 }
00642 $this->databaseSetupDone = true;
00643 $this->oldTablePrefix = $wgDBprefix;
00644
00645 # CREATE TEMPORARY TABLE breaks if there is more than one server
00646 # FIXME: r40209 makes temporary tables break even with just one server
00647 # FIXME: (bug 15892); disabling the feature entirely as a temporary fix
00648 if ( true || wfGetLB()->getServerCount() != 1 ) {
00649 $this->useTemporaryTables = false;
00650 }
00651
00652 $temporary = $this->useTemporaryTables ? 'TEMPORARY' : '';
00653
00654 $db = wfGetDB( DB_MASTER );
00655 $tables = $this->listTables();
00656
00657 if ( !( $wgDBtype == 'mysql' && strcmp( $db->getServerVersion(), '4.1' ) < 0 ) ) {
00658 # Database that supports CREATE TABLE ... LIKE
00659
00660 if( $wgDBtype == 'postgres' ) {
00661 $def = 'INCLUDING DEFAULTS';
00662 $temporary = 'TEMPORARY';
00663 } else {
00664 $def = '';
00665 }
00666 foreach ( $tables as $tbl ) {
00667 # Clean up from previous aborted run. So that table escaping
00668 # works correctly across DB engines, we need to change the pre-
00669 # fix back and forth so tableName() works right.
00670 $this->changePrefix( $this->oldTablePrefix );
00671 $oldTableName = $db->tableName( $tbl );
00672 $this->changePrefix( 'parsertest_' );
00673 $newTableName = $db->tableName( $tbl );
00674
00675 if ( $db->tableExists( $tbl ) && $wgDBtype != 'postgres' ) {
00676 $db->query( "DROP TABLE $newTableName" );
00677 }
00678 # Create new table
00679 $db->query( "CREATE $temporary TABLE $newTableName (LIKE $oldTableName $def)" );
00680 }
00681 } else {
00682 # Hack for MySQL versions < 4.1, which don't support
00683 # "CREATE TABLE ... LIKE". Note that
00684 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
00685 # would not create the indexes we need....
00686 #
00687 # Note that we don't bother changing around the prefixes here be-
00688 # cause we know we're using MySQL anyway.
00689 foreach ($tables as $tbl) {
00690 $oldTableName = $db->tableName( $tbl );
00691 $res = $db->query("SHOW CREATE TABLE $oldTableName");
00692 $row = $db->fetchRow($res);
00693 $create = $row[1];
00694 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/',
00695 "CREATE $temporary TABLE `parsertest_$tbl`", $create);
00696 if ($create === $create_tmp) {
00697 # Couldn't do replacement
00698 wfDie("could not create temporary table $tbl");
00699 }
00700 $db->query($create_tmp);
00701 }
00702 }
00703
00704 $this->changePrefix( 'parsertest_' );
00705
00706 # Hack: insert a few Wikipedia in-project interwiki prefixes,
00707 # for testing inter-language links
00708 $db->insert( 'interwiki', array(
00709 array( 'iw_prefix' => 'wikipedia',
00710 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
00711 'iw_local' => 0 ),
00712 array( 'iw_prefix' => 'meatball',
00713 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
00714 'iw_local' => 0 ),
00715 array( 'iw_prefix' => 'zh',
00716 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
00717 'iw_local' => 1 ),
00718 array( 'iw_prefix' => 'es',
00719 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
00720 'iw_local' => 1 ),
00721 array( 'iw_prefix' => 'fr',
00722 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
00723 'iw_local' => 1 ),
00724 array( 'iw_prefix' => 'ru',
00725 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
00726 'iw_local' => 1 ),
00727 ) );
00728
00729 # Hack: Insert an image to work with
00730 $db->insert( 'image', array(
00731 'img_name' => 'Foobar.jpg',
00732 'img_size' => 12345,
00733 'img_description' => 'Some lame file',
00734 'img_user' => 1,
00735 'img_user_text' => 'WikiSysop',
00736 'img_timestamp' => $db->timestamp( '20010115123500' ),
00737 'img_width' => 1941,
00738 'img_height' => 220,
00739 'img_bits' => 24,
00740 'img_media_type' => MEDIATYPE_BITMAP,
00741 'img_major_mime' => "image",
00742 'img_minor_mime' => "jpeg",
00743 'img_metadata' => serialize( array() ),
00744 ) );
00745
00746 # Update certain things in site_stats
00747 $db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 1, 'ss_good_articles' => 1 ) );
00748 }
00749
00753 protected function changePrefix( $prefix ) {
00754 global $wgDBprefix;
00755 wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
00756 $wgDBprefix = $prefix;
00757 }
00758
00759 public function changeLBPrefix( $lb, $prefix ) {
00760 $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
00761 }
00762
00763 public function changeDBPrefix( $db, $prefix ) {
00764 $db->tablePrefix( $prefix );
00765 }
00766
00767 private function teardownDatabase() {
00768 global $wgDBprefix;
00769 if ( !$this->databaseSetupDone ) {
00770 return;
00771 }
00772 $this->changePrefix( $this->oldTablePrefix );
00773 $this->databaseSetupDone = false;
00774 if ( $this->useTemporaryTables ) {
00775 # Don't need to do anything
00776 return;
00777 }
00778
00779
00780
00781
00782
00783
00784
00785 }
00786
00792 private function setupUploadDir() {
00793 global $IP;
00794 if ( $this->keepUploads ) {
00795 $dir = wfTempDir() . '/mwParser-images';
00796 if ( is_dir( $dir ) ) {
00797 return $dir;
00798 }
00799 } else {
00800 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00801 }
00802
00803 wfDebug( "Creating upload directory $dir\n" );
00804 if ( file_exists( $dir ) ) {
00805 wfDebug( "Already exists!\n" );
00806 return $dir;
00807 }
00808 wfMkdirParents( $dir . '/3/3a' );
00809 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
00810 return $dir;
00811 }
00812
00817 private function teardownGlobals() {
00818 RepoGroup::destroySingleton();
00819 FileCache::destroySingleton();
00820 LinkCache::singleton()->clear();
00821 foreach( $this->savedGlobals as $var => $val ) {
00822 $GLOBALS[$var] = $val;
00823 }
00824 if( isset( $this->uploadDir ) ) {
00825 $this->teardownUploadDir( $this->uploadDir );
00826 unset( $this->uploadDir );
00827 }
00828 }
00829
00833 private function teardownUploadDir( $dir ) {
00834 if ( $this->keepUploads ) {
00835 return;
00836 }
00837
00838
00839 self::deleteFiles(
00840 array (
00841 "$dir/3/3a/Foobar.jpg",
00842 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00843 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00844 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00845 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00846 )
00847 );
00848
00849 self::deleteDirs(
00850 array (
00851 "$dir/3/3a",
00852 "$dir/3",
00853 "$dir/thumb/6/65",
00854 "$dir/thumb/6",
00855 "$dir/thumb/3/3a/Foobar.jpg",
00856 "$dir/thumb/3/3a",
00857 "$dir/thumb/3",
00858 "$dir/thumb",
00859 "$dir",
00860 )
00861 );
00862 }
00863
00868 private static function deleteFiles( $files ) {
00869 foreach( $files as $file ) {
00870 if( file_exists( $file ) ) {
00871 unlink( $file );
00872 }
00873 }
00874 }
00875
00880 private static function deleteDirs( $dirs ) {
00881 foreach( $dirs as $dir ) {
00882 if( is_dir( $dir ) ) {
00883 rmdir( $dir );
00884 }
00885 }
00886 }
00887
00891 protected function showTesting( $desc ) {
00892 print "Running test $desc... ";
00893 }
00894
00901 protected function showSuccess( $desc ) {
00902 if( $this->showProgress ) {
00903 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
00904 }
00905 return true;
00906 }
00907
00917 protected function showFailure( $desc, $result, $html ) {
00918 if( $this->showFailure ) {
00919 if( !$this->showProgress ) {
00920 # In quiet mode we didn't show the 'Testing' message before the
00921 # test, in case it succeeded. Show it now:
00922 $this->showTesting( $desc );
00923 }
00924 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
00925 if ( $this->showOutput ) {
00926 print "--- Expected ---\n$result\n--- Actual ---\n$html\n";
00927 }
00928 if( $this->showDiffs ) {
00929 print $this->quickDiff( $result, $html );
00930 if( !$this->wellFormed( $html ) ) {
00931 print "XML error: $this->mXmlError\n";
00932 }
00933 }
00934 }
00935 return false;
00936 }
00937
00948 protected function quickDiff( $input, $output, $inFileTail='expected', $outFileTail='actual' ) {
00949 $prefix = wfTempDir() . "/mwParser-" . mt_rand();
00950
00951 $infile = "$prefix-$inFileTail";
00952 $this->dumpToFile( $input, $infile );
00953
00954 $outfile = "$prefix-$outFileTail";
00955 $this->dumpToFile( $output, $outfile );
00956
00957 $diff = `diff -au $infile $outfile`;
00958 unlink( $infile );
00959 unlink( $outfile );
00960
00961 return $this->colorDiff( $diff );
00962 }
00963
00970 private function dumpToFile( $data, $filename ) {
00971 $file = fopen( $filename, "wt" );
00972 fwrite( $file, $data . "\n" );
00973 fclose( $file );
00974 }
00975
00983 protected function colorDiff( $text ) {
00984 return preg_replace(
00985 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
00986 array( $this->term->color( 34 ) . '$1' . $this->term->reset(),
00987 $this->term->color( 31 ) . '$1' . $this->term->reset() ),
00988 $text );
00989 }
00990
00996 protected function showRunFile( $path ){
00997 print $this->term->color( 1 ) .
00998 "Reading tests from \"$path\"..." .
00999 $this->term->reset() .
01000 "\n";
01001 }
01002
01009 private function addArticle($name, $text, $line) {
01010 $this->setupGlobals();
01011 $title = Title::newFromText( $name );
01012 if ( is_null($title) ) {
01013 wfDie( "invalid title at line $line\n" );
01014 }
01015
01016 $aid = $title->getArticleID( GAID_FOR_UPDATE );
01017 if ($aid != 0) {
01018 wfDie( "duplicate article at line $line\n" );
01019 }
01020
01021 $art = new Article($title);
01022 $art->insertNewArticle($text, '', false, false );
01023 $this->teardownGlobals();
01024 }
01025
01032 private function requireHook( $name ) {
01033 global $wgParser;
01034 if( isset( $wgParser->mTagHooks[$name] ) ) {
01035 $this->hooks[$name] = $wgParser->mTagHooks[$name];
01036 } else {
01037 wfDie( "This test suite requires the '$name' hook extension.\n" );
01038 }
01039 }
01040
01047 private function requireFunctionHook( $name ) {
01048 global $wgParser;
01049 if( isset( $wgParser->mFunctionHooks[$name] ) ) {
01050 $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
01051 } else {
01052 wfDie( "This test suite requires the '$name' function hook extension.\n" );
01053 }
01054 }
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064 private function tidy( $text ) {
01065 global $wgUseTidy;
01066 if ($wgUseTidy) {
01067 $text = Parser::tidy($text);
01068 }
01069 return $text;
01070 }
01071
01072 private function wellFormed( $text ) {
01073 $html =
01074 Sanitizer::hackDocType() .
01075 '<html>' .
01076 $text .
01077 '</html>';
01078
01079 $parser = xml_parser_create( "UTF-8" );
01080
01081 # case folding violates XML standard, turn it off
01082 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
01083
01084 if( !xml_parse( $parser, $html, true ) ) {
01085 $err = xml_error_string( xml_get_error_code( $parser ) );
01086 $position = xml_get_current_byte_index( $parser );
01087 $fragment = $this->extractFragment( $html, $position );
01088 $this->mXmlError = "$err at byte $position:\n$fragment";
01089 xml_parser_free( $parser );
01090 return false;
01091 }
01092 xml_parser_free( $parser );
01093 return true;
01094 }
01095
01096 private function extractFragment( $text, $position ) {
01097 $start = max( 0, $position - 10 );
01098 $before = $position - $start;
01099 $fragment = '...' .
01100 $this->term->color( 34 ) .
01101 substr( $text, $start, $before ) .
01102 $this->term->color( 0 ) .
01103 $this->term->color( 31 ) .
01104 $this->term->color( 1 ) .
01105 substr( $text, $position, 1 ) .
01106 $this->term->color( 0 ) .
01107 $this->term->color( 34 ) .
01108 substr( $text, $position + 1, 9 ) .
01109 $this->term->color( 0 ) .
01110 '...';
01111 $display = str_replace( "\n", ' ', $fragment );
01112 $caret = ' ' .
01113 str_repeat( ' ', $before ) .
01114 $this->term->color( 31 ) .
01115 '^' .
01116 $this->term->color( 0 );
01117 return "$display\n$caret";
01118 }
01119 }
01120
01121 class AnsiTermColorer {
01122 function __construct() {
01123 }
01124
01131 public function color( $color ) {
01132 global $wgCommandLineDarkBg;
01133 $light = $wgCommandLineDarkBg ? "1;" : "0;";
01134 return "\x1b[{$light}{$color}m";
01135 }
01136
01142 public function reset() {
01143 return $this->color( 0 );
01144 }
01145 }
01146
01147
01148 class DummyTermColorer {
01149 public function color( $color ) {
01150 return '';
01151 }
01152
01153 public function reset() {
01154 return '';
01155 }
01156 }
01157
01158 class TestRecorder {
01159 var $parent;
01160 var $term;
01161
01162 function __construct( $parent ) {
01163 $this->parent = $parent;
01164 $this->term = $parent->term;
01165 }
01166
01167 function start() {
01168 $this->total = 0;
01169 $this->success = 0;
01170 }
01171
01172 function record( $test, $result ) {
01173 $this->total++;
01174 $this->success += ($result ? 1 : 0);
01175 }
01176
01177 function end() {
01178
01179 }
01180
01181 function report() {
01182 if( $this->total > 0 ) {
01183 $this->reportPercentage( $this->success, $this->total );
01184 } else {
01185 wfDie( "No tests found.\n" );
01186 }
01187 }
01188
01189 function reportPercentage( $success, $total ) {
01190 $ratio = wfPercent( 100 * $success / $total );
01191 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
01192 if( $success == $total ) {
01193 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
01194 } else {
01195 $failed = $total - $success ;
01196 print $this->term->color( 31 ) . "$failed tests failed!";
01197 }
01198 print $this->term->reset() . "\n";
01199 return ($success == $total);
01200 }
01201 }
01202
01203 class DbTestPreviewer extends TestRecorder {
01204 protected $lb;
01205 protected $db;
01206 protected $curRun;
01207 protected $prevRun;
01208 protected $results;
01209
01213 function __construct( $parent ) {
01214 parent::__construct( $parent );
01215 $this->lb = wfGetLBFactory()->newMainLB();
01216
01217 $this->db = $this->lb->getConnection( DB_MASTER );
01218 }
01219
01224 function start() {
01225 global $wgDBtype, $wgDBprefix;
01226 parent::start();
01227
01228 if( ! $this->db->tableExists( 'testrun' )
01229 or ! $this->db->tableExists( 'testitem' ) )
01230 {
01231 print "WARNING> `testrun` table not found in database.\n";
01232 $this->prevRun = false;
01233 } else {
01234
01235 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
01236 }
01237 $this->results = array();
01238 }
01239
01240 function record( $test, $result ) {
01241 parent::record( $test, $result );
01242 $this->results[$test] = $result;
01243 }
01244
01245 function report() {
01246 if( $this->prevRun ) {
01247
01248
01249 $table = array(
01250 'fp' => 'previously failing test(s) now PASSING! :)',
01251 'pn' => 'previously PASSING test(s) removed o_O',
01252 'np' => 'new PASSING test(s) :)',
01253
01254 'pf' => 'previously passing test(s) now FAILING! :(',
01255 'fn' => 'previously FAILING test(s) removed O_o',
01256 'nf' => 'new FAILING test(s) :(',
01257 'ff' => 'still FAILING test(s) :(',
01258 );
01259
01260 $prevResults = array();
01261
01262 $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ),
01263 array( 'ti_run' => $this->prevRun ), __METHOD__ );
01264 foreach ( $res as $row ) {
01265 if ( !$this->parent->regex
01266 || preg_match( "/{$this->parent->regex}/i", $row->ti_name ) )
01267 {
01268 $prevResults[$row->ti_name] = $row->ti_success;
01269 }
01270 }
01271
01272 $combined = array_keys( $this->results + $prevResults );
01273
01274 # Determine breakdown by change type
01275 $breakdown = array();
01276 foreach ( $combined as $test ) {
01277 if ( !isset( $prevResults[$test] ) ) {
01278 $before = 'n';
01279 } elseif ( $prevResults[$test] == 1 ) {
01280 $before = 'p';
01281 } else {
01282 $before = 'f';
01283 }
01284 if ( !isset( $this->results[$test] ) ) {
01285 $after = 'n';
01286 } elseif ( $this->results[$test] == 1 ) {
01287 $after = 'p';
01288 } else {
01289 $after = 'f';
01290 }
01291 $code = $before . $after;
01292 if ( isset( $table[$code] ) ) {
01293 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
01294 }
01295 }
01296
01297 # Write out results
01298 foreach ( $table as $code => $label ) {
01299 if( !empty( $breakdown[$code] ) ) {
01300 $count = count($breakdown[$code]);
01301 printf( "\n%4d %s\n", $count, $label );
01302 foreach ($breakdown[$code] as $differing_test_name => $statusInfo) {
01303 print " * $differing_test_name [$statusInfo]\n";
01304 }
01305 }
01306 }
01307 } else {
01308 print "No previous test runs to compare against.\n";
01309 }
01310 print "\n";
01311 parent::report();
01312 }
01313
01319 private function getTestStatusInfo($testname, $after) {
01320
01321
01322 if ( $after == 'n' ) {
01323 $changedRun = $this->db->selectField ( 'testitem',
01324 'MIN(ti_run)',
01325 array( 'ti_name' => $testname ),
01326 __METHOD__ );
01327 $appear = $this->db->selectRow ( 'testrun',
01328 array( 'tr_date', 'tr_mw_version' ),
01329 array( 'tr_id' => $changedRun ),
01330 __METHOD__ );
01331 return "First recorded appearance: "
01332 . date( "d-M-Y H:i:s", strtotime ( $appear->tr_date ) )
01333 . ", " . $appear->tr_mw_version;
01334 }
01335
01336
01337
01338 $conds = array(
01339 'ti_name' => $testname,
01340 'ti_success' => ($after == 'f' ? "1" : "0") );
01341 if ( $this->curRun ) {
01342 $conds[] = "ti_run != " . $this->db->addQuotes ( $this->curRun );
01343 }
01344
01345 $changedRun = $this->db->selectField ( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
01346
01347
01348 if ( is_null ( $changedRun ) ) {
01349 if ($after == "f") {
01350 return "Has never passed";
01351 } else {
01352 return "Has never failed";
01353 }
01354 }
01355
01356
01357
01358
01359 $pre = $this->db->selectRow ( 'testrun',
01360 array( 'tr_date', 'tr_mw_version' ),
01361 array( 'tr_id' => $changedRun ),
01362 __METHOD__ );
01363 $post = $this->db->selectRow ( 'testrun',
01364 array( 'tr_date', 'tr_mw_version' ),
01365 array( "tr_id > " . $this->db->addQuotes ( $changedRun) ),
01366 __METHOD__,
01367 array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
01368 );
01369
01370 if ( $post ) {
01371 $postDate = date( "d-M-Y H:i:s", strtotime ( $post->tr_date ) ) . ", {$post->tr_mw_version}";
01372 } else {
01373 $postDate = 'now';
01374 }
01375 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
01376 . date( "d-M-Y H:i:s", strtotime ( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
01377 . " and $postDate";
01378
01379 }
01380
01384 function end() {
01385 $this->lb->commitMasterChanges();
01386 $this->lb->closeAll();
01387 parent::end();
01388 }
01389
01390 }
01391
01392 class DbTestRecorder extends DbTestPreviewer {
01397 function start() {
01398 global $wgDBtype, $wgDBprefix;
01399 $this->db->begin();
01400
01401 if( ! $this->db->tableExists( 'testrun' )
01402 or ! $this->db->tableExists( 'testitem' ) )
01403 {
01404 print "WARNING> `testrun` table not found in database. Trying to create table.\n";
01405 if ($wgDBtype === 'postgres')
01406 $this->db->sourceFile( dirname(__FILE__) . '/testRunner.postgres.sql' );
01407 else
01408 $this->db->sourceFile( dirname(__FILE__) . '/testRunner.sql' );
01409 echo "OK, resuming.\n";
01410 }
01411
01412 parent::start();
01413
01414 $this->db->insert( 'testrun',
01415 array(
01416 'tr_date' => $this->db->timestamp(),
01417 'tr_mw_version' => SpecialVersion::getVersion(),
01418 'tr_php_version' => phpversion(),
01419 'tr_db_version' => $this->db->getServerVersion(),
01420 'tr_uname' => php_uname()
01421 ),
01422 __METHOD__ );
01423 if ($wgDBtype === 'postgres')
01424 $this->curRun = $this->db->currentSequenceValue('testrun_id_seq');
01425 else
01426 $this->curRun = $this->db->insertId();
01427 }
01428
01434 function record( $test, $result ) {
01435 parent::record( $test, $result );
01436 $this->db->insert( 'testitem',
01437 array(
01438 'ti_run' => $this->curRun,
01439 'ti_name' => $test,
01440 'ti_success' => $result ? 1 : 0,
01441 ),
01442 __METHOD__ );
01443 }
01444 }