jenkins-bot has submitted this change and it was merged.

Change subject: Unify HTMLForm message handling
......................................................................


Unify HTMLForm message handling

Improves Ida647973a which unified message handling for form fields
but did not make the functionality available to HTMLForm itself.

Change-Id: I2e6195ba13afbd8b993acb47409fab1be91c547e
---
M includes/Message.php
M includes/htmlform/HTMLForm.php
M includes/htmlform/HTMLFormField.php
M includes/htmlform/OOUIHTMLForm.php
M tests/phpunit/includes/MessageTest.php
5 files changed, 65 insertions(+), 32 deletions(-)

Approvals:
  Anomie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Message.php b/includes/Message.php
index fd016fc..8ac73b3 100644
--- a/includes/Message.php
+++ b/includes/Message.php
@@ -383,6 +383,31 @@
        }
 
        /**
+        * Transform a MessageSpecifier or a primitive value used 
interchangeably with
+        * specifiers (a message key string, or a key + params array) into a 
proper Message
+        * @param string|array|MessageSpecifier $value
+        * @return Message
+        * @throws InvalidArgumentException
+        */
+       public static function newFromSpecifier( $value ) {
+               if ( $value instanceof RawMessage ) {
+                       $message = new RawMessage( $value->getKey(), 
$value->getParams() );
+               } elseif ( $value instanceof MessageSpecifier ) {
+                       $message = new Message( $value );
+               } elseif ( is_array( $value ) ) {
+                       $key = array_shift( $value );
+                       $message = new Message( $key, $value );
+               } elseif ( is_string( $value ) ) {
+                       $message = new Message( $value );
+               } else {
+                       throw new InvalidArgumentException( __METHOD__ . ': 
invalid argument type '
+                               . gettype( $value ) );
+               }
+
+               return $message;
+       }
+
+       /**
         * Factory function accepting multiple message keys and returning a 
message instance
         * for the first message which is non-empty. If all messages are empty 
then an
         * instance of the first message key is returned.
diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php
index d50fac0..57796d5 100644
--- a/includes/htmlform/HTMLForm.php
+++ b/includes/htmlform/HTMLForm.php
@@ -1119,7 +1119,7 @@
                        ];
 
                        if ( isset( $button['label-message'] ) ) {
-                               $label = $this->msg( $button['label-message'] 
)->parse();
+                               $label = $this->getMessage( 
$button['label-message'] )->parse();
                        } elseif ( isset( $button['label'] ) ) {
                                $label = htmlspecialchars( $button['label'] );
                        } elseif ( isset( $button['label-raw'] ) ) {
@@ -1198,17 +1198,10 @@
                $errorstr = '';
 
                foreach ( $errors as $error ) {
-                       if ( is_array( $error ) ) {
-                               $msg = array_shift( $error );
-                       } else {
-                               $msg = $error;
-                               $error = [];
-                       }
-
                        $errorstr .= Html::rawElement(
                                'li',
                                [],
-                               $this->msg( $msg, $error )->parse()
+                               $this->getMessage( $error )->parse()
                        );
                }
 
@@ -1717,4 +1710,14 @@
 
                return $this;
        }
+
+       /**
+        * Turns a *-message parameter (which could be a MessageSpecifier, or a 
message name, or a
+        * name + parameters array) into a Message.
+        * @param mixed $value
+        * @return Message
+        */
+       protected function getMessage( $value ) {
+               return Message::newFromSpecifier( $value )->setContext( $this );
+       }
 }
diff --git a/includes/htmlform/HTMLFormField.php 
b/includes/htmlform/HTMLFormField.php
index e86d4c4..a5d994d 100644
--- a/includes/htmlform/HTMLFormField.php
+++ b/includes/htmlform/HTMLFormField.php
@@ -1095,15 +1095,6 @@
         * @return Message
         */
        protected function getMessage( $value ) {
-               if ( $value instanceof Message ) {
-                       return $value;
-               } elseif ( $value instanceof MessageSpecifier ) {
-                       return Message::newFromKey( $value );
-               } elseif ( is_array( $value ) ) {
-                       $msg = array_shift( $value );
-                       return $this->msg( $msg, $value );
-               } else {
-                       return $this->msg( $value, [] );
-               }
+               return Message::newFromSpecifier( $value )->setContext( 
$this->mParent );
        }
 }
diff --git a/includes/htmlform/OOUIHTMLForm.php 
b/includes/htmlform/OOUIHTMLForm.php
index 4f8365e..20f4e45 100644
--- a/includes/htmlform/OOUIHTMLForm.php
+++ b/includes/htmlform/OOUIHTMLForm.php
@@ -100,7 +100,7 @@
                        if ( $isBadIE ) {
                                $label = $button['value'];
                        } elseif ( isset( $button['label-message'] ) ) {
-                               $label = new OOUI\HtmlSnippet( $this->msg( 
$button['label-message'] )->parse() );
+                               $label = new OOUI\HtmlSnippet( 
$this->getMessage( $button['label-message'] )->parse() );
                        } elseif ( isset( $button['label'] ) ) {
                                $label = $button['label'];
                        } elseif ( isset( $button['label-raw'] ) ) {
@@ -196,18 +196,7 @@
                }
 
                foreach ( $errors as &$error ) {
-                       if ( is_array( $error ) ) {
-                               $msg = array_shift( $error );
-                       } else {
-                               $msg = $error;
-                               $error = [];
-                       }
-                       // if the error is already a message object, don't use 
it as a message key
-                       if ( !$msg instanceof Message ) {
-                               $error = $this->msg( $msg, $error )->parse();
-                       } else {
-                               $error = $msg->parse();
-                       }
+                       $error = $this->getMessage( $error )->parse();
                        $error = new OOUI\HtmlSnippet( $error );
                }
 
diff --git a/tests/phpunit/includes/MessageTest.php 
b/tests/phpunit/includes/MessageTest.php
index cf34b18..224b0cb 100644
--- a/tests/phpunit/includes/MessageTest.php
+++ b/tests/phpunit/includes/MessageTest.php
@@ -581,4 +581,29 @@
                $msg = unserialize( serialize( $msg ) );
                $this->assertEquals( 'Hauptseite', $msg->plain() );
        }
+
+       /**
+        * @covers Message::newFromSpecifier
+        * @dataProvider provideNewFromSpecifier
+        */
+       public function testNewFromSpecifier( $value, $expectedText ) {
+               $message = Message::newFromSpecifier( $value );
+               $this->assertInstanceOf( Message::class, $message );
+               $this->assertSame( $expectedText, $message->text() );
+       }
+
+       public function provideNewFromSpecifier() {
+               $messageSpecifier = $this->getMockForAbstractClass( 
MessageSpecifier::class );
+               $messageSpecifier->expects( $this->any() )->method( 'getKey' 
)->willReturn( 'mainpage' );
+               $messageSpecifier->expects( $this->any() )->method( 'getParams' 
)->willReturn( [] );
+
+               return [
+                       'string' => [ 'mainpage', 'Main Page' ],
+                       'array' => [ [ 'youhavenewmessages', 'foo', 'bar' ], 
'You have foo (bar).' ],
+                       'Message' => [ new Message( 'youhavenewmessages', [ 
'foo', 'bar' ] ), 'You have foo (bar).' ],
+                       'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] 
), 'foo (bar)' ],
+                       'MessageSpecifier' => [ $messageSpecifier, 'Main Page' 
],
+               ];
+       }
 }
+

-- 
To view, visit https://gerrit.wikimedia.org/r/283846
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2e6195ba13afbd8b993acb47409fab1be91c547e
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: GergÅ‘ Tisza <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Florianschmidtwelzow <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to