(bug 35753) Allow {{FORMATNUM}} to only do digit transform
authorPavel Selitskas <p.selitskas@gmail.com>
Wed, 28 Nov 2012 10:24:45 +0000 (13:24 +0300)
committerSiebrand <siebrand@wikimedia.org>
Tue, 26 Feb 2013 08:30:16 +0000 (08:30 +0000)
{{formatnum:1234567.89|NOSEP}} will perform only digit transformations.

Change-Id: Ic882d5ba087665030144e85248680acece8aa901

includes/parser/CoreParserFunctions.php
languages/Language.php
languages/messages/MessagesEn.php
tests/parser/parserTests.txt

index 8917b6d..3643368 100644 (file)
@@ -269,12 +269,14 @@ class CoreParserFunctions {
        /**
         * @param $parser Parser
         * @param string $num
-        * @param null $raw
-        * @return
+        * @param string $arg
+        * @return string
         */
-       static function formatnum( $parser, $num = '', $raw = null) {
-               if ( self::isRaw( $raw ) ) {
+       static function formatnum( $parser, $num = '', $arg = null ) {
+               if ( self::matchAgainstMagicword( 'rawsuffix', $arg ) ) {
                        $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
+               } elseif ( self::matchAgainstMagicword( 'nocommafysuffix', $arg ) ) {
+                       $func = array( $parser->getFunctionLang(), 'formatNumNoSeparators' );
                } else {
                        $func = array( $parser->getFunctionLang(), 'formatNum' );
                }
@@ -386,20 +388,23 @@ class CoreParserFunctions {
                return '';
        }
 
-       static function isRaw( $param ) {
-               static $mwRaw;
-               if ( !$mwRaw ) {
-                       $mwRaw =& MagicWord::get( 'rawsuffix' );
-               }
-               if ( is_null( $param ) ) {
+       /**
+        * Matches the given value against the value of given magic word
+        *
+        * @param string $magicword magic word key
+        * @param mixed $value value to match
+        * @return boolean true on successful match
+        */
+       static private function matchAgainstMagicword( $magicword, $value ) {
+               if ( strval( $value ) === '' ) {
                        return false;
-               } else {
-                       return $mwRaw->match( $param );
                }
+               $mwObject = MagicWord::get( $magicword );
+               return $mwObject->match( $value );
        }
 
        static function formatRaw( $num, $raw ) {
-               if( self::isRaw( $raw ) ) {
+               if( self::matchAgainstMagicword( 'rawsuffix', $raw ) ) {
                        return $num;
                } else {
                        global $wgContLang;
@@ -585,7 +590,7 @@ class CoreParserFunctions {
                static $cache = array();
 
                // split the given option to its variable
-               if( self::isRaw( $arg1 ) ) {
+               if( self::matchAgainstMagicword( 'rawsuffix', $arg1 ) ) {
                        //{{pagesincategory:|raw[|type]}}
                        $raw = $arg1;
                        $type = $magicWords->matchStartToEnd( $arg2 );
index 7475d2f..cd3acb5 100644 (file)
@@ -2910,6 +2910,18 @@ class Language {
                return $number;
        }
 
+       /**
+        * Front-end for non-commafied formatNum
+        *
+        * @param mixed $number the string to be formatted, should be an integer
+        *        or a floating point number.
+        * @since 1.21
+        * @return string
+        */
+       public function formatNumNoSeparators( $number ) {
+               return $this->formatNum( $number, true );
+       }
+
        /**
         * @param $number string
         * @return string
index 8cf05dc..ec38ab6 100644 (file)
@@ -322,6 +322,7 @@ $magicWords = array(
        'raw'                     => array( 0,    'RAW:' ),
        'displaytitle'            => array( 1,    'DISPLAYTITLE' ),
        'rawsuffix'               => array( 1,    'R' ),
+       'nocommafysuffix'         => array( 0,    'NOSEP' ),
        'newsectionlink'          => array( 1,    '__NEWSECTIONLINK__' ),
        'nonewsectionlink'        => array( 1,    '__NONEWSECTIONLINK__' ),
        'currentversion'          => array( 1,    'CURRENTVERSION' ),
index bc24236..eef2549 100644 (file)
@@ -12003,6 +12003,19 @@ Strip marker in formatNum
 </p>
 !! end
 
+!! test
+Check noCommafy in formatNum
+!! options
+language=be-tarask
+!! input
+{{formatnum:123456.78}}
+{{formatnum:123456.78|NOSEP}}
+!! result
+<p>123 456,78
+123456.78
+</p>
+!! end
+
 !! test
 Strip marker in grammar
 !! options