24use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
144 use ProtectedHookAccessorTrait;
148 'api' => HTMLApiField::class,
149 'text' => HTMLTextField::class,
150 'textwithbutton' => HTMLTextFieldWithButton::class,
151 'textarea' => HTMLTextAreaField::class,
152 'select' => HTMLSelectField::class,
153 'combobox' => HTMLComboboxField::class,
154 'radio' => HTMLRadioField::class,
155 'multiselect' => HTMLMultiSelectField::class,
156 'limitselect' => HTMLSelectLimitField::class,
157 'check' => HTMLCheckField::class,
158 'toggle' => HTMLCheckField::class,
159 'int' => HTMLIntField::class,
160 'file' => HTMLFileField::class,
161 'float' => HTMLFloatField::class,
162 'info' => HTMLInfoField::class,
163 'selectorother' => HTMLSelectOrOtherField::class,
164 'selectandother' => HTMLSelectAndOtherField::class,
165 'namespaceselect' => HTMLSelectNamespace::class,
166 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
167 'tagfilter' => HTMLTagFilter::class,
168 'sizefilter' => HTMLSizeFilterField::class,
169 'submit' => HTMLSubmitField::class,
170 'hidden' => HTMLHiddenField::class,
171 'edittools' => HTMLEditTools::class,
172 'checkmatrix' => HTMLCheckMatrix::class,
173 'cloner' => HTMLFormFieldCloner::class,
174 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
175 'language' => HTMLSelectLanguageField::class,
176 'date' => HTMLDateTimeField::class,
177 'time' => HTMLDateTimeField::class,
178 'datetime' => HTMLDateTimeField::class,
179 'expiry' => HTMLExpiryField::class,
183 'email' => HTMLTextField::class,
184 'password' => HTMLTextField::class,
185 'url' => HTMLTextField::class,
186 'title' => HTMLTitleTextField::class,
187 'user' => HTMLUserTextField::class,
188 'tagmultiselect' => HTMLTagMultiselectField::class,
189 'usersmultiselect' => HTMLUsersMultiselectField::class,
190 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
191 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
326 public static function factory( $displayFormat, ...$arguments ) {
333 $form =
new self( ...$arguments );
355 $this->mTitle =
false;
356 $this->mMessagePrefix = $messagePrefix;
357 } elseif (
$context ===
null && $messagePrefix !==
'' ) {
358 $this->mMessagePrefix = $messagePrefix;
359 } elseif ( is_string(
$context ) && $messagePrefix ===
'' ) {
367 !$this->
getConfig()->
get(
'HTMLFormAllowTableFormat' )
368 && $this->displayFormat ===
'table'
370 $this->displayFormat =
'div';
386 $loadedDescriptor = [];
388 foreach ( $descriptor as $fieldname => $info ) {
390 $section = $info[
'section'] ??
'';
392 if ( isset( $info[
'type'] ) && $info[
'type'] ===
'file' ) {
393 $this->mUseMultipart =
true;
396 $field = static::loadInputFromParameters( $fieldname, $info, $this );
398 $setSection =& $loadedDescriptor;
400 foreach ( explode(
'/', $section ) as $newName ) {
401 if ( !isset( $setSection[$newName] ) ) {
402 $setSection[$newName] = [];
405 $setSection =& $setSection[$newName];
409 $setSection[$fieldname] = $field;
410 $this->mFlatFields[$fieldname] = $field;
413 $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
423 return isset( $this->mFlatFields[$fieldname] );
432 if ( !$this->
hasField( $fieldname ) ) {
433 throw new DomainException( __METHOD__ .
': no field named ' . $fieldname );
435 return $this->mFlatFields[$fieldname];
450 in_array( $format, $this->availableSubclassDisplayFormats,
true ) ||
451 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats,
true )
453 throw new MWException(
'Cannot change display format after creation, ' .
454 'use HTMLForm::factory() instead' );
457 if ( !in_array( $format, $this->availableDisplayFormats,
true ) ) {
458 throw new MWException(
'Display format must be one of ' .
461 $this->availableDisplayFormats,
462 $this->availableSubclassDisplayFormats
469 if ( !$this->
getConfig()->
get(
'HTMLFormAllowTableFormat' ) && $format ===
'table' ) {
473 $this->displayFormat = $format;
484 return $this->displayFormat;
505 if ( isset( $descriptor[
'class'] ) ) {
506 $class = $descriptor[
'class'];
507 } elseif ( isset( $descriptor[
'type'] ) ) {
508 $class = static::$typeMappings[$descriptor[
'type']];
509 $descriptor[
'class'] = $class;
515 throw new MWException(
"Descriptor with no class for $fieldname: "
516 . print_r( $descriptor,
true ) );
537 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
539 $descriptor[
'fieldname'] = $fieldname;
541 $descriptor[
'parent'] = $parent;
544 # @todo This will throw a fatal error whenever someone try to use
545 # 'class' to feed a CSS class instead of 'cssclass'. Would be
546 # great to avoid the fatal error and show a nice error.
547 return new $class( $descriptor );
560 # Check if we have the info we need
561 if ( !$this->mTitle instanceof
PageReference && $this->mTitle !==
false ) {
562 throw new MWException(
'You must call setTitle() on an HTMLForm' );
565 # Load data from the request.
567 $this->mFormIdentifier ===
null ||
568 $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier
572 $this->mFieldData = [];
585 if ( $this->mFormIdentifier ===
null ) {
588 $identOkay = $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier;
594 } elseif ( $this->
getRequest()->wasPosted() ) {
595 $editToken = $this->
getRequest()->getVal(
'wpEditToken' );
596 if ( $this->
getUser()->isRegistered() || $editToken !==
null ) {
600 $tokenOkay = $this->
getUser()->matchEditToken( $editToken, $this->mTokenSalt );
606 if ( $tokenOkay && $identOkay ) {
607 $this->mWasSubmitted =
true;
625 if ( $result ===
true || ( $result instanceof
Status && $result->
isGood() ) ) {
663 $hoistedErrors = Status::newGood();
664 if ( $this->mValidationErrorMessage ) {
665 foreach ( $this->mValidationErrorMessage as $error ) {
666 $hoistedErrors->fatal( ...$error );
669 $hoistedErrors->fatal(
'htmlform-invalid-input' );
672 $this->mWasSubmitted =
true;
674 # Check for cancelled submission
675 foreach ( $this->mFlatFields as $fieldname => $field ) {
676 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
679 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
680 $this->mWasSubmitted =
false;
685 # Check for validation
686 foreach ( $this->mFlatFields as $fieldname => $field ) {
687 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
690 if ( $field->isHidden( $this->mFieldData ) ) {
693 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
694 if (
$res !==
true ) {
696 if (
$res !==
false && !$field->canDisplayErrors() ) {
697 if ( is_string(
$res ) ) {
698 $hoistedErrors->fatal(
'rawmessage',
$res );
700 $hoistedErrors->fatal(
$res );
707 return $hoistedErrors;
710 $callback = $this->mSubmitCallback;
711 if ( !is_callable( $callback ) ) {
712 throw new MWException(
'HTMLForm: no submit callback provided. Use ' .
713 'setSubmitCallback() to set one.' );
718 $res = call_user_func( $callback, $data, $this );
719 if (
$res ===
false ) {
720 $this->mWasSubmitted =
false;
738 return $this->mWasSubmitted;
752 $this->mSubmitCallback = $cb;
766 $this->mValidationErrorMessage = $msg;
831 if ( $section ===
null ) {
832 $this->mHeader .= $msg;
834 if ( !isset( $this->mSectionHeaders[$section] ) ) {
835 $this->mSectionHeaders[$section] =
'';
837 $this->mSectionHeaders[$section] .= $msg;
853 if ( $section ===
null ) {
854 $this->mHeader = $msg;
856 $this->mSectionHeaders[$section] = $msg;
871 if ( $section ===
null ) {
872 return $this->mHeader;
874 return $this->mSectionHeaders[$section] ??
'';
887 if ( $section ===
null ) {
888 $this->mFooter .= $msg;
890 if ( !isset( $this->mSectionFooters[$section] ) ) {
891 $this->mSectionFooters[$section] =
'';
893 $this->mSectionFooters[$section] .= $msg;
909 if ( $section ===
null ) {
910 $this->mFooter = $msg;
912 $this->mSectionFooters[$section] = $msg;
926 if ( $section ===
null ) {
927 return $this->mFooter;
929 return $this->mSectionFooters[$section] ??
'';
941 $this->mPost .= $msg;
969 $attribs += [
'name' => $name ];
970 $this->mHiddenFields[] = [ $value, $attribs ];
986 foreach ( $fields as $name => $value ) {
987 $this->mHiddenFields[] = [ $value, [
'name' => $name ] ];
1017 if ( !is_array( $data ) ) {
1018 $args = func_get_args();
1019 if ( count(
$args ) < 2 || count(
$args ) > 4 ) {
1020 throw new InvalidArgumentException(
1021 'Incorrect number of arguments for deprecated calling style'
1026 'value' =>
$args[1],
1027 'id' =>
$args[2] ??
null,
1028 'attribs' =>
$args[3] ??
null,
1031 if ( !isset( $data[
'name'] ) ) {
1032 throw new InvalidArgumentException(
'A name is required' );
1034 if ( !isset( $data[
'value'] ) ) {
1035 throw new InvalidArgumentException(
'A value is required' );
1038 $this->mButtons[] = $data + [
1058 $this->mTokenSalt = $salt;
1092 # For good measure (it is the default)
1093 $this->
getOutput()->preventClickjacking();
1094 $this->
getOutput()->addModules(
'mediawiki.htmlform' );
1095 $this->
getOutput()->addModuleStyles(
'mediawiki.htmlform.styles' );
1097 if ( $this->mCollapsible ) {
1099 $this->
getOutput()->addModules(
'jquery.makeCollapsible' );
1113 return '' . $this->mPre . $html . $this->mPost;
1124 $this->mCollapsible =
true;
1125 $this->mCollapsed = $collapsedByDefault;
1135 # Use multipart/form-data
1136 $encType = $this->mUseMultipart
1137 ?
'multipart/form-data'
1138 :
'application/x-www-form-urlencoded';
1141 'class' =>
'mw-htmlform',
1144 'enctype' => $encType,
1147 $attribs[
'id'] = $this->mId;
1149 if ( is_string( $this->mAutocomplete ) ) {
1150 $attribs[
'autocomplete'] = $this->mAutocomplete;
1152 if ( $this->mName ) {
1153 $attribs[
'name'] = $this->mName;
1156 $attribs[
'novalidate'] =
true;
1170 # Include a <fieldset> wrapper for style, if requested.
1171 if ( $this->mWrapperLegend !==
false ) {
1172 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend :
false;
1173 $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1176 return Html::rawElement(
1189 if ( $this->mFormIdentifier !==
null ) {
1190 $html .= Html::hidden(
1192 $this->mFormIdentifier
1196 $html .= Html::hidden(
1198 $this->
getUser()->getEditToken( $this->mTokenSalt ),
1199 [
'id' =>
'wpEditToken' ]
1201 $html .= Html::hidden(
'title', $this->
getTitle()->getPrefixedText() ) .
"\n";
1204 $articlePath = $this->
getConfig()->get(
'ArticlePath' );
1205 if ( strpos( $articlePath,
'?' ) !==
false && $this->
getMethod() ===
'get' ) {
1206 $html .= Html::hidden(
'title', $this->
getTitle()->getPrefixedText() ) .
"\n";
1209 foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1210 $html .= Html::hidden( $attribs[
'name'], $value, $attribs ) .
"\n";
1223 $useMediaWikiUIEverywhere = $this->
getConfig()->get(
'UseMediaWikiUIEverywhere' );
1225 if ( $this->mShowSubmit ) {
1228 if ( isset( $this->mSubmitID ) ) {
1229 $attribs[
'id'] = $this->mSubmitID;
1232 if ( isset( $this->mSubmitName ) ) {
1233 $attribs[
'name'] = $this->mSubmitName;
1236 if ( isset( $this->mSubmitTooltip ) ) {
1240 $attribs[
'class'] = [
'mw-htmlform-submit' ];
1242 if ( $useMediaWikiUIEverywhere ) {
1243 foreach ( $this->mSubmitFlags as $flag ) {
1244 $attribs[
'class'][] =
'mw-ui-' . $flag;
1246 $attribs[
'class'][] =
'mw-ui-button';
1249 $buttons .= Xml::submitButton( $this->
getSubmitText(), $attribs ) .
"\n";
1252 if ( $this->mShowReset ) {
1253 $buttons .= Html::element(
1257 'value' => $this->
msg(
'htmlform-reset' )->text(),
1258 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1263 if ( $this->mShowCancel ) {
1265 $buttons .= Html::element(
1268 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1271 $this->
msg(
'cancel' )->text()
1275 foreach ( $this->mButtons as $button ) {
1278 'name' => $button[
'name'],
1279 'value' => $button[
'value']
1282 if ( isset( $button[
'label-message'] ) ) {
1283 $label = $this->
getMessage( $button[
'label-message'] )->parse();
1284 } elseif ( isset( $button[
'label'] ) ) {
1285 $label = htmlspecialchars( $button[
'label'] );
1286 } elseif ( isset( $button[
'label-raw'] ) ) {
1287 $label = $button[
'label-raw'];
1289 $label = htmlspecialchars( $button[
'value'] );
1292 if ( $button[
'attribs'] ) {
1293 $attrs += $button[
'attribs'];
1296 if ( isset( $button[
'id'] ) ) {
1297 $attrs[
'id'] = $button[
'id'];
1300 if ( $useMediaWikiUIEverywhere ) {
1301 $attrs[
'class'] = isset( $attrs[
'class'] ) ? (array)$attrs[
'class'] : [];
1302 $attrs[
'class'][] =
'mw-ui-button';
1305 $buttons .= Html::rawElement(
'button', $attrs, $label ) .
"\n";
1312 return Html::rawElement(
'span',
1313 [
'class' =>
'mw-htmlform-submit-buttons' ],
"\n$buttons" ) .
"\n";
1322 return $this->
displaySection( $this->mFieldTree, $this->mTableId );
1335 if ( !in_array( $elementsType, [
'error',
'warning' ],
true ) ) {
1336 throw new DomainException( $elementsType .
' is not a valid type.' );
1338 $elementstr =
false;
1339 if ( $elements instanceof
Status ) {
1340 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1341 $status = $elementsType ===
'error' ? $errorStatus : $warningStatus;
1342 if ( $status->isGood() ) {
1345 $elementstr = $status
1347 ->setContext( $this )
1348 ->setInterfaceMessageFlag(
true )
1351 } elseif ( $elementsType ===
'error' ) {
1352 if ( is_array( $elements ) ) {
1354 } elseif ( $elements && $elements !==
true ) {
1355 $elementstr = (string)$elements;
1360 ? Html::rawElement(
'div', [
'class' => $elementsType .
'box' ], $elementstr )
1374 foreach ( $errors as $error ) {
1375 $errorstr .= Html::rawElement(
1382 $errorstr = Html::rawElement(
'ul', [], $errorstr );
1395 $this->mSubmitText =
$t;
1407 $this->mSubmitFlags = [
'destructive',
'primary' ];
1421 if ( !$msg instanceof
Message ) {
1422 $msg = $this->
msg( $msg );
1434 return $this->mSubmitText ?: $this->
msg(
'htmlform-submit' )->text();
1443 $this->mSubmitName = $name;
1454 $this->mSubmitTooltip = $name;
1468 $this->mSubmitID =
$t;
1489 $this->mFormIdentifier = $ident;
1505 $this->mShowSubmit = !$suppressSubmit;
1517 $this->mShowCancel = $show;
1529 $target = TitleValue::castPageToLinkTarget( $target );
1532 $this->mCancelTarget = $target;
1541 if ( is_string( $this->mCancelTarget ) ) {
1542 return $this->mCancelTarget;
1545 $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1546 return $target->getLocalURL();
1560 $this->mTableId = $id;
1581 $this->mName = $name;
1598 $this->mWrapperLegend = $legend;
1611 $this->mWrapperAttributes = $attributes;
1626 if ( !$msg instanceof
Message ) {
1627 $msg = $this->
msg( $msg );
1644 $this->mMessagePrefix = $p;
1658 $this->mTitle = Title::castFromPageReference(
$t );
1667 return $this->mTitle ?: $this->
getContext()->getTitle();
1678 $this->mMethod = strtolower( $method );
1687 return $this->mMethod;
1701 return Xml::fieldset( $legend, $section, $attributes ) .
"\n";
1723 $fieldsetIDPrefix =
'',
1724 &$hasUserVisibleFields =
false
1726 if ( $this->mFieldData ===
null ) {
1727 throw new LogicException(
'HTMLForm::displaySection() called on uninitialized field data. '
1728 .
'You probably called displayForm() without calling prepareForm() first.' );
1734 $subsectionHtml =
'';
1741 foreach ( $fields as $key => $value ) {
1743 $v = array_key_exists( $key, $this->mFieldData )
1744 ? $this->mFieldData[$key]
1745 : $value->getDefault();
1747 $retval = $value->$getFieldHtmlMethod( $v );
1751 if ( $value->hasVisibleOutput() ) {
1754 $labelValue = trim( $value->getLabel() );
1755 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1759 $hasUserVisibleFields =
true;
1761 } elseif ( is_array( $value ) ) {
1762 $subsectionHasVisibleFields =
false;
1766 "$fieldsetIDPrefix$key-",
1767 $subsectionHasVisibleFields );
1770 if ( $subsectionHasVisibleFields ===
true ) {
1772 $hasUserVisibleFields =
true;
1781 if ( $fieldsetIDPrefix ) {
1782 $attributes[
'id'] = Sanitizer::escapeIdForAttribute(
"$fieldsetIDPrefix$key" );
1785 $legend, $section, $attributes, $fields === $this->mFieldTree
1789 $subsectionHtml .= $section;
1794 $html = $this->
formatSection( $html, $sectionName, $hasLabel );
1796 if ( $subsectionHtml ) {
1797 if ( $this->mSubSectionBeforeFields ) {
1798 return $subsectionHtml .
"\n" . $html;
1800 return $html .
"\n" . $subsectionHtml;
1815 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1816 if ( !$fieldsHtml ) {
1823 $html = implode(
'', $fieldsHtml );
1831 if ( !$anyFieldHasLabel ) {
1832 $classes[] =
'mw-htmlform-nolabel';
1835 $attribs = [
'class' => $classes ];
1837 if ( $sectionName ) {
1838 $attribs[
'id'] = Sanitizer::escapeIdForAttribute( $sectionName );
1842 return Html::rawElement(
'table',
1844 Html::rawElement(
'tbody', [],
"\n$html\n" ) ) .
"\n";
1846 return Html::rawElement(
'span', $attribs,
"\n$html\n" );
1848 return Html::rawElement(
'div', $attribs,
"\n$html\n" );
1858 foreach ( $this->mFlatFields as $fieldname => $field ) {
1860 if ( $field->skipLoadData( $request ) ) {
1863 if ( !empty( $field->mParams[
'disabled'] ) ) {
1864 $fieldData[$fieldname] = $field->getDefault();
1866 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1871 foreach ( $fieldData as $name => &$value ) {
1872 $field = $this->mFlatFields[$name];
1873 $value = $field->filter( $value, $this->mFlatFields );
1876 $this->mFieldData = $fieldData;
1887 $this->mShowReset = !$suppressReset;
1916 return $this->
msg( $this->mMessagePrefix ?
"{$this->mMessagePrefix}-$key" : $key )->text();
1930 $this->mAction = $action;
1944 if ( $this->mAction !==
false ) {
1945 return $this->mAction;
1948 $articlePath = $this->
getConfig()->get(
'ArticlePath' );
1954 if ( strpos( $articlePath,
'?' ) !==
false && $this->
getMethod() ===
'get' ) {
1958 return $this->
getTitle()->getLocalURL();
1972 $this->mAutocomplete = $autocomplete;
1997 foreach ( $this->mFlatFields as $fieldname => $field ) {
1998 if ( $field->needsJSForHtml5FormValidation() ) {
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
setContext(IContextSource $context)
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
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...
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for objects which can provide a MediaWiki context on request.