5use InvalidArgumentException;
32 protected $mValidationCallback;
34 protected $mFilterCallback;
140 public function msg( $key, ...$params ) {
141 if ( $this->mParent ) {
142 return $this->mParent->msg( $key, ...$params );
181 $cloner = $this->mParams[
'cloner'] ??
null;
183 $field = $cloner->findNearestField( $this, $name );
189 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
190 !$this->mParent->hasField( $name )
193 return $this->mParent->getField( substr( $name, 2 ) );
195 return $this->mParent->getField( $name );
212 $cloner = $field->mParams[
'cloner'] ??
null;
214 $value = $cloner->extractFieldData( $field, $alldata );
219 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
223 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
251 $origParams = $params;
252 $op = array_shift( $params );
254 $makeException =
function (
string $details ) use ( $origParams ): InvalidArgumentException {
255 return new InvalidArgumentException(
256 "Invalid hide-if or disable-if specification for $this->mName: " .
257 $details .
" in " . var_export( $origParams,
true )
263 if ( count( $params ) !== 1 ) {
264 throw $makeException(
"NOT takes exactly one parameter" );
272 foreach ( $params as $i => $p ) {
273 if ( !is_array( $p ) ) {
274 $type = get_debug_type( $p );
275 throw $makeException(
"Expected array, found $type at index $i" );
284 if ( count( $params ) !== 2 ) {
285 throw $makeException(
"$op takes exactly two parameters" );
287 [ $name, $value ] = $params;
288 if ( !is_string( $name ) || !is_string( $value ) ) {
289 throw $makeException(
"Parameters for $op must be strings" );
294 throw $makeException(
"Unknown operation" );
306 $op = array_shift( $params );
307 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' => true ];
308 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' => true ];
315 foreach ( $params as $p ) {
317 return !$valueRet[$op];
320 return $valueRet[$op];
328 [ $field, $value ] = $params;
332 return ( $value === (
string)$testValue );
334 return ( $value !== (
string)$testValue );
336 return in_array( $value, $testValue,
true );
350 $op = array_shift( $params );
358 foreach ( $params as $p ) {
369 [ $name, $value ] = $params;
371 return [ $op, $field->getName(), $value ];
382 foreach ( $this->mCondState as $type => $params ) {
397 return isset( $this->mCondState[
'hide'] ) &&
410 return ( $this->mParams[
'disabled'] ??
false ) ||
412 ( isset( $this->mCondState[
'disable'] )
444 if ( $this->
isHidden( $alldata ) ) {
448 if ( isset( $this->mParams[
'required'] )
449 && $this->mParams[
'required'] !==
false
450 && ( $value ===
'' || $value ===
false || $value ===
null )
452 return $this->
msg(
'htmlform-required' );
455 if ( $this->mValidationCallback ===
null ) {
459 $p = ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
462 $language = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
464 return $p->isGood() ? true : Status::wrap( $p )->getHTML(
false,
false, $language );
478 public function filter( $value, $alldata ) {
479 if ( $this->mFilterCallback !==
null ) {
480 $value = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
507 $this->mShowEmptyLabels = $show;
525 || $request->
getCheck(
'wpFormIdentifier' );
537 if ( $request->getCheck( $this->mName ) ) {
538 return $request->getText( $this->mName );
553 $this->mParams = $params;
555 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
556 $this->mParent = $params[
'parent'];
562 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
567 # Generate the label from a message, if possible
568 if ( isset( $params[
'label-message'] ) ) {
569 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
570 } elseif ( isset( $params[
'label'] ) ) {
571 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
573 $this->mLabel =
"\u{00A0}";
575 $this->mLabel = htmlspecialchars( $params[
'label'] );
577 } elseif ( isset( $params[
'label-raw'] ) ) {
578 $this->mLabel = $params[
'label-raw'];
581 $this->mName = $params[
'name'] ??
'wp' . $params[
'fieldname'];
583 if ( isset( $params[
'dir'] ) ) {
584 $this->mDir = $params[
'dir'];
587 $this->mID =
"mw-input-{$this->mName}";
589 if ( isset( $params[
'default'] ) ) {
590 $this->mDefault = $params[
'default'];
593 if ( isset( $params[
'id'] ) ) {
594 $this->mID = $params[
'id'];
597 if ( isset( $params[
'cssclass'] ) ) {
598 $this->mClass = $params[
'cssclass'];
601 if ( isset( $params[
'csshelpclass'] ) ) {
602 $this->mHelpClass = $params[
'csshelpclass'];
605 if ( isset( $params[
'validation-callback'] ) ) {
606 $this->mValidationCallback = $params[
'validation-callback'];
609 if ( isset( $params[
'filter-callback'] ) ) {
610 $this->mFilterCallback = $params[
'filter-callback'];
613 if ( isset( $params[
'hidelabel'] ) ) {
614 $this->mShowEmptyLabels =
false;
616 if ( isset( $params[
'notices'] ) ) {
617 $this->mNotices = $params[
'notices'];
620 if ( isset( $params[
'hide-if'] ) && $params[
'hide-if'] ) {
622 $this->mCondState[
'hide'] = $params[
'hide-if'];
623 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
625 if ( !( isset( $params[
'disabled'] ) && $params[
'disabled'] ) &&
626 isset( $params[
'disable-if'] ) && $params[
'disable-if']
629 $this->mCondState[
'disable'] = $params[
'disable-if'];
630 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
648 $cellAttributes = [];
652 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
653 $cellAttributes[
'colspan'] = 2;
654 $verticalLabel =
true;
656 $verticalLabel =
false;
661 $field = Html::rawElement(
663 [
'class' =>
'mw-input' ] + $cellAttributes,
664 $inputHtml .
"\n$errors"
667 if ( $this->mCondState ) {
669 $rowClasses .= implode(
' ', $this->mCondStateClass );
670 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
671 $rowClasses .=
' mw-htmlform-hide-if-hidden';
675 if ( $verticalLabel ) {
676 $html = Html::rawElement(
'tr',
677 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
678 $html .= Html::rawElement(
'tr',
680 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
684 $html = Html::rawElement(
'tr',
686 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
691 return $html . $helptext;
709 $cellAttributes = [];
714 'mw-htmlform-nolabel' => ( $label ===
'' )
717 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
719 if ( $horizontalLabel ) {
720 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
722 $field = Html::rawElement(
725 [
'class' => $outerDivClass ] + $cellAttributes,
726 $inputHtml .
"\n$errors"
730 $wrapperAttributes = [
'class' => [
731 "mw-htmlform-field-$fieldType",
735 if ( $this->mCondState ) {
737 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
738 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
739 $wrapperAttributes[
'class'][] =
'mw-htmlform-hide-if-hidden';
742 return Html::rawElement(
'div', $wrapperAttributes, $label . $field ) .
756 if ( $this->getDescriptionMessages() !== [] ) {
757 throw new InvalidArgumentException(
758 "OOUIHTMLForm does not support the descriptions for fields. Please use Codex"
763 if ( !$inputField ) {
768 new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
774 if ( is_string( $inputField ) ) {
778 $inputField = new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $inputField ) ] );
785 foreach ( $errors as &$error ) {
786 $error = new \OOUI\HtmlSnippet( $error );
790 'classes' => [
"mw-htmlform-field-$fieldType" ],
792 'help' => ( $help !==
null && $help !==
'' ) ?
new \OOUI\HtmlSnippet( $help ) :
null,
794 'infusable' => $infusable,
796 'notices' => $this->mNotices ?: [],
798 if ( $this->mClass !==
'' ) {
802 $preloadModules =
false;
805 $preloadModules =
true;
806 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
808 if ( $this->mCondState ) {
809 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
810 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
811 $config[
'classes'][] =
'mw-htmlform-hide-if-hidden';
817 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
818 $config[
'label'] = new \OOUI\HtmlSnippet( $label );
821 if ( $this->mCondState ) {
822 $preloadModules =
true;
828 if ( $preloadModules ) {
829 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
830 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
844 $isDisabled = ( $this->mParams[
'disabled'] ?? false );
848 $labelValue = trim( $this->
getLabel() );
851 if ( $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ' ) {
853 $labelClasses = [
'cdx-label' ];
855 $labelClasses[] =
'cdx-label--disabled';
859 [
'cdx-label__description' ]
862 if ( $this->showOptionalFlag() ) {
863 $messageKey = $this->mParams[
'optional-message'] ??
'htmlform-optional-flag';
864 $optionalHtml = Html::rawElement(
866 [
'class' =>
'cdx-label__label__optional-flag' ],
867 ' ' . $this->
getMessage( $messageKey )->parse(),
871 $labelDiv = Html::rawElement(
'div', [
'class' => $labelClasses ],
873 Html::rawElement(
'label', [
'class' =>
'cdx-label__label' ] + $labelFor,
875 Html::rawElement(
'span', [
'class' =>
'cdx-label__label__text' ],
878 ) . $descriptionHtml,
890 $validationMessage =
'';
892 if ( $errors !==
'' ) {
893 $validationMessage = Html::rawElement(
'div', [
'class' =>
'cdx-field__validation-message' ],
901 $controlClasses = [
'cdx-field__control' ];
903 $controlClasses[] =
'cdx-field__control--has-help-text';
905 $control = Html::rawElement(
'div', [
'class' => $controlClasses ], $inputHtml );
909 "mw-htmlform-field-{$this->getClassName()}",
915 $fieldClasses[] =
'cdx-field--disabled';
917 $fieldAttributes = [];
919 if ( $this->mCondState ) {
921 $fieldClasses = array_merge( $fieldClasses, $this->mCondStateClass );
922 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
923 $fieldClasses[] =
'mw-htmlform-hide-if-hidden';
927 return Html::rawElement(
'div', [
'class' => $fieldClasses ] + $fieldAttributes,
928 $labelDiv . $control . $helptext . $validationMessage
932 private function showOptionalFlag(): bool {
933 $shouldShowOptionalFlag = $this->mParams[
'show-optional-flag'] ?? false;
934 if ( !$shouldShowOptionalFlag ) {
938 $isRequired = $this->mParams[
'required'] ??
false;
941 throw new \InvalidArgumentException(
'A field cannot be both optional and required.' );
954 $name = explode(
'\\', static::class );
987 return !$this->isHelpInline() && $this->getHelpMessages();
1012 [ $errors, ] = $this->getErrorsAndErrorClass( $value );
1013 return "\n" . $errors .
1014 $this->getLabelHtml() .
1015 $this->getInputHTML( $value ) .
1016 $this->getHelpTextHtmlRaw( $this->getHelpText() );
1027 [ $errors, ] = $this->getErrorsAndErrorClass( $value );
1028 return "\n" . $errors .
1029 $this->getLabelHtml() .
1031 $this->getInputHTML( $value ) .
1032 $this->getHelpTextHtmlDiv( $this->getHelpText() );
1043 if ( $helptext ===
null ) {
1047 $rowAttributes = [];
1048 if ( $this->mCondState ) {
1049 $rowAttributes[
'data-cond-state'] = FormatJson::encode( $this->parseCondStateForClient() );
1050 $rowAttributes[
'class'] = $this->mCondStateClass;
1053 $tdClasses = [
'htmlform-tip' ];
1054 if ( $this->mHelpClass !==
false ) {
1055 $tdClasses[] = $this->mHelpClass;
1057 return Html::rawElement(
'tr', $rowAttributes,
1058 Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext )
1072 if ( $helptext ===
null ) {
1076 $wrapperAttributes = [
1077 'class' => array_merge( $cssClasses, [
'htmlform-tip' ] ),
1079 if ( $this->mHelpClass !==
false ) {
1080 $wrapperAttributes[
'class'][] = $this->mHelpClass;
1082 if ( $this->mCondState ) {
1083 $wrapperAttributes[
'data-cond-state'] = FormatJson::encode( $this->parseCondStateForClient() );
1084 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
1086 return Html::rawElement(
'div', $wrapperAttributes, $helptext );
1090 if ( $descriptionHtml === null ) {
1094 return Html::rawElement(
'span', [
'class' => $cssClasses ], $descriptionHtml );
1105 return $this->getHelpTextHtmlDiv( $helptext );
1108 private function getHelpMessages(): array {
1109 if ( isset( $this->mParams[
'help-message'] ) ) {
1110 return [ $this->mParams[
'help-message'] ];
1111 } elseif ( isset( $this->mParams[
'help-messages'] ) ) {
1112 return $this->mParams[
'help-messages'];
1113 } elseif ( isset( $this->mParams[
'help-raw'] ) ) {
1114 return [
new HtmlArmor( $this->mParams[
'help-raw'] ) ];
1115 } elseif ( isset( $this->mParams[
'help'] ) ) {
1117 return [
new HtmlArmor( $this->mParams[
'help'] ) ];
1132 foreach ( $this->getHelpMessages() as $msg ) {
1134 $html[] = HtmlArmor::getHtml( $msg );
1136 $msg = $this->getMessage( $msg );
1137 if ( $msg->exists() ) {
1138 $html[] = $msg->parse();
1143 return $html ? implode( $this->msg(
'word-separator' )->escaped(), $html ) :
null;
1146 private function getDescriptionMessages(): array {
1147 if ( isset( $this->mParams[
'description-message'] ) ) {
1148 return [ $this->mParams[
'description-message'] ];
1151 if ( isset( $this->mParams[
'description-messages'] ) ) {
1152 return $this->mParams[
'description-messages'];
1155 if ( isset( $this->mParams[
'description-raw'] ) ) {
1156 return [
new HtmlArmor( $this->mParams[
'description-raw'] ) ];
1171 foreach ( $this->getDescriptionMessages() as $msg ) {
1173 $html[] = HtmlArmor::getHtml( $msg );
1175 $msg = $this->getMessage( $msg );
1176 if ( $msg->exists() ) {
1177 $html[] = $msg->parse();
1182 return $html ? implode( $this->msg(
'word-separator' )->escaped(), $html ) : null;
1194 return $this->mParams[
'help-inline'] ??
true;
1210 $errors = $this->validate( $value, $this->mParent->mFieldData );
1212 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1216 return [ self::formatErrors( $errors ),
'mw-htmlform-invalid-input' ];
1227 $errors = $this->validate( $value, $this->mParent->mFieldData );
1229 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1233 if ( !is_array( $errors ) ) {
1234 $errors = [ $errors ];
1236 foreach ( $errors as &$error ) {
1237 if ( $error instanceof
Message ) {
1238 $error = $error->parse();
1250 return $this->mLabel ??
'';
1260 # Don't output a for= attribute for labels with no associated input.
1261 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1262 $for = $this->needsLabel() ? [
'for' => $this->mID ] : [];
1264 $labelValue = trim( $this->getLabel() );
1265 $hasLabel = $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ';
1267 $displayFormat = $this->mParent->getDisplayFormat();
1268 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1270 if ( $displayFormat ===
'table' ) {
1271 return Html::rawElement(
'td',
1272 [
'class' =>
'mw-label' ] + $cellAttributes,
1273 Html::rawElement(
'label', $for, $labelValue ) );
1274 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1275 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1276 return Html::rawElement(
'div',
1277 [
'class' =>
'mw-label' ] + $cellAttributes,
1278 Html::rawElement(
'label', $for, $labelValue ) );
1280 return Html::rawElement(
'label', $for, $labelValue );
1292 return $this->mDefault ??
null;
1301 if ( empty( $this->mParams[
'tooltip'] ) ) {
1305 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1314 if ( empty( $this->mParams[
'tooltip'] ) ) {
1319 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1320 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1332 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1335 foreach ( $list as $key ) {
1336 if ( in_array( $key, $boolAttribs ) ) {
1337 if ( !empty( $this->mParams[$key] ) ) {
1340 } elseif ( isset( $this->mParams[$key] ) ) {
1341 $ret[$key] = $this->mParams[$key];
1357 private function lookupOptionsKeys( $options, $needsParse ) {
1359 foreach ( $options as $key => $value ) {
1360 $msg = $this->msg( $key );
1361 $msgAsText = $needsParse ? $msg->parse() : $msg->plain();
1362 if ( array_key_exists( $msgAsText, $ret ) ) {
1363 LoggerFactory::getInstance(
'translation-problem' )->error(
1364 'The option that uses the message key {msg_key_one} has the same translation as ' .
1365 'another option in {lang}. This means that {msg_key_one} will not be used as an option.',
1367 'msg_key_one' => $key,
1368 'lang' => $this->mParent ?
1369 $this->mParent->getLanguageCode()->toBcp47Code() :
1375 $ret[$msgAsText] = is_array( $value )
1376 ? $this->lookupOptionsKeys( $value, $needsParse )
1390 if ( is_array( $array ) ) {
1391 return array_map( self::forceToStringRecursive( ... ), $array );
1393 return strval( $array );
1404 if ( $this->mOptions ===
false ) {
1405 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1406 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1407 if ( $needsParse ) {
1408 $this->mOptionsLabelsNotFromMessage =
true;
1410 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1411 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1412 $this->mOptionsLabelsNotFromMessage =
true;
1413 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1414 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1415 $message = $this->getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1416 $this->mOptions = Html::listDropdownOptions( $message );
1418 $this->mOptions =
null;
1422 return $this->mOptions;
1431 $oldoptions = $this->getOptions();
1433 if ( $oldoptions ===
null ) {
1437 return Html::listDropdownOptionsOoui( $oldoptions );
1450 foreach ( $options as $value ) {
1451 if ( is_array( $value ) ) {
1452 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1454 $flatOpts[] = $value;
1475 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1476 $errors = array_shift( $errors );
1479 if ( is_array( $errors ) ) {
1480 foreach ( $errors as &$error ) {
1481 $error = Html::rawElement(
'li', [],
1485 $errors = Html::rawElement(
'ul', [], implode(
"\n", $errors ) );
1486 } elseif ( $errors instanceof
Message ) {
1487 $errors = $errors->parse();
1490 return Html::errorBox( $errors );
1500 $message = Message::newFromSpecifier( $value );
1502 if ( $this->mParent ) {
1503 $message->setContext( $this->mParent );
1517 return !empty( $this->mParams[
'nodata'] );
1529 return (
bool)$this->mCondState;
1544 return $this->mOptionsLabelsNotFromMessage
1545 ? $label : htmlspecialchars( $label, ENT_NOQUOTES );
1560 return $this->mOptionsLabelsNotFromMessage
1561 ? new \OOUI\HtmlSnippet( $label ) : $label;
1566class_alias( HTMLFormField::class,
'HTMLFormField' );
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.
if(!defined('MW_SETUP_CALLBACK'))
Group all the pieces relevant to the context of a request into one instance.
Generic operation result class Has warning/error list, boolean status and arbitrary value.