From 7fb251ba6c14cffb64401cb621812a3f01a898c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Thu, 10 Feb 2011 09:10:47 +0000 Subject: [PATCH] Fixed two bugs in message parameter handling: * missing check for is_array caused $foo["raw"] to return true when $foo is string: bug 27298 * Some parameters could be replaced twice or more: r80764 --- includes/Message.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/includes/Message.php b/includes/Message.php index 7dd14e15c4..550ea7f33a 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -350,6 +350,7 @@ class Message { /** * Check whether a message does not exist, or is an empty string * @return Bool: true if is is and false if not + * @todo Merge with isDisabled()? */ public function isBlank() { $message = $this->fetchMessage(); @@ -375,26 +376,42 @@ class Message { /** * Substitutes any paramaters into the message text. - * @param $message String, the message text + * @param $message String: the message text * @param $type String: either before or after * @return String */ protected function replaceParameters( $message, $type = 'before' ) { $replacementKeys = array(); foreach( $this->parameters as $n => $param ) { - if ( $type === 'before' && !is_array( $param ) ) { - $replacementKeys['$' . ($n + 1)] = $param; - } elseif ( $type === 'after' && isset( $param['raw'] ) ) { - $replacementKeys['$' . ($n + 1)] = $param['raw']; - } elseif ( isset( $param['num'] ) ) { - $replacementKeys['$' . ($n + 1)] = - $this->language->formatNum( $param['num'] ); + list( $paramType, $value ) = $this->extractParam( $param ); + if ( $type === $paramType ) { + $replacementKeys['$' . ($n + 1)] = $value; } } $message = strtr( $message, $replacementKeys ); return $message; } + /** + * Extracts the parameter type and preprocessed the value if needed. + * @param $param String|Array: Parameter as defined in this class. + * @return Tuple(type, value) + * @throws MWException + */ + protected function extractParam( $param ) { + if ( is_array( $param ) && isset( $param['raw'] ) ) { + return array( 'after', $param['raw'] ); + } elseif ( is_array( $param ) && isset( $param['num'] ) ) { + // Replace number params always in before step for now. + // No support for combined raw and num params + return array( 'before', $this->language->formatNum( $param['num'] ) ); + } elseif ( !is_array( $param ) ) { + return array( 'before', $param ); + } else { + throw new MWException( "Invalid message parameter" ); + } + } + /** * Wrapper for what ever method we use to parse wikitext. * @param $string String: Wikitext message contents -- 2.20.1