19 protected $mLabel; # String label, as HTML. Set on construction.
93 public function msg( $key, ...$params ) {
94 if ( $this->mParent ) {
95 return $this->mParent->msg( $key, ...$params );
134 if ( isset( $this->mParams[
'cloner'] ) ) {
135 $field = $this->mParams[
'cloner']->findNearestField( $this, $name );
141 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
142 !$this->mParent->hasField( $name )
145 return $this->mParent->getField( substr( $name, 2 ) );
147 return $this->mParent->getField( $name );
164 if ( isset( $field->mParams[
'cloner'] ) ) {
165 $value = $field->mParams[
'cloner']->extractFieldData( $field, $alldata );
170 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
174 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
203 $origParams = $params;
204 $op = array_shift( $params );
209 if ( count( $params ) !== 1 ) {
210 throw new MWException(
"NOT takes exactly one parameter" );
218 foreach ( $params as $i => $p ) {
219 if ( !is_array( $p ) ) {
220 $type = gettype( $p );
221 throw new MWException(
"Expected array, found $type at index $i" );
229 if ( count( $params ) !== 2 ) {
230 throw new MWException(
"$op takes exactly two parameters" );
232 [ $name, $value ] = $params;
233 if ( !is_string( $name ) || !is_string( $value ) ) {
234 throw new MWException(
"Parameters for $op must be strings" );
243 "Invalid hide-if or disable-if specification for $this->mName: " .
244 $ex->getMessage() .
" in " . var_export( $origParams,
true ),
259 $op = array_shift( $params );
260 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' =>
true ];
261 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' =>
true ];
268 foreach ( $params as $p ) {
270 return !$valueRet[$op];
273 return $valueRet[$op];
280 [ $field, $value ] = $params;
284 return ( $value === $testValue );
286 return ( $value !== $testValue );
300 $op = array_shift( $params );
308 foreach ( $params as $p ) {
318 [ $name, $value ] = $params;
320 return [ $op, $field->getName(), $value ];
331 foreach ( $this->mCondState as
$type => $params ) {
346 if ( !( $this->mCondState && isset( $this->mCondState[
'hide'] ) ) ) {
362 if ( $this->mParams[
'disabled'] ??
false ) {
365 $hidden = $this->
isHidden( $alldata );
366 if ( !$this->mCondState || !isset( $this->mCondState[
'disable'] ) ) {
370 return $hidden || $this->
checkStateRecurse( $alldata, $this->mCondState[
'disable'] );
401 if ( $this->
isHidden( $alldata ) ) {
405 if ( isset( $this->mParams[
'required'] )
406 && $this->mParams[
'required'] !==
false
407 && ( $value ===
'' || $value ===
false || $value ===
null )
409 return $this->
msg(
'htmlform-required' );
412 if ( isset( $this->mValidationCallback ) ) {
413 return ( $this->mValidationCallback )( $value, $alldata,
$this->mParent );
427 public function filter( $value, $alldata ) {
428 if ( isset( $this->mFilterCallback ) ) {
456 $this->mShowEmptyLabels = $show;
474 || $request->
getCheck(
'wpFormIdentifier' );
486 if ( $request->getCheck( $this->mName ) ) {
487 return $request->getText( $this->mName );
503 $this->mParams = $params;
505 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
506 $this->mParent = $params[
'parent'];
512 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
517 # Generate the label from a message, if possible
518 if ( isset( $params[
'label-message'] ) ) {
519 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
520 } elseif ( isset( $params[
'label'] ) ) {
521 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
523 $this->mLabel =
"\u{00A0}";
525 $this->mLabel = htmlspecialchars( $params[
'label'] );
527 } elseif ( isset( $params[
'label-raw'] ) ) {
528 $this->mLabel = $params[
'label-raw'];
531 $this->mName =
"wp{$params['fieldname']}";
532 if ( isset( $params[
'name'] ) ) {
533 $this->mName = $params[
'name'];
536 if ( isset( $params[
'dir'] ) ) {
537 $this->mDir = $params[
'dir'];
540 $this->mID =
"mw-input-{$this->mName}";
542 if ( isset( $params[
'default'] ) ) {
543 $this->mDefault = $params[
'default'];
546 if ( isset( $params[
'id'] ) ) {
547 $this->mID = $params[
'id'];
550 if ( isset( $params[
'cssclass'] ) ) {
551 $this->mClass = $params[
'cssclass'];
554 if ( isset( $params[
'csshelpclass'] ) ) {
555 $this->mHelpClass = $params[
'csshelpclass'];
558 if ( isset( $params[
'validation-callback'] ) ) {
559 $this->mValidationCallback = $params[
'validation-callback'];
562 if ( isset( $params[
'filter-callback'] ) ) {
563 $this->mFilterCallback = $params[
'filter-callback'];
566 if ( isset( $params[
'hidelabel'] ) ) {
567 $this->mShowEmptyLabels =
false;
570 if ( isset( $params[
'hide-if'] ) && $params[
'hide-if'] ) {
572 $this->mCondState[
'hide'] = $params[
'hide-if'];
573 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
575 if ( !( isset( $params[
'disabled'] ) && $params[
'disabled'] ) &&
576 isset( $params[
'disable-if'] ) && $params[
'disable-if']
579 $this->mCondState[
'disable'] = $params[
'disable-if'];
580 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
598 $cellAttributes = [];
602 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
603 $cellAttributes[
'colspan'] = 2;
604 $verticalLabel =
true;
606 $verticalLabel =
false;
613 [
'class' =>
'mw-input' ] + $cellAttributes,
614 $inputHtml .
"\n$errors"
617 if ( $this->mCondState ) {
619 $rowClasses .= implode(
' ', $this->mCondStateClass );
622 if ( $verticalLabel ) {
624 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
627 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
633 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
638 return $html . $helptext;
656 $cellAttributes = [];
661 'mw-htmlform-nolabel' => ( $label ===
'' )
664 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
666 if ( $horizontalLabel ) {
667 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
672 [
'class' => $outerDivClass ] + $cellAttributes,
673 $inputHtml .
"\n$errors"
676 $divCssClasses = [
"mw-htmlform-field-$fieldType",
679 $wrapperAttributes = [
680 'class' => $divCssClasses,
682 if ( $this->mCondState ) {
684 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
704 if ( !$inputField ) {
709 new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
715 if ( is_string( $inputField ) ) {
719 $inputField =
new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $inputField ) ] );
726 foreach ( $errors as &$error ) {
727 $error =
new OOUI\HtmlSnippet( $error );
731 'classes' => [
"mw-htmlform-field-$fieldType" ],
733 'help' => ( $help !==
null && $help !==
'' ) ?
new OOUI\HtmlSnippet( $help ) :
null,
735 'infusable' => $infusable,
738 if ( $this->mClass !==
'' ) {
742 $preloadModules =
false;
745 $preloadModules =
true;
746 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
748 if ( $this->mCondState ) {
749 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
754 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
755 $config[
'label'] =
new OOUI\HtmlSnippet( $label );
758 if ( $this->mCondState ) {
759 $preloadModules =
true;
765 if ( $preloadModules ) {
766 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
767 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
781 $name = explode(
'\\', static::class );
842 $cellAttributes = [];
864 $this->mVFormClass =
' mw-ui-vform-field';
865 return $this->
getDiv( $value );
879 $cellAttributes = [];
882 $html =
"\n" . $errors .
883 $label .
"\u{00A0}" .
898 if ( $helptext ===
null ) {
903 if ( $this->mCondState ) {
908 $tdClasses = [
'htmlform-tip' ];
909 if ( $this->mHelpClass !==
false ) {
912 $row =
Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext );
927 if ( $helptext ===
null ) {
931 $wrapperAttributes = [
932 'class' => [
'htmlform-tip' ],
934 if ( $this->mHelpClass !==
false ) {
937 if ( $this->mCondState ) {
939 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
966 if ( isset( $this->mParams[
'help-message'] ) ) {
967 $this->mParams[
'help-messages'] = [ $this->mParams[
'help-message'] ];
970 if ( isset( $this->mParams[
'help-messages'] ) ) {
971 foreach ( $this->mParams[
'help-messages'] as $msg ) {
974 if ( $msg->exists() ) {
975 if ( $helptext ===
null ) {
978 $helptext .= $this->
msg(
'word-separator' )->escaped();
980 $helptext .= $msg->parse();
983 } elseif ( isset( $this->mParams[
'help'] ) ) {
984 $helptext = $this->mParams[
'help'];
999 return $this->mParams[
'help-inline'] ??
true;
1015 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1017 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1022 $errorClass =
'mw-htmlform-invalid-input';
1025 return [ $errors, $errorClass ];
1036 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1038 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1042 if ( !is_array( $errors ) ) {
1043 $errors = [ $errors ];
1045 foreach ( $errors as &$error ) {
1046 if ( $error instanceof
Message ) {
1047 $error = $error->parse();
1059 return $this->mLabel ??
'';
1069 # Don't output a for= attribute for labels with no associated input.
1070 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1077 $labelValue = trim( $this->
getLabel() );
1079 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1083 $displayFormat = $this->mParent->getDisplayFormat();
1085 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1087 if ( $displayFormat ===
'table' ) {
1090 [
'class' =>
'mw-label' ] + $cellAttributes,
1092 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1093 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1096 [
'class' =>
'mw-label' ] + $cellAttributes,
1111 return $this->mDefault ??
null;
1120 if ( empty( $this->mParams[
'tooltip'] ) ) {
1124 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1133 if ( empty( $this->mParams[
'tooltip'] ) ) {
1138 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1139 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1151 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1154 foreach ( $list as $key ) {
1155 if ( in_array( $key, $boolAttribs ) ) {
1156 if ( !empty( $this->mParams[$key] ) ) {
1159 } elseif ( isset( $this->mParams[$key] ) ) {
1160 $ret[$key] = $this->mParams[$key];
1176 private function lookupOptionsKeys( $options, $needsParse ) {
1178 foreach ( $options as $key => $value ) {
1179 $msg = $this->
msg( $key );
1180 $key = $needsParse ? $msg->parse() : $msg->plain();
1181 $ret[$key] = is_array( $value )
1182 ? $this->lookupOptionsKeys( $value, $needsParse )
1196 if ( is_array( $array ) ) {
1197 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1199 return strval( $array );
1210 if ( $this->mOptions ===
false ) {
1211 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1212 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1213 if ( $needsParse ) {
1214 $this->mOptionsLabelsNotFromMessage =
true;
1216 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1217 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1218 $this->mOptionsLabelsNotFromMessage =
true;
1220 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1221 $message = $this->
getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1224 $this->mOptions =
null;
1239 if ( $oldoptions ===
null ) {
1256 foreach ( $options as $value ) {
1257 if ( is_array( $value ) ) {
1258 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1260 $flatOpts[] = $value;
1281 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1282 $errors = array_shift( $errors );
1285 if ( is_array( $errors ) ) {
1287 foreach ( $errors as $error ) {
1288 if ( $error instanceof
Message ) {
1297 if ( $errors instanceof
Message ) {
1298 $errors = $errors->parse();
1314 if ( $this->mParent ) {
1315 $message->setContext( $this->mParent );
1329 return !empty( $this->mParams[
'nodata'] );
1340 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.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
static errorBox( $html, $heading='', $className='')
Return an error box.
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