15 protected $mLabel; # String label, as HTML. Set on construction.
83 public function msg( $key, ...$params ) {
84 if ( $this->mParent ) {
85 return $this->mParent->msg( $key, ...$params );
116 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
117 array_unshift( $thisKeys, $m[2] );
120 if ( substr( $tmp, 0, 2 ) ==
'wp' &&
121 !array_key_exists( $tmp, $alldata ) &&
122 array_key_exists( substr( $tmp, 2 ), $alldata )
125 $tmp = substr( $tmp, 2 );
127 array_unshift( $thisKeys, $tmp );
131 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
132 array_unshift( $nameKeys, $m[2] );
135 array_unshift( $nameKeys, $tmp );
138 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
139 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
141 foreach (
$keys as $key ) {
142 if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
147 $testValue = (string)$data;
163 $origParams = $params;
164 $op = array_shift( $params );
169 foreach ( $params as $i => $p ) {
170 if ( !is_array( $p ) ) {
172 "Expected array, found " . gettype( $p ) .
" at index $i"
182 foreach ( $params as $i => $p ) {
183 if ( !is_array( $p ) ) {
185 "Expected array, found " . gettype( $p ) .
" at index $i"
195 foreach ( $params as $i => $p ) {
196 if ( !is_array( $p ) ) {
198 "Expected array, found " . gettype( $p ) .
" at index $i"
208 foreach ( $params as $i => $p ) {
209 if ( !is_array( $p ) ) {
211 "Expected array, found " . gettype( $p ) .
" at index $i"
221 if ( count( $params ) !== 1 ) {
222 throw new MWException(
"NOT takes exactly one parameter" );
225 if ( !is_array( $p ) ) {
227 "Expected array, found " . gettype( $p ) .
" at index 0"
234 if ( count( $params ) !== 2 ) {
235 throw new MWException(
"$op takes exactly two parameters" );
237 list( $field, $value ) = $params;
238 if ( !is_string( $field ) || !is_string( $value ) ) {
239 throw new MWException(
"Parameters for $op must be strings" );
244 return ( $value === $testValue );
246 return ( $value !== $testValue );
252 }
catch ( Exception $ex ) {
254 "Invalid hide-if specification for $this->mName: " .
255 $ex->getMessage() .
" in " . var_export( $origParams,
true ),
270 if ( !$this->mHideIf ) {
303 if ( $this->
isHidden( $alldata ) ) {
307 if ( isset( $this->mParams[
'required'] )
308 && $this->mParams[
'required'] !==
false
311 return $this->
msg(
'htmlform-required' );
314 if ( isset( $this->mValidationCallback ) ) {
315 return ( $this->mValidationCallback )( $value, $alldata, $this->mParent );
321 public function filter( $value, $alldata ) {
322 if ( isset( $this->mFilterCallback ) ) {
323 $value = ( $this->mFilterCallback )( $value, $alldata, $this->mParent );
349 $this->mShowEmptyLabels = $show;
363 return $request->
getCheck(
'wpEditToken' ) || $request->
getCheck(
'wpFormIdentifier' );
374 if ( $request->getCheck( $this->mName ) ) {
375 return $request->getText( $this->mName );
390 $this->mParams = $params;
392 if ( isset( $params[
'parent'] ) && $params[
'parent'] instanceof
HTMLForm ) {
393 $this->mParent = $params[
'parent'];
396 # Generate the label from a message, if possible
397 if ( isset( $params[
'label-message'] ) ) {
398 $this->mLabel = $this->
getMessage( $params[
'label-message'] )->parse();
399 } elseif ( isset( $params[
'label'] ) ) {
400 if ( $params[
'label'] ===
' ' || $params[
'label'] ===
"\u{00A0}" ) {
402 $this->mLabel =
"\u{00A0}";
404 $this->mLabel = htmlspecialchars( $params[
'label'] );
406 } elseif ( isset( $params[
'label-raw'] ) ) {
407 $this->mLabel = $params[
'label-raw'];
410 $this->mName =
"wp{$params['fieldname']}";
411 if ( isset( $params[
'name'] ) ) {
412 $this->mName = $params[
'name'];
415 if ( isset( $params[
'dir'] ) ) {
416 $this->mDir = $params[
'dir'];
419 $validName = urlencode( $this->mName );
420 $validName = str_replace( [
'%5B',
'%5D' ], [
'[',
']' ], $validName );
421 if ( $this->mName != $validName && !isset( $params[
'nodata'] ) ) {
422 throw new MWException(
"Invalid name '{$this->mName}' passed to " . __METHOD__ );
425 $this->mID =
"mw-input-{$this->mName}";
427 if ( isset( $params[
'default'] ) ) {
428 $this->mDefault = $params[
'default'];
431 if ( isset( $params[
'id'] ) ) {
433 $validId = urlencode( $id );
435 if ( $id != $validId ) {
436 throw new MWException(
"Invalid id '$id' passed to " . __METHOD__ );
442 if ( isset( $params[
'cssclass'] ) ) {
443 $this->mClass = $params[
'cssclass'];
446 if ( isset( $params[
'csshelpclass'] ) ) {
447 $this->mHelpClass = $params[
'csshelpclass'];
450 if ( isset( $params[
'validation-callback'] ) ) {
451 $this->mValidationCallback = $params[
'validation-callback'];
454 if ( isset( $params[
'filter-callback'] ) ) {
455 $this->mFilterCallback = $params[
'filter-callback'];
458 if ( isset( $params[
'hidelabel'] ) ) {
459 $this->mShowEmptyLabels =
false;
462 if ( isset( $params[
'hide-if'] ) ) {
463 $this->mHideIf = $params[
'hide-if'];
478 $fieldType = static::class;
480 $cellAttributes = [];
484 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
485 $cellAttributes[
'colspan'] = 2;
486 $verticalLabel =
true;
488 $verticalLabel =
false;
493 $field = Html::rawElement(
495 [
'class' =>
'mw-input' ] + $cellAttributes,
496 $inputHtml .
"\n$errors"
499 if ( $this->mHideIf ) {
500 $rowAttributes[
'data-hide-if'] = FormatJson::encode( $this->mHideIf );
501 $rowClasses .=
' mw-htmlform-hide-if';
504 if ( $verticalLabel ) {
505 $html = Html::rawElement(
'tr',
506 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
507 $html .= Html::rawElement(
'tr',
509 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
514 Html::rawElement(
'tr',
516 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
521 return $html . $helptext;
536 $fieldType = static::class;
538 $cellAttributes = [];
543 'mw-htmlform-nolabel' => ( $label ===
'' )
546 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
548 if ( $horizontalLabel ) {
549 $field =
"\u{00A0}" . $inputHtml .
"\n$errors";
551 $field = Html::rawElement(
553 [
'class' => $outerDivClass ] + $cellAttributes,
554 $inputHtml .
"\n$errors"
557 $divCssClasses = [
"mw-htmlform-field-$fieldType",
558 $this->mClass, $this->mVFormClass, $errorClass ];
560 $wrapperAttributes = [
561 'class' => $divCssClasses,
563 if ( $this->mHideIf ) {
564 $wrapperAttributes[
'data-hide-if'] = FormatJson::encode( $this->mHideIf );
565 $wrapperAttributes[
'class'][] =
' mw-htmlform-hide-if';
567 $html = Html::rawElement(
'div', $wrapperAttributes, $label . $field );
584 if ( !$inputField ) {
589 new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $this->
getDiv( $value ) ) ] ),
595 if ( is_string( $inputField ) ) {
599 $inputField =
new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $inputField ) ] );
603 $fieldType = static::class;
606 foreach ( $errors as &$error ) {
607 $error =
new OOUI\HtmlSnippet( $error );
611 'classes' => [
"mw-htmlform-field-$fieldType", $this->mClass ],
613 'help' => (
$help !==
null &&
$help !==
'' ) ?
new OOUI\HtmlSnippet(
$help ) :
null,
615 'infusable' => $infusable,
619 $preloadModules =
false;
622 $preloadModules =
true;
623 $config[
'classes'][] =
'mw-htmlform-field-autoinfuse';
628 if ( $label && $label !==
"\u{00A0}" && $label !==
' ' ) {
629 $config[
'label'] =
new OOUI\HtmlSnippet( $label );
632 if ( $this->mHideIf ) {
633 $preloadModules =
true;
634 $config[
'hideIf'] = $this->mHideIf;
639 if ( $preloadModules ) {
640 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
641 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
663 if ( isset( $this->mClassWithButton ) ) {
664 $buttonWidget = $this->mClassWithButton->getInputOOUI(
'' );
705 $cellAttributes = [];
726 $this->mVFormClass =
' mw-ui-vform-field';
727 return $this->
getDiv( $value );
740 $cellAttributes = [];
743 $html =
"\n" . $errors .
744 $label .
"\u{00A0}" .
759 if ( is_null( $helptext ) ) {
764 if ( $this->mHideIf ) {
765 $rowAttributes[
'data-hide-if'] = FormatJson::encode( $this->mHideIf );
766 $rowAttributes[
'class'] =
'mw-htmlform-hide-if';
769 $tdClasses = [
'htmlform-tip' ];
770 if ( $this->mHelpClass !==
false ) {
771 $tdClasses[] = $this->mHelpClass;
773 $row = Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext );
774 $row = Html::rawElement(
'tr', $rowAttributes, $row );
788 if ( is_null( $helptext ) ) {
792 $wrapperAttributes = [
793 'class' =>
'htmlform-tip',
795 if ( $this->mHelpClass !==
false ) {
796 $wrapperAttributes[
'class'] .=
" {$this->mHelpClass}";
798 if ( $this->mHideIf ) {
799 $wrapperAttributes[
'data-hide-if'] = FormatJson::encode( $this->mHideIf );
800 $wrapperAttributes[
'class'] .=
' mw-htmlform-hide-if';
802 $div = Html::rawElement(
'div', $wrapperAttributes, $helptext );
826 if ( isset( $this->mParams[
'help-message'] ) ) {
827 $this->mParams[
'help-messages'] = [ $this->mParams[
'help-message'] ];
830 if ( isset( $this->mParams[
'help-messages'] ) ) {
831 foreach ( $this->mParams[
'help-messages'] as $msg ) {
834 if ( $msg->exists() ) {
835 if ( is_null( $helptext ) ) {
838 $helptext .= $this->
msg(
'word-separator' )->escaped();
840 $helptext .= $msg->parse();
843 } elseif ( isset( $this->mParams[
'help'] ) ) {
844 $helptext = $this->mParams[
'help'];
859 return $this->mParams[
'help-inline'] ??
true;
875 $errors = $this->
validate( $value, $this->mParent->mFieldData );
877 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
881 $errors = self::formatErrors( $errors );
882 $errorClass =
'mw-htmlform-invalid-input';
885 return [ $errors, $errorClass ];
896 $errors = $this->
validate( $value, $this->mParent->mFieldData );
898 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
902 if ( !is_array( $errors ) ) {
903 $errors = [ $errors ];
905 foreach ( $errors as &$error ) {
906 if ( $error instanceof
Message ) {
907 $error = $error->parse();
918 return $this->mLabel ??
'';
922 # Don't output a for= attribute for labels with no associated input.
923 # Kind of hacky here, possibly we don't want these to be <label>s at all.
927 $for[
'for'] = $this->mID;
930 $labelValue = trim( $this->
getLabel() );
932 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
936 $displayFormat = $this->mParent->getDisplayFormat();
938 $horizontalLabel = $this->mParams[
'horizontal-label'] ??
false;
940 if ( $displayFormat ===
'table' ) {
942 Html::rawElement(
'td',
943 [
'class' =>
'mw-label' ] + $cellAttributes,
944 Html::rawElement(
'label', $for, $labelValue ) );
945 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
946 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
948 Html::rawElement(
'div',
949 [
'class' =>
'mw-label' ] + $cellAttributes,
950 Html::rawElement(
'label', $for, $labelValue ) );
952 $html = Html::rawElement(
'label', $for, $labelValue );
960 return $this->mDefault ??
null;
969 if ( empty( $this->mParams[
'tooltip'] ) ) {
982 if ( empty( $this->mParams[
'tooltip'] ) ) {
999 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1002 foreach ( $list as $key ) {
1003 if ( in_array( $key, $boolAttribs ) ) {
1004 if ( !empty( $this->mParams[$key] ) ) {
1007 } elseif ( isset( $this->mParams[$key] ) ) {
1008 $ret[$key] = $this->mParams[$key];
1024 foreach ( $options as $key => $value ) {
1025 $key = $this->
msg( $key )->plain();
1026 $ret[$key] = is_array( $value )
1041 if ( is_array( $array ) ) {
1042 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1044 return strval( $array );
1055 if ( $this->mOptions ===
false ) {
1056 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1057 $this->mOptions = $this->
lookupOptionsKeys( $this->mParams[
'options-messages'] );
1058 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1059 $this->mOptionsLabelsNotFromMessage =
true;
1060 $this->mOptions = self::forceToStringRecursive( $this->mParams[
'options'] );
1061 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1062 $message = $this->
getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1063 $this->mOptions = Xml::listDropDownOptions( $message );
1065 $this->mOptions =
null;
1069 return $this->mOptions;
1079 if ( $oldoptions ===
null ) {
1083 return Xml::listDropDownOptionsOoui( $oldoptions );
1096 foreach ( $options as $value ) {
1097 if ( is_array( $value ) ) {
1098 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1100 $flatOpts[] = $value;
1124 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1125 $errors = array_shift( $errors );
1128 if ( is_array( $errors ) ) {
1130 foreach ( $errors as $error ) {
1131 if ( $error instanceof
Message ) {
1132 $lines[] = Html::rawElement(
'li', [], $error->parse() );
1134 $lines[] = Html::rawElement(
'li', [], $error );
1138 return Html::rawElement(
'ul', [
'class' =>
'error' ], implode(
"\n",
$lines ) );
1140 if ( $errors instanceof
Message ) {
1141 $errors = $errors->parse();
1144 return Html::rawElement(
'span', [
'class' =>
'error' ], $errors );
1157 if ( $this->mParent ) {
1158 $message->setContext( $this->mParent );
1171 return !empty( $this->mParams[
'nodata'] );
1182 if ( $this->mHideIf ) {
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static titleAttrib( $name, $options=null, array $msgParams=[])
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
The Message class provides methods which fulfil two basic services:
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").