Unify HTMLForm message handling
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormField.php
index ebbe323..a5d994d 100644 (file)
@@ -179,7 +179,7 @@ abstract class HTMLFormField {
                                        return true;
 
                                case 'OR':
-                                       foreach ( $params as $p ) {
+                                       foreach ( $params as $i => $p ) {
                                                if ( !is_array( $p ) ) {
                                                        throw new MWException(
                                                                "Expected array, found " . gettype( $p ) . " at index $i"
@@ -205,7 +205,7 @@ abstract class HTMLFormField {
                                        return false;
 
                                case 'NOR':
-                                       foreach ( $params as $p ) {
+                                       foreach ( $params as $i => $p ) {
                                                if ( !is_array( $p ) ) {
                                                        throw new MWException(
                                                                "Expected array, found " . gettype( $p ) . " at index $i"
@@ -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'] === ' ' ) {
                                // Apparently some things set &nbsp directly and in an odd format
@@ -610,12 +601,17 @@ abstract class HTMLFormField {
                $config = [
                        'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass ],
                        'align' => $this->getLabelAlignOOUI(),
-                       'label' => new OOUI\HtmlSnippet( $this->getLabel() ),
                        'help' => $helpText !== null ? new OOUI\HtmlSnippet( $helpText ) : null,
                        'errors' => $errors,
                        'infusable' => $infusable,
                ];
 
+               // the element could specify, that the label doesn't need to be added
+               $label = $this->getLabel();
+               if ( $label ) {
+                       $config['label'] = new OOUI\HtmlSnippet( $label );
+               }
+
                return $this->getFieldLayoutOOUI( $inputField, $config );
        }
 
@@ -778,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 ) ) {
@@ -912,45 +907,23 @@ abstract class HTMLFormField {
                return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
        }
 
-       /**
-        * Get a translated key if necessary.
-        * @param array|null $mappings Array of mappings, 'original' => 'translated'
-        * @param string $key
-        * @return string
-        */
-       protected function getMappedKey( $mappings, $key ) {
-               if ( !is_array( $mappings ) ) {
-                       return $key;
-               }
-
-               if ( !empty( $mappings[$key] ) ) {
-                       return $mappings[$key];
-               }
-
-               return $key;
-       }
-
        /**
         * Returns the given attributes from the parameters
         *
         * @param array $list List of attributes to get
-        * @param array $mappings Optional - Key/value map of attribute names to use
-        *   instead of the ones passed in.
         * @return array Attributes
         */
-       public function getAttributes( array $list, array $mappings = null ) {
+       public function getAttributes( array $list ) {
                static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
 
                $ret = [];
                foreach ( $list as $key ) {
-                       $mappedKey = $this->getMappedKey( $mappings, $key );
-
                        if ( in_array( $key, $boolAttribs ) ) {
                                if ( !empty( $this->mParams[$key] ) ) {
-                                       $ret[$mappedKey] = $mappedKey;
+                                       $ret[$key] = '';
                                }
                        } elseif ( isset( $this->mParams[$key] ) ) {
-                               $ret[$mappedKey] = $this->mParams[$key];
+                               $ret[$key] = $this->mParams[$key];
                        }
                }
 
@@ -1005,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 = [];
@@ -1114,4 +1087,14 @@ 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 ) {
+               return Message::newFromSpecifier( $value )->setContext( $this->mParent );
+       }
 }