Merge "Add support for FormData in mw.Api.upload"
[lhc/web/wiklou.git] / includes / Message.php
index 59c5120..329d97a 100644 (file)
  *
  * @since 1.17
  */
-class Message {
+class Message implements MessageSpecifier {
 
        /**
         * In which language to get this message. True, which is the default,
@@ -249,7 +249,7 @@ class Message {
                $this->key = reset( $this->keysToTry );
 
                $this->parameters = array_values( $params );
-               $this->language = $language ? $language : $wgLang;
+               $this->language = $language ?: $wgLang;
        }
 
        /**
@@ -276,7 +276,7 @@ class Message {
         * Returns the message key.
         *
         * If a list of multiple possible keys was supplied to the constructor, this method may
-        * return any of these keys. After the message ahs been fetched, this method will return
+        * return any of these keys. After the message has been fetched, this method will return
         * the key that was actually used to fetch the message.
         *
         * @since 1.21
@@ -364,6 +364,31 @@ class Message {
                return new self( $keys );
        }
 
+       /**
+        * Get a title object for a mediawiki message, where it can be found in the mediawiki namespace.
+        * The title will be for the current language, if the message key is in
+        * $wgForceUIMsgAsContentMsg it will be append with the language code (except content
+        * language), because Message::inContentLanguage will also return in user language.
+        *
+        * @see $wgForceUIMsgAsContentMsg
+        * @return Title
+        * @since 1.26
+        */
+       public function getTitle() {
+               global $wgContLang, $wgForceUIMsgAsContentMsg;
+
+               $code = $this->language->getCode();
+               $title = $this->key;
+               if (
+                       $wgContLang->getCode() !== $code
+                       && in_array( $this->key, (array)$wgForceUIMsgAsContentMsg )
+               ) {
+                       $title .= '/' . $code;
+               }
+
+               return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( strtr( $title, ' ', '_' ) ) );
+       }
+
        /**
         * Adds parameters to the parameter list of this message.
         *
@@ -540,6 +565,30 @@ class Message {
                return $this;
        }
 
+       /**
+        * Add parameters that are plaintext and will be passed through without
+        * the content being evaluated.  Plaintext parameters are not valid as
+        * arguments to parser functions. This differs from self::rawParams in
+        * that the Message class handles escaping to match the output format.
+        *
+        * @since 1.25
+        *
+        * @param string|string[] $param,... plaintext parameters, or a single argument that is
+        * an array of plaintext parameters.
+        *
+        * @return Message $this
+        */
+       public function plaintextParams( /*...*/ ) {
+               $params = func_get_args();
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+               foreach ( $params as $param ) {
+                       $this->parameters[] = self::plaintextParam( $param );
+               }
+               return $this;
+       }
+
        /**
         * Set the language and the title from a context object
         *
@@ -573,7 +622,7 @@ class Message {
                if ( $lang instanceof Language || $lang instanceof StubUserLang ) {
                        $this->language = $lang;
                } elseif ( is_string( $lang ) ) {
-                       if ( $this->language->getCode() != $lang ) {
+                       if ( !$this->language instanceof Language || $this->language->getCode() != $lang ) {
                                $this->language = Language::factory( $lang );
                        }
                } else {
@@ -915,6 +964,17 @@ class Message {
                return array( 'bitrate' => $bitrate );
        }
 
+       /**
+        * @since 1.25
+        *
+        * @param string $plaintext
+        *
+        * @return string[] Array with a single "plaintext" key.
+        */
+       public static function plaintextParam( $plaintext ) {
+               return array( 'plaintext' => $plaintext );
+       }
+
        /**
         * Substitutes any parameters into the message text.
         *
@@ -964,6 +1024,8 @@ class Message {
                                return array( 'before', $this->language->formatSize( $param['size'] ) );
                        } elseif ( isset( $param['bitrate'] ) ) {
                                return array( 'before', $this->language->formatBitrate( $param['bitrate'] ) );
+                       } elseif ( isset( $param['plaintext'] ) ) {
+                               return array( 'after', $this->formatPlaintext( $param['plaintext'] ) );
                        } else {
                                $warning = 'Invalid parameter for message "' . $this->getKey() . '": ' .
                                        htmlspecialchars( serialize( $param ) );
@@ -1049,6 +1111,31 @@ class Message {
                return $this->message;
        }
 
+       /**
+        * Formats a message parameter wrapped with 'plaintext'. Ensures that
+        * the entire string is displayed unchanged when displayed in the output
+        * format.
+        *
+        * @since 1.25
+        *
+        * @param string $plaintext String to ensure plaintext output of
+        *
+        * @return string Input plaintext encoded for output to $this->format
+        */
+       protected function formatPlaintext( $plaintext ) {
+               switch ( $this->format ) {
+               case 'text':
+               case 'plain':
+                       return $plaintext;
+
+               case 'parse':
+               case 'block-parse':
+               case 'escaped':
+               default:
+                       return htmlspecialchars( $plaintext, ENT_QUOTES );
+
+               }
+       }
 }
 
 /**