Merge "Unify HTMLForm message handling"
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormField.php
index a9c7632..380b11d 100644 (file)
@@ -117,8 +117,8 @@ abstract class HTMLFormField {
                        $tmp = $m[1];
                }
                if ( substr( $tmp, 0, 2 ) == 'wp' &&
-                       !isset( $alldata[$tmp] ) &&
-                       isset( $alldata[substr( $tmp, 2 )] )
+                       !array_key_exists( $tmp, $alldata ) &&
+                       array_key_exists( substr( $tmp, 2 ), $alldata )
                ) {
                        // Adjust for name mangling.
                        $tmp = substr( $tmp, 2 );
@@ -139,7 +139,7 @@ abstract class HTMLFormField {
                        $data = $alldata;
                        while ( $keys ) {
                                $key = array_shift( $keys );
-                               if ( !is_array( $data ) || !isset( $data[$key] ) ) {
+                               if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
                                        continue 2;
                                }
                                $data = $data[$key];
@@ -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
@@ -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,24 @@ 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 );
+       }
+
+       /**
+        * Skip this field when collecting data.
+        * @param WebRequest $request
+        * @return bool
+        * @since 1.27
+        */
+       public function skipLoadData( $request ) {
+               return !empty( $this->mParams['nodata'] );
+       }
 }