clean r61713 (and r61710) per code review
authorConrad Irwin <conrad@users.mediawiki.org>
Mon, 15 Feb 2010 09:34:51 +0000 (09:34 +0000)
committerConrad Irwin <conrad@users.mediawiki.org>
Mon, 15 Feb 2010 09:34:51 +0000 (09:34 +0000)
includes/MagicWord.php
includes/parser/Parser.php

index 87efa55..fe30215 100644 (file)
@@ -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;
        }
 }
index d6f8d08..3bc6a1c 100644 (file)
@@ -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;