Merge "Memory setting for djvutext is now a constant"
[lhc/web/wiklou.git] / includes / Status.php
index a2df380..2553f54 100644 (file)
@@ -47,7 +47,7 @@ class Status {
        /**
         * Factory function for fatal errors
         *
-        * @param $message String: message name
+        * @param $message String|Message: message name or object
         * @return Status
         */
        static function newFatal( $message /*, parameters...*/ ) {
@@ -103,7 +103,7 @@ class Status {
        /**
         * Add a new warning
         *
-        * @param $message String: message name
+        * @param $message String|Message: message name or object
         */
        function warning( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
@@ -117,7 +117,7 @@ class Status {
         * Add an error, do not set fatal flag
         * This can be used for non-fatal errors
         *
-        * @param $message String: message name
+        * @param $message String|Message: message name or object
         */
        function error( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
@@ -131,7 +131,7 @@ class Status {
         * Add an error and set OK to false, indicating that the operation
         * as a whole was fatal
         *
-        * @param $message String: message name
+        * @param $message String|Message: message name or object
         */
        function fatal( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
@@ -164,35 +164,6 @@ class Status {
                return $cleanParams;
        }
 
-       /**
-        * @param $item
-        * @return string
-        */
-       protected function getItemXML( $item ) {
-               $params = $this->cleanParams( $item['params'] );
-               $xml = "<{$item['type']}>\n" .
-                       Xml::element( 'message', null, $item['message'] ) . "\n" .
-                       Xml::element( 'text', null, wfMsg( $item['message'], $params ) ) ."\n";
-               foreach ( $params as $param ) {
-                       $xml .= Xml::element( 'param', null, $param );
-               }
-               $xml .= "</{$item['type']}>\n";
-               return $xml;
-       }
-
-       /**
-        * Get the error list as XML
-        * @return string
-        */
-       function getXML() {
-               $xml = "<errors>\n";
-               foreach ( $this->errors as $error ) {
-                       $xml .= $this->getItemXML( $error );
-               }
-               $xml .= "</errors>\n";
-               return $xml;
-       }
-
        /**
         * Get the error list as a wikitext formatted list
         *
@@ -212,26 +183,26 @@ class Status {
                        }
                }
                if ( count( $this->errors ) == 1 ) {
-                       $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0]  );
+                       $s = $this->getErrorMessage( $this->errors[0] );
                        if ( $shortContext ) {
-                               $s = wfMsgNoTrans( $shortContext, $s );
+                               $s = wfMessage( $shortContext, $s )->plain();
                        } elseif ( $longContext ) {
-                               $s = wfMsgNoTrans( $longContext, "* $s\n" );
+                               $s = wfMessage( $longContext, "* $s\n" )->plain();
                        }
                } else {
                        $s = '* '. implode("\n* ",
-                               $this->getWikiTextArray( $this->errors ) ) . "\n";
+                               $this->getErrorMessageArray( $this->errors ) ) . "\n";
                        if ( $longContext ) {
-                               $s = wfMsgNoTrans( $longContext, $s );
+                               $s = wfMessage( $longContext, $s )->plain();
                        } elseif ( $shortContext ) {
-                               $s = wfMsgNoTrans( $shortContext, "\n$s\n" );
+                               $s = wfMessage( $shortContext, "\n$s\n" )->plain();
                        }
                }
                return $s;
        }
 
        /**
-        * Return the wiki text for a single error.
+        * Return the message for a single error.
         * @param $error Mixed With an array & two values keyed by
         * 'message' and 'params', use those keys-value pairs.
         * Otherwise, if its an array, just use the first value as the
@@ -239,19 +210,31 @@ class Status {
         *
         * @return String
         */
-       protected function getWikiTextForError( $error ) {
+       protected function getErrorMessage( $error ) {
                if ( is_array( $error ) ) {
-                       if ( isset( $error['message'] ) && isset( $error['params'] ) ) {
-                               return wfMsgNoTrans( $error['message'],
+                       if( isset( $error['message'] ) && $error['message'] instanceof Message ) {
+                               $msg = $error['message'];
+                       } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) {
+                               $msg = wfMessage( $error['message'],
                                        array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) )  );
                        } else {
-                               $message = array_shift($error);
-                               return wfMsgNoTrans( $message,
+                               $msgName = array_shift( $error );
+                               $msg = wfMessage( $msgName,
                                        array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) );
                        }
                } else {
-                       return wfMsgNoTrans( $error );
+                       $msg = wfMessage( $error );
                }
+               return $msg->plain();
+       }
+
+       /**
+        * Get the error message as HTML. This is done by parsing the wikitext error
+        * message.
+        */
+       public function getHTML( $shortContext = false, $longContext = false ) {
+               $text = $this->getWikiText( $shortContext, $longContext );
+               return MessageCache::singleton()->transform( $text, true );
        }
 
        /**
@@ -259,8 +242,8 @@ class Status {
         * @param $errors Array
         * @return Array
         */
-       function getWikiTextArray( $errors ) {
-               return array_map( array( $this, 'getWikiTextForError' ), $errors );
+       protected function getErrorMessageArray( $errors ) {
+               return array_map( array( $this, 'getErrorMessage' ), $errors );
        }
 
        /**
@@ -307,7 +290,9 @@ class Status {
                $result = array();
                foreach ( $this->errors as $error ) {
                        if ( $error['type'] === $type ) {
-                               if( $error['params'] ) {
+                               if( $error['message'] instanceof Message ) {
+                                       $result[] = $error['message'];
+                               } elseif( $error['params'] ) {
                                        $result[] = array_merge( array( $error['message'] ), $error['params'] );
                                } else {
                                        $result[] = array( $error['message'] );
@@ -338,6 +323,9 @@ class Status {
        /**
         * Returns true if the specified message is present as a warning or error
         *
+        * Note, due to the lack of tools for comparing Message objects, this
+        * function will not work when using a Message object as a parameter.
+        *
         * @param $msg String: message name
         * @return Boolean
         */
@@ -354,9 +342,12 @@ class Status {
         * If the specified source message exists, replace it with the specified
         * destination message, but keep the same parameters as in the original error.
         *
-        * Return true if the replacement was done, false otherwise.
+        * Note, due to the lack of tools for comparing Message objects, this
+        * function will not work when using a Message object as the search parameter.
         *
-        * @return bool
+        * @param $source Message|String: Message key or object to search for
+        * @param $dest Message|String: Replacement message key or object
+        * @return bool Return true if the replacement was done, false otherwise.
         */
        function replaceMessage( $source, $dest ) {
                $replaced = false;