Unify handling of *-message(s) settings in HTMLForm
authorGergő Tisza <tgr.huwiki@gmail.com>
Fri, 1 Apr 2016 12:00:44 +0000 (15:00 +0300)
committerGergő Tisza <tgr.huwiki@gmail.com>
Fri, 1 Apr 2016 14:07:14 +0000 (17:07 +0300)
*-message(s) settings were documented as message key strings or arrays
of message key strings, but some actually accepted [key, params...]
arrays as well. They did not accept Message objects, which would be
the cleanest and most flexible method of message passing.

The patch adds a new method to process these settings (which accepts
a messages key, a [key, params...] array or a Message object), and
makes all *-message(s) usage call that.

Change-Id: Ida647973a58bea83fdbd53335e63b5a8615c16e4

includes/htmlform/HTMLAutoCompleteSelectField.php
includes/htmlform/HTMLButtonField.php
includes/htmlform/HTMLEditTools.php
includes/htmlform/HTMLForm.php
includes/htmlform/HTMLFormField.php
includes/htmlform/HTMLFormFieldCloner.php
includes/htmlform/HTMLSelectAndOtherField.php

index 6606ca3..76a88d5 100644 (file)
@@ -53,7 +53,7 @@ class HTMLAutoCompleteSelectField extends HTMLTextField {
                $this->getOptions();
                if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
                        if ( isset( $params['other-message'] ) ) {
-                               $msg = wfMessage( $params['other-message'] )->text();
+                               $msg = $this->getMessage( $params['other-message'] )->text();
                        } elseif ( isset( $params['other'] ) ) {
                                $msg = $params['other'];
                        } else {
index 16417fc..0b8343b 100644 (file)
@@ -35,16 +35,7 @@ class HTMLButtonField extends HTMLFormField {
 
                # Generate the label from a message, if possible
                if ( isset( $info['buttonlabel-message'] ) ) {
-                       $msgInfo = $info['buttonlabel-message'];
-
-                       if ( is_array( $msgInfo ) ) {
-                               $msg = array_shift( $msgInfo );
-                       } else {
-                               $msg = $msgInfo;
-                               $msgInfo = [];
-                       }
-
-                       $this->buttonLabel = $this->msg( $msg, $msgInfo )->parse();
+                       $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
                } elseif ( isset( $info['buttonlabel'] ) ) {
                        if ( $info['buttonlabel'] === '&#160;' ) {
                                // Apparently some things set &nbsp directly and in an odd format
index 77924ef..1b5d1fb 100644 (file)
@@ -39,7 +39,7 @@ class HTMLEditTools extends HTMLFormField {
                if ( empty( $this->mParams['message'] ) ) {
                        $msg = $this->msg( 'edittools' );
                } else {
-                       $msg = $this->msg( $this->mParams['message'] );
+                       $msg = $this->getMessage( $this->mParams['message'] );
                        if ( $msg->isDisabled() ) {
                                $msg = $this->msg( 'edittools' );
                        }
index ba43244..b7d91d8 100644 (file)
  *                             Some field types support multi-level arrays.
  *    'options-messages'    -- associative array mapping message keys to values.
  *                             Some field types support multi-level arrays.
- *    'options-message'     -- message key to be parsed to extract the list of
+ *    'options-message'     -- message key or object to be parsed to extract the list of
  *                             options (like 'ipbreason-dropdown').
- *    'label-message'       -- message key for a message to use as the label.
+ *    'label-message'       -- message key or object for a message to use as the label.
  *                             can be an array of msg key and then parameters to
  *                             the message.
  *    'label'               -- alternatively, a raw text message. Overridden by
  *                             label-message
  *    'help'                -- message text for a message to use as a help text.
- *    'help-message'        -- message key for a message to use as a help text.
+ *    'help-message'        -- message key or object for a message to use as a help text.
  *                             can be an array of msg key and then parameters to
  *                             the message.
  *                             Overwrites 'help-messages' and 'help'.
- *    'help-messages'       -- array of message key. As above, each item can
+ *    'help-messages'       -- array of message keys/objects. As above, each item can
  *                             be an array of msg key and then parameters.
  *                             Overwrites 'help'.
  *    'required'            -- passed through to the object, indicating that it
index a9c7632..d100392 100644 (file)
@@ -381,16 +381,7 @@ abstract class HTMLFormField {
 
                # Generate the label from a message, if possible
                if ( isset( $params['label-message'] ) ) {
-                       $msgInfo = $params['label-message'];
-
-                       if ( is_array( $msgInfo ) ) {
-                               $msg = array_shift( $msgInfo );
-                       } else {
-                               $msg = $msgInfo;
-                               $msgInfo = [];
-                       }
-
-                       $this->mLabel = $this->msg( $msg, $msgInfo )->parse();
+                       $this->mLabel = $this->getMessage( $params['label-message'] )->parse();
                } elseif ( isset( $params['label'] ) ) {
                        if ( $params['label'] === '&#160;' ) {
                                // Apparently some things set &nbsp directly and in an odd format
@@ -783,9 +774,8 @@ abstract class HTMLFormField {
                }
 
                if ( isset( $this->mParams['help-messages'] ) ) {
-                       foreach ( $this->mParams['help-messages'] as $name ) {
-                               $helpMessage = (array)$name;
-                               $msg = $this->msg( array_shift( $helpMessage ), $helpMessage );
+                       foreach ( $this->mParams['help-messages'] as $msg ) {
+                               $msg = $this->getMessage( $msg );
 
                                if ( $msg->exists() ) {
                                        if ( is_null( $helptext ) ) {
@@ -988,7 +978,7 @@ abstract class HTMLFormField {
                                $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
                        } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
                                /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
-                               $message = $this->msg( $this->mParams['options-message'] )->inContentLanguage()->plain();
+                               $message = $this->getMessage( $this->mParams['options-message'] )->inContentLanguage()->plain();
 
                                $optgroup = false;
                                $this->mOptions = [];
@@ -1097,4 +1087,23 @@ abstract class HTMLFormField {
                        return Html::rawElement( 'span', [ 'class' => 'error' ], $errors );
                }
        }
+
+       /**
+        * 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 ) {
+               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, [] );
+               }
+       }
 }
index 6553b56..4f2460f 100644 (file)
@@ -14,9 +14,9 @@
  *     'table', 'div', or 'raw'.
  *   row-legend - If non-empty, each group of subfields will be enclosed in a
  *     fieldset. The value is the name of a message key to use as the legend.
- *   create-button-message - Message key to use as the text of the button to
+ *   create-button-message - Message to use as the text of the button to
  *     add an additional group of fields.
- *   delete-button-message - Message key to use as the text of automatically-
+ *   delete-button-message - Message to use as the text of automatically-
  *     generated 'delete' button. Ignored if 'delete' is included in 'fields'.
  *
  * In the generated HTML, the subfields will be named along the lines of
@@ -299,7 +299,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                                'name' => $name,
                                'id' => Sanitizer::escapeId( "{$this->mID}--$key--delete" ),
                                'cssclass' => 'mw-htmlform-cloner-delete-button',
-                               'default' => $this->msg( $label )->text(),
+                               'default' => $this->getMessage( $label )->text(),
                        ], $this->mParent );
                        $v = $field->getDefault();
 
@@ -371,7 +371,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                        'name' => $name,
                        'id' => Sanitizer::escapeId( "{$this->mID}--create" ),
                        'cssclass' => 'mw-htmlform-cloner-create-button',
-                       'default' => $this->msg( $label )->text(),
+                       'default' => $this->getMessage( $label )->text(),
                ], $this->mParent );
                $html .= $field->getInputHTML( $field->getDefault() );
 
index e44ffa3..e75c2b2 100644 (file)
@@ -15,9 +15,9 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                if ( array_key_exists( 'other', $params ) ) {
                        // Do nothing
                } elseif ( array_key_exists( 'other-message', $params ) ) {
-                       $params['other'] = wfMessage( $params['other-message'] )->plain();
+                       $params['other'] = $this->getMessage( $params['other-message'] )->plain();
                } else {
-                       $params['other'] = wfMessage( 'htmlform-selectorother-other' )->plain();
+                       $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
                }
 
                parent::__construct( $params );