Return messages in a consistent form from Status objects
authordaniel <daniel.kinzler@wikimedia.de>
Wed, 17 Jul 2013 17:11:56 +0000 (19:11 +0200)
committerTim Starling <tstarling@wikimedia.org>
Tue, 30 Jul 2013 02:11:48 +0000 (02:11 +0000)
Also fixes Message::getKey() to always return a string.

Rationale:

Some code, like RollbackAction, assumes that Status::getErrorArray will
return an array of the form ( messagekey, param... ), but this was not
the case when a Message object was passed to the Status.

This change makes sure Status::getErrorArray will always return arrays
of the expected form. This is especially important since the messages in
the Status object may be provided by extensions.

In order to convert Message objects to arrays of message keys and parameters,
Message::getKey() needed to be fixed to return a single key always.

Bug: 49338
Change-Id: I0deaa9888e9d86726a8e41ca606c571f56190c91

includes/Message.php
includes/Status.php

index de0b17e..74b4021 100644 (file)
@@ -234,7 +234,13 @@ class Message {
         * @return string
         */
        public function getKey() {
-               return $this->key;
+               if ( is_array( $this->key ) ) {
+                       // May happen if some kind of fallback is applied.
+                       // For now, just use the first key. We really need a better solution.
+                       return $this->key[0];
+               } else {
+                       return $this->key;
+               }
        }
 
        /**
index f0253df..7ec1b0f 100644 (file)
@@ -269,7 +269,8 @@ class Status {
        /**
         * Get the list of errors (but not warnings)
         *
-        * @return Array
+        * @return array A list in which each entry is an array with a message key as its first element.
+        *         The remaining array elements are the message parameters.
         */
        function getErrorsArray() {
                return $this->getStatusArray( "error" );
@@ -278,7 +279,8 @@ class Status {
        /**
         * Get the list of warnings (but not errors)
         *
-        * @return Array
+        * @return array A list in which each entry is an array with a message key as its first element.
+        *         The remaining array elements are the message parameters.
         */
        function getWarningsArray() {
                return $this->getStatusArray( "warning" );
@@ -295,7 +297,7 @@ class Status {
                foreach ( $this->errors as $error ) {
                        if ( $error['type'] === $type ) {
                                if ( $error['message'] instanceof Message ) {
-                                       $result[] = $error['message'];
+                                       $result[] = array_merge( array( $error['message']->getKey() ), $error['message']->getParams() );
                                } elseif ( $error['params'] ) {
                                        $result[] = array_merge( array( $error['message'] ), $error['params'] );
                                } else {