r3736 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r3735‎ | r3736 | r3737 >
Date:05:19, 29 May 2004
Author:timstarling
Status:old
Tags:
Comment:
repaired gwicke's horrible mangling of brace expansion, partial support for nested expansion (restoring 1.2 capabilities)
Modified paths:
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -1216,18 +1216,23 @@
12171217 $this->initialiseVariables();
12181218 }
12191219 $titleChars = Title::legalChars();
 1220+ $nonBraceChars = str_replace( array( "{", "}" ), array( "", "" ), $titleChars );
12201221
12211222 # This function is called recursively. To keep track of arguments we need a stack:
12221223 array_push( $this->mArgStack, $args );
12231224
12241225 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
12251226 $GLOBALS['wgCurParser'] =& $this;
 1227+
12261228
1227 - # Argument substitution
12281229 if ( $this->mOutputType == OT_HTML ) {
 1230+ # Variable substitution
 1231+ $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", "wfVariableSubstitution", $text );
 1232+
 1233+ # Argument substitution
12291234 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", "wfArgSubstitution", $text );
12301235 }
1231 - # Double brace substitution
 1236+ # Template substitution
12321237 $regex = "/(\\n?){{([$titleChars]*)(\\|.*?|)}}/s";
12331238 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
12341239
@@ -1237,6 +1242,17 @@
12381243 return $text;
12391244 }
12401245
 1246+ function variableSubstitution( $matches )
 1247+ {
 1248+ if ( array_key_exists( $matches[1], $this->mVariables ) ) {
 1249+ $text = $this->mVariables[$matches[1]];
 1250+ $this->mOutput->mContainsOldMagic = true;
 1251+ } else {
 1252+ $text = $matches[0];
 1253+ }
 1254+ return $text;
 1255+ }
 1256+
12411257 function braceSubstitution( $matches )
12421258 {
12431259 global $wgLinkCache, $wgLang;
@@ -2091,31 +2107,6 @@
20922108 function wfBraceSubstitution( $matches )
20932109 {
20942110 global $wgCurParser;
2095 - $titleChars = Title::legalChars();
2096 -
2097 - # not really nested stuff, just multiple includes separated by titlechars
2098 - if(preg_match("/^([^}{]*)}}([^}{]*{{)(.*)$/s", $matches[2], $m)) {
2099 - $text = wfInternalBraceSubstitution( $m[1] );
2100 - $string = $text.$m[2].$m[3];
2101 - $i = 0;
2102 - while(preg_match("/^([^}{]*){{([$titleChars]*?)(}}[^}{]*{{.*)?$/s", $string, $m) && ($i < 30)) {
2103 - $text = wfInternalBraceSubstitution( $m[2] );
2104 - $trail = !empty($m[3])? preg_replace("/^}}/", '', $m[3]):'';
2105 - $string = $m[1].$text.$trail;
2106 - $i++;
2107 - }
2108 - return $string;
2109 - }
2110 -
2111 - # Double brace substitution, expand bar in {{foo{{bar}}}}
2112 - $i = 0;
2113 - while(preg_match("/{{([$titleChars]*?)}}/", $matches[2], $internalmatches) && ($i < 30)) {
2114 - $text = wfInternalBraceSubstitution( $internalmatches[1] );
2115 - $matches[0] = str_replace($internalmatches[0], $text , $matches[0]);
2116 - $matches[2] = str_replace($internalmatches[0], $text , $matches[2]);
2117 - $i++;
2118 - }
2119 -
21202111 return $wgCurParser->braceSubstitution( $matches );
21212112 }
21222113
@@ -2125,135 +2116,10 @@
21262117 return $wgCurParser->argSubstitution( $matches );
21272118 }
21282119
2129 -# XXX: i don't think this is the most elegant way to do it..
2130 -function wfInternalBraceSubstitution( $part1 ) {
2131 - global $wgLinkCache, $wgLang, $wgCurParser;
2132 - $fname = "wfInternalBraceSubstitution";
2133 - $found = false;
2134 - $nowiki = false;
2135 - $noparse = false;
2136 -
2137 - $title = NULL;
2138 -
2139 - # $newline is an optional newline character before the braces
2140 - # $part1 is the bit before the first |, and must contain only title characters
2141 - # $args is a list of arguments, starting from index 0, not including $part1
2142 -
2143 - # SUBST
2144 - if ( !$found ) {
2145 - $mwSubst =& MagicWord::get( MAG_SUBST );
2146 - if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
2147 - if ( $wgCurParser->mOutputType != OT_WIKI ) {
2148 - # Invalid SUBST not replaced at PST time
2149 - # Return without further processing
2150 - $text = $matches[0];
2151 - $found = true;
2152 - $noparse= true;
2153 - }
2154 - } elseif ( $wgCurParser->mOutputType == OT_WIKI ) {
2155 - # SUBST not found in PST pass, do nothing
2156 - $text = $matches[0];
2157 - $found = true;
2158 - }
2159 - }
2160 -
2161 - # MSG, MSGNW and INT
2162 - if ( !$found ) {
2163 - # Check for MSGNW:
2164 - $mwMsgnw =& MagicWord::get( MAG_MSGNW );
2165 - if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
2166 - $nowiki = true;
2167 - } else {
2168 - # Remove obsolete MSG:
2169 - $mwMsg =& MagicWord::get( MAG_MSG );
2170 - $mwMsg->matchStartAndRemove( $part1 );
2171 - }
2172 -
2173 - # Check if it is an internal message
2174 - $mwInt =& MagicWord::get( MAG_INT );
2175 - if ( $mwInt->matchStartAndRemove( $part1 ) ) {
2176 - if ( $wgCurParser->incrementIncludeCount( "int:$part1" ) ) {
2177 - $text = wfMsgReal( $part1, array(), true );
2178 - $found = true;
2179 - }
2180 - }
2181 - }
2182 -
2183 - # NS
2184 - if ( !$found ) {
2185 - # Check for NS: (namespace expansion)
2186 - $mwNs = MagicWord::get( MAG_NS );
2187 - if ( $mwNs->matchStartAndRemove( $part1 ) ) {
2188 - if ( intval( $part1 ) ) {
2189 - $text = $wgLang->getNsText( intval( $part1 ) );
2190 - $found = true;
2191 - } else {
2192 - $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
2193 - if ( !is_null( $index ) ) {
2194 - $text = $wgLang->getNsText( $index );
2195 - $found = true;
2196 - }
2197 - }
2198 - }
2199 - }
2200 -
2201 - # LOCALURL and LOCALURLE
2202 - if ( !$found ) {
2203 - $mwLocal = MagicWord::get( MAG_LOCALURL );
2204 - $mwLocalE = MagicWord::get( MAG_LOCALURLE );
2205 -
2206 - if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
2207 - $func = 'getLocalURL';
2208 - } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
2209 - $func = 'escapeLocalURL';
2210 - } else {
2211 - $func = '';
2212 - }
2213 -
2214 - if ( $func !== '' ) {
2215 - $title = Title::newFromText( $part1 );
2216 - if ( !is_null( $title ) ) {
2217 - $text = $title->$func();
2218 - $found = true;
2219 - }
2220 - }
2221 - }
2222 -
2223 - # Internal variables
2224 - if ( !$found && array_key_exists( $part1, $wgCurParser->mVariables ) ) {
2225 - $text = $wgCurParser->mVariables[$part1];
2226 - $found = true;
2227 - $wgCurParser->mOutput->mContainsOldMagic = true;
2228 - }
2229 -
2230 - # Load from database
2231 - if ( !$found ) {
2232 - $title = Title::newFromText( $part1, NS_TEMPLATE );
2233 - if ( !is_null( $title ) && !$title->isExternal() ) {
2234 - # Check for excessive inclusion
2235 - $dbk = $title->getPrefixedDBkey();
2236 - if ( $wgCurParser->incrementIncludeCount( $dbk ) ) {
2237 - $article = new Article( $title );
2238 - $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
2239 - if ( $articleContent !== false ) {
2240 - $found = true;
2241 - $text = $articleContent;
2242 -
2243 - }
2244 - }
2245 -
2246 - # If the title is valid but undisplayable, make a link to it
2247 - if ( $wgCurParser->mOutputType == OT_HTML && !$found ) {
2248 - $text = "[[" . $title->getPrefixedText() . "]]";
2249 - $found = true;
2250 - }
2251 - }
2252 - }
2253 -
2254 - if ( !$found ) {
2255 - return $matches[0];
2256 - } else {
2257 - return $text;
2258 - }
 2120+function wfVariableSubstitution( $matches )
 2121+{
 2122+ global $wgCurParser;
 2123+ return $wgCurParser->variableSubstitution( $matches );
22592124 }
 2125+
22602126 ?>

Status & tagging log