repaired gwicke's horrible mangling of brace expansion, partial support for nested...
authorTim Starling <tstarling@users.mediawiki.org>
Sat, 29 May 2004 05:19:27 +0000 (05:19 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sat, 29 May 2004 05:19:27 +0000 (05:19 +0000)
includes/Parser.php

index 0afa39d..003198a 100644 (file)
@@ -1216,18 +1216,23 @@ class Parser
                        $this->initialiseVariables();
                }
                $titleChars = Title::legalChars();
+               $nonBraceChars = str_replace( array( "{", "}" ), array( "", "" ), $titleChars );
 
                # This function is called recursively. To keep track of arguments we need a stack:
                array_push( $this->mArgStack, $args );
 
                # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
                $GLOBALS['wgCurParser'] =& $this;
+               
 
-               # Argument substitution
                if ( $this->mOutputType == OT_HTML ) {
+                       # Variable substitution
+                       $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", "wfVariableSubstitution", $text );
+                       
+                       # Argument substitution
                        $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", "wfArgSubstitution", $text );
                }
-               # Double brace substitution
+               # Template substitution
                $regex = "/(\\n?){{([$titleChars]*)(\\|.*?|)}}/s";
                $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
 
@@ -1237,6 +1242,17 @@ class Parser
                return $text;
        }
 
+       function variableSubstitution( $matches )
+       {
+               if ( array_key_exists( $matches[1], $this->mVariables ) ) {
+                       $text = $this->mVariables[$matches[1]];
+                       $this->mOutput->mContainsOldMagic = true;
+               } else {
+                       $text = $matches[0];
+               }
+               return $text;
+       }
+
        function braceSubstitution( $matches )
        {
                global $wgLinkCache, $wgLang;
@@ -2091,31 +2107,6 @@ class ParserOptions
 function wfBraceSubstitution( $matches )
 {
        global $wgCurParser;
-       $titleChars = Title::legalChars();
-
-       # not really nested stuff, just multiple includes separated by titlechars
-       if(preg_match("/^([^}{]*)}}([^}{]*{{)(.*)$/s", $matches[2], $m)) {
-               $text = wfInternalBraceSubstitution( $m[1] );
-               $string = $text.$m[2].$m[3];
-               $i = 0;
-               while(preg_match("/^([^}{]*){{([$titleChars]*?)(}}[^}{]*{{.*)?$/s", $string, $m) && ($i < 30)) {
-                       $text = wfInternalBraceSubstitution( $m[2] );
-                       $trail = !empty($m[3])? preg_replace("/^}}/", '', $m[3]):'';
-                       $string = $m[1].$text.$trail;
-                       $i++;
-               }
-               return $string;
-       }
-               
-       # Double brace substitution, expand bar in {{foo{{bar}}}}
-       $i = 0;
-       while(preg_match("/{{([$titleChars]*?)}}/", $matches[2], $internalmatches) && ($i < 30)) {
-               $text = wfInternalBraceSubstitution( $internalmatches[1] );
-               $matches[0] = str_replace($internalmatches[0], $text , $matches[0]);
-               $matches[2] = str_replace($internalmatches[0], $text , $matches[2]);
-               $i++;
-       }
-
        return $wgCurParser->braceSubstitution( $matches );
 }
 
@@ -2125,135 +2116,10 @@ function wfArgSubstitution( $matches )
        return $wgCurParser->argSubstitution( $matches );
 }
 
-# XXX: i don't think this is the most elegant way to do it..
-function wfInternalBraceSubstitution( $part1 ) {
-       global $wgLinkCache, $wgLang, $wgCurParser;
-       $fname = "wfInternalBraceSubstitution";
-       $found = false;
-       $nowiki = false;
-       $noparse = false;
-
-       $title = NULL;
-
-       # $newline is an optional newline character before the braces
-       # $part1 is the bit before the first |, and must contain only title characters
-       # $args is a list of arguments, starting from index 0, not including $part1
-
-       # SUBST
-       if ( !$found ) {
-               $mwSubst =& MagicWord::get( MAG_SUBST );
-               if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
-                       if ( $wgCurParser->mOutputType != OT_WIKI ) {
-                               # Invalid SUBST not replaced at PST time
-                               # Return without further processing
-                               $text = $matches[0];
-                               $found = true;
-                               $noparse= true;
-                       }
-               } elseif ( $wgCurParser->mOutputType == OT_WIKI ) {
-                       # SUBST not found in PST pass, do nothing
-                       $text = $matches[0];
-                       $found = true;
-               }
-       }
-
-       # MSG, MSGNW and INT
-       if ( !$found ) {
-               # Check for MSGNW:
-               $mwMsgnw =& MagicWord::get( MAG_MSGNW );
-               if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
-                       $nowiki = true;
-               } else {
-                       # Remove obsolete MSG:
-                       $mwMsg =& MagicWord::get( MAG_MSG );
-                       $mwMsg->matchStartAndRemove( $part1 );
-               }
-
-               # Check if it is an internal message
-               $mwInt =& MagicWord::get( MAG_INT );
-               if ( $mwInt->matchStartAndRemove( $part1 ) ) {
-                       if ( $wgCurParser->incrementIncludeCount( "int:$part1" ) ) {
-                               $text = wfMsgReal( $part1, array(), true );
-                               $found = true;
-                       }
-               }
-       }
-
-       # NS
-       if ( !$found ) {
-               # Check for NS: (namespace expansion)
-               $mwNs = MagicWord::get( MAG_NS );
-               if ( $mwNs->matchStartAndRemove( $part1 ) ) {
-                       if ( intval( $part1 ) ) {
-                               $text = $wgLang->getNsText( intval( $part1 ) );
-                               $found = true;
-                       } else {
-                               $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
-                               if ( !is_null( $index ) ) {
-                                       $text = $wgLang->getNsText( $index );
-                                       $found = true;
-                               }
-                       }
-               }
-       }
-
-       # LOCALURL and LOCALURLE
-       if ( !$found ) {
-               $mwLocal = MagicWord::get( MAG_LOCALURL );
-               $mwLocalE = MagicWord::get( MAG_LOCALURLE );
-
-               if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
-                       $func = 'getLocalURL';
-               } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
-                       $func = 'escapeLocalURL';
-               } else {
-                       $func = '';
-               }
-
-               if ( $func !== '' ) {
-                       $title = Title::newFromText( $part1 );
-                       if ( !is_null( $title ) ) {
-                               $text = $title->$func();
-                               $found = true;
-                       }
-               }
-       }
-
-       # Internal variables
-       if ( !$found && array_key_exists( $part1, $wgCurParser->mVariables ) ) {
-               $text = $wgCurParser->mVariables[$part1];
-               $found = true;
-               $wgCurParser->mOutput->mContainsOldMagic = true;
-       }
-
-       # Load from database
-       if ( !$found ) {
-               $title = Title::newFromText( $part1, NS_TEMPLATE );
-               if ( !is_null( $title ) && !$title->isExternal() ) {
-                       # Check for excessive inclusion
-                       $dbk = $title->getPrefixedDBkey();
-                       if ( $wgCurParser->incrementIncludeCount( $dbk ) ) {
-                               $article = new Article( $title );
-                               $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
-                               if ( $articleContent !== false ) {
-                                       $found = true;
-                                       $text = $articleContent;
-
-                               }
-                       }
-
-                       # If the title is valid but undisplayable, make a link to it
-                       if ( $wgCurParser->mOutputType == OT_HTML && !$found ) {
-                               $text = "[[" . $title->getPrefixedText() . "]]";
-                               $found = true;
-                       }
-               }
-       }
-
-       if ( !$found ) {
-               return $matches[0];
-       } else {
-               return $text;
-       }
+function wfVariableSubstitution( $matches )
+{
+       global $wgCurParser;
+       return $wgCurParser->variableSubstitution( $matches );
 }
+
 ?>