$args = func_get_args();
if ( $this->mParent ) {
- $callback = array( $this->mParent, 'msg' );
+ $callback = [ $this->mParent, 'msg' ];
} else {
$callback = 'wfMessage';
}
*/
protected function getNearestFieldByName( $alldata, $name ) {
$tmp = $this->mName;
- $thisKeys = array();
+ $thisKeys = [];
while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
array_unshift( $thisKeys, $m[2] );
$tmp = $m[1];
array_unshift( $thisKeys, $tmp );
$tmp = $name;
- $nameKeys = array();
+ $nameKeys = [];
while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
array_unshift( $nameKeys, $m[2] );
$tmp = $m[1];
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"
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"
# 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 = array();
- }
-
- $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
}
$validName = Sanitizer::escapeId( $this->mName );
- $validName = str_replace( array( '.5B', '.5D' ), array( '[', ']' ), $validName );
+ $validName = str_replace( [ '.5B', '.5D' ], [ '[', ']' ], $validName );
if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
}
$inputHtml = $this->getInputHTML( $value );
$fieldType = get_class( $this );
$helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
- $cellAttributes = array();
- $rowAttributes = array();
+ $cellAttributes = [];
+ $rowAttributes = [];
$rowClasses = '';
if ( !empty( $this->mParams['vertical-label'] ) ) {
$field = Html::rawElement(
'td',
- array( 'class' => 'mw-input' ) + $cellAttributes,
+ [ 'class' => 'mw-input' ] + $cellAttributes,
$inputHtml . "\n$errors"
);
if ( $verticalLabel ) {
$html = Html::rawElement( 'tr',
- $rowAttributes + array( 'class' => "mw-htmlform-vertical-label $rowClasses" ), $label );
+ $rowAttributes + [ 'class' => "mw-htmlform-vertical-label $rowClasses" ], $label );
$html .= Html::rawElement( 'tr',
- $rowAttributes + array(
+ $rowAttributes + [
'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
- ),
+ ],
$field );
} else {
$html =
Html::rawElement( 'tr',
- $rowAttributes + array(
+ $rowAttributes + [
'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
- ),
+ ],
$label . $field );
}
$inputHtml = $this->getInputHTML( $value );
$fieldType = get_class( $this );
$helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
- $cellAttributes = array();
+ $cellAttributes = [];
$label = $this->getLabelHtml( $cellAttributes );
- $outerDivClass = array(
+ $outerDivClass = [
'mw-input',
'mw-htmlform-nolabel' => ( $label === '' )
- );
+ ];
$horizontalLabel = isset( $this->mParams['horizontal-label'] )
? $this->mParams['horizontal-label'] : false;
} else {
$field = Html::rawElement(
'div',
- array( 'class' => $outerDivClass ) + $cellAttributes,
+ [ 'class' => $outerDivClass ] + $cellAttributes,
$inputHtml . "\n$errors"
);
}
- $divCssClasses = array( "mw-htmlform-field-$fieldType",
- $this->mClass, $this->mVFormClass, $errorClass );
+ $divCssClasses = [ "mw-htmlform-field-$fieldType",
+ $this->mClass, $this->mVFormClass, $errorClass ];
- $wrapperAttributes = array(
+ $wrapperAttributes = [
'class' => $divCssClasses,
- );
+ ];
if ( $this->mHideIf ) {
$wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
$wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
// generate the whole field, label and errors and all, then wrap it in a Widget.
// It might look weird, but it'll work OK.
return $this->getFieldLayoutOOUI(
- new OOUI\Widget( array( 'content' => new OOUI\HtmlSnippet( $this->getDiv( $value ) ) ) ),
- array( 'infusable' => false, 'align' => 'top' )
+ new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $this->getDiv( $value ) ) ] ),
+ [ 'infusable' => false, 'align' => 'top' ]
);
}
// We have an OOUI implementation, but it's not proper, and we got a load of HTML.
// Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
// JavaScript doesn't know how to rebuilt the contents.
- $inputField = new OOUI\Widget( array( 'content' => new OOUI\HtmlSnippet( $inputField ) ) );
+ $inputField = new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $inputField ) ] );
$infusable = false;
}
$error = new OOUI\HtmlSnippet( $error );
}
- $config = array(
- 'classes' => array( "mw-htmlform-field-$fieldType", $this->mClass ),
+ $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 );
}
list( $errors, ) = $this->getErrorsAndErrorClass( $value );
$inputHtml = $this->getInputHTML( $value );
$helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
- $cellAttributes = array();
+ $cellAttributes = [];
$label = $this->getLabelHtml( $cellAttributes );
$html = "\n$errors";
list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
$inputHtml = $this->getInputHTML( $value );
$helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
- $cellAttributes = array();
+ $cellAttributes = [];
$label = $this->getLabelHtml( $cellAttributes );
$html = "\n" . $errors .
return '';
}
- $rowAttributes = array();
+ $rowAttributes = [];
if ( $this->mHideIf ) {
$rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
$rowAttributes['class'] = 'mw-htmlform-hide-if';
}
- $tdClasses = array( 'htmlform-tip' );
+ $tdClasses = [ 'htmlform-tip' ];
if ( $this->mHelpClass !== false ) {
$tdClasses[] = $this->mHelpClass;
}
- $row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => $tdClasses ), $helptext );
+ $row = Html::rawElement( 'td', [ 'colspan' => 2, 'class' => $tdClasses ], $helptext );
$row = Html::rawElement( 'tr', $rowAttributes, $row );
return $row;
return '';
}
- $wrapperAttributes = array(
+ $wrapperAttributes = [
'class' => 'htmlform-tip',
- );
+ ];
if ( $this->mHelpClass !== false ) {
$wrapperAttributes['class'] .= " {$this->mHelpClass}";
}
$helptext = null;
if ( isset( $this->mParams['help-message'] ) ) {
- $this->mParams['help-messages'] = array( $this->mParams['help-message'] );
+ $this->mParams['help-messages'] = [ $this->mParams['help-message'] ];
}
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 ) ) {
$errorClass = 'mw-htmlform-invalid-input';
}
- return array( $errors, $errorClass );
+ return [ $errors, $errorClass ];
}
/**
$errors = $this->validate( $value, $this->mParent->mFieldData );
if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
- $errors = array();
+ $errors = [];
}
if ( !is_array( $errors ) ) {
- $errors = array( $errors );
+ $errors = [ $errors ];
}
foreach ( $errors as &$error ) {
if ( $error instanceof Message ) {
return is_null( $this->mLabel ) ? '' : $this->mLabel;
}
- function getLabelHtml( $cellAttributes = array() ) {
+ function getLabelHtml( $cellAttributes = [] ) {
# Don't output a for= attribute for labels with no associated input.
# Kind of hacky here, possibly we don't want these to be <label>s at all.
- $for = array();
+ $for = [];
if ( $this->needsLabel() ) {
$for['for'] = $this->mID;
if ( $displayFormat === 'table' ) {
$html =
Html::rawElement( 'td',
- array( 'class' => 'mw-label' ) + $cellAttributes,
+ [ 'class' => 'mw-label' ] + $cellAttributes,
Html::rawElement( 'label', $for, $labelValue ) );
} elseif ( $hasLabel || $this->mShowEmptyLabels ) {
if ( $displayFormat === 'div' && !$horizontalLabel ) {
$html =
Html::rawElement( 'div',
- array( 'class' => 'mw-label' ) + $cellAttributes,
+ [ 'class' => 'mw-label' ] + $cellAttributes,
Html::rawElement( 'label', $for, $labelValue ) );
} else {
$html = Html::rawElement( 'label', $for, $labelValue );
*/
public function getTooltipAndAccessKey() {
if ( empty( $this->mParams['tooltip'] ) ) {
- return array();
+ return [];
}
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 ) {
- static $boolAttribs = array( 'disabled', 'required', 'autofocus', 'multiple', 'readonly' );
+ public function getAttributes( array $list ) {
+ static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
- $ret = array();
+ $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];
}
}
* @return array
*/
private function lookupOptionsKeys( $options ) {
- $ret = array();
+ $ret = [];
foreach ( $options as $key => $value ) {
$key = $this->msg( $key )->plain();
$ret[$key] = is_array( $value )
*/
static function forceToStringRecursive( $array ) {
if ( is_array( $array ) ) {
- return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
+ return array_map( [ __CLASS__, 'forceToStringRecursive' ], $array );
} else {
return strval( $array );
}
$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 = array();
+ $this->mOptions = [];
foreach ( explode( "\n", $message ) as $option ) {
$value = trim( $option );
if ( $value == '' ) {
return null;
}
- $options = array();
+ $options = [];
foreach ( $oldoptions as $text => $data ) {
- $options[] = array(
+ $options[] = [
'data' => (string)$data,
'label' => (string)$text,
- );
+ ];
}
return $options;
* @return array Flattened input
*/
public static function flattenOptions( $options ) {
- $flatOpts = array();
+ $flatOpts = [];
foreach ( $options as $value ) {
if ( is_array( $value ) ) {
}
if ( is_array( $errors ) ) {
- $lines = array();
+ $lines = [];
foreach ( $errors as $error ) {
if ( $error instanceof Message ) {
- $lines[] = Html::rawElement( 'li', array(), $error->parse() );
+ $lines[] = Html::rawElement( 'li', [], $error->parse() );
} else {
- $lines[] = Html::rawElement( 'li', array(), $error );
+ $lines[] = Html::rawElement( 'li', [], $error );
}
}
- return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
+ return Html::rawElement( 'ul', [ 'class' => 'error' ], implode( "\n", $lines ) );
} else {
if ( $errors instanceof Message ) {
$errors = $errors->parse();
}
- return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
+ 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, [] );
}
}
}