00001 <?php
00002 # Copyright (C) 2004, 2010 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', 'run-disabled' );
00029 $optionsWithArgs = array( 'regex', 'seed', 'setversion' );
00030
00031 if ( !defined( "NO_COMMAND_LINE" ) ) {
00032 require_once( dirname(__FILE__) . '/commandLine.inc' );
00033 }
00034 require_once( "$IP/maintenance/parserTestsParserHook.php" );
00035 require_once( "$IP/maintenance/parserTestsStaticParserHook.php" );
00036 require_once( "$IP/maintenance/parserTestsParserTime.php" );
00037
00041 class ParserTest {
00045 private $color;
00046
00050 private $showOutput;
00051
00055 private $useTemporaryTables = true;
00056
00060 private $databaseSetupDone = false;
00061
00065 private $oldTablePrefix;
00066
00067 private $maxFuzzTestLength = 300;
00068 private $fuzzSeed = 0;
00069 private $memoryLimit = 50;
00070
00075 public function ParserTest() {
00076 global $options;
00077
00078 # Only colorize output if stdout is a terminal.
00079 $this->color = !wfIsWindows() && posix_isatty(1);
00080
00081 if( isset( $options['color'] ) ) {
00082 switch( $options['color'] ) {
00083 case 'no':
00084 $this->color = false;
00085 break;
00086 case 'yes':
00087 default:
00088 $this->color = true;
00089 break;
00090 }
00091 }
00092 $this->term = $this->color
00093 ? new AnsiTermColorer()
00094 : new DummyTermColorer();
00095
00096 $this->showDiffs = !isset( $options['quick'] );
00097 $this->showProgress = !isset( $options['quiet'] );
00098 $this->showFailure = !(
00099 isset( $options['quiet'] )
00100 && ( isset( $options['record'] )
00101 || isset( $options['compare'] ) ) );
00102
00103 $this->showOutput = isset( $options['show-output'] );
00104
00105
00106 if (isset($options['regex'])) {
00107 if ( isset( $options['record'] ) ) {
00108 echo "Warning: --record cannot be used with --regex, disabling --record\n";
00109 unset( $options['record'] );
00110 }
00111 $this->regex = $options['regex'];
00112 } else {
00113 # Matches anything
00114 $this->regex = '';
00115 }
00116
00117 if( isset( $options['record'] ) ) {
00118 $this->recorder = new DbTestRecorder( $this );
00119 } elseif( isset( $options['compare'] ) ) {
00120 $this->recorder = new DbTestPreviewer( $this );
00121 } elseif( isset( $options['upload'] ) ) {
00122 $this->recorder = new RemoteTestRecorder( $this );
00123 } elseif( class_exists( 'PHPUnitTestRecorder' ) ) {
00124 $this->recorder = new PHPUnitTestRecorder( $this );
00125 } else {
00126 $this->recorder = new TestRecorder( $this );
00127 }
00128 $this->keepUploads = isset( $options['keep-uploads'] );
00129
00130 if ( isset( $options['seed'] ) ) {
00131 $this->fuzzSeed = intval( $options['seed'] ) - 1;
00132 }
00133
00134 $this->runDisabled = isset( $options['run-disabled'] );
00135
00136 $this->hooks = array();
00137 $this->functionHooks = array();
00138 }
00139
00143 public function chomp($s) {
00144 if (substr($s, -1) === "\n") {
00145 return substr($s, 0, -1);
00146 }
00147 else {
00148 return $s;
00149 }
00150 }
00151
00156 function fuzzTest( $filenames ) {
00157 $dict = $this->getFuzzInput( $filenames );
00158 $dictSize = strlen( $dict );
00159 $logMaxLength = log( $this->maxFuzzTestLength );
00160 $this->setupDatabase();
00161 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
00162
00163 $numTotal = 0;
00164 $numSuccess = 0;
00165 $user = new User;
00166 $opts = ParserOptions::newFromUser( $user );
00167 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
00168
00169 while ( true ) {
00170
00171 mt_srand( ++$this->fuzzSeed );
00172 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
00173 $input = '';
00174 while ( strlen( $input ) < $totalLength ) {
00175 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
00176 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
00177 $offset = mt_rand( 0, $dictSize - $hairLength );
00178 $input .= substr( $dict, $offset, $hairLength );
00179 }
00180
00181 $this->setupGlobals();
00182 $parser = $this->getParser();
00183
00184 try {
00185 $parser->parse( $input, $title, $opts );
00186 $fail = false;
00187 } catch ( Exception $exception ) {
00188 $fail = true;
00189 }
00190
00191 if ( $fail ) {
00192 echo "Test failed with seed {$this->fuzzSeed}\n";
00193 echo "Input:\n";
00194 var_dump( $input );
00195 echo "\n\n";
00196 echo "$exception\n";
00197 } else {
00198 $numSuccess++;
00199 }
00200 $numTotal++;
00201 $this->teardownGlobals();
00202 $parser->__destruct();
00203
00204 if ( $numTotal % 100 == 0 ) {
00205 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
00206 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
00207 if ( $usage > 90 ) {
00208 echo "Out of memory:\n";
00209 $memStats = $this->getMemoryBreakdown();
00210 foreach ( $memStats as $name => $usage ) {
00211 echo "$name: $usage\n";
00212 }
00213 $this->abort();
00214 }
00215 }
00216 }
00217 }
00218
00222 function getFuzzInput( $filenames ) {
00223 $dict = '';
00224 foreach( $filenames as $filename ) {
00225 $contents = file_get_contents( $filename );
00226 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
00227 foreach ( $matches[1] as $match ) {
00228 $dict .= $match . "\n";
00229 }
00230 }
00231 return $dict;
00232 }
00233
00237 function getMemoryBreakdown() {
00238 $memStats = array();
00239 foreach ( $GLOBALS as $name => $value ) {
00240 $memStats['$'.$name] = strlen( serialize( $value ) );
00241 }
00242 $classes = get_declared_classes();
00243 foreach ( $classes as $class ) {
00244 $rc = new ReflectionClass( $class );
00245 $props = $rc->getStaticProperties();
00246 $memStats[$class] = strlen( serialize( $props ) );
00247 $methods = $rc->getMethods();
00248 foreach ( $methods as $method ) {
00249 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
00250 }
00251 }
00252 $functions = get_defined_functions();
00253 foreach ( $functions['user'] as $function ) {
00254 $rf = new ReflectionFunction( $function );
00255 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
00256 }
00257 asort( $memStats );
00258 return $memStats;
00259 }
00260
00261 function abort() {
00262 $this->abort();
00263 }
00264
00276 public function runTestsFromFiles( $filenames ) {
00277 $this->recorder->start();
00278 $this->setupDatabase();
00279 $ok = true;
00280 foreach( $filenames as $filename ) {
00281 $tests = new TestFileIterator( $filename, $this );
00282 $ok = $this->runTests( $tests ) && $ok;
00283 }
00284 $this->teardownDatabase();
00285 $this->recorder->report();
00286 $this->recorder->end();
00287 return $ok;
00288 }
00289
00290 function runTests($tests) {
00291 $ok = true;
00292 foreach($tests as $i => $t) {
00293 $result =
00294 $this->runTest($t['test'], $t['input'], $t['result'], $t['options'], $t['config']);
00295 $ok = $ok && $result;
00296 $this->recorder->record( $t['test'], $result );
00297 }
00298 if ( $this->showProgress ) {
00299 print "\n";
00300 }
00301 }
00302
00306 function getParser() {
00307 global $wgParserConf;
00308 $class = $wgParserConf['class'];
00309 $parser = new $class( $wgParserConf );
00310 foreach( $this->hooks as $tag => $callback ) {
00311 $parser->setHook( $tag, $callback );
00312 }
00313 foreach( $this->functionHooks as $tag => $bits ) {
00314 list( $callback, $flags ) = $bits;
00315 $parser->setFunctionHook( $tag, $callback, $flags );
00316 }
00317 wfRunHooks( 'ParserTestParser', array( &$parser ) );
00318 return $parser;
00319 }
00320
00330 public function runTest( $desc, $input, $result, $opts, $config ) {
00331 if( $this->showProgress ) {
00332 $this->showTesting( $desc );
00333 }
00334
00335 $opts = $this->parseOptions( $opts );
00336 $this->setupGlobals($opts, $config);
00337
00338 $user = new User();
00339 $options = ParserOptions::newFromUser( $user );
00340
00341 $m = array();
00342 if (isset( $opts['title'] ) ) {
00343 $titleText = $opts['title'];
00344 }
00345 else {
00346 $titleText = 'Parser test';
00347 }
00348
00349 $noxml = isset( $opts['noxml'] );
00350 $local = isset( $opts['local'] );
00351 $parser = $this->getParser();
00352 $title = Title::newFromText( $titleText );
00353
00354 $matches = array();
00355 if( isset( $opts['pst'] ) ) {
00356 $out = $parser->preSaveTransform( $input, $title, $user, $options );
00357 } elseif( isset( $opts['msg'] ) ) {
00358 $out = $parser->transformMsg( $input, $options );
00359 } elseif( isset( $opts['section'] ) ) {
00360 $section = $opts['section'];
00361 $out = $parser->getSection( $input, $section );
00362 } elseif( isset( $opts['replace'] ) ) {
00363 $section = $opts['replace'][0];
00364 $replace = $opts['replace'][1];
00365 $out = $parser->replaceSection( $input, $section, $replace );
00366 } elseif( isset( $opts['comment'] ) ) {
00367 $linker = $user->getSkin();
00368 $out = $linker->formatComment( $input, $title, $local );
00369 } else {
00370 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
00371 $out = $output->getText();
00372
00373 if ( isset( $opts['showtitle'] ) ) {
00374 if($output->getTitleText()) $title = $output->getTitleText();
00375 $out = "$title\n$out";
00376 }
00377 if (isset( $opts['ill'] ) ) {
00378 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
00379 } elseif( isset( $opts['cat'] ) ) {
00380 global $wgOut;
00381 $wgOut->addCategoryLinks($output->getCategories());
00382 $cats = $wgOut->getCategoryLinks();
00383 if ( isset( $cats['normal'] ) ) {
00384 $out = $this->tidy( implode( ' ', $cats['normal'] ) );
00385 } else {
00386 $out = '';
00387 }
00388 }
00389
00390 $result = $this->tidy($result);
00391 }
00392
00393
00394 $this->teardownGlobals();
00395
00396 if( $result === $out && ( $noxml === true || $this->wellFormed( $out ) ) ) {
00397 return $this->showSuccess( $desc );
00398 } else {
00399 return $this->showFailure( $desc, $result, $out );
00400 }
00401 }
00402
00403
00410 private static function getOptionValue( $key, $opts, $default ) {
00411 $key = strtolower( $key );
00412 if( isset( $opts[$key] ) ) {
00413 return $opts[$key];
00414 } else {
00415 return $default;
00416 }
00417 }
00418
00419 private function parseOptions( $instring ) {
00420 $opts = array();
00421 $lines = explode( "\n", $instring );
00422
00423
00424
00425
00426
00427 $regex = '/\b
00428 ([\w-]+) # Key
00429 \b
00430 (?:\s*
00431 = # First sub-value
00432 \s*
00433 (
00434 "
00435 [^"]* # Quoted val
00436 "
00437 |
00438 \[\[
00439 [^]]* # Link target
00440 \]\]
00441 |
00442 [\w-]+ # Plain word
00443 )
00444 (?:\s*
00445 , # Sub-vals 1..N
00446 \s*
00447 (
00448 "[^"]*" # Quoted val
00449 |
00450 \[\[[^]]*\]\] # Link target
00451 |
00452 [\w-]+ # Plain word
00453 )
00454 )*
00455 )?
00456 /x';
00457
00458 if( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
00459 foreach( $matches as $bits ) {
00460 $match = array_shift( $bits );
00461 $key = strtolower( array_shift( $bits ) );
00462 if( count( $bits ) == 0 ) {
00463 $opts[$key] = true;
00464 } elseif( count( $bits ) == 1 ) {
00465 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
00466 } else {
00467
00468 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
00469 }
00470 }
00471 }
00472 return $opts;
00473 }
00474
00475 private function cleanupOption( $opt ) {
00476 if( substr( $opt, 0, 1 ) == '"' ) {
00477 return substr( $opt, 1, -1 );
00478 }
00479 if( substr( $opt, 0, 2 ) == '[[' ) {
00480 return substr( $opt, 2, -2 );
00481 }
00482 return $opt;
00483 }
00484
00489 private function setupGlobals($opts = '', $config = '') {
00490 global $wgDBtype;
00491 if( !isset( $this->uploadDir ) ) {
00492 $this->uploadDir = $this->setupUploadDir();
00493 }
00494
00495 # Find out values for some special options.
00496 $lang =
00497 self::getOptionValue( 'language', $opts, 'en' );
00498 $variant =
00499 self::getOptionValue( 'variant', $opts, false );
00500 $maxtoclevel =
00501 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
00502 $linkHolderBatchSize =
00503 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
00504
00505 $settings = array(
00506 'wgServer' => 'http://localhost',
00507 'wgScript' => '/index.php',
00508 'wgScriptPath' => '/',
00509 'wgArticlePath' => '/wiki/$1',
00510 'wgActionPaths' => array(),
00511 'wgLocalFileRepo' => array(
00512 'class' => 'LocalRepo',
00513 'name' => 'local',
00514 'directory' => $this->uploadDir,
00515 'url' => 'http://example.com/images',
00516 'hashLevels' => 2,
00517 'transformVia404' => false,
00518 ),
00519 'wgEnableUploads' => true,
00520 'wgStyleSheetPath' => '/skins',
00521 'wgSitename' => 'MediaWiki',
00522 'wgServerName' => 'Britney-Spears',
00523 'wgLanguageCode' => $lang,
00524 'wgContLanguageCode' => $lang,
00525 'wgDBprefix' => $wgDBtype != 'oracle' ? 'parsertest_' : 'pt_',
00526 'wgRawHtml' => isset( $opts['rawhtml'] ),
00527 'wgLang' => null,
00528 'wgContLang' => null,
00529 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
00530 'wgMaxTocLevel' => $maxtoclevel,
00531 'wgCapitalLinks' => true,
00532 'wgNoFollowLinks' => true,
00533 'wgNoFollowDomainExceptions' => array(),
00534 'wgThumbnailScriptPath' => false,
00535 'wgUseImageResize' => false,
00536 'wgUseTeX' => isset( $opts['math'] ),
00537 'wgMathDirectory' => $this->uploadDir . '/math',
00538 'wgLocaltimezone' => 'UTC',
00539 'wgAllowExternalImages' => true,
00540 'wgUseTidy' => false,
00541 'wgDefaultLanguageVariant' => $variant,
00542 'wgVariantArticlePath' => false,
00543 'wgGroupPermissions' => array( '*' => array(
00544 'createaccount' => true,
00545 'read' => true,
00546 'edit' => true,
00547 'createpage' => true,
00548 'createtalk' => true,
00549 ) ),
00550 'wgNamespaceProtection' => array( NS_MEDIAWIKI => 'editinterface' ),
00551 'wgDefaultExternalStore' => array(),
00552 'wgForeignFileRepos' => array(),
00553 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
00554 'wgExperimentalHtmlIds' => false,
00555 'wgExternalLinkTarget' => false,
00556 'wgAlwaysUseTidy' => false,
00557 'wgHtml5' => true,
00558 'wgWellFormedXml' => true,
00559 'wgAllowMicrodataAttributes' => true,
00560 );
00561
00562 if ($config) {
00563 $configLines = explode( "\n", $config );
00564
00565 foreach( $configLines as $line ) {
00566 list( $var, $value ) = explode( '=', $line, 2 );
00567
00568 $settings[$var] = eval("return $value;" );
00569 }
00570 }
00571
00572 $this->savedGlobals = array();
00573 foreach( $settings as $var => $val ) {
00574 if( array_key_exists( $var, $GLOBALS ) ) {
00575 $this->savedGlobals[$var] = $GLOBALS[$var];
00576 }
00577 $GLOBALS[$var] = $val;
00578 }
00579 $langObj = Language::factory( $lang );
00580 $GLOBALS['wgLang'] = $langObj;
00581 $GLOBALS['wgContLang'] = $langObj;
00582 $GLOBALS['wgMemc'] = new FakeMemCachedClient;
00583 $GLOBALS['wgOut'] = new OutputPage;
00584
00585 MagicWord::clearCache();
00586
00587 global $wgUser;
00588 $wgUser = new User();
00589 }
00590
00595 private function listTables() {
00596 global $wgDBtype;
00597 $tables = array('user', 'page', 'page_restrictions',
00598 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
00599 'categorylinks', 'templatelinks', 'externallinks', 'langlinks',
00600 'site_stats', 'hitcounter', 'ipblocks', 'image', 'oldimage',
00601 'recentchanges', 'watchlist', 'math', 'interwiki',
00602 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
00603 'archive', 'user_groups', 'page_props', 'category'
00604 );
00605
00606 if ($wgDBtype === 'mysql')
00607 array_push( $tables, 'searchindex' );
00608
00609
00610
00611
00612 wfRunHooks( 'ParserTestTables', array( &$tables ) );
00613
00614 return $tables;
00615 }
00616
00622 function setupDatabase() {
00623 global $wgDBprefix, $wgDBtype;
00624 if ( $this->databaseSetupDone ) {
00625 return;
00626 }
00627 if ( $wgDBprefix === 'parsertest_' || ($wgDBtype == 'oracle' && $wgDBprefix === 'pt_')) {
00628 throw new MWException( 'setupDatabase should be called before setupGlobals' );
00629 }
00630 $this->databaseSetupDone = true;
00631 $this->oldTablePrefix = $wgDBprefix;
00632
00633 # CREATE TEMPORARY TABLE breaks if there is more than one server
00634 # FIXME: r40209 makes temporary tables break even with just one server
00635 # FIXME: (bug 15892); disabling the feature entirely as a temporary fix
00636 if ( true || wfGetLB()->getServerCount() != 1 ) {
00637 $this->useTemporaryTables = false;
00638 }
00639
00640 $temporary = $this->useTemporaryTables || $wgDBtype == 'postgres';
00641
00642 $db = wfGetDB( DB_MASTER );
00643 $tables = $this->listTables();
00644
00645 foreach ( $tables as $tbl ) {
00646 # Clean up from previous aborted run. So that table escaping
00647 # works correctly across DB engines, we need to change the pre-
00648 # fix back and forth so tableName() works right.
00649 $this->changePrefix( $this->oldTablePrefix );
00650 $oldTableName = $db->tableName( $tbl );
00651 $this->changePrefix( $wgDBtype != 'oracle' ? 'parsertest_' : 'pt_' );
00652 $newTableName = $db->tableName( $tbl );
00653
00654 if ( $db->tableExists( $tbl ) && $wgDBtype != 'postgres' && $wgDBtype != 'oracle' ) {
00655 $db->query( "DROP TABLE $newTableName" );
00656 }
00657 # Create new table
00658 $db->duplicateTableStructure( $oldTableName, $newTableName, $temporary );
00659 }
00660 if ($wgDBtype == 'oracle')
00661 $db->query('BEGIN FILL_WIKI_INFO; END;');
00662
00663 $this->changePrefix( $wgDBtype != 'oracle' ? 'parsertest_' : 'pt_' );
00664
00665 # Hack: insert a few Wikipedia in-project interwiki prefixes,
00666 # for testing inter-language links
00667 $db->insert( 'interwiki', array(
00668 array( 'iw_prefix' => 'wikipedia',
00669 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
00670 'iw_local' => 0 ),
00671 array( 'iw_prefix' => 'meatball',
00672 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
00673 'iw_local' => 0 ),
00674 array( 'iw_prefix' => 'zh',
00675 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
00676 'iw_local' => 1 ),
00677 array( 'iw_prefix' => 'es',
00678 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
00679 'iw_local' => 1 ),
00680 array( 'iw_prefix' => 'fr',
00681 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
00682 'iw_local' => 1 ),
00683 array( 'iw_prefix' => 'ru',
00684 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
00685 'iw_local' => 1 ),
00686 ) );
00687
00688
00689 if ($wgDBtype == 'oracle') {
00690 # Insert 0 and 1 user_ids to prevent FK violations
00691
00692 #Anonymous user
00693 $db->insert( 'user', array(
00694 'user_id' => 0,
00695 'user_name' => 'Anonymous') );
00696
00697 # Hack-on-Hack: Insert a test user to be able to insert an image
00698 $db->insert( 'user', array(
00699 'user_id' => 1,
00700 'user_name' => 'Tester') );
00701 }
00702
00703 # Hack: Insert an image to work with
00704 $db->insert( 'image', array(
00705 'img_name' => 'Foobar.jpg',
00706 'img_size' => 12345,
00707 'img_description' => 'Some lame file',
00708 'img_user' => 1,
00709 'img_user_text' => 'WikiSysop',
00710 'img_timestamp' => $db->timestamp( '20010115123500' ),
00711 'img_width' => 1941,
00712 'img_height' => 220,
00713 'img_bits' => 24,
00714 'img_media_type' => MEDIATYPE_BITMAP,
00715 'img_major_mime' => "image",
00716 'img_minor_mime' => "jpeg",
00717 'img_metadata' => serialize( array() ),
00718 ) );
00719
00720 # This image will be blacklisted in [[MediaWiki:Bad image list]]
00721 $db->insert( 'image', array(
00722 'img_name' => 'Bad.jpg',
00723 'img_size' => 12345,
00724 'img_description' => 'zomgnotcensored',
00725 'img_user' => 1,
00726 'img_user_text' => 'WikiSysop',
00727 'img_timestamp' => $db->timestamp( '20010115123500' ),
00728 'img_width' => 320,
00729 'img_height' => 240,
00730 'img_bits' => 24,
00731 'img_media_type' => MEDIATYPE_BITMAP,
00732 'img_major_mime' => "image",
00733 'img_minor_mime' => "jpeg",
00734 'img_metadata' => serialize( array() ),
00735 ) );
00736
00737 # Update certain things in site_stats
00738 $db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
00739
00740 # Reinitialise the LocalisationCache to match the database state
00741 Language::getLocalisationCache()->unloadAll();
00742
00743 # Make a new message cache
00744 global $wgMessageCache, $wgMemc;
00745 $wgMessageCache = new MessageCache( $wgMemc, true, 3600, '' );
00746 }
00747
00751 protected function changePrefix( $prefix ) {
00752 global $wgDBprefix;
00753 wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
00754 $wgDBprefix = $prefix;
00755 }
00756
00757 public function changeLBPrefix( $lb, $prefix ) {
00758 $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
00759 }
00760
00761 public function changeDBPrefix( $db, $prefix ) {
00762 $db->tablePrefix( $prefix );
00763 }
00764
00765 private function teardownDatabase() {
00766 global $wgDBtype;
00767 if ( !$this->databaseSetupDone ) {
00768 return;
00769 }
00770 $this->changePrefix( $this->oldTablePrefix );
00771 $this->databaseSetupDone = false;
00772 if ( $this->useTemporaryTables ) {
00773 # Don't need to do anything
00774 return;
00775 }
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 }
00788
00794 private function setupUploadDir() {
00795 global $IP;
00796 if ( $this->keepUploads ) {
00797 $dir = wfTempDir() . '/mwParser-images';
00798 if ( is_dir( $dir ) ) {
00799 return $dir;
00800 }
00801 } else {
00802 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
00803 }
00804
00805 wfDebug( "Creating upload directory $dir\n" );
00806 if ( file_exists( $dir ) ) {
00807 wfDebug( "Already exists!\n" );
00808 return $dir;
00809 }
00810 wfMkdirParents( $dir . '/3/3a' );
00811 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
00812
00813 wfMkdirParents( $dir . '/0/09' );
00814 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
00815 return $dir;
00816 }
00817
00822 private function teardownGlobals() {
00823 RepoGroup::destroySingleton();
00824 LinkCache::singleton()->clear();
00825 foreach( $this->savedGlobals as $var => $val ) {
00826 $GLOBALS[$var] = $val;
00827 }
00828 if( isset( $this->uploadDir ) ) {
00829 $this->teardownUploadDir( $this->uploadDir );
00830 unset( $this->uploadDir );
00831 }
00832 }
00833
00837 private function teardownUploadDir( $dir ) {
00838 if ( $this->keepUploads ) {
00839 return;
00840 }
00841
00842
00843 self::deleteFiles(
00844 array (
00845 "$dir/3/3a/Foobar.jpg",
00846 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
00847 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
00848 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
00849 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
00850
00851 "$dir/0/09/Bad.jpg",
00852
00853 "$dir/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
00854 )
00855 );
00856
00857 self::deleteDirs(
00858 array (
00859 "$dir/3/3a",
00860 "$dir/3",
00861 "$dir/thumb/6/65",
00862 "$dir/thumb/6",
00863 "$dir/thumb/3/3a/Foobar.jpg",
00864 "$dir/thumb/3/3a",
00865 "$dir/thumb/3",
00866
00867 "$dir/0/09/",
00868 "$dir/0/",
00869 "$dir/thumb",
00870 "$dir/math/f/a/5",
00871 "$dir/math/f/a",
00872 "$dir/math/f",
00873 "$dir/math",
00874 "$dir",
00875 )
00876 );
00877 }
00878
00883 private static function deleteFiles( $files ) {
00884 foreach( $files as $file ) {
00885 if( file_exists( $file ) ) {
00886 unlink( $file );
00887 }
00888 }
00889 }
00890
00895 private static function deleteDirs( $dirs ) {
00896 foreach( $dirs as $dir ) {
00897 if( is_dir( $dir ) ) {
00898 rmdir( $dir );
00899 }
00900 }
00901 }
00902
00906 protected function showTesting( $desc ) {
00907 print "Running test $desc... ";
00908 }
00909
00916 protected function showSuccess( $desc ) {
00917 if( $this->showProgress ) {
00918 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
00919 }
00920 return true;
00921 }
00922
00932 protected function showFailure( $desc, $result, $html ) {
00933 if( $this->showFailure ) {
00934 if( !$this->showProgress ) {
00935 # In quiet mode we didn't show the 'Testing' message before the
00936 # test, in case it succeeded. Show it now:
00937 $this->showTesting( $desc );
00938 }
00939 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
00940 if ( $this->showOutput ) {
00941 print "--- Expected ---\n$result\n--- Actual ---\n$html\n";
00942 }
00943 if( $this->showDiffs ) {
00944 print $this->quickDiff( $result, $html );
00945 if( !$this->wellFormed( $html ) ) {
00946 print "XML error: $this->mXmlError\n";
00947 }
00948 }
00949 }
00950 return false;
00951 }
00952
00963 protected function quickDiff( $input, $output, $inFileTail='expected', $outFileTail='actual' ) {
00964 $prefix = wfTempDir() . "/mwParser-" . mt_rand();
00965
00966 $infile = "$prefix-$inFileTail";
00967 $this->dumpToFile( $input, $infile );
00968
00969 $outfile = "$prefix-$outFileTail";
00970 $this->dumpToFile( $output, $outfile );
00971
00972 $diff = `diff -au $infile $outfile`;
00973 unlink( $infile );
00974 unlink( $outfile );
00975
00976 return $this->colorDiff( $diff );
00977 }
00978
00985 private function dumpToFile( $data, $filename ) {
00986 $file = fopen( $filename, "wt" );
00987 fwrite( $file, $data . "\n" );
00988 fclose( $file );
00989 }
00990
00998 protected function colorDiff( $text ) {
00999 return preg_replace(
01000 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
01001 array( $this->term->color( 34 ) . '$1' . $this->term->reset(),
01002 $this->term->color( 31 ) . '$1' . $this->term->reset() ),
01003 $text );
01004 }
01005
01011 public function showRunFile( $path ){
01012 print $this->term->color( 1 ) .
01013 "Reading tests from \"$path\"..." .
01014 $this->term->reset() .
01015 "\n";
01016 }
01017
01024 public function addArticle($name, $text, $line) {
01025 $this->setupGlobals();
01026 $title = Title::newFromText( $name );
01027 if ( is_null($title) ) {
01028 wfDie( "invalid title at line $line\n" );
01029 }
01030
01031 $aid = $title->getArticleID( GAID_FOR_UPDATE );
01032 if ($aid != 0) {
01033 wfDie( "duplicate article '$name' at line $line\n" );
01034 }
01035
01036 $art = new Article($title);
01037 $art->insertNewArticle($text, '', false, false );
01038
01039 $this->teardownGlobals();
01040 }
01041
01048 public function requireHook( $name ) {
01049 global $wgParser;
01050 $wgParser->firstCallInit( );
01051 if( isset( $wgParser->mTagHooks[$name] ) ) {
01052 $this->hooks[$name] = $wgParser->mTagHooks[$name];
01053 } else {
01054 wfDie( "This test suite requires the '$name' hook extension.\n" );
01055 }
01056 }
01057
01064 private function requireFunctionHook( $name ) {
01065 global $wgParser;
01066 $wgParser->firstCallInit( );
01067 if( isset( $wgParser->mFunctionHooks[$name] ) ) {
01068 $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
01069 } else {
01070 wfDie( "This test suite requires the '$name' function hook extension.\n" );
01071 }
01072 }
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082 private function tidy( $text ) {
01083 global $wgUseTidy;
01084 if ($wgUseTidy) {
01085 $text = Parser::tidy($text);
01086 }
01087 return $text;
01088 }
01089
01090 private function wellFormed( $text ) {
01091 $html =
01092 Sanitizer::hackDocType() .
01093 '<html>' .
01094 $text .
01095 '</html>';
01096
01097 $parser = xml_parser_create( "UTF-8" );
01098
01099 # case folding violates XML standard, turn it off
01100 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
01101
01102 if( !xml_parse( $parser, $html, true ) ) {
01103 $err = xml_error_string( xml_get_error_code( $parser ) );
01104 $position = xml_get_current_byte_index( $parser );
01105 $fragment = $this->extractFragment( $html, $position );
01106 $this->mXmlError = "$err at byte $position:\n$fragment";
01107 xml_parser_free( $parser );
01108 return false;
01109 }
01110 xml_parser_free( $parser );
01111 return true;
01112 }
01113
01114 private function extractFragment( $text, $position ) {
01115 $start = max( 0, $position - 10 );
01116 $before = $position - $start;
01117 $fragment = '...' .
01118 $this->term->color( 34 ) .
01119 substr( $text, $start, $before ) .
01120 $this->term->color( 0 ) .
01121 $this->term->color( 31 ) .
01122 $this->term->color( 1 ) .
01123 substr( $text, $position, 1 ) .
01124 $this->term->color( 0 ) .
01125 $this->term->color( 34 ) .
01126 substr( $text, $position + 1, 9 ) .
01127 $this->term->color( 0 ) .
01128 '...';
01129 $display = str_replace( "\n", ' ', $fragment );
01130 $caret = ' ' .
01131 str_repeat( ' ', $before ) .
01132 $this->term->color( 31 ) .
01133 '^' .
01134 $this->term->color( 0 );
01135 return "$display\n$caret";
01136 }
01137 }
01138
01139 class AnsiTermColorer {
01140 function __construct() {
01141 }
01142
01149 public function color( $color ) {
01150 global $wgCommandLineDarkBg;
01151 $light = $wgCommandLineDarkBg ? "1;" : "0;";
01152 return "\x1b[{$light}{$color}m";
01153 }
01154
01160 public function reset() {
01161 return $this->color( 0 );
01162 }
01163 }
01164
01165
01166 class DummyTermColorer {
01167 public function color( $color ) {
01168 return '';
01169 }
01170
01171 public function reset() {
01172 return '';
01173 }
01174 }
01175
01176 class TestRecorder {
01177 var $parent;
01178 var $term;
01179
01180 function __construct( $parent ) {
01181 $this->parent = $parent;
01182 $this->term = $parent->term;
01183 }
01184
01185 function start() {
01186 $this->total = 0;
01187 $this->success = 0;
01188 }
01189
01190 function record( $test, $result ) {
01191 $this->total++;
01192 $this->success += ($result ? 1 : 0);
01193 }
01194
01195 function end() {
01196
01197 }
01198
01199 function report() {
01200 if( $this->total > 0 ) {
01201 $this->reportPercentage( $this->success, $this->total );
01202 } else {
01203 wfDie( "No tests found.\n" );
01204 }
01205 }
01206
01207 function reportPercentage( $success, $total ) {
01208 $ratio = wfPercent( 100 * $success / $total );
01209 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
01210 if( $success == $total ) {
01211 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
01212 } else {
01213 $failed = $total - $success ;
01214 print $this->term->color( 31 ) . "$failed tests failed!";
01215 }
01216 print $this->term->reset() . "\n";
01217 return ($success == $total);
01218 }
01219 }
01220
01221 class DbTestPreviewer extends TestRecorder {
01222 protected $lb;
01223 protected $db;
01224 protected $curRun;
01225 protected $prevRun;
01226 protected $results;
01227
01231 function __construct( $parent ) {
01232 parent::__construct( $parent );
01233 $this->lb = wfGetLBFactory()->newMainLB();
01234
01235 $this->db = $this->lb->getConnection( DB_MASTER );
01236 }
01237
01242 function start() {
01243 global $wgDBtype;
01244 parent::start();
01245
01246 if( ! $this->db->tableExists( 'testrun' )
01247 or ! $this->db->tableExists( 'testitem' ) )
01248 {
01249 print "WARNING> `testrun` table not found in database.\n";
01250 $this->prevRun = false;
01251 } else {
01252
01253 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
01254 }
01255 $this->results = array();
01256 }
01257
01258 function record( $test, $result ) {
01259 parent::record( $test, $result );
01260 $this->results[$test] = $result;
01261 }
01262
01263 function report() {
01264 if( $this->prevRun ) {
01265
01266
01267 $table = array(
01268 'fp' => 'previously failing test(s) now PASSING! :)',
01269 'pn' => 'previously PASSING test(s) removed o_O',
01270 'np' => 'new PASSING test(s) :)',
01271
01272 'pf' => 'previously passing test(s) now FAILING! :(',
01273 'fn' => 'previously FAILING test(s) removed O_o',
01274 'nf' => 'new FAILING test(s) :(',
01275 'ff' => 'still FAILING test(s) :(',
01276 );
01277
01278 $prevResults = array();
01279
01280 $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ),
01281 array( 'ti_run' => $this->prevRun ), __METHOD__ );
01282 foreach ( $res as $row ) {
01283 if ( !$this->parent->regex
01284 || preg_match( "/{$this->parent->regex}/i", $row->ti_name ) )
01285 {
01286 $prevResults[$row->ti_name] = $row->ti_success;
01287 }
01288 }
01289
01290 $combined = array_keys( $this->results + $prevResults );
01291
01292 # Determine breakdown by change type
01293 $breakdown = array();
01294 foreach ( $combined as $test ) {
01295 if ( !isset( $prevResults[$test] ) ) {
01296 $before = 'n';
01297 } elseif ( $prevResults[$test] == 1 ) {
01298 $before = 'p';
01299 } else {
01300 $before = 'f';
01301 }
01302 if ( !isset( $this->results[$test] ) ) {
01303 $after = 'n';
01304 } elseif ( $this->results[$test] == 1 ) {
01305 $after = 'p';
01306 } else {
01307 $after = 'f';
01308 }
01309 $code = $before . $after;
01310 if ( isset( $table[$code] ) ) {
01311 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
01312 }
01313 }
01314
01315 # Write out results
01316 foreach ( $table as $code => $label ) {
01317 if( !empty( $breakdown[$code] ) ) {
01318 $count = count($breakdown[$code]);
01319 printf( "\n%4d %s\n", $count, $label );
01320 foreach ($breakdown[$code] as $differing_test_name => $statusInfo) {
01321 print " * $differing_test_name [$statusInfo]\n";
01322 }
01323 }
01324 }
01325 } else {
01326 print "No previous test runs to compare against.\n";
01327 }
01328 print "\n";
01329 parent::report();
01330 }
01331
01337 private function getTestStatusInfo($testname, $after) {
01338
01339
01340 if ( $after == 'n' ) {
01341 $changedRun = $this->db->selectField ( 'testitem',
01342 'MIN(ti_run)',
01343 array( 'ti_name' => $testname ),
01344 __METHOD__ );
01345 $appear = $this->db->selectRow ( 'testrun',
01346 array( 'tr_date', 'tr_mw_version' ),
01347 array( 'tr_id' => $changedRun ),
01348 __METHOD__ );
01349 return "First recorded appearance: "
01350 . date( "d-M-Y H:i:s", strtotime ( $appear->tr_date ) )
01351 . ", " . $appear->tr_mw_version;
01352 }
01353
01354
01355
01356 $conds = array(
01357 'ti_name' => $testname,
01358 'ti_success' => ($after == 'f' ? "1" : "0") );
01359 if ( $this->curRun ) {
01360 $conds[] = "ti_run != " . $this->db->addQuotes ( $this->curRun );
01361 }
01362
01363 $changedRun = $this->db->selectField ( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
01364
01365
01366 if ( is_null ( $changedRun ) ) {
01367 if ($after == "f") {
01368 return "Has never passed";
01369 } else {
01370 return "Has never failed";
01371 }
01372 }
01373
01374
01375
01376
01377 $pre = $this->db->selectRow ( 'testrun',
01378 array( 'tr_date', 'tr_mw_version' ),
01379 array( 'tr_id' => $changedRun ),
01380 __METHOD__ );
01381 $post = $this->db->selectRow ( 'testrun',
01382 array( 'tr_date', 'tr_mw_version' ),
01383 array( "tr_id > " . $this->db->addQuotes ( $changedRun) ),
01384 __METHOD__,
01385 array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
01386 );
01387
01388 if ( $post ) {
01389 $postDate = date( "d-M-Y H:i:s", strtotime ( $post->tr_date ) ) . ", {$post->tr_mw_version}";
01390 } else {
01391 $postDate = 'now';
01392 }
01393 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
01394 . date( "d-M-Y H:i:s", strtotime ( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
01395 . " and $postDate";
01396
01397 }
01398
01402 function end() {
01403 $this->lb->commitMasterChanges();
01404 $this->lb->closeAll();
01405 parent::end();
01406 }
01407
01408 }
01409
01410 class DbTestRecorder extends DbTestPreviewer {
01415 function start() {
01416 global $wgDBtype, $options;
01417 $this->db->begin();
01418
01419 if( ! $this->db->tableExists( 'testrun' )
01420 or ! $this->db->tableExists( 'testitem' ) )
01421 {
01422 print "WARNING> `testrun` table not found in database. Trying to create table.\n";
01423 if ($wgDBtype === 'postgres')
01424 $this->db->sourceFile( dirname(__FILE__) . '/testRunner.postgres.sql' );
01425 elseif ($wgDBtype === 'oracle')
01426 $this->db->sourceFile( dirname(__FILE__) . '/testRunner.ora.sql' );
01427 else
01428 $this->db->sourceFile( dirname(__FILE__) . '/testRunner.sql' );
01429 echo "OK, resuming.\n";
01430 }
01431
01432 parent::start();
01433
01434 $this->db->insert( 'testrun',
01435 array(
01436 'tr_date' => $this->db->timestamp(),
01437 'tr_mw_version' => isset( $options['setversion'] ) ?
01438 $options['setversion'] : SpecialVersion::getVersion(),
01439 'tr_php_version' => phpversion(),
01440 'tr_db_version' => $this->db->getServerVersion(),
01441 'tr_uname' => php_uname()
01442 ),
01443 __METHOD__ );
01444 if ($wgDBtype === 'postgres')
01445 $this->curRun = $this->db->currentSequenceValue('testrun_id_seq');
01446 else
01447 $this->curRun = $this->db->insertId();
01448 }
01449
01455 function record( $test, $result ) {
01456 parent::record( $test, $result );
01457 $this->db->insert( 'testitem',
01458 array(
01459 'ti_run' => $this->curRun,
01460 'ti_name' => $test,
01461 'ti_success' => $result ? 1 : 0,
01462 ),
01463 __METHOD__ );
01464 }
01465 }
01466
01467 class RemoteTestRecorder extends TestRecorder {
01468 function start() {
01469 parent::start();
01470 $this->results = array();
01471 $this->ping( 'running' );
01472 }
01473
01474 function record( $test, $result ) {
01475 parent::record( $test, $result );
01476 $this->results[$test] = (bool)$result;
01477 }
01478
01479 function end() {
01480 $this->ping( 'complete', $this->results );
01481 parent::end();
01482 }
01483
01492 function ping( $status, $results=false ) {
01493 global $wgParserTestRemote, $IP;
01494
01495 $remote = $wgParserTestRemote;
01496 $revId = SpecialVersion::getSvnRevision( $IP );
01497 $jsonResults = json_encode( $results );
01498
01499 if( !$remote ) {
01500 print "Can't do remote upload without configuring \$wgParserTestRemote!\n";
01501 exit( 1 );
01502 }
01503
01504
01505 $message = array(
01506 $remote['repo'],
01507 $remote['suite'],
01508 $revId,
01509 $status,
01510 );
01511 if( $status == "complete" ) {
01512 $message[] = $jsonResults;
01513 }
01514 $hmac = hash_hmac( "sha1", implode( "|", $message ), $remote['secret'] );
01515
01516 $postData = array(
01517 'action' => 'codetestupload',
01518 'format' => 'json',
01519 'repo' => $remote['repo'],
01520 'suite' => $remote['suite'],
01521 'rev' => $revId,
01522 'status' => $status,
01523 'hmac' => $hmac,
01524 );
01525 if( $status == "complete" ) {
01526 $postData['results'] = $jsonResults;
01527 }
01528 $response = $this->post( $remote['api-url'], $postData );
01529
01530 if( $response === false ) {
01531 print "CodeReview info upload failed to reach server.\n";
01532 exit( 1 );
01533 }
01534 $responseData = json_decode( $response, true );
01535 if( !is_array( $responseData ) ) {
01536 print "CodeReview API response not recognized...\n";
01537 wfDebug( "Unrecognized CodeReview API response: $response\n" );
01538 exit( 1 );
01539 }
01540 if( isset( $responseData['error'] ) ) {
01541 $code = $responseData['error']['code'];
01542 $info = $responseData['error']['info'];
01543 print "CodeReview info upload failed: $code $info\n";
01544 exit( 1 );
01545 }
01546 }
01547
01548 function post( $url, $data ) {
01549 return Http::post( $url, array( 'postData' => $data) );
01550 }
01551 }
01552
01553 class TestFileIterator implements Iterator {
01554 private $file;
01555 private $fh;
01556 private $parser;
01557 private $index = 0;
01558 private $test;
01559 private $lineNum;
01560 private $eof;
01561
01562 function __construct( $file, $parser = null ) {
01563 global $IP;
01564
01565 $this->file = $file;
01566 $this->fh = fopen($this->file, "rt");
01567 if( !$this->fh ) {
01568 wfDie( "Couldn't open file '$file'\n" );
01569 }
01570
01571 $this->parser = $parser;
01572
01573 if( $this->parser ) $this->parser->showRunFile( wfRelativePath( $this->file, $IP ) );
01574 $this->lineNum = $this->index = 0;
01575 }
01576
01577 function setParser( ParserTest $parser ) {
01578 $this->parser = $parser;
01579 }
01580
01581 function rewind() {
01582 if(fseek($this->fh, 0)) {
01583 wfDie( "Couldn't fseek to the start of '$filename'\n" );
01584 }
01585 $this->index = 0;
01586 $this->lineNum = 0;
01587 $this->eof = false;
01588 $this->readNextTest();
01589
01590 return true;
01591 }
01592
01593 function current() {
01594 return $this->test;
01595 }
01596
01597 function key() {
01598 return $this->index;
01599 }
01600
01601 function next() {
01602 if($this->readNextTest()) {
01603 $this->index++;
01604 return true;
01605 } else {
01606 $this->eof = true;
01607 }
01608 }
01609
01610 function valid() {
01611 return $this->eof != true;
01612 }
01613
01614 function readNextTest() {
01615 $data = array();
01616 $section = null;
01617
01618 while( false !== ($line = fgets( $this->fh ) ) ) {
01619 $this->lineNum++;
01620 $matches = array();
01621 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
01622 $section = strtolower( $matches[1] );
01623 if( $section == 'endarticle') {
01624 if( !isset( $data['text'] ) ) {
01625 wfDie( "'endarticle' without 'text' at line {$this->lineNum} of $filename\n" );
01626 }
01627 if( !isset( $data['article'] ) ) {
01628 wfDie( "'endarticle' without 'article' at line {$this->lineNum} of $filename\n" );
01629 }
01630 if( $this->parser ) $this->parser->addArticle($this->parser->chomp($data['article']), $this->parser->chomp($data['text']),
01631 $this->lineNum);
01632 $data = array();
01633 $section = null;
01634 continue;
01635 }
01636 if( $section == 'endhooks' ) {
01637 if( !isset( $data['hooks'] ) ) {
01638 wfDie( "'endhooks' without 'hooks' at line {$this->lineNum} of $filename\n" );
01639 }
01640 foreach( explode( "\n", $data['hooks'] ) as $line ) {
01641 $line = trim( $line );
01642 if( $line ) {
01643 if( $this->parser ) $this->parser->requireHook( $line );
01644 }
01645 }
01646 $data = array();
01647 $section = null;
01648 continue;
01649 }
01650 if( $section == 'endfunctionhooks' ) {
01651 if( !isset( $data['functionhooks'] ) ) {
01652 wfDie( "'endfunctionhooks' without 'functionhooks' at line {$this->lineNum} of $filename\n" );
01653 }
01654 foreach( explode( "\n", $data['functionhooks'] ) as $line ) {
01655 $line = trim( $line );
01656 if( $line ) {
01657 if( $this->parser ) $this->parser->requireFunctionHook( $line );
01658 }
01659 }
01660 $data = array();
01661 $section = null;
01662 continue;
01663 }
01664 if( $section == 'end' ) {
01665 if( !isset( $data['test'] ) ) {
01666 wfDie( "'end' without 'test' at line {$this->lineNum} of $filename\n" );
01667 }
01668 if( !isset( $data['input'] ) ) {
01669 wfDie( "'end' without 'input' at line {$this->lineNum} of $filename\n" );
01670 }
01671 if( !isset( $data['result'] ) ) {
01672 wfDie( "'end' without 'result' at line {$this->lineNum} of $filename\n" );
01673 }
01674 if( !isset( $data['options'] ) ) {
01675 $data['options'] = '';
01676 }
01677 if (!isset( $data['config'] ) )
01678 $data['config'] = '';
01679
01680 if ( $this->parser && (preg_match('/\\bdisabled\\b/i', $data['options'])
01681 || !preg_match("/{$this->parser->regex}/i", $data['test'])) && !$this->parser->runDisabled ) {
01682 # disabled test
01683 $data = array();
01684 $section = null;
01685 continue;
01686 }
01687 if ( $this->parser &&
01688 preg_match('/\\bmath\\b/i', $data['options']) && !$this->parser->savedGlobals['wgUseTeX'] ) {
01689 # don't run math tests if $wgUseTeX is set to false in LocalSettings
01690 $data = array();
01691 $section = null;
01692 continue;
01693 }
01694
01695 if( $this->parser ) {
01696 $this->test = array(
01697 'test' => $this->parser->chomp( $data['test'] ),
01698 'input' => $this->parser->chomp( $data['input'] ),
01699 'result' => $this->parser->chomp( $data['result'] ),
01700 'options' => $this->parser->chomp( $data['options'] ),
01701 'config' => $this->parser->chomp( $data['config'] ) );
01702 } else {
01703 $this->test['test'] = $data['test'];
01704 }
01705 return true;
01706 }
01707 if ( isset ($data[$section] ) ) {
01708 wfDie( "duplicate section '$section' at line {$this->lineNum} of $filename\n" );
01709 }
01710 $data[$section] = '';
01711 continue;
01712 }
01713 if( $section ) {
01714 $data[$section] .= $line;
01715 }
01716 }
01717 return false;
01718 }
01719 }