14 protected $mLabel; # String label,
as HTML. Set
on construction.
77 public function msg() {
78 $args = func_get_args();
80 if ( $this->mParent ) {
83 $callback =
'wfMessage';
86 return call_user_func_array( $callback,
$args );
115 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
116 array_unshift( $thisKeys, $m[2] );
119 if ( substr( $tmp, 0, 2 ) ==
'wp' &&
120 !array_key_exists( $tmp, $alldata ) &&
121 array_key_exists( substr( $tmp, 2 ), $alldata )
124 $tmp = substr( $tmp, 2 );
126 array_unshift( $thisKeys, $tmp );
130 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
131 array_unshift( $nameKeys, $m[2] );
134 array_unshift( $nameKeys, $tmp );
137 for ( $i =
count( $thisKeys ) - 1; $i >= 0; $i-- ) {
138 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
141 $key = array_shift(
$keys );
142 if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
147 $testValue = (
string)$data;
170 if ( !is_array( $p ) ) {
172 "Expected array, found " . gettype( $p ) .
" at index $i"
183 if ( !is_array( $p ) ) {
185 "Expected array, found " . gettype( $p ) .
" at index $i"
196 if ( !is_array( $p ) ) {
198 "Expected array, found " . gettype( $p ) .
" at index $i"
209 if ( !is_array( $p ) ) {
211 "Expected array, found " . gettype( $p ) .
" at index $i"
222 throw new MWException(
"NOT takes exactly one parameter" );
225 if ( !is_array( $p ) ) {
227 "Expected array, found " . gettype( $p ) .
" at index 0"
235 throw new MWException(
"$op takes exactly two parameters" );
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 call_user_func( $this->mValidationCallback,
$value, $alldata, $this->mParent );
322 if ( isset( $this->mFilterCallback ) ) {
323 $value = call_user_func( $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 );
393 $this->mParent =
$params[
'parent'];
396 # Generate the label from a message, if possible
397 if ( isset(
$params[
'label-message'] ) ) {
399 } elseif ( isset(
$params[
'label'] ) ) {
400 if (
$params[
'label'] ===
' ' ) {
402 $this->mLabel =
' ';
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'] ) ) {
419 $validName = Sanitizer::escapeId( $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 = Sanitizer::escapeId( $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'];
480 $cellAttributes = [];
484 if ( !empty( $this->mParams[
'vertical-label'] ) ) {
485 $cellAttributes[
'colspan'] = 2;
486 $verticalLabel =
true;
488 $verticalLabel =
false;
495 [
'class' =>
'mw-input' ] + $cellAttributes,
496 $inputHtml .
"\n$errors"
499 if ( $this->mHideIf ) {
501 $rowClasses .=
' mw-htmlform-hide-if';
504 if ( $verticalLabel ) {
506 $rowAttributes + [
'class' =>
"mw-htmlform-vertical-label $rowClasses" ], $label );
509 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
516 'class' =>
"mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
521 return $html . $helptext;
538 $cellAttributes = [];
543 'mw-htmlform-nolabel' => ( $label ===
'' )
546 $horizontalLabel = isset( $this->mParams[
'horizontal-label'] )
547 ? $this->mParams[
'horizontal-label'] :
false;
549 if ( $horizontalLabel ) {
550 $field =
' ' . $inputHtml .
"\n$errors";
554 [
'class' => $outerDivClass ] + $cellAttributes,
555 $inputHtml .
"\n$errors"
558 $divCssClasses = [
"mw-htmlform-field-$fieldType",
561 $wrapperAttributes = [
562 'class' => $divCssClasses,
564 if ( $this->mHideIf ) {
566 $wrapperAttributes[
'class'][] =
' mw-htmlform-hide-if';
585 if ( !$inputField ) {
590 new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $this->
getDiv(
$value ) ) ] ),
591 [
'infusable' =>
false,
'align' =>
'top' ]
596 if ( is_string( $inputField ) ) {
600 $inputField =
new OOUI\Widget( [
'content' =>
new OOUI\HtmlSnippet( $inputField ) ] );
607 foreach ( $errors
as &$error ) {
608 $error =
new OOUI\HtmlSnippet( $error );
612 foreach ( $notices
as &$notice ) {
613 $notice =
new OOUI\HtmlSnippet( $notice );
617 'classes' => [
"mw-htmlform-field-$fieldType",
$this->mClass ],
619 'help' => (
$help !==
null &&
$help !==
'' ) ?
new OOUI\HtmlSnippet(
$help ) :
null,
621 'notices' => $notices,
622 'infusable' => $infusable,
625 $preloadModules =
false;
628 $preloadModules =
true;
629 $config[
'classes'][] =
'mw-htmlform-field-autoinfuse';
635 $config[
'label'] =
new OOUI\HtmlSnippet( $label );
638 if ( $this->mHideIf ) {
639 $preloadModules =
true;
645 if ( $preloadModules ) {
646 $this->mParent->getOutput()->addModules(
'mediawiki.htmlform.ooui' );
647 $this->mParent->getOutput()->addModules( $this->
getOOUIModules() );
666 if ( isset( $this->mClassWithButton ) ) {
667 $buttonWidget = $this->mClassWithButton->getInputOOUI(
'' );
708 $cellAttributes = [];
729 $this->mVFormClass =
' mw-ui-vform-field';
743 $cellAttributes = [];
746 $html =
"\n" . $errors .
762 if ( is_null( $helptext ) ) {
767 if ( $this->mHideIf ) {
769 $rowAttributes[
'class'] =
'mw-htmlform-hide-if';
772 $tdClasses = [
'htmlform-tip' ];
773 if ( $this->mHelpClass !==
false ) {
776 $row =
Html::rawElement(
'td', [
'colspan' => 2,
'class' => $tdClasses ], $helptext );
791 if ( is_null( $helptext ) ) {
795 $wrapperAttributes = [
796 'class' =>
'htmlform-tip',
798 if ( $this->mHelpClass !==
false ) {
799 $wrapperAttributes[
'class'] .=
" {$this->mHelpClass}";
801 if ( $this->mHideIf ) {
803 $wrapperAttributes[
'class'] .=
' mw-htmlform-hide-if';
829 if ( isset( $this->mParams[
'help-message'] ) ) {
830 $this->mParams[
'help-messages'] = [ $this->mParams[
'help-message'] ];
833 if ( isset( $this->mParams[
'help-messages'] ) ) {
834 foreach ( $this->mParams[
'help-messages']
as $msg ) {
837 if ( $msg->exists() ) {
838 if ( is_null( $helptext ) ) {
841 $helptext .= $this->
msg(
'word-separator' )->escaped();
843 $helptext .= $msg->parse();
846 } elseif ( isset( $this->mParams[
'help'] ) ) {
847 $helptext = $this->mParams[
'help'];
863 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
868 $errorClass =
'mw-htmlform-invalid-input';
871 return [ $errors, $errorClass ];
884 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
888 if ( !is_array( $errors ) ) {
889 $errors = [ $errors ];
891 foreach ( $errors
as &$error ) {
892 if ( $error instanceof Message ) {
893 $error = $error->parse();
909 if ( isset( $this->mParams[
'notice-message'] ) ) {
910 $notices[] = $this->
getMessage( $this->mParams[
'notice-message'] )->parse();
913 if ( isset( $this->mParams[
'notice-messages'] ) ) {
914 foreach ( $this->mParams[
'notice-messages']
as $msg ) {
915 $notices[] = $this->
getMessage( $msg )->parse();
917 } elseif ( isset( $this->mParams[
'notice'] ) ) {
918 $notices[] = $this->mParams[
'notice'];
932 # Don't output a for= attribute for labels with no associated input.
933 # Kind of hacky here, possibly we don't want these to be <label>s at all.
940 $labelValue = trim( $this->
getLabel() );
942 if ( $labelValue !==
' ' && $labelValue !==
'' ) {
946 $displayFormat = $this->mParent->getDisplayFormat();
948 $horizontalLabel = isset( $this->mParams[
'horizontal-label'] )
949 ? $this->mParams[
'horizontal-label'] :
false;
951 if ( $displayFormat ===
'table' ) {
954 [
'class' =>
'mw-label' ] + $cellAttributes,
956 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
957 if ( $displayFormat ===
'div' && !$horizontalLabel ) {
960 [
'class' =>
'mw-label' ] + $cellAttributes,
971 if ( isset( $this->mDefault ) ) {
984 if ( empty( $this->mParams[
'tooltip'] ) ) {
998 static $boolAttribs = [
'disabled',
'required',
'autofocus',
'multiple',
'readonly' ];
1001 foreach ( $list
as $key ) {
1002 if ( in_array( $key, $boolAttribs ) ) {
1003 if ( !empty( $this->mParams[$key] ) ) {
1006 } elseif ( isset( $this->mParams[$key] ) ) {
1007 $ret[$key] = $this->mParams[$key];
1024 $key = $this->
msg( $key )->plain();
1040 if ( is_array( $array ) ) {
1041 return array_map( [ __CLASS__,
'forceToStringRecursive' ], $array );
1043 return strval( $array );
1054 if ( $this->mOptions ===
false ) {
1055 if ( array_key_exists(
'options-messages', $this->mParams ) ) {
1056 $this->mOptions = $this->
lookupOptionsKeys( $this->mParams[
'options-messages'] );
1057 } elseif ( array_key_exists(
'options', $this->mParams ) ) {
1058 $this->mOptionsLabelsNotFromMessage =
true;
1060 } elseif ( array_key_exists(
'options-message', $this->mParams ) ) {
1062 $message = $this->
getMessage( $this->mParams[
'options-message'] )->inContentLanguage()->plain();
1065 $this->mOptions = [];
1066 foreach ( explode(
"\n", $message )
as $option ) {
1067 $value = trim( $option );
1070 } elseif ( substr(
$value, 0, 1 ) ==
'*' && substr(
$value, 1, 1 ) !=
'*' ) {
1071 # A new group is starting...
1074 } elseif ( substr(
$value, 0, 2 ) ==
'**' ) {
1077 if ( $optgroup ===
false ) {
1080 $this->mOptions[$optgroup][
$opt] =
$opt;
1083 # groupless reason list
1085 $this->mOptions[$option] = $option;
1089 $this->mOptions =
null;
1103 if ( $oldoptions ===
null ) {
1109 foreach ( $oldoptions
as $text => $data ) {
1112 'label' => (
string)$text,
1130 if ( is_array(
$value ) ) {
1131 $flatOpts = array_merge( $flatOpts, self::flattenOptions(
$value ) );
1151 if ( is_array( $errors ) &&
count( $errors ) === 1 ) {
1152 $errors = array_shift( $errors );
1155 if ( is_array( $errors ) ) {
1157 foreach ( $errors
as $error ) {
1158 if ( $error instanceof Message ) {
1167 if ( $errors instanceof Message ) {
1168 $errors = $errors->parse();
1182 $message = Message::newFromSpecifier(
$value );
1184 if ( $this->mParent ) {
1185 $message->setContext( $this->mParent );
1198 return !empty( $this->mParams[
'nodata'] );
1209 if ( $this->mHideIf ) {