Do not assume that the current working dir is phase3/config
[lhc/web/wiklou.git] / includes / Message.php
index 43c766c..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' )->params( $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' )->language( $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,31 +101,34 @@ class Message {
         * @return Message: $this
         */
        public function __construct( $key, $params = array() ) {
+               global $wgLang;
                $this->key = $key;
-               foreach( $params as $param ){
-                       $this->params( $param );
-               }
+               $this->parameters = array_values( $params );
+               $this->language = $wgLang;
        }
 
        /**
         * Factory function that is just wrapper for the real constructor. It is
         * intented to be used instead of the real constructor, because it allows
         * chaining method calls, while new objects don't.
-        * //FIXME: key or get or something else?
         * @param $key String: message key
+        * @param Varargs: parameters as Strings
         * @return Message: $this
         */
-       public static function key( $key ) {
-               return new Message( $key );
+       public static function newFromKey( $key /*...*/ ) {
+               $params = func_get_args();
+               array_shift( $params );
+               return new self( $key, $params );
        }
 
        /**
         * Adds parameters to the parameter list of this message.
-        * @param Vargars: parameters as Strings
+        * @param Varargs: parameters as Strings
         * @return Message: $this
         */
        public function params( /*...*/ ) {
-               $this->parameters += array_values( func_get_args() );
+               $args_values = array_values( func_get_args() );
+               $this->parameters = array_merge( $this->parameters, $args_values );
                return $this;
        }
 
@@ -141,8 +142,8 @@ class Message {
         */
        public function rawParams( /*...*/ ) {
                $params = func_get_args();
-               foreach( $params as $param ){
-                       $this->parameters[] =  array( 'raw' => $param );
+               foreach( $params as $param ) {
+                       $this->parameters[] = self::rawParam( $param );
                }
                return $this;
        }
@@ -151,14 +152,16 @@ class Message {
         * Request the message in any language that is supported.
         * As a side effect interface message status is unconditionally
         * turned off.
-        * @param $lang Mixed: langauge code or language object.
+        * @param $lang Mixed: language code or Language object.
         * @return Message: $this
         */
-       public function language( $lang ) {
+       public function inLanguage( $lang ) {
                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 "
@@ -174,8 +177,9 @@ class Message {
         * @return Message: $this
         */
        public function inContentLanguage() {
+               global $wgContLang;
                $this->interface = false;
-               $this->language = null;
+               $this->language = $wgContLang;
                return $this;
        }
 
@@ -232,9 +236,9 @@ class Message {
         * Fully parse the text from wikitext to HTML
         * @return String parsed HTML
         */
-       public function parse(){
+       public function parse() {
                $this->format = 'parse';
-               return $this->tostring();
+               return $this->toString();
        }
 
        /**
@@ -243,7 +247,7 @@ class Message {
         */
        public function text() {
                $this->format = 'text';
-               return $this->tostring();
+               return $this->toString();
        }
 
        /**
@@ -252,7 +256,7 @@ class Message {
         */
        public function plain() {
                $this->format = 'plain';
-               return $this->tostring();
+               return $this->toString();
        }
 
        /**
@@ -261,17 +265,17 @@ class Message {
         */
        public function parseAsBlock() {
                $this->format = 'block-parse';
-               return $this->tostring();
+               return $this->toString();
        }
 
        /**
         * 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() {
                $this->format = 'escaped';
-               return $this->tostring();
+               return $this->toString();
        }
 
        /**
@@ -279,7 +283,11 @@ class Message {
         * @return Bool: true if it is and false if not.
         */
        public function exists() {
-               return $this->fetchMessage() === false;
+               return $this->fetchMessage() !== false;
+       }
+
+       public static function rawParam( $value ) {
+               return array( 'raw' => $value );
        }
 
        /**
@@ -307,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' );
                }