From 8b23dcc084c5efad84cd1051ac1170d7c20faa26 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 15 Feb 2010 09:34:51 +0000 Subject: [PATCH] clean r61713 (and r61710) per code review --- includes/MagicWord.php | 30 ++++++++++++++++-------------- includes/parser/Parser.php | 27 +++++++++++++++++++-------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/includes/MagicWord.php b/includes/MagicWord.php index 87efa552c5..fe30215595 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -572,7 +572,7 @@ class MagicWordArray { } /** - * Get an unanchored regex + * Get an unanchored regex that does not match parameters */ function getRegex() { if ( is_null( $this->regex ) ) { @@ -589,29 +589,29 @@ class MagicWordArray { } /** - * Get a regex for matching variables + * Get a regex for matching variables with parameters */ function getVariableRegex() { return str_replace( "\\$1", "(.*?)", $this->getRegex() ); } /** - * Get a regex for matching a prefix. Does not match parameters. + * Get a regex anchored to the start of the string that does not match parameters */ function getRegexStart() { $base = $this->getBaseRegex(); $newRegex = array( '', '' ); if ( $base[0] !== '' ) { - $newRegex[0] = str_replace( "\\$1", "", "/^(?:{$base[0]})/iuS" ); + $newRegex[0] = "/^(?:{$base[0]})/iuS"; } if ( $base[1] !== '' ) { - $newRegex[1] = str_replace( "\\$1", "", "/^(?:{$base[1]})/S" ); + $newRegex[1] = "/^(?:{$base[1]})/S"; } return $newRegex; } /** - * Get an anchored regex for matching variables + * Get an anchored regex for matching variables with parameters */ function getVariableStartToEndRegex() { $base = $this->getBaseRegex(); @@ -710,22 +710,24 @@ class MagicWordArray { } /** - * Returns the magic word id removed from the start, or false - * does not match parameters. + * Return the ID of the magic word at the start of $text, and remove + * the prefix from $text. + * Return false if no match found and $text is not modified. + * Does not match parameters. */ public function matchStartAndRemove( &$text ) { - $found = FALSE; $regexes = $this->getRegexStart(); foreach ( $regexes as $regex ) { if ( $regex === '' ) { continue; } - preg_match_all( $regex, $text, $matches, PREG_SET_ORDER ); - foreach ( $matches as $m ) { - list( $found, $param ) = $this->parseMatch( $m ); + preg_match( $regex, $text, $match ); + if ( $match ) { + list( $found, $param ) = $this->parseMatch( $match ); + $text = substr( $text, strlen( $found ) + 1 ); + return $found; } - $text = preg_replace( $regex, '', $text ); } - return $found; + return false; } } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index d6f8d083c3..3bc6a1ced2 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -94,7 +94,7 @@ class Parser */ # Persistent: var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables, - $mSubsts, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex, + $mSubstWords, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex, $mPreprocessor, $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList, $mVarCache, $mConf, $mFunctionTagHooks; @@ -2690,7 +2690,7 @@ class Parser $substIDs = MagicWord::getSubstIDs(); $this->mVariables = new MagicWordArray( $variableIDs ); - $this->mSubsts = new MagicWordArray( $substIDs ); + $this->mSubstWords = new MagicWordArray( $substIDs ); wfProfileOut( __METHOD__ ); } @@ -2862,14 +2862,25 @@ class Parser wfProfileIn( __METHOD__.'-modifiers' ); if ( !$found ) { - $substMatch = $this->mSubsts->matchStartAndRemove( $part1 ); + $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 ); # Possibilities for substMatch: "subst", "safesubst" or FALSE - # Whether to include depends also on whether we are in the pre-save-transform - # - # safesubst || (subst && PST) || (false && !PST) => transclude (skip the if) - # (false && PST) || (subst && !PST) => return input (handled by if) - if ( $substMatch != 'safesubst' && ($substMatch == 'subst' xor $this->ot['wiki']) ) { + # Decide whether to expand template or keep wikitext as-is. + if ( $this->ot['wiki'] ) + { + if ( $substMatch === false ) { + $literal = true; # literal when in PST with no prefix + } else { + $literal = false; # expand when in PST with subst: or safesubst: + } + } else { + if ( $substMatch == 'subst' ) { + $literal = true; # literal when not in PST with plain subst: + } else { + $literal = false; # expand when not in PST with safesubst: or no prefix + } + } + if ( $literal ) { $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args ); $isLocalObj = true; $found = true; -- 2.20.1