Index: trunk/phase3/maintenance/parserTests.inc |
— | — | @@ -424,51 +424,52 @@ |
425 | 425 | $this->showTesting( $desc ); |
426 | 426 | } |
427 | 427 | |
| 428 | + $opts = $this->parseOptions( $opts ); |
428 | 429 | $this->setupGlobals($opts, $config); |
429 | 430 | |
430 | 431 | $user = new User(); |
431 | 432 | $options = ParserOptions::newFromUser( $user ); |
432 | 433 | |
433 | | - if (preg_match('/\\bmath\\b/i', $opts)) { |
| 434 | + if ( isset( $opts['math'] ) ) { |
434 | 435 | # XXX this should probably be done by the ParserOptions |
435 | 436 | $options->setUseTex(true); |
436 | 437 | } |
437 | 438 | |
438 | 439 | $m = array(); |
439 | | - if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) { |
440 | | - $titleText = $m[1]; |
| 440 | + if (isset( $opts['title'] ) ) { |
| 441 | + $titleText = $opts['title']; |
441 | 442 | } |
442 | 443 | else { |
443 | 444 | $titleText = 'Parser test'; |
444 | 445 | } |
445 | 446 | |
446 | | - $noxml = (bool)preg_match( '~\\b noxml \\b~x', $opts ); |
447 | | - $local = (bool)preg_match( '~\\b local \\b~x', $opts ); |
| 447 | + $noxml = isset( $opts['noxml'] ); |
| 448 | + $local = isset( $opts['local'] ); |
448 | 449 | $parser = $this->getParser(); |
449 | 450 | $title =& Title::makeTitle( NS_MAIN, $titleText ); |
450 | 451 | |
451 | 452 | $matches = array(); |
452 | | - if (preg_match('/\\bpst\\b/i', $opts)) { |
| 453 | + if( isset( $opts['pst'] ) ) { |
453 | 454 | $out = $parser->preSaveTransform( $input, $title, $user, $options ); |
454 | | - } elseif (preg_match('/\\bmsg\\b/i', $opts)) { |
| 455 | + } elseif( isset( $opts['msg'] ) ) { |
455 | 456 | $out = $parser->transformMsg( $input, $options ); |
456 | | - } elseif( preg_match( '/\\bsection=([\w-]+)\b/i', $opts, $matches ) ) { |
457 | | - $section = $matches[1]; |
| 457 | + } elseif( isset( $opts['section'] ) ) { |
| 458 | + $section = $opts['section']; |
458 | 459 | $out = $parser->getSection( $input, $section ); |
459 | | - } elseif( preg_match( '/\\breplace=([\w-]+),"(.*?)"/i', $opts, $matches ) ) { |
460 | | - $section = $matches[1]; |
461 | | - $replace = $matches[2]; |
| 460 | + } elseif( isset( $opts['replace'] ) ) { |
| 461 | + $section = $opts['replace'][0]; |
| 462 | + $replace = $opts['replace'][1]; |
462 | 463 | $out = $parser->replaceSection( $input, $section, $replace ); |
463 | | - } elseif( preg_match( '/\\bcomment\\b/i', $opts ) ) { |
| 464 | + } elseif( isset( $opts['comment'] ) ) { |
464 | 465 | $linker = $user->getSkin(); |
465 | 466 | $out = $linker->formatComment( $input, $title, $local ); |
466 | 467 | } else { |
467 | 468 | $output = $parser->parse( $input, $title, $options, true, true, 1337 ); |
468 | 469 | $out = $output->getText(); |
469 | 470 | |
470 | | - if (preg_match('/\\bill\\b/i', $opts)) { |
| 471 | + if (isset( $opts['ill'] ) ) { |
471 | 472 | $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) ); |
472 | | - } else if (preg_match('/\\bcat\\b/i', $opts)) { |
| 473 | + } elseif( isset( $opts['cat'] ) ) { |
473 | 474 | global $wgOut; |
474 | 475 | $wgOut->addCategoryLinks($output->getCategories()); |
475 | 476 | $cats = $wgOut->getCategoryLinks(); |
— | — | @@ -494,19 +495,84 @@ |
495 | 496 | |
496 | 497 | /** |
497 | 498 | * Use a regex to find out the value of an option |
498 | | - * @param $regex A regex, the first group will be the value returned |
499 | | - * @param $opts Options line to look in |
500 | | - * @param $defaults Default value returned if the regex does not match |
| 499 | + * @param $key name of option val to retrieve |
| 500 | + * @param $opts Options array to look in |
| 501 | + * @param $defaults Default value returned if not found |
501 | 502 | */ |
502 | | - private static function getOptionValue( $regex, $opts, $default ) { |
503 | | - $m = array(); |
504 | | - if( preg_match( $regex, $opts, $m ) ) { |
505 | | - return $m[1]; |
| 503 | + private static function getOptionValue( $key, $opts, $default ) { |
| 504 | + if( isset( $opts[$key] ) ) { |
| 505 | + return $opts[$key]; |
506 | 506 | } else { |
507 | 507 | return $default; |
508 | 508 | } |
509 | 509 | } |
| 510 | + |
| 511 | + private function parseOptions( $instring ) { |
| 512 | + $opts = array(); |
| 513 | + $lines = explode( "\n", $instring ); |
| 514 | + // foo |
| 515 | + // foo=bar |
| 516 | + // foo="bar baz" |
| 517 | + // foo=[[bar baz]] |
| 518 | + // foo=bar,"baz quux" |
| 519 | + $regex = '/\b |
| 520 | + ([\w-]+) # Key |
| 521 | + \b |
| 522 | + (?:\s* |
| 523 | + = # First sub-value |
| 524 | + \s* |
| 525 | + ( |
| 526 | + " |
| 527 | + [^"]* # Quoted val |
| 528 | + " |
| 529 | + | |
| 530 | + \[\[ |
| 531 | + [^]]* # Link target |
| 532 | + \]\] |
| 533 | + | |
| 534 | + [\w-]+ # Plain word |
| 535 | + ) |
| 536 | + (?:\s* |
| 537 | + , # Sub-vals 1..N |
| 538 | + \s* |
| 539 | + ( |
| 540 | + "[^"]*" # Quoted val |
| 541 | + | |
| 542 | + \[\[[^]]*\]\] # Link target |
| 543 | + | |
| 544 | + [\w-]+ # Plain word |
| 545 | + ) |
| 546 | + )* |
| 547 | + )? |
| 548 | + /x'; |
510 | 549 | |
| 550 | + if( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) { |
| 551 | + foreach( $matches as $bits ) { |
| 552 | + $match = array_shift( $bits ); |
| 553 | + $key = strtolower( array_shift( $bits ) ); |
| 554 | + if( count( $bits ) == 0 ) { |
| 555 | + $opts[$key] = true; |
| 556 | + } elseif( count( $bits ) == 1 ) { |
| 557 | + $opts[$key] = $this->cleanupOption( array_shift( $bits ) ); |
| 558 | + } else { |
| 559 | + // Array! |
| 560 | + $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits ); |
| 561 | + } |
| 562 | + } |
| 563 | + } |
| 564 | + return $opts; |
| 565 | + } |
| 566 | + |
| 567 | + private function cleanupOption( $opt ) { |
| 568 | + if( substr( $opt, 0, 1 ) == '"' ) { |
| 569 | + return substr( $opt, 1, -1 ); |
| 570 | + } |
| 571 | + if( substr( $opt, 0, 2 ) == '[[' ) { |
| 572 | + return substr( $opt, 2, -2 ); |
| 573 | + } |
| 574 | + return $opt; |
| 575 | + } |
| 576 | + |
511 | 577 | /** |
512 | 578 | * Set up the global variables for a consistent environment for each test. |
513 | 579 | * Ideally this should replace the global configuration entirely. |
— | — | @@ -518,13 +584,13 @@ |
519 | 585 | |
520 | 586 | # Find out values for some special options. |
521 | 587 | $lang = |
522 | | - self::getOptionValue( '/language=([a-z]+(?:_[a-z]+)?)/', $opts, 'en' ); |
| 588 | + self::getOptionValue( 'language', $opts, 'en' ); |
523 | 589 | $variant = |
524 | | - self::getOptionValue( '/variant=([a-z]+(?:-[a-z]+)?)/', $opts, false ); |
| 590 | + self::getOptionValue( 'variant', $opts, false ); |
525 | 591 | $maxtoclevel = |
526 | | - self::getOptionValue( '/wgMaxTocLevel=(\d+)/', $opts, 999 ); |
| 592 | + self::getOptionValue( 'wgMaxTocLevel', $opts, 999 ); |
527 | 593 | $linkHolderBatchSize = |
528 | | - self::getOptionValue( '/wgLinkHolderBatchSize=(\d+)/', $opts, 1000 ); |
| 594 | + self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 ); |
529 | 595 | |
530 | 596 | $settings = array( |
531 | 597 | 'wgServer' => 'http://localhost', |
— | — | @@ -547,10 +613,10 @@ |
548 | 614 | 'wgLanguageCode' => $lang, |
549 | 615 | 'wgContLanguageCode' => $lang, |
550 | 616 | 'wgDBprefix' => 'parsertest_', |
551 | | - 'wgRawHtml' => preg_match('/\\brawhtml\\b/i', $opts), |
| 617 | + 'wgRawHtml' => isset( $opts['rawhtml'] ), |
552 | 618 | 'wgLang' => null, |
553 | 619 | 'wgContLang' => null, |
554 | | - 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)), |
| 620 | + 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ), |
555 | 621 | 'wgMaxTocLevel' => $maxtoclevel, |
556 | 622 | 'wgCapitalLinks' => true, |
557 | 623 | 'wgNoFollowLinks' => true, |