From 8dc5e1857dc50772279c30b1d502c36f802cc1a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Fri, 1 Apr 2016 15:00:44 +0300 Subject: [PATCH] Unify handling of *-message(s) settings in HTMLForm *-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 --- .../htmlform/HTMLAutoCompleteSelectField.php | 2 +- includes/htmlform/HTMLButtonField.php | 11 +----- includes/htmlform/HTMLEditTools.php | 2 +- includes/htmlform/HTMLForm.php | 8 ++-- includes/htmlform/HTMLFormField.php | 37 ++++++++++++------- includes/htmlform/HTMLFormFieldCloner.php | 8 ++-- includes/htmlform/HTMLSelectAndOtherField.php | 4 +- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/includes/htmlform/HTMLAutoCompleteSelectField.php b/includes/htmlform/HTMLAutoCompleteSelectField.php index 6606ca3833..76a88d5121 100644 --- a/includes/htmlform/HTMLAutoCompleteSelectField.php +++ b/includes/htmlform/HTMLAutoCompleteSelectField.php @@ -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 { diff --git a/includes/htmlform/HTMLButtonField.php b/includes/htmlform/HTMLButtonField.php index 16417fc4b5..0b8343b00c 100644 --- a/includes/htmlform/HTMLButtonField.php +++ b/includes/htmlform/HTMLButtonField.php @@ -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'] === ' ' ) { // Apparently some things set   directly and in an odd format diff --git a/includes/htmlform/HTMLEditTools.php b/includes/htmlform/HTMLEditTools.php index 77924ef24e..1b5d1fb4ea 100644 --- a/includes/htmlform/HTMLEditTools.php +++ b/includes/htmlform/HTMLEditTools.php @@ -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' ); } diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index ba432442f9..b7d91d88ad 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -56,19 +56,19 @@ * 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 diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index a9c76321d7..d100392778 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -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   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, [] ); + } + } } diff --git a/includes/htmlform/HTMLFormFieldCloner.php b/includes/htmlform/HTMLFormFieldCloner.php index 6553b561bc..4f2460f905 100644 --- a/includes/htmlform/HTMLFormFieldCloner.php +++ b/includes/htmlform/HTMLFormFieldCloner.php @@ -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() ); diff --git a/includes/htmlform/HTMLSelectAndOtherField.php b/includes/htmlform/HTMLSelectAndOtherField.php index e44ffa3083..e75c2b2507 100644 --- a/includes/htmlform/HTMLSelectAndOtherField.php +++ b/includes/htmlform/HTMLSelectAndOtherField.php @@ -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 ); -- 2.20.1