* (bug 23276) Add hook to Special:NewPages to modify query
[lhc/web/wiklou.git] / includes / Message.php
index 70738c3..c4ac5ec 100644 (file)
@@ -15,8 +15,8 @@
  * Messages can have parameters:
  *    Message::key( '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()
+ *    Message::key( 'are-friends', $user, $friend );
+ *    Message::key( '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()
@@ -25,7 +25,7 @@
  *    Message::key( 'mysterious-message' )->exists()
  * </pre>
  * If you want to use a different language:
- *    Message::key( 'email-header' )->language( $user->getOption( 'language' ) )->plain()
+ *    Message::key( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain()
  *        Note that you cannot parse the text except in the content or interface
  *        languages
  * </pre>
@@ -79,60 +79,55 @@ class Message {
         * List of parameters which will be substituted into the message.
         */
        protected $parameters = array();
-       
+
+       /**
+        * Format for the message.
+        * Supported formats are:
+        * * text (transform)
+        * * escaped (transform+htmlspecialchars)
+        * * block-parse
+        * * parse (default)
+        * * plain
+        */
+       protected $format = 'parse';
+
        /**
-        * Some situations need exotic combinations of options to the
-        * underlying Language modules; which can be specified here.
-        * Dependencies:
-        *     'parse' implies 'transform', 'escape'
+        * Whether database can be used.
         */
-       protected $options = array(
-               # Don't wrap the output in a block-level element
-               'inline' => true,
-               # Expand {{ constructs
-               'transform' => true,
-               # Output will be safe HTML
-               'escape' => true,
-               # Parse the text with the full parser
-               'parse' => true,
-               # Access the database when getting the message text
-               'usedb' => true,
-       );
+       protected $useDatabase = true;
 
        /**
         * Constructor.
         * @param $key String: message key
         * @param $params Array message parameters
-        * @param $options Array message options
         * @return Message: $this
         */
-       public function __construct( $key, $params=array(), $options=array() ) {
+       public function __construct( $key, $params = array() ) {
                $this->key = $key;
-               foreach( $params as $param ){
-                       $this->params( $param );
-               }
-               $this->options( $options );
+               $this->parameters = array_values( $params );
        }
 
        /**
         * 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 function key( $key ) {
-               return new Message( $key );
+       public static function key( $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() );
+               $this->parameters = array_merge( $this->parameters, array_values( func_get_args() ) );
                return $this;
        }
 
@@ -146,35 +141,20 @@ 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;
        }
        
-       /**
-        * Set some of the individual options, if you need to use some 
-        * funky combination of them.
-        * @param $options Array $option => $value
-        * @return Message $this
-        */
-       public function options( array $options ){
-               foreach( $options as $key => $value ){
-                       if( in_array( $key, $this->options ) ){
-                               $this->options[$key] = (bool)$value;
-                       }
-               }
-               return $this;
-       }
-
        /**
         * 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 ) ) {
@@ -199,6 +179,16 @@ class Message {
                return $this;
        }
 
+       /**
+        * Enable or disable database use.
+        * @param $value Boolean
+        * @return Message: $this
+        */
+       public function useDatabase( $value ) {
+               $this->useDatabase = (bool) $value;
+               return $this;
+       }
+
        /**
         * Returns the message parsed from wikitext to HTML.
         * TODO: in PHP >= 5.2.0, we can make this a magic method,
@@ -216,28 +206,20 @@ class Message {
                $string = $this->replaceParameters( $string, 'before' );
                
                # Maybe transform using the full parser
-               if( $this->options['parse'] ){
+               if( $this->format === 'parse' ) {
                        $string = $this->parseText( $string );
-               } else {
-                       
-                       # Transform {{ constructs
-                       if( $this->options['transform'] ){
-                               $string = $this->transformText( $string );
-                       }
-                       
-                       # Sanitise
-                       if( $this->options['escape'] ){
-                               # FIXME: Sanitizer method here?
-                               $string = htmlspecialchars( $string );
-                       }
-               }
-               
-               # Strip the block element
-               if( !$this->options['inline'] ){
                        $m = array();
                        if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
                                $string = $m[1];
                        }
+               } elseif( $this->format === 'block-parse' ){
+                       $string = $this->parseText( $string );
+               } elseif( $this->format === 'text' ){
+                       $string = $this->transformText( $string );
+               } elseif( $this->format === 'escaped' ){
+                       # FIXME: Sanitizer method here?
+                       $string = $this->transformText( $string );
+                       $string = htmlspecialchars( $string );
                }
                
                # Raw parameter replacement
@@ -250,11 +232,9 @@ class Message {
         * Fully parse the text from wikitext to HTML
         * @return String parsed HTML
         */
-       public function parse(){
-               $this->options( array(
-                       'parse' => true,
-               ));             
-               return $this->tostring();
+       public function parse() {
+               $this->format = 'parse';
+               return $this->toString();
        }
 
        /**
@@ -262,12 +242,8 @@ class Message {
         * @return String: Unescaped message text.
         */
        public function text() {
-               $this->options( array(
-                       'parse' => false,
-                       'transform' => true,
-                       'escape' => false,
-               ));             
-               return $this->tostring();
+               $this->format = 'text';
+               return $this->toString();
        }
 
        /**
@@ -275,13 +251,8 @@ class Message {
         * @return String: Unescaped untransformed message text.
         */
        public function plain() {
-               $this->options( array(
-                       'parse' => false,
-                       'transform' => false,
-                       'escape' => false,
-                       'inline' => false,
-               ));             
-               return $this->tostring();
+               $this->format = 'plain';
+               return $this->toString();
        }
 
        /**
@@ -289,11 +260,8 @@ class Message {
         * @return String: HTML
         */
        public function parseAsBlock() {
-               $this->options( array(
-                       'parse' => true,
-                       'inline' => false,
-               ));             
-               return $this->tostring();
+               $this->format = 'block-parse';
+               return $this->toString();
        }
 
        /**
@@ -302,13 +270,8 @@ class Message {
         * @return String: Escaped message text.
         */
        public function escaped() {
-               $this->options( array(
-                       'parse' => false,
-                       'transform' => true,
-                       'escape' => true,
-                       'inline' => false,
-               ));             
-               return $this->tostring();
+               $this->format = 'escaped';
+               return $this->toString();
        }
 
        /**
@@ -316,12 +279,11 @@ class Message {
         * @return Bool: true if it is and false if not.
         */
        public function exists() {
-               global $wgMessageCache;
-               return $wgMessageCache->get( 
-                       $this->key, 
-                       $this->options['usedb'], 
-                       $this->language 
-               ) !== false;
+               return $this->fetchMessage() !== false;
+       }
+
+       public static function rawParam( $value ) {
+               return array( 'raw' => $value );
        }
 
        /**
@@ -368,15 +330,27 @@ class Message {
        }
 
        /**
-        * Wrapper for what ever method we use to get message contents
-        * @return Unmodified message contents
+        * Returns the textual value for the message.
+        * @return Message contents or placeholder
         */
        protected function getMessageText() {
-               global $wgMessageCache;
-               $message = $wgMessageCache->get( $this->key, $this->options['usedb'], $this->language );
-               return $message === false
-                       ? '&lt;' . htmlspecialchars( $this->key ) . '&gt;'
-                       : $message;
+               $message = $this->fetchMessage();
+               if ( $message === false ) {
+                       return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
+               } else {
+                       return $message;
+               }
+       }
+
+       /**
+        * Wrapper for what ever method we use to get message contents
+        */
+       protected function fetchMessage() {
+               if ( !isset( $this->message ) ) {
+                       global $wgMessageCache;
+                       $this->message = $wgMessageCache->get( $this->key, $this->useDatabase, $this->language );
+               }
+               return $this->message;
        }
 
 }
\ No newline at end of file