r53508 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53507‎ | r53508 | r53509 >
Date:01:40, 20 July 2009
Author:brion
Status:resolved
Tags:
Comment:
Finally add a damn string->array parser for parser test case options, so it's easier to add more.
Modified paths:
  • /trunk/phase3/maintenance/parserTests.inc (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/parserTests.inc
@@ -424,51 +424,52 @@
425425 $this->showTesting( $desc );
426426 }
427427
 428+ $opts = $this->parseOptions( $opts );
428429 $this->setupGlobals($opts, $config);
429430
430431 $user = new User();
431432 $options = ParserOptions::newFromUser( $user );
432433
433 - if (preg_match('/\\bmath\\b/i', $opts)) {
 434+ if ( isset( $opts['math'] ) ) {
434435 # XXX this should probably be done by the ParserOptions
435436 $options->setUseTex(true);
436437 }
437438
438439 $m = array();
439 - if (preg_match('/title=\[\[(.*)\]\]/', $opts, $m)) {
440 - $titleText = $m[1];
 440+ if (isset( $opts['title'] ) ) {
 441+ $titleText = $opts['title'];
441442 }
442443 else {
443444 $titleText = 'Parser test';
444445 }
445446
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'] );
448449 $parser = $this->getParser();
449450 $title =& Title::makeTitle( NS_MAIN, $titleText );
450451
451452 $matches = array();
452 - if (preg_match('/\\bpst\\b/i', $opts)) {
 453+ if( isset( $opts['pst'] ) ) {
453454 $out = $parser->preSaveTransform( $input, $title, $user, $options );
454 - } elseif (preg_match('/\\bmsg\\b/i', $opts)) {
 455+ } elseif( isset( $opts['msg'] ) ) {
455456 $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'];
458459 $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];
462463 $out = $parser->replaceSection( $input, $section, $replace );
463 - } elseif( preg_match( '/\\bcomment\\b/i', $opts ) ) {
 464+ } elseif( isset( $opts['comment'] ) ) {
464465 $linker = $user->getSkin();
465466 $out = $linker->formatComment( $input, $title, $local );
466467 } else {
467468 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
468469 $out = $output->getText();
469470
470 - if (preg_match('/\\bill\\b/i', $opts)) {
 471+ if (isset( $opts['ill'] ) ) {
471472 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
472 - } else if (preg_match('/\\bcat\\b/i', $opts)) {
 473+ } elseif( isset( $opts['cat'] ) ) {
473474 global $wgOut;
474475 $wgOut->addCategoryLinks($output->getCategories());
475476 $cats = $wgOut->getCategoryLinks();
@@ -494,19 +495,84 @@
495496
496497 /**
497498 * 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
501502 */
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];
506506 } else {
507507 return $default;
508508 }
509509 }
 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';
510549
 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+
511577 /**
512578 * Set up the global variables for a consistent environment for each test.
513579 * Ideally this should replace the global configuration entirely.
@@ -518,13 +584,13 @@
519585
520586 # Find out values for some special options.
521587 $lang =
522 - self::getOptionValue( '/language=([a-z]+(?:_[a-z]+)?)/', $opts, 'en' );
 588+ self::getOptionValue( 'language', $opts, 'en' );
523589 $variant =
524 - self::getOptionValue( '/variant=([a-z]+(?:-[a-z]+)?)/', $opts, false );
 590+ self::getOptionValue( 'variant', $opts, false );
525591 $maxtoclevel =
526 - self::getOptionValue( '/wgMaxTocLevel=(\d+)/', $opts, 999 );
 592+ self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
527593 $linkHolderBatchSize =
528 - self::getOptionValue( '/wgLinkHolderBatchSize=(\d+)/', $opts, 1000 );
 594+ self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
529595
530596 $settings = array(
531597 'wgServer' => 'http://localhost',
@@ -547,10 +613,10 @@
548614 'wgLanguageCode' => $lang,
549615 'wgContLanguageCode' => $lang,
550616 'wgDBprefix' => 'parsertest_',
551 - 'wgRawHtml' => preg_match('/\\brawhtml\\b/i', $opts),
 617+ 'wgRawHtml' => isset( $opts['rawhtml'] ),
552618 'wgLang' => null,
553619 'wgContLang' => null,
554 - 'wgNamespacesWithSubpages' => array( 0 => preg_match('/\\bsubpage\\b/i', $opts)),
 620+ 'wgNamespacesWithSubpages' => array( 0 => isset( $opts['subpage'] ) ),
555621 'wgMaxTocLevel' => $maxtoclevel,
556622 'wgCapitalLinks' => true,
557623 'wgNoFollowLinks' => true,

Status & tagging log