20 protected $mLabel; # String label, as HTML. Set on construction.
34 protected $mCondState = [];
41 protected $mShowEmptyLabels =
true;
94 public function msg( $key, ...$params ) {
95 if ( $this->mParent ) {
96 return $this->mParent->msg( $key, ...$params );
135 if ( isset( $this->mParams[
'cloner'] ) ) {
136 $field = $this->mParams[
'cloner']->findNearestField( $this, $name );
142 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
143 !$this->mParent->hasField( $name )
146 return $this->mParent->getField( substr( $name, 2 ) );
148 return $this->mParent->getField( $name );
165 if ( isset( $field->mParams[
'cloner'] ) ) {
166 $value = $field->mParams[
'cloner']->extractFieldData( $field, $alldata );
171 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
175 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
204 $origParams = $params;
205 $op = array_shift( $params );
210 if ( count( $params ) !== 1 ) {
211 throw new MWException(
"NOT takes exactly one parameter" );
219 foreach ( $params as $i => $p ) {
220 if ( !is_array( $p ) ) {
221 $type = gettype( $p );
222 throw new MWException(
"Expected array, found $type at index $i" );
230 if ( count( $params ) !== 2 ) {
231 throw new MWException(
"$op takes exactly two parameters" );
233 [ $name, $value ] = $params;
234 if ( !is_string( $name ) || !is_string( $value ) ) {
235 throw new MWException(
"Parameters for $op must be strings" );
244 "Invalid hide-if or disable-if specification for $this->mName: " .
245 $ex->getMessage() .
" in " . var_export( $origParams,
true ),
260 $op = array_shift( $params );
261 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' =>
true ];
262 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' =>
true ];
269 foreach ( $params as $p ) {
271 return !$valueRet[$op];
274 return $valueRet[$op];
281 [ $field, $value ] = $params;
285 return ( $value === $testValue );
287 return ( $value !== $testValue );
301 $op = array_shift( $params );
309 foreach ( $params as $p ) {
319 [ $name, $value ] = $params;
321 return [ $op, $field->getName(), $value ];
332 foreach ( $this->mCondState as
$type => $params ) {
347 if ( !( $this->mCondState && isset( $this->mCondState[
'hide'] ) ) ) {
363 if ( $this->mParams[
'disabled'] ??
false ) {
366 $hidden = $this->
isHidden( $alldata );
367 if ( !$this->mCondState || !isset( $this->mCondState[
'disable'] ) ) {
371 return $hidden || $this->
checkStateRecurse( $alldata, $this->mCondState[
'disable'] );
402 if ( $this->
isHidden( $alldata ) ) {
406 if ( isset( $this->mParams[
'required'] )
407 && $this->mParams[
'required'] !==
false
408 && ( $value ===
'' || $value ===
false || $value ===
null )
410 return $this->
msg(
'htmlform-required' );
413 if ( isset( $this->mValidationCallback ) ) {
414 return ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
428 public function filter( $value, $alldata ) {
429 if ( isset( $this->mFilterCallback ) ) {
430 $value = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
457 $this->mShowEmptyLabels = $show;
475 || $request->
getCheck(
'wpFormIdentifier' );
487 if ( $request->getCheck( $this->mName ) ) {
488 return $request->getText( $this->mName );
504 $this->mParams = $params;
506 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
507 $this->mParent = $params[
'parent'];
513 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
518 # Generate the label from a message, if possible
519 if ( isset( $params[
'label-message'] ) ) {
520 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
521 } elseif ( isset( $params[
'label'] ) ) {
522 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
524 $this->mLabel =
"\u{00A0}";
526 $this->mLabel = htmlspecialchars( $params[
'label'] );
528 } elseif ( isset( $params[
'label-raw'] ) ) {
529 $this->mLabel = $params[
'label-raw'];
532 $this->mName =
"wp{$params['fieldname']}";
533 if ( isset( $params[
'name'] ) ) {
534 $this->mName = $params[
'name'];
537 if ( isset( $params[
'dir'] ) ) {
538 $this->mDir = $params[
'dir'];
541 $this->mID =
"mw-input-{$this->mName}";
543 if ( isset( $params[
'default'] ) ) {
544 $this->mDefault = $params[
'default'];
547 if ( isset( $params[
'id'] ) ) {
548 $this->mID = $params[
'id'];
551 if ( isset( $params[
'cssclass'] ) ) {
552 $this->mClass = $params[
'cssclass'];
555 if ( isset( $params[
'csshelpclass'] ) ) {
556 $this->mHelpClass = $params[
'csshelpclass'];
559 if ( isset( $params[
'validation-callback'] ) ) {
560 $this->mValidationCallback = $params[
'validation-callback'];
563 if ( isset( $params[
'filter-callback'] ) ) {
564 $this->mFilterCallback = $params[
'filter-callback'];
567 if ( isset( $params[
'hidelabel'] ) ) {
568 $this->mShowEmptyLabels =
false;
571 if ( isset( $params[
'hide-if'] ) && $params[
'hide-if'] ) {
573 $this->mCondState[
'hide'] = $params[
'hide-if'];
574 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
576 if ( !( isset( $params[
'disabled'] ) && $params[
'disabled'] ) &&
577 isset( $params[
'disable-if'] ) && $params[
'disable-if']
580 $this->mCondState[
'disable'] = $params[
'disable-if'];
581 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
599 $cellAttributes = [];
603 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
604 $cellAttributes[
'colspan'] = 2;
605 $verticalLabel =
true;
607 $verticalLabel =
false;
612 $field = Html::rawElement(
614 [
'class' =>
'mw-input' ] + $cellAttributes,
615 $inputHtml .
"\n$errors"
618 if ( $this->mCondState ) {
620 $rowClasses .= implode(
' ', $this->mCondStateClass );
623 if ( $verticalLabel ) {
624 $html = Html::rawElement(
'tr',
625 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
626 $html .= Html::rawElement(
'tr',
628 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
632 $html = Html::rawElement(
'tr',
634 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
639 return $html . $helptext;
657 $cellAttributes = [];
662 'mw-htmlform-nolabel' => ( $label ===
'' )
665 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
667 if ( $horizontalLabel ) {
668 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
670 $field = Html::rawElement(
673 [
'class' => $outerDivClass ] + $cellAttributes,
674 $inputHtml .
"\n$errors"
677 $divCssClasses = [
"mw-htmlform-field-$fieldType",
678 $this->mClass, $this->mVFormClass, $errorClass ];
680 $wrapperAttributes = [
681 'class' => $divCssClasses,
683 if ( $this->mCondState ) {
685 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
687 $html = Html::rawElement(
'div', $wrapperAttributes, $label . $field );
705 if ( !$inputField ) {
710 new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
716 if ( is_string( $inputField ) ) {
720 $inputField =
new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $inputField ) ] );
727 foreach ( $errors as &$error ) {
728 $error =
new OOUI\HtmlSnippet( $error );
732 'classes' => [
"mw-htmlform-field-$fieldType" ],
734 'help' => ( $help !==
null && $help !==
'' ) ?
new OOUI\HtmlSnippet( $help ) :
null,
736 'infusable' => $infusable,
739 if ( $this->mClass !==
'' ) {
740 $config[
'classes'][] = $this->mClass;
743 $preloadModules =
false;
746 $preloadModules =
true;
747 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
749 if ( $this->mCondState ) {
750 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
755 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
756 $config[
'label'] =
new OOUI\HtmlSnippet( $label );
759 if ( $this->mCondState ) {
760 $preloadModules =
true;
766 if ( $preloadModules ) {
767 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
768 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
782 $name = explode(
'\\', static::class );
843 $cellAttributes = [];
865 $this->mVFormClass =
' mw-ui-vform-field';
866 return $this->
getDiv( $value );
880 $cellAttributes = [];
883 $html =
"\n" . $errors .
884 $label .
"\u{00A0}" .
899 if ( $helptext ===
null ) {
904 if ( $this->mCondState ) {
906 $rowAttributes[
'class'] = $this->mCondStateClass;
909 $tdClasses = [
'htmlform-tip' ];
910 if ( $this->mHelpClass !==
false ) {
911 $tdClasses[] = $this->mHelpClass;
913 $row = Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext );
914 $row = Html::rawElement(
'tr', $rowAttributes, $row );
928 if ( $helptext ===
null ) {
932 $wrapperAttributes = [
933 'class' => [
'htmlform-tip' ],
935 if ( $this->mHelpClass !==
false ) {
936 $wrapperAttributes[
'class'][] = $this->mHelpClass;
938 if ( $this->mCondState ) {
940 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
942 $div = Html::rawElement(
'div', $wrapperAttributes, $helptext );
967 if ( isset( $this->mParams[
'help-message'] ) ) {
968 $this->mParams[
'help-messages'] = [ $this->mParams[
'help-message'] ];
971 if ( isset( $this->mParams[
'help-messages'] ) ) {
972 foreach ( $this->mParams[
'help-messages'] as $msg ) {
975 if ( $msg->exists() ) {
976 if ( $helptext ===
null ) {
979 $helptext .= $this->
msg(
'word-separator' )->escaped();
981 $helptext .= $msg->parse();
984 } elseif ( isset( $this->mParams[
'help'] ) ) {
985 $helptext = $this->mParams[
'help'];
1000 return $this->mParams[
'help-inline'] ??
true;
1016 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1018 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1022 $errors = self::formatErrors( $errors );
1023 $errorClass =
'mw-htmlform-invalid-input';
1026 return [ $errors, $errorClass ];
1037 $errors = $this->
validate( $value, $this->mParent->mFieldData );
1039 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1043 if ( !is_array( $errors ) ) {
1044 $errors = [ $errors ];
1046 foreach ( $errors as &$error ) {
1047 if ( $error instanceof
Message ) {
1048 $error = $error->parse();
1060 return $this->mLabel ??
'';
1070 # Don't output a for= attribute for labels with no associated input.
1071 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1075 $for[
'for'] = $this->mID;
1078 $labelValue = trim( $this->
getLabel() );
1080 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1084 $displayFormat = $this->mParent->getDisplayFormat();
1086 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1088 if ( $displayFormat ===
'table' ) {
1090 Html::rawElement(
'td',
1091 [
'class' =>
'mw-label' ] + $cellAttributes,
1092 Html::rawElement(
'label', $for, $labelValue ) );
1093 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1094 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1096 Html::rawElement(
'div',
1097 [
'class' =>
'mw-label' ] + $cellAttributes,
1098 Html::rawElement(
'label', $for, $labelValue ) );
1100 $html = Html::rawElement(
'label', $for, $labelValue );
1112 return $this->mDefault ??
null;
1121 if ( empty( $this->mParams[
'tooltip'] ) ) {
1125 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1134 if ( empty( $this->mParams[
'tooltip'] ) ) {
1139 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1140 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1152 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1155 foreach ( $list as $key ) {
1156 if ( in_array( $key, $boolAttribs ) ) {
1157 if ( !empty( $this->mParams[$key] ) ) {
1160 } elseif ( isset( $this->mParams[$key] ) ) {
1161 $ret[$key] = $this->mParams[$key];
1177 private function lookupOptionsKeys( $options, $needsParse ) {
1179 foreach ( $options as $key => $value ) {
1180 $msg = $this->msg( $key );
1181 $key = $needsParse ? $msg->parse() : $msg->plain();
1182 $ret[$key] = is_array( $value )
1183 ? $this->lookupOptionsKeys( $value, $needsParse )
1197 if ( is_array( $array ) ) {
1198 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1200 return strval( $array );
1211 if ( $this->mOptions ===
false ) {
1212 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1213 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1214 if ( $needsParse ) {
1215 $this->mOptionsLabelsNotFromMessage =
true;
1217 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1218 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1219 $this->mOptionsLabelsNotFromMessage =
true;
1220 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1221 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1222 $message = $this->
getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1223 $this->mOptions = Xml::listDropDownOptions( $message );
1225 $this->mOptions =
null;
1229 return $this->mOptions;
1240 if ( $oldoptions ===
null ) {
1244 return Xml::listDropDownOptionsOoui( $oldoptions );
1257 foreach ( $options as $value ) {
1258 if ( is_array( $value ) ) {
1259 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1261 $flatOpts[] = $value;
1282 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1283 $errors = array_shift( $errors );
1286 if ( is_array( $errors ) ) {
1288 foreach ( $errors as $error ) {
1289 if ( $error instanceof
Message ) {
1290 $lines[] = Html::rawElement(
'li', [], $error->parse() );
1292 $lines[] = Html::rawElement(
'li', [], $error );
1296 $errors = Html::rawElement(
'ul', [], implode(
"\n",
$lines ) );
1298 if ( $errors instanceof
Message ) {
1299 $errors = $errors->parse();
1303 return Html::errorBox( $errors );
1315 if ( $this->mParent ) {
1316 $message->setContext( $this->mParent );
1330 return !empty( $this->mParams[
'nodata'] );
1341 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,...
if(!file_exists( $CREDITS)) $lines