5use InvalidArgumentException;
32 protected $mValidationCallback;
34 protected $mFilterCallback;
142 public function msg( $key, ...$params ) {
143 if ( $this->mParent ) {
144 return $this->mParent->msg( $key, ...$params );
183 $cloner = $this->mParams[
'cloner'] ??
null;
185 $field = $cloner->findNearestField( $this, $name );
191 if ( $backCompat && str_starts_with( $name,
'wp' ) &&
192 !$this->mParent->hasField( $name )
195 return $this->mParent->getField( substr( $name, 2 ) );
197 return $this->mParent->getField( $name );
214 $cloner = $field->mParams[
'cloner'] ??
null;
216 $value = $cloner->extractFieldData( $field, $alldata );
221 $value = $alldata[$field->mParams[
'fieldname']] ?? $field->getDefault();
225 if ( $asDisplay && $field instanceof
HTMLCheckField && ( $field->mParams[
'invert'] ??
false ) ) {
253 $origParams = $params;
254 $op = array_shift( $params );
256 $makeException =
function (
string $details ) use ( $origParams ): InvalidArgumentException {
257 return new InvalidArgumentException(
258 "Invalid hide-if or disable-if specification for $this->mName: " .
259 $details .
" in " . var_export( $origParams,
true )
265 if ( count( $params ) !== 1 ) {
266 throw $makeException(
"NOT takes exactly one parameter" );
274 foreach ( $params as $i => $p ) {
275 if ( !is_array( $p ) ) {
276 $type = get_debug_type( $p );
277 throw $makeException(
"Expected array, found $type at index $i" );
286 if ( count( $params ) !== 2 ) {
287 throw $makeException(
"$op takes exactly two parameters" );
289 [ $name, $value ] = $params;
290 if ( !is_string( $name ) || !is_string( $value ) ) {
291 throw $makeException(
"Parameters for $op must be strings" );
296 throw $makeException(
"Unknown operation" );
308 $op = array_shift( $params );
309 $valueChk = [
'AND' =>
false,
'OR' =>
true,
'NAND' =>
false,
'NOR' => true ];
310 $valueRet = [
'AND' =>
true,
'OR' =>
false,
'NAND' =>
false,
'NOR' => true ];
317 foreach ( $params as $p ) {
319 return !$valueRet[$op];
322 return $valueRet[$op];
330 [ $field, $value ] = $params;
334 return ( $value === (
string)$testValue );
336 return ( $value !== (
string)$testValue );
338 return in_array( $value, $testValue,
true );
352 $op = array_shift( $params );
360 foreach ( $params as $p ) {
371 [ $name, $value ] = $params;
373 return [ $op, $field->getName(), $value ];
384 foreach ( $this->mCondState as $type => $params ) {
399 return isset( $this->mCondState[
'hide'] ) &&
412 return ( $this->mParams[
'disabled'] ??
false ) ||
414 ( isset( $this->mCondState[
'disable'] )
446 if ( $this->
isHidden( $alldata ) ) {
450 if ( isset( $this->mParams[
'required'] )
451 && $this->mParams[
'required'] !==
false
452 && ( $value ===
'' || $value ===
false || $value ===
null )
454 return $this->
msg(
'htmlform-required' );
457 if ( $this->mValidationCallback ===
null ) {
461 $p = ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
464 $language = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
466 return $p->isGood() ? true : Status::wrap( $p )->getHTML(
false,
false, $language );
480 public function filter( $value, $alldata ) {
481 if ( $this->mFilterCallback !==
null ) {
482 $value = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
509 $this->mShowEmptyLabels = $show;
527 || $request->
getCheck(
'wpFormIdentifier' );
539 if ( $request->getCheck( $this->mName ) ) {
540 return $request->getText( $this->mName );
555 $this->mParams = $params;
557 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
558 $this->mParent = $params[
'parent'];
564 __METHOD__ .
": Constructing an HTMLFormField without a 'parent' parameter",
569 # Generate the label from a message, if possible
570 if ( isset( $params[
'label-message'] ) ) {
571 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
572 } elseif ( isset( $params[
'label'] ) ) {
573 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
575 $this->mLabel =
"\u{00A0}";
577 $this->mLabel = htmlspecialchars( $params[
'label'] );
579 } elseif ( isset( $params[
'label-raw'] ) ) {
580 $this->mLabel = $params[
'label-raw'];
583 $this->mName = $params[
'name'] ??
'wp' . $params[
'fieldname'];
585 if ( isset( $params[
'dir'] ) ) {
586 $this->mDir = $params[
'dir'];
589 $this->mID =
"mw-input-{$this->mName}";
591 if ( isset( $params[
'default'] ) ) {
592 $this->mDefault = $params[
'default'];
595 if ( isset( $params[
'id'] ) ) {
596 $this->mID = $params[
'id'];
599 if ( isset( $params[
'cssclass'] ) ) {
600 $this->mClass = $params[
'cssclass'];
603 if ( isset( $params[
'csshelpclass'] ) ) {
604 $this->mHelpClass = $params[
'csshelpclass'];
607 if ( isset( $params[
'validation-callback'] ) ) {
608 $this->mValidationCallback = $params[
'validation-callback'];
611 if ( isset( $params[
'filter-callback'] ) ) {
612 $this->mFilterCallback = $params[
'filter-callback'];
615 if ( isset( $params[
'hidelabel'] ) ) {
616 $this->mShowEmptyLabels =
false;
618 if ( isset( $params[
'notices'] ) ) {
619 $this->mNotices = $params[
'notices'];
622 if ( isset( $params[
'hide-if'] ) && $params[
'hide-if'] ) {
624 $this->mCondState[
'hide'] = $params[
'hide-if'];
625 $this->mCondStateClass[] =
'mw-htmlform-hide-if';
627 if ( !( isset( $params[
'disabled'] ) && $params[
'disabled'] ) &&
628 isset( $params[
'disable-if'] ) && $params[
'disable-if']
631 $this->mCondState[
'disable'] = $params[
'disable-if'];
632 $this->mCondStateClass[] =
'mw-htmlform-disable-if';
650 $cellAttributes = [];
654 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
655 $cellAttributes[
'colspan'] = 2;
656 $verticalLabel =
true;
658 $verticalLabel =
false;
663 $field = Html::rawElement(
665 [
'class' =>
'mw-input' ] + $cellAttributes,
666 $inputHtml .
"\n$errors"
669 if ( $this->mCondState ) {
671 $rowClasses .= implode(
' ', $this->mCondStateClass );
672 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
673 $rowClasses .=
' mw-htmlform-hide-if-hidden';
677 if ( $verticalLabel ) {
678 $html = Html::rawElement(
'tr',
679 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
680 $html .= Html::rawElement(
'tr',
682 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
686 $html = Html::rawElement(
'tr',
688 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
693 return $html . $helptext;
711 $cellAttributes = [];
716 'mw-htmlform-nolabel' => ( $label ===
'' )
719 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
721 if ( $horizontalLabel ) {
722 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
724 $field = Html::rawElement(
727 [
'class' => $outerDivClass ] + $cellAttributes,
728 $inputHtml .
"\n$errors"
732 $wrapperAttributes = [
'class' => [
733 "mw-htmlform-field-$fieldType",
738 if ( $this->mCondState ) {
740 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
741 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
742 $wrapperAttributes[
'class'][] =
'mw-htmlform-hide-if-hidden';
745 return Html::rawElement(
'div', $wrapperAttributes, $label . $field ) .
761 if ( !$inputField ) {
766 new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
772 if ( is_string( $inputField ) ) {
776 $inputField = new \OOUI\Widget( [
'content' =>
new \OOUI\HtmlSnippet( $inputField ) ] );
783 foreach ( $errors as &$error ) {
784 $error = new \OOUI\HtmlSnippet( $error );
788 'classes' => [
"mw-htmlform-field-$fieldType" ],
790 'help' => ( $help !==
null && $help !==
'' ) ?
new \OOUI\HtmlSnippet( $help ) :
null,
792 'infusable' => $infusable,
794 'notices' => $this->mNotices ?: [],
796 if ( $this->mClass !==
'' ) {
800 $preloadModules =
false;
803 $preloadModules =
true;
804 $config[
'classes'][] =
'mw-htmlform-autoinfuse';
806 if ( $this->mCondState ) {
807 $config[
'classes'] = array_merge( $config[
'classes'], $this->mCondStateClass );
808 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
809 $config[
'classes'][] =
'mw-htmlform-hide-if-hidden';
815 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
816 $config[
'label'] = new \OOUI\HtmlSnippet( $label );
819 if ( $this->mCondState ) {
820 $preloadModules =
true;
826 if ( $preloadModules ) {
827 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
828 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
842 $isDisabled = ( $this->mParams[
'disabled'] ?? false );
846 $labelValue = trim( $this->
getLabel() );
849 if ( $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ' ) {
851 $labelClasses = [
'cdx-label' ];
853 $labelClasses[] =
'cdx-label--disabled';
856 $labelDiv = Html::rawElement(
'div', [
'class' => $labelClasses ],
858 Html::rawElement(
'label', [
'class' =>
'cdx-label__label' ] + $labelFor,
860 Html::rawElement(
'span', [
'class' =>
'cdx-label__label__text' ],
875 $validationMessage =
'';
877 if ( $errors !==
'' ) {
878 $validationMessage = Html::rawElement(
'div', [
'class' =>
'cdx-field__validation-message' ],
886 $controlClasses = [
'cdx-field__control' ];
888 $controlClasses[] =
'cdx-field__control--has-help-text';
890 $control = Html::rawElement(
'div', [
'class' => $controlClasses ], $inputHtml );
894 "mw-htmlform-field-{$this->getClassName()}",
900 $fieldClasses[] =
'cdx-field--disabled';
902 $fieldAttributes = [];
904 if ( $this->mCondState ) {
906 $fieldClasses = array_merge( $fieldClasses, $this->mCondStateClass );
907 if ( $this->
isHidden( $this->mParent->mFieldData ) ) {
908 $fieldClasses[] =
'mw-htmlform-hide-if-hidden';
912 return Html::rawElement(
'div', [
'class' => $fieldClasses ] + $fieldAttributes,
913 $labelDiv . $control . $helptext . $validationMessage
925 $name = explode(
'\\', static::class );
958 return !$this->
isHelpInline() && $this->getHelpMessages();
984 return "\n" . $errors .
1003 $this->mVFormClass =
' mw-ui-vform-field';
1004 return $this->
getDiv( $value );
1016 return "\n" . $errors .
1031 if ( $helptext ===
null ) {
1035 $rowAttributes = [];
1036 if ( $this->mCondState ) {
1041 $tdClasses = [
'htmlform-tip' ];
1042 if ( $this->mHelpClass !==
false ) {
1045 return Html::rawElement(
'tr', $rowAttributes,
1046 Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext )
1060 if ( $helptext ===
null ) {
1064 $wrapperAttributes = [
1065 'class' => array_merge( $cssClasses, [
'htmlform-tip' ] ),
1067 if ( $this->mHelpClass !==
false ) {
1070 if ( $this->mCondState ) {
1072 $wrapperAttributes[
'class'] = array_merge( $wrapperAttributes[
'class'], $this->mCondStateClass );
1074 return Html::rawElement(
'div', $wrapperAttributes, $helptext );
1088 private function getHelpMessages(): array {
1089 if ( isset( $this->mParams[
'help-message'] ) ) {
1090 return [ $this->mParams[
'help-message'] ];
1091 } elseif ( isset( $this->mParams[
'help-messages'] ) ) {
1092 return $this->mParams[
'help-messages'];
1093 } elseif ( isset( $this->mParams[
'help-raw'] ) ) {
1094 return [
new HtmlArmor( $this->mParams[
'help-raw'] ) ];
1095 } elseif ( isset( $this->mParams[
'help'] ) ) {
1097 return [
new HtmlArmor( $this->mParams[
'help'] ) ];
1112 foreach ( $this->getHelpMessages() as $msg ) {
1114 $html[] = HtmlArmor::getHtml( $msg );
1116 $msg = $this->getMessage( $msg );
1117 if ( $msg->exists() ) {
1118 $html[] = $msg->parse();
1123 return $html ? implode( $this->msg(
'word-separator' )->escaped(), $html ) :
null;
1135 return $this->mParams[
'help-inline'] ??
true;
1151 $errors = $this->validate( $value, $this->mParent->mFieldData );
1153 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1157 return [ self::formatErrors( $errors ),
'mw-htmlform-invalid-input' ];
1168 $errors = $this->validate( $value, $this->mParent->mFieldData );
1170 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
1174 if ( !is_array( $errors ) ) {
1175 $errors = [ $errors ];
1177 foreach ( $errors as &$error ) {
1178 if ( $error instanceof
Message ) {
1179 $error = $error->parse();
1191 return $this->mLabel ??
'';
1201 # Don't output a for= attribute for labels with no associated input.
1202 # Kind of hacky here, possibly we don't want these to be <label>s at all.
1203 $for = $this->needsLabel() ? [
'for' => $this->mID ] : [];
1205 $labelValue = trim( $this->getLabel() );
1206 $hasLabel = $labelValue !==
'' && $labelValue !==
"\u{00A0}" && $labelValue !==
' ';
1208 $displayFormat = $this->mParent->getDisplayFormat();
1209 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
1211 if ( $displayFormat ===
'table' ) {
1212 return Html::rawElement(
'td',
1213 [
'class' =>
'mw-label' ] + $cellAttributes,
1214 Html::rawElement(
'label', $for, $labelValue ) );
1215 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
1216 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
1217 return Html::rawElement(
'div',
1218 [
'class' =>
'mw-label' ] + $cellAttributes,
1219 Html::rawElement(
'label', $for, $labelValue ) );
1221 return Html::rawElement(
'label', $for, $labelValue );
1233 return $this->mDefault ??
null;
1242 if ( empty( $this->mParams[
'tooltip'] ) ) {
1246 return Linker::tooltipAndAccesskeyAttribs( $this->mParams[
'tooltip'] );
1255 if ( empty( $this->mParams[
'tooltip'] ) ) {
1260 'title' => Linker::titleAttrib( $this->mParams[
'tooltip'] ),
1261 'accessKey' => Linker::accesskey( $this->mParams[
'tooltip'] ),
1273 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1276 foreach ( $list as $key ) {
1277 if ( in_array( $key, $boolAttribs ) ) {
1278 if ( !empty( $this->mParams[$key] ) ) {
1281 } elseif ( isset( $this->mParams[$key] ) ) {
1282 $ret[$key] = $this->mParams[$key];
1298 private function lookupOptionsKeys( $options, $needsParse ) {
1300 foreach ( $options as $key => $value ) {
1301 $msg = $this->msg( $key );
1302 $msgAsText = $needsParse ? $msg->parse() : $msg->plain();
1303 if ( array_key_exists( $msgAsText, $ret ) ) {
1304 LoggerFactory::getInstance(
'translation-problem' )->error(
1305 'The option that uses the message key {msg_key_one} has the same translation as ' .
1306 'another option in {lang}. This means that {msg_key_one} will not be used as an option.',
1308 'msg_key_one' => $key,
1309 'lang' => $this->mParent ?
1310 $this->mParent->getLanguageCode()->toBcp47Code() :
1316 $ret[$msgAsText] = is_array( $value )
1317 ? $this->lookupOptionsKeys( $value, $needsParse )
1331 if ( is_array( $array ) ) {
1332 return array_map( self::forceToStringRecursive( ... ), $array );
1334 return strval( $array );
1345 if ( $this->mOptions ===
false ) {
1346 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1347 $needsParse = $this->mParams[
'options-messages-parse'] ??
false;
1348 if ( $needsParse ) {
1349 $this->mOptionsLabelsNotFromMessage =
true;
1351 $this->mOptions = $this->lookupOptionsKeys( $this->mParams[
'options-messages'], $needsParse );
1352 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1353 $this->mOptionsLabelsNotFromMessage =
true;
1354 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1355 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1356 $message = $this->getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1357 $this->mOptions = Html::listDropdownOptions( $message );
1359 $this->mOptions =
null;
1363 return $this->mOptions;
1372 $oldoptions = $this->getOptions();
1374 if ( $oldoptions ===
null ) {
1378 return Html::listDropdownOptionsOoui( $oldoptions );
1391 foreach ( $options as $value ) {
1392 if ( is_array( $value ) ) {
1393 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1395 $flatOpts[] = $value;
1416 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1417 $errors = array_shift( $errors );
1420 if ( is_array( $errors ) ) {
1421 foreach ( $errors as &$error ) {
1422 $error = Html::rawElement(
'li', [],
1426 $errors = Html::rawElement(
'ul', [], implode(
"\n", $errors ) );
1427 } elseif ( $errors instanceof
Message ) {
1428 $errors = $errors->parse();
1431 return Html::errorBox( $errors );
1443 if ( $this->mParent ) {
1444 $message->setContext( $this->mParent );
1458 return !empty( $this->mParams[
'nodata'] );
1470 return (
bool)$this->mCondState;
1485 return $this->mOptionsLabelsNotFromMessage
1486 ? $label : htmlspecialchars( $label, ENT_NOQUOTES );
1501 return $this->mOptionsLabelsNotFromMessage
1502 ? new \OOUI\HtmlSnippet( $label ) : $label;
1507class_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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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.