From: Ævar Arnfjörð Bjarmason Date: Tue, 20 Sep 2005 19:34:30 +0000 (+0000) Subject: * Support default arguments for template arguments in the form of {{{argument|default... X-Git-Tag: 1.6.0~1595 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=22d660e8d00784643b64b31b31f0ff11d694bfde;p=lhc%2Fweb%2Fwiklou.git * Support default arguments for template arguments in the form of {{{argument|default argument}}} --- diff --git a/includes/Parser.php b/includes/Parser.php index d33ece0ffe..936ee8fb4c 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -1965,6 +1965,7 @@ class Parser wfProfileIn( $fname ); $titleChars = Title::legalChars(); + $dtitleChars = "$titleChars|{}\[\]"; // [] so that {{{arg|[[foo|bar]]}} will work # This function is called recursively. To keep track of arguments we need a stack: array_push( $this->mArgStack, $args ); @@ -1972,9 +1973,12 @@ class Parser # Variable substitution $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text ); - if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) { + if ( $this->mOutputType == OT_WIKI ) { # Argument substitution - $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text ); + $text = preg_replace_callback( "/{{{([$dtitleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text ); + } elseif ( $this->mOutputType == OT_HTML ) { + # Argument substitution + default argument substitution + $text = preg_replace_callback( "/{{{([$dtitleChars]*?)}}}/", array( &$this, 'argSubstitutionWDefaults' ), $text ); } # Template substitution $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s'; @@ -2419,6 +2423,31 @@ class Parser if ( array_key_exists( $arg, $inputArgs ) ) { $text = $inputArgs[$arg]; } + + return $text; + } + + /** + * Triple brace replacement with default arguments-- used for template arguments + * @access private + */ + function argSubstitutionWDefaults( $matches ) { + $arg = trim( $matches[1] ); + + if ( strpos( $arg, '|' ) !== false ) { + list( $arg, $default ) = explode( '|', $arg, 2 ); + $arg = rtrim( $arg ); + $default = ltrim( $default ); + } else + $default = null; + + $text = $matches[0]; + $inputArgs = end( $this->mArgStack ); + + if ( array_key_exists( $arg, $inputArgs ) ) { + $text = $inputArgs[$arg]; + } else if ( $default !== null ) + $text = $default; return $text; }