Remove ParserOptions clonations, already cloned in getParserOptions().
[lhc/web/wiklou.git] / includes / Message.php
index 2ec07fa..72232ec 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 /**
- * OBS!!! *EXPERIMENTAL* This class is still under discussion.
- *
  * This class provides methods for fetching interface messages and
  * processing them into variety of formats that are needed in MediaWiki.
  *
  *
  * Examples:
  * Fetching a message text for interface message
- *    $button = Xml::button( Message::key( 'submit' )->text() );
+ *    $button = Xml::button( wfMessage( 'submit' )->text() );
  * </pre>
  * Messages can have parameters:
- *    Message::key( 'welcome-to' )->params( $wgSitename )->text(); 
+ *    wfMessage( 'welcome-to' )->params( $wgSitename )->text(); 
  *        {{GRAMMAR}} and friends work correctly
- *    Message::key( 'are-friends', $user, $friend );
- *    Message::key( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
+ *    wfMessage( 'are-friends', $user, $friend );
+ *    wfMessage( 'bad-message' )->rawParams( '<script>...</script>' )->escaped();
  * </pre>
  * Sometimes the message text ends up in the database, so content language is needed.
- *    Message::key( 'file-log' )->params( $user, $filename )->inContentLanguage()->text()
+ *    wfMessage( 'file-log', $user, $filename )->inContentLanguage()->text()
  * </pre>
  * Checking if message exists:
- *    Message::key( 'mysterious-message' )->exists()
+ *    wfMessage( 'mysterious-message' )->exists()
  * </pre>
  * If you want to use a different language:
- *    Message::key( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
+ *    wfMessage( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
  *        Note that you cannot parse the text except in the content or interface
  *        languages
  * </pre>
  *
  * Use full parsing.
  *     wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
- *     === Message::key( 'key' )->params( 'apple' )->parse();
+ *     === wfMessage( 'key', 'apple' )->parse();
  * </pre>
  * Parseinline is used because it is more useful when pre-building html.
  * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
  *
  * Places where html cannot be used. {{-transformation is done.
  *     wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
- *     === Message::key( 'key' )->params( 'apple', 'pear' )->text();
+ *     === wfMessage( 'key', 'apple', 'pear' )->text();
  * </pre>
  *
  * Shortcut for escaping the message too, similar to wfMsgHTML, but
  * parameters are not replaced after escaping by default.
- * $escaped = Message::key( 'key' )->rawParams( 'apple' )->escaped();
+ * $escaped = wfMessage( 'key' )->rawParams( 'apple' )->escaped();
  * </pre>
  *
  * TODO:
  * - test, can we have tests?
  * - sort out the details marked with fixme
- * - should we have _m() or similar global wrapper?
  *
  * @since 1.17
+ * @author Niklas Laxström
  */
 class Message {
        /**
@@ -103,8 +101,10 @@ class Message {
         * @return Message: $this
         */
        public function __construct( $key, $params = array() ) {
+               global $wgLang;
                $this->key = $key;
                $this->parameters = array_values( $params );
+               $this->language = $wgLang;
        }
 
        /**
@@ -115,8 +115,10 @@ class Message {
         * @param Varargs: parameters as Strings
         * @return Message: $this
         */
-       public static function key( $key, /*...*/ ) {
-               return new self( $key, array_shift( func_get_args() );
+       public static function newFromKey( $key /*...*/ ) {
+               $params = func_get_args();
+               array_shift( $params );
+               return new self( $key, $params );
        }
 
        /**
@@ -125,7 +127,8 @@ class Message {
         * @return Message: $this
         */
        public function params( /*...*/ ) {
-               $this->parameters = array_merge( $this->parameters, array_values( func_get_args() ) );
+               $args_values = array_values( func_get_args() );
+               $this->parameters = array_merge( $this->parameters, $args_values );
                return $this;
        }
 
@@ -156,7 +159,9 @@ class Message {
                if( $lang instanceof Language ){
                        $this->language = $lang;
                } elseif ( is_string( $lang ) ) {
-                       $this->language = Language::factory( $lang );
+                       if( $this->language->getCode() != $lang ) {
+                               $this->language = Language::factory( $lang );
+                       }
                } else {
                        $type = gettype( $lang );
                        throw new MWException( __METHOD__ . " must be "
@@ -172,8 +177,9 @@ class Message {
         * @return Message: $this
         */
        public function inContentLanguage() {
+               global $wgContLang;
                $this->interface = false;
-               $this->language = null;
+               $this->language = $wgContLang;
                return $this;
        }
 
@@ -264,7 +270,7 @@ class Message {
 
        /**
         * Returns the message text. {{-transformation is done and the result
-        * is excaped excluding any raw parameters.
+        * is escaped excluding any raw parameters.
         * @return String: Escaped message text.
         */
        public function escaped() {
@@ -309,8 +315,8 @@ class Message {
         * @return Wikitext parsed into HTML
         */
        protected function parseText( $string ) {
-               global $wgOut;
-               if ( $this->language !== null ) {
+               global $wgOut, $wgLang, $wgContLang;
+               if ( $this->language !== $wgLang && $this->language !== $wgContLang ) {
                        # FIXME: remove this limitation
                        throw new MWException( 'Can only parse in interface or content language' );
                }