Added more Message parameter functions
authorparent5446 <tylerromeo@gmail.com>
Fri, 23 Aug 2013 20:32:43 +0000 (16:32 -0400)
committerBartosz Dziewoński <matma.rex@gmail.com>
Sat, 19 Oct 2013 12:34:14 +0000 (14:34 +0200)
Similar to numParams(), added functions for other parameters
that can be formatted by the Language class. Adds functions for
expiry, size, timePeriod, duration, and bitrate parameters so
that the formatting doesn't have to be done at the caller.

Change-Id: I7b435fcc11824ead55e4c0f5512418187eae9a6f

includes/Message.php
tests/phpunit/includes/MessageTest.php

index 73e0af2..208f96e 100644 (file)
@@ -356,6 +356,96 @@ class Message {
                return $this;
        }
 
+       /**
+        * Add parameters that are durations of time and will be passed through
+        * Language::formatDuration before substitution
+        * @since 1.22
+        * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
+        * @return Message: $this
+        */
+       public function durationParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::durationParam( $param );
+               }
+               return $this;
+       }
+
+       /**
+        * Add parameters that are expiration times and will be passed through
+        * Language::formatExpiry before substitution
+        * @since 1.22
+        * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
+        * @return Message: $this
+        */
+       public function expiryParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::expiryParam( $param );
+               }
+               return $this;
+       }
+
+       /**
+        * Add parameters that are time periods and will be passed through
+        * Language::formatTimePeriod before substitution
+        * @since 1.22
+        * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
+        * @return Message: $this
+        */
+       public function timeperiodParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::timeperiodParam( $param );
+               }
+               return $this;
+       }
+
+       /**
+        * Add parameters that are file sizes and will be passed through
+        * Language::formatSize before substitution
+        * @since 1.22
+        * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
+        * @return Message: $this
+        */
+       public function sizeParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::sizeParam( $param );
+               }
+               return $this;
+       }
+
+       /**
+        * Add parameters that are bitrates and will be passed through
+        * Language::formatBitrate before substitution
+        * @since 1.22
+        * @param Varargs: numeric parameters (or single argument that is array of numeric parameters)
+        * @return Message: $this
+        */
+       public function bitrateParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::bitrateParam( $param );
+               }
+               return $this;
+       }
+
        /**
         * Set the language and the title from a context object
         * @since 1.19
@@ -638,6 +728,51 @@ class Message {
                return array( 'num' => $value );
        }
 
+       /**
+        * @since 1.22
+        * @param $value
+        * @return array
+        */
+       public static function durationParam( $value ) {
+               return array( 'duration' => $value );
+       }
+
+       /**
+        * @since 1.22
+        * @param $value
+        * @return array
+        */
+       public static function expiryParam( $value ) {
+               return array( 'expiry' => $value );
+       }
+
+       /**
+        * @since 1.22
+        * @param $value
+        * @return array
+        */
+       public static function timeperiodParam( $value ) {
+               return array( 'period' => $value );
+       }
+
+       /**
+        * @since 1.22
+        * @param $value
+        * @return array
+        */
+       public static function sizeParam( $value ) {
+               return array( 'size' => $value );
+       }
+
+       /**
+        * @since 1.22
+        * @param $value
+        * @return array
+        */
+       public static function bitrateParam( $value ) {
+               return array( 'bitrate' => $value );
+       }
+
        /**
         * Substitutes any parameters into the message text.
         * @since 1.17
@@ -664,20 +799,32 @@ class Message {
         * @return Tuple(type, value)
         */
        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 );
+               if ( is_array( $param ) ){
+                       if ( isset( $param['raw'] ) ) {
+                               return array( 'after', $param['raw'] );
+                       } elseif ( 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 ( isset( $param['duration'] ) ) {
+                               return array( 'before', $this->language->formatDuration( $param['duration'] ) );
+                       } elseif ( isset( $param['expiry'] ) ) {
+                               return array( 'before', $this->language->formatExpiry( $param['expiry'] ) );
+                       } elseif ( isset( $param['period'] ) ) {
+                               return array( 'before', $this->language->formatTimePeriod( $param['period'] ) );
+                       } elseif ( isset( $param['size'] ) ) {
+                               return array( 'before', $this->language->formatSize( $param['size'] ) );
+                       } elseif ( isset( $param['bitrate'] ) ) {
+                               return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
+                       } else {
+                               trigger_error(
+                                       "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
+                                       E_USER_WARNING
+                               );
+                               return array( 'before', '[INVALID]' );
+                       }
                } else {
-                       trigger_error(
-                               "Invalid message parameter: " . htmlspecialchars( serialize( $param ) ),
-                               E_USER_WARNING
-                       );
-                       return array( 'before', '[INVALID]' );
+                       return array( 'before', $param );
                }
        }
 
index 0f74899..1ba2d40 100644 (file)
@@ -56,6 +56,56 @@ class MessageTest extends MediaWikiLangTestCase {
                $this->assertEquals( 'abcdefghijka2', $msg->params( $params )->plain(), 'Params > 9 are replaced correctly' );
        }
 
+       /**
+        * FIXME: This should not need database, but Language#formatExpiry does (bug 55912)
+        * @group Database
+        */
+       function testMessageParamTypes() {
+               $lang = Language::factory( 'en' );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatNum( 123456.789 ),
+                       $msg->inLanguage( $lang )->numParams( 123456.789 )->plain(),
+                       'numParams is handled correctly'
+               );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatDuration( 1234 ),
+                       $msg->inLanguage( $lang )->durationParams( 1234 )->plain(),
+                       'durationParams is handled correctly'
+               );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatExpiry( wfTimestampNow() ),
+                       $msg->inLanguage( $lang )->expiryParams( wfTimestampNow() )->plain(),
+                       'expiryParams is handled correctly'
+               );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatTimePeriod( 1234 ),
+                       $msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(),
+                       'timeperiodParams is handled correctly'
+               );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatSize( 123456 ),
+                       $msg->inLanguage( $lang )->sizeParams( 123456 )->plain(),
+                       'sizeParams is handled correctly'
+               );
+
+               $msg = new RawMessage( '$1' );
+               $this->assertEquals(
+                       $lang->formatBitrate( 123456 ),
+                       $msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(),
+                       'bitrateParams is handled correctly'
+               );
+       }
+
        function testInContentLanguageDisabled() {
                $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) );