20 protected $mLabel; # String label, as HTML. Set on construction.
96 public function msg( $key, ...$params ) {
97 if ( $this->mParent ) {
98 return $this->mParent->msg( $key, ...$params );
137 if ( isset( $this->mParams[
'cloner'] ) ) {
138 $field = $this->mParams[
'cloner']->findNearestField( $this, $name );
144 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
145 !$this->mParent->hasField( $name )
148 return $this->mParent->getField( substr( $name, 2 ) );
150 return $this->mParent->getField( $name );
167 if ( isset( $field->mParams[
'cloner'] ) ) {
168 $value = $field->mParams[
'cloner']->extractFieldData( $field, $alldata );
173 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
177 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
206 $origParams = $params;
207 $op = array_shift( $params );
212 if ( count( $params ) !== 1 ) {
213 throw new MWException(
"NOT takes exactly one parameter" );
221 foreach ( $params as $i => $p ) {
222 if ( !is_array( $p ) ) {
223 $type = gettype( $p );
224 throw new MWException(
"Expected array, found $type at index $i" );
232 if ( count( $params ) !== 2 ) {
233 throw new MWException(
"$op takes exactly two parameters" );
235 [ $name, $value ] = $params;
236 if ( !is_string( $name ) || !is_string( $value ) ) {
237 throw new MWException(
"Parameters for $op must be strings" );
246 "Invalid hide-if or disable-if specification for $this->mName: " .
247 $ex->getMessage() .
" in " . var_export( $origParams,
true ),
262 $op = array_shift( $params );
263 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' =>
true ];
264 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' =>
true ];
271 foreach ( $params as $p ) {
273 return !$valueRet[$op];
276 return $valueRet[$op];
283 [ $field, $value ] = $params;
287 return ( $value === $testValue );
289 return ( $value !== $testValue );
303 $op = array_shift( $params );
311 foreach ( $params as $p ) {
321 [ $name, $value ] = $params;
323 return [ $op, $field->getName(), $value ];
334 foreach ( $this->mCondState as
$type => $params ) {
349 if ( !( $this->mCondState && isset( $this->mCondState[
'hide'] ) ) ) {
365 if ( $this->mParams[
'disabled'] ??
false ) {
368 $hidden = $this->
isHidden( $alldata );
369 if ( !$this->mCondState || !isset( $this->mCondState[
'disable'] ) ) {
373 return $hidden || $this->
checkStateRecurse( $alldata, $this->mCondState[
'disable'] );
404 if ( $this->
isHidden( $alldata ) ) {
408 if ( isset( $this->mParams[
'required'] )
409 && $this->mParams[
'required'] !==
false
410 && ( $value ===
'' || $value ===
false || $value ===
null )
412 return $this->
msg(
'htmlform-required' );
415 if ( isset( $this->mValidationCallback ) ) {
416 return ( $this->mValidationCallback )( $value, $alldata,
$this->mParent );
430 public function filter( $value, $alldata ) {
431 if ( isset( $this->mFilterCallback ) ) {
459 $this->mShowEmptyLabels = $show;
477 || $request->
getCheck(
'wpFormIdentifier' );
489 if ( $request->getCheck( $this->mName ) ) {
490 return $request->getText( $this->mName );
506 $this->mParams = $params;
508 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
509 $this->mParent = $params[
'parent'];
515 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
520 # Generate the label from a message, if possible
521 if ( isset( $params[
'label-message'] ) ) {
522 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
523 } elseif ( isset( $params[
'label'] ) ) {
524 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
526 $this->mLabel =
"\u{00A0}";
528 $this->mLabel = htmlspecialchars( $params[
'label'] );
530 } elseif ( isset( $params[
'label-raw'] ) ) {
531 $this->mLabel = $params[
'label-raw'];
534 $this->mName =
"wp{$params['fieldname']}";
535 if ( isset( $params[
'name'] ) ) {
536 $this->mName = $params[
'name'];
539 if ( isset( $params[
'dir'] ) ) {
540 $this->mDir = $params[
'dir'];
543 $this->mID =
"mw-input-{$this->mName}";
545 if ( isset( $params[
'default'] ) ) {
546 $this->mDefault = $params[
'default'];
549 if ( isset( $params[
'id'] ) ) {
550 $this->mID = $params[
'id'];
553 if ( isset( $params[
'cssclass'] ) ) {
554 $this->mClass = $params[
'cssclass'];
557 if ( isset( $params[
'csshelpclass'] ) ) {
558 $this->mHelpClass = $params[
'csshelpclass'];
561 if ( isset( $params[
'validation-callback'] ) ) {
562 $this->mValidationCallback = $params[
'validation-callback'];
565 if ( isset( $params[
'filter-callback'] ) ) {
566 $this->mFilterCallback = $params[
'filter-callback'];
569 if ( isset( $params[
'hidelabel'] ) ) {
570 $this->mShowEmptyLabels =
false;
572 if ( isset( $params[
'notices'] ) ) {
573 $this->mNotices = $params[
'notices'];
576 if ( isset( $params[
'hide-if'] ) && $params[
'hide-if'] ) {
578 $this->mCondState[
'hide'] = $params[
'hide-if'];
579 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
581 if ( !( isset( $params[
'disabled'] ) && $params[
'disabled'] ) &&
582 isset( $params[
'disable-if'] ) && $params[
'disable-if']
585 $this->mCondState[
'disable'] = $params[
'disable-if'];
586 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
604 $cellAttributes = [];
608 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
609 $cellAttributes[
'colspan'] = 2;
610 $verticalLabel =
true;
612 $verticalLabel =
false;
617 $field = Html::rawElement(
619 [
'class' =>
'mw-input' ] + $cellAttributes,
620 $inputHtml .
"\n$errors"
623 if ( $this->mCondState ) {
625 $rowClasses .= implode(
' ', $this->mCondStateClass );
628 if ( $verticalLabel ) {
629 $html = Html::rawElement(
'tr',
630 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
631 $html .= Html::rawElement(
'tr',
633 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
637 $html = Html::rawElement(
'tr',
639 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
644 return $html . $helptext;
662 $cellAttributes = [];
667 'mw-htmlform-nolabel' => ( $label ===
'' )
670 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
672 if ( $horizontalLabel ) {
673 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
675 $field = Html::rawElement(
678 [
'class' => $outerDivClass ] + $cellAttributes,
679 $inputHtml .
"\n$errors"
682 $divCssClasses = [
"mw-htmlform-field-$fieldType",
685 $wrapperAttributes = [
686 'class' => $divCssClasses,
688 if ( $this->mCondState ) {
690 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
692 $html = Html::rawElement(
'div', $wrapperAttributes, $label . $field );
710 if ( !$inputField ) {
715 new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
721 if ( is_string( $inputField ) ) {
725 $inputField =
new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $inputField ) ] );
732 foreach ( $errors as &$error ) {
733 $error =
new OOUI\HtmlSnippet( $error );
737 'classes' => [
"mw-htmlform-field-$fieldType" ],
739 'help' => ( $help !==
null && $help !==
'' ) ?
new OOUI\HtmlSnippet( $help ) :
null,
741 'infusable' => $infusable,
743 'notices' => $this->mNotices ?: [],
745 if ( $this->mClass !==
'' ) {
749 $preloadModules =
false;
752 $preloadModules =
true;
753 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
755 if ( $this->mCondState ) {
756 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
761 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
762 $config[
'label'] =
new OOUI\HtmlSnippet( $label );
765 if ( $this->mCondState ) {
766 $preloadModules =
true;
772 if ( $preloadModules ) {
773 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
774 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
788 $name = explode(
'\\', static::class );
849 $cellAttributes = [];
871 $this->mVFormClass =
' mw-ui-vform-field';
872 return $this->
getDiv( $value );
886 $cellAttributes = [];
889 $html =
"\n" . $errors .
890 $label .
"\u{00A0}" .
905 if ( $helptext ===
null ) {
910 if ( $this->mCondState ) {
915 $tdClasses = [
'htmlform-tip' ];
916 if ( $this->mHelpClass !==
false ) {
919 $row = Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext );
920 $row = Html::rawElement(
'tr', $rowAttributes, $row );
934 if ( $helptext ===
null ) {
938 $wrapperAttributes = [
939 'class' => [
'htmlform-tip' ],
941 if ( $this->mHelpClass !==
false ) {
944 if ( $this->mCondState ) {
946 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
948 $div = Html::rawElement(
'div', $wrapperAttributes, $helptext );
973 if ( isset( $this->mParams[
'help-message'] ) ) {
974 $this->mParams[
'help-messages'] = [ $this->mParams[
'help-message'] ];
977 if ( isset( $this->mParams[
'help-messages'] ) ) {
978 foreach ( $this->mParams[
'help-messages'] as $msg ) {
981 if ( $msg->exists() ) {
982 if ( $helptext ===
null ) {
985 $helptext .= $this->
msg(
'word-separator' )->escaped();
987 $helptext .= $msg->parse();
990 } elseif ( isset( $this->mParams[
'help'] ) ) {
991 $helptext = $this->mParams[
'help'];
1006 return $this->mParams[
'help-inline'] ??
true;
1022 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1024 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1029 $errorClass =
'mw-htmlform-invalid-input';
1032 return [ $errors, $errorClass ];
1043 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1045 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1049 if ( !is_array( $errors ) ) {
1050 $errors = [ $errors ];
1052 foreach ( $errors as &$error ) {
1053 if ( $error instanceof
Message ) {
1054 $error = $error->parse();
1066 return $this->mLabel ??
'';
1076 # Don't output a for= attribute for labels with no associated input.
1077 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1084 $labelValue = trim( $this->
getLabel() );
1086 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1090 $displayFormat = $this->mParent->getDisplayFormat();
1092 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1094 if ( $displayFormat ===
'table' ) {
1096 Html::rawElement(
'td',
1097 [
'class' =>
'mw-label' ] + $cellAttributes,
1098 Html::rawElement(
'label', $for, $labelValue ) );
1099 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1100 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1102 Html::rawElement(
'div',
1103 [
'class' =>
'mw-label' ] + $cellAttributes,
1104 Html::rawElement(
'label', $for, $labelValue ) );
1106 $html = Html::rawElement(
'label', $for, $labelValue );
1118 return $this->mDefault ??
null;
1127 if ( empty( $this->mParams[
'tooltip'] ) ) {
1131 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1140 if ( empty( $this->mParams[
'tooltip'] ) ) {
1145 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1146 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1158 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1161 foreach ( $list as $key ) {
1162 if ( in_array( $key, $boolAttribs ) ) {
1163 if ( !empty( $this->mParams[$key] ) ) {
1166 } elseif ( isset( $this->mParams[$key] ) ) {
1167 $ret[$key] = $this->mParams[$key];
1183 private function lookupOptionsKeys( $options, $needsParse ) {
1185 foreach ( $options as $key => $value ) {
1186 $msg = $this->
msg( $key );
1187 $key = $needsParse ? $msg->parse() : $msg->plain();
1188 $ret[$key] = is_array( $value )
1189 ? $this->lookupOptionsKeys( $value, $needsParse )
1203 if ( is_array( $array ) ) {
1204 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1206 return strval( $array );
1217 if ( $this->mOptions ===
false ) {
1218 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1219 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1220 if ( $needsParse ) {
1221 $this->mOptionsLabelsNotFromMessage =
true;
1223 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1224 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1225 $this->mOptionsLabelsNotFromMessage =
true;
1227 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1228 $message = $this->
getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1231 $this->mOptions =
null;
1246 if ( $oldoptions ===
null ) {
1263 foreach ( $options as $value ) {
1264 if ( is_array( $value ) ) {
1265 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1267 $flatOpts[] = $value;
1288 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1289 $errors = array_shift( $errors );
1292 if ( is_array( $errors ) ) {
1294 foreach ( $errors as $error ) {
1295 if ( $error instanceof
Message ) {
1296 $lines[] = Html::rawElement(
'li', [], $error->parse() );
1298 $lines[] = Html::rawElement(
'li', [], $error );
1302 $errors = Html::rawElement(
'ul', [], implode(
"\n",
$lines ) );
1304 if ( $errors instanceof
Message ) {
1305 $errors = $errors->parse();
1309 return Html::errorBox( $errors );
1321 if ( $this->mParent ) {
1322 $message->setContext( $this->mParent );
1336 return !empty( $this->mParams[
'nodata'] );
1347 if ( $this->mCondState ) {
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
The Message class deals with fetching and processing of interface message into a variety of formats.
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getCheck( $name)
Return true if the named value is set in the input, whatever that value is (even "0").
wasPosted()
Returns true if the present request was reached by a POST operation, false otherwise (GET,...
static listDropDownOptions( $list, $params=[])
Build options for a drop-down box from a textual list.
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
if(!file_exists( $CREDITS)) $lines