24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
151 use ProtectedHookAccessorTrait;
155 'api' => HTMLApiField::class,
156 'text' => HTMLTextField::class,
157 'textwithbutton' => HTMLTextFieldWithButton::class,
158 'textarea' => HTMLTextAreaField::class,
159 'select' => HTMLSelectField::class,
160 'combobox' => HTMLComboboxField::class,
161 'radio' => HTMLRadioField::class,
162 'multiselect' => HTMLMultiSelectField::class,
163 'limitselect' => HTMLSelectLimitField::class,
164 'check' => HTMLCheckField::class,
165 'toggle' => HTMLCheckField::class,
166 'int' => HTMLIntField::class,
167 'file' => HTMLFileField::class,
168 'float' => HTMLFloatField::class,
169 'info' => HTMLInfoField::class,
170 'selectorother' => HTMLSelectOrOtherField::class,
171 'selectandother' => HTMLSelectAndOtherField::class,
172 'namespaceselect' => HTMLSelectNamespace::class,
173 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
174 'tagfilter' => HTMLTagFilter::class,
175 'sizefilter' => HTMLSizeFilterField::class,
176 'submit' => HTMLSubmitField::class,
177 'hidden' => HTMLHiddenField::class,
178 'edittools' => HTMLEditTools::class,
179 'checkmatrix' => HTMLCheckMatrix::class,
180 'cloner' => HTMLFormFieldCloner::class,
181 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
182 'language' => HTMLSelectLanguageField::class,
183 'date' => HTMLDateTimeField::class,
184 'time' => HTMLDateTimeField::class,
185 'datetime' => HTMLDateTimeField::class,
186 'expiry' => HTMLExpiryField::class,
190 'email' => HTMLTextField::class,
191 'password' => HTMLTextField::class,
192 'url' => HTMLTextField::class,
193 'title' => HTMLTitleTextField::class,
194 'user' => HTMLUserTextField::class,
195 'tagmultiselect' => HTMLTagMultiselectField::class,
196 'usersmultiselect' => HTMLUsersMultiselectField::class,
197 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
198 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
353 $form =
new self( $descriptor,
$context, $messagePrefix );
374 $this->mMessagePrefix = $messagePrefix;
378 !$this->
getConfig()->
get( MainConfigNames::HTMLFormAllowTableFormat )
379 && $this->displayFormat ===
'table'
381 $this->displayFormat =
'div';
397 $loadedDescriptor = [];
399 foreach ( $descriptor as $fieldname => $info ) {
401 $section = $info[
'section'] ??
'';
403 if ( isset( $info[
'type'] ) && $info[
'type'] ===
'file' ) {
404 $this->mUseMultipart =
true;
407 $field = static::loadInputFromParameters( $fieldname, $info, $this );
409 $setSection =& $loadedDescriptor;
411 foreach ( explode(
'/', $section ) as $newName ) {
412 if ( !isset( $setSection[$newName] ) ) {
413 $setSection[$newName] = [];
416 $setSection =& $setSection[$newName];
420 $setSection[$fieldname] = $field;
421 $this->mFlatFields[$fieldname] = $field;
424 $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
434 return isset( $this->mFlatFields[$fieldname] );
443 if ( !$this->
hasField( $fieldname ) ) {
444 throw new DomainException( __METHOD__ .
': no field named ' . $fieldname );
446 return $this->mFlatFields[$fieldname];
461 in_array( $format, $this->availableSubclassDisplayFormats,
true ) ||
462 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats,
true )
464 throw new MWException(
'Cannot change display format after creation, ' .
465 'use HTMLForm::factory() instead' );
468 if ( !in_array( $format, $this->availableDisplayFormats,
true ) ) {
469 throw new MWException(
'Display format must be one of ' .
472 $this->availableDisplayFormats,
473 $this->availableSubclassDisplayFormats
480 if ( !$this->
getConfig()->
get( MainConfigNames::HTMLFormAllowTableFormat ) &&
481 $format ===
'table' ) {
485 $this->displayFormat = $format;
517 if ( isset( $descriptor[
'class'] ) ) {
518 $class = $descriptor[
'class'];
519 } elseif ( isset( $descriptor[
'type'] ) ) {
520 $class = static::$typeMappings[$descriptor[
'type']];
521 $descriptor[
'class'] = $class;
527 throw new MWException(
"Descriptor with no class for $fieldname: "
528 . print_r( $descriptor,
true ) );
549 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
551 $descriptor[
'fieldname'] = $fieldname;
553 $descriptor[
'parent'] = $parent;
556 # @todo This will throw a fatal error whenever someone try to use
557 # 'class' to feed a CSS class instead of 'cssclass'. Would be
558 # great to avoid the fatal error and show a nice error.
559 return new $class( $descriptor );
572 # Load data from the request.
574 $this->mFormIdentifier ===
null ||
575 $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier
579 $this->mFieldData = [];
592 if ( $this->mFormIdentifier ===
null ) {
601 } elseif ( $this->
getRequest()->wasPosted() ) {
602 $editToken = $this->
getRequest()->getVal(
'wpEditToken' );
603 if ( $this->
getUser()->isRegistered() || $editToken !==
null ) {
607 $tokenOkay = $this->
getUser()->matchEditToken( $editToken, $this->mTokenSalt );
613 if ( $tokenOkay && $identOkay ) {
614 $this->mWasSubmitted =
true;
632 if ( $result ===
true || ( $result instanceof
Status && $result->
isGood() ) ) {
671 if ( $this->mValidationErrorMessage ) {
672 foreach ( $this->mValidationErrorMessage as $error ) {
673 $hoistedErrors->fatal( ...$error );
676 $hoistedErrors->fatal(
'htmlform-invalid-input' );
679 $this->mWasSubmitted =
true;
681 # Check for cancelled submission
682 foreach ( $this->mFlatFields as $fieldname => $field ) {
683 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
686 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
687 $this->mWasSubmitted =
false;
692 # Check for validation
693 foreach ( $this->mFlatFields as $fieldname => $field ) {
694 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
697 if ( $field->isDisabled( $this->mFieldData ) ) {
700 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
701 if (
$res !==
true ) {
703 if (
$res !==
false && !$field->canDisplayErrors() ) {
704 if ( is_string(
$res ) ) {
705 $hoistedErrors->fatal(
'rawmessage',
$res );
707 $hoistedErrors->fatal(
$res );
714 return $hoistedErrors;
718 if ( !is_callable( $callback ) ) {
719 throw new MWException(
'HTMLForm: no submit callback provided. Use ' .
720 'setSubmitCallback() to set one.' );
725 $res = call_user_func( $callback, $data, $this );
726 if (
$res ===
false ) {
727 $this->mWasSubmitted =
false;
759 $this->mSubmitCallback = $cb;
773 $this->mValidationErrorMessage = $msg;
813 $this->mPre .= $html;
873 if ( $section ===
null ) {
874 $this->mHeader .= $html;
876 if ( !isset( $this->mSectionHeaders[$section] ) ) {
877 $this->mSectionHeaders[$section] =
'';
879 $this->mSectionHeaders[$section] .= $html;
895 if ( $section ===
null ) {
896 $this->mHeader = $html;
898 $this->mSectionHeaders[$section] = $html;
913 if ( $section ===
null ) {
916 return $this->mSectionHeaders[$section] ??
'';
970 if ( $section ===
null ) {
971 $this->mFooter .= $html;
973 if ( !isset( $this->mSectionFooters[$section] ) ) {
974 $this->mSectionFooters[$section] =
'';
976 $this->mSectionFooters[$section] .= $html;
992 if ( $section ===
null ) {
993 $this->mFooter = $html;
995 $this->mSectionFooters[$section] = $html;
1009 if ( $section ===
null ) {
1012 return $this->mSectionFooters[$section] ??
'';
1064 $this->mPost .= $html;
1078 $this->mPost = $html;
1127 $attribs += [
'name' => $name ];
1128 $this->mHiddenFields[] = [ $value, $attribs ];
1144 foreach ( $fields as $name => $value ) {
1145 $this->mHiddenFields[] = [ $value, [
'name' => $name ] ];
1175 if ( !is_array( $data ) ) {
1176 $args = func_get_args();
1177 if ( count(
$args ) < 2 || count(
$args ) > 4 ) {
1178 throw new InvalidArgumentException(
1179 'Incorrect number of arguments for deprecated calling style'
1184 'value' =>
$args[1],
1185 'id' =>
$args[2] ??
null,
1186 'attribs' =>
$args[3] ??
null,
1189 if ( !isset( $data[
'name'] ) ) {
1190 throw new InvalidArgumentException(
'A name is required' );
1192 if ( !isset( $data[
'value'] ) ) {
1193 throw new InvalidArgumentException(
'A value is required' );
1196 $this->mButtons[] = $data + [
1216 $this->mTokenSalt = $salt;
1244 if ( $this->hiddenTitleAddedToForm ) {
1254 $this->hiddenTitleAddedToForm =
true;
1269 # For good measure (it is the default)
1270 $this->getOutput()->setPreventClickjacking(
true );
1271 $this->getOutput()->addModules(
'mediawiki.htmlform' );
1272 $this->getOutput()->addModuleStyles(
'mediawiki.htmlform.styles' );
1274 if ( $this->mCollapsible ) {
1276 $this->getOutput()->addModules(
'jquery.makeCollapsible' );
1280 . $this->getErrorsOrWarnings( $submitResult,
'error' )
1281 . $this->getErrorsOrWarnings( $submitResult,
'warning' )
1282 . $this->getHeaderText()
1283 . $this->getHiddenTitle()
1285 . $this->getHiddenFields()
1286 . $this->getButtons()
1287 . $this->getFooterText();
1289 $html = $this->wrapForm( $html );
1291 return '' . $this->mPre . $html . $this->mPost;
1302 $this->mCollapsible =
true;
1303 $this->mCollapsed = $collapsedByDefault;
1313 # Use multipart/form-data
1314 $encType = $this->mUseMultipart
1315 ?
'multipart/form-data'
1316 :
'application/x-www-form-urlencoded';
1319 'class' =>
'mw-htmlform',
1320 'action' => $this->getAction(),
1321 'method' => $this->getMethod(),
1322 'enctype' => $encType,
1325 $attribs[
'id'] = $this->mId;
1327 if ( is_string( $this->mAutocomplete ) ) {
1328 $attribs[
'autocomplete'] = $this->mAutocomplete;
1330 if ( $this->mName ) {
1331 $attribs[
'name'] = $this->mName;
1333 if ( $this->needsJSForHtml5FormValidation() ) {
1334 $attribs[
'novalidate'] =
true;
1348 # Include a <fieldset> wrapper for style, if requested.
1349 if ( $this->mWrapperLegend !==
false ) {
1350 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend :
false;
1351 $html =
Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1356 $this->getFormAttributes(),
1370 $html .= $this->getHiddenTitle();
1372 if ( $this->mFormIdentifier !==
null ) {
1375 $this->mFormIdentifier
1378 if ( $this->getMethod() ===
'post' ) {
1381 $this->
getUser()->getEditToken( $this->mTokenSalt ),
1382 [
'id' =>
'wpEditToken' ]
1386 foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1387 $html .=
Html::hidden( $attribs[
'name'], $value, $attribs ) .
"\n";
1400 $useMediaWikiUIEverywhere =
1401 $this->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
1403 if ( $this->mShowSubmit ) {
1406 if ( isset( $this->mSubmitID ) ) {
1407 $attribs[
'id'] = $this->mSubmitID;
1410 if ( isset( $this->mSubmitName ) ) {
1411 $attribs[
'name'] = $this->mSubmitName;
1414 if ( isset( $this->mSubmitTooltip ) ) {
1418 $attribs[
'class'] = [
'mw-htmlform-submit' ];
1420 if ( $useMediaWikiUIEverywhere ) {
1421 foreach ( $this->mSubmitFlags as $flag ) {
1422 $attribs[
'class'][] =
'mw-ui-' . $flag;
1424 $attribs[
'class'][] =
'mw-ui-button';
1430 if ( $this->mShowReset ) {
1435 'value' => $this->msg(
'htmlform-reset' )->text(),
1436 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1441 if ( $this->mShowCancel ) {
1442 $target = $this->getCancelTargetURL();
1446 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1449 $this->msg(
'cancel' )->text()
1453 foreach ( $this->mButtons as $button ) {
1456 'name' => $button[
'name'],
1457 'value' => $button[
'value']
1460 if ( isset( $button[
'label-message'] ) ) {
1461 $label = $this->getMessage( $button[
'label-message'] )->parse();
1462 } elseif ( isset( $button[
'label'] ) ) {
1463 $label = htmlspecialchars( $button[
'label'] );
1464 } elseif ( isset( $button[
'label-raw'] ) ) {
1465 $label = $button[
'label-raw'];
1467 $label = htmlspecialchars( $button[
'value'] );
1471 if ( $button[
'attribs'] ) {
1473 $attrs += $button[
'attribs'];
1476 if ( isset( $button[
'id'] ) ) {
1477 $attrs[
'id'] = $button[
'id'];
1480 if ( $useMediaWikiUIEverywhere ) {
1481 $attrs[
'class'] = isset( $attrs[
'class'] ) ? (array)$attrs[
'class'] : [];
1482 $attrs[
'class'][] =
'mw-ui-button';
1493 [
'class' =>
'mw-htmlform-submit-buttons' ],
"\n$buttons" ) .
"\n";
1502 return $this->displaySection( $this->mFieldTree, $this->mTableId );
1515 if ( !in_array( $elementsType, [
'error',
'warning' ],
true ) ) {
1516 throw new DomainException( $elementsType .
' is not a valid type.' );
1518 $elementstr =
false;
1519 if ( $elements instanceof
Status ) {
1520 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1521 $status = $elementsType ===
'error' ? $errorStatus : $warningStatus;
1522 if ( $status->isGood() ) {
1525 $elementstr = $status
1527 ->setContext( $this )
1528 ->setInterfaceMessageFlag(
true )
1531 } elseif ( $elementsType ===
'error' ) {
1532 if ( is_array( $elements ) ) {
1533 $elementstr = $this->formatErrors( $elements );
1534 } elseif ( $elements && $elements !==
true ) {
1535 $elementstr = (string)$elements;
1539 if ( !$elementstr ) {
1541 } elseif ( $elementsType ===
'error' ) {
1558 foreach ( $errors as $error ) {
1562 $this->getMessage( $error )->parse()
1579 $this->mSubmitText =
$t;
1591 $this->mSubmitFlags = [
'destructive',
'primary' ];
1605 if ( !$msg instanceof
Message ) {
1606 $msg = $this->msg( $msg );
1608 $this->setSubmitText( $msg->text() );
1618 return $this->mSubmitText ?: $this->msg(
'htmlform-submit' )->text();
1627 $this->mSubmitName = $name;
1638 $this->mSubmitTooltip = $name;
1652 $this->mSubmitID =
$t;
1673 $this->mFormIdentifier = $ident;
1689 $this->mShowSubmit = !$suppressSubmit;
1701 $this->mShowCancel = $show;
1716 $this->mCancelTarget = $target;
1725 if ( is_string( $this->mCancelTarget ) ) {
1726 return $this->mCancelTarget;
1730 return $target->getLocalURL();
1744 $this->mTableId = $id;
1765 $this->mName = $name;
1782 $this->mWrapperLegend = $legend;
1795 $this->mWrapperAttributes = $attributes;
1810 if ( !$msg instanceof
Message ) {
1811 $msg = $this->msg( $msg );
1813 $this->setWrapperLegend( $msg->text() );
1828 $this->mMessagePrefix = $p;
1851 return $this->mTitle ?: $this->
getContext()->getTitle();
1862 $this->mMethod = strtolower( $method );
1871 return $this->mMethod;
1885 return Xml::fieldset( $legend, $section, $attributes ) .
"\n";
1907 $fieldsetIDPrefix =
'',
1908 &$hasUserVisibleFields =
false
1910 if ( $this->mFieldData ===
null ) {
1911 throw new LogicException(
'HTMLForm::displaySection() called on uninitialized field data. '
1912 .
'You probably called displayForm() without calling prepareForm() first.' );
1915 $displayFormat = $this->getDisplayFormat();
1918 $subsectionHtml =
'';
1923 $getFieldHtmlMethod = $displayFormat ===
'table' ?
'getTableRow' : (
'get' . $displayFormat );
1925 foreach ( $fields as $key => $value ) {
1927 $v = array_key_exists( $key, $this->mFieldData )
1928 ? $this->mFieldData[$key]
1929 : $value->getDefault();
1931 $retval = $value->$getFieldHtmlMethod( $v );
1935 if ( $value->hasVisibleOutput() ) {
1938 $labelValue = trim( $value->getLabel() );
1939 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1943 $hasUserVisibleFields =
true;
1945 } elseif ( is_array( $value ) ) {
1946 $subsectionHasVisibleFields =
false;
1948 $this->displaySection( $value,
1950 "$fieldsetIDPrefix$key-",
1951 $subsectionHasVisibleFields );
1954 if ( $subsectionHasVisibleFields ===
true ) {
1956 $hasUserVisibleFields =
true;
1958 $legend = $this->getLegend( $key );
1960 $section = $this->getHeaderText( $key ) .
1962 $this->getFooterText( $key );
1965 if ( $fieldsetIDPrefix ) {
1968 $subsectionHtml .= $this->wrapFieldSetSection(
1969 $legend, $section, $attributes, $fields === $this->mFieldTree
1973 $subsectionHtml .= $section;
1978 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1980 if ( $subsectionHtml ) {
1981 if ( $this->mSubSectionBeforeFields ) {
1982 return $subsectionHtml .
"\n" . $html;
1984 return $html .
"\n" . $subsectionHtml;
1999 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
2000 if ( !$fieldsHtml ) {
2006 $displayFormat = $this->getDisplayFormat();
2007 $html = implode(
'', $fieldsHtml );
2009 if ( $displayFormat ===
'raw' ) {
2015 if ( !$anyFieldHasLabel ) {
2016 $classes[] =
'mw-htmlform-nolabel';
2019 $attribs = [
'class' => $classes ];
2021 if ( $sectionName ) {
2025 if ( $displayFormat ===
'table' ) {
2029 } elseif ( $displayFormat ===
'inline' ) {
2040 $this->prepareForm();
2048 $request = $this->getRequest();
2050 foreach ( $this->mFlatFields as $fieldname => $field ) {
2051 if ( $field->skipLoadData( $request ) ) {
2054 if ( $field->mParams[
'disabled'] ??
false ) {
2055 $fieldData[$fieldname] = $field->getDefault();
2057 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2063 foreach ( $fieldData as $name => &$value ) {
2064 $field = $this->mFlatFields[$name];
2065 if ( $field->isDisabled( $fieldData ) ) {
2066 $value = $field->getDefault();
2071 foreach ( $fieldData as $name => &$value ) {
2072 $field = $this->mFlatFields[$name];
2073 $value = $field->filter( $value, $fieldData );
2076 $this->mFieldData = $fieldData;
2087 $this->mShowReset = !$suppressReset;
2116 return $this->msg( $this->mMessagePrefix ?
"{$this->mMessagePrefix}-$key" : $key )->text();
2130 $this->mAction = $action;
2144 if ( $this->mAction !==
false ) {
2145 return $this->mAction;
2148 $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2154 if ( strpos( $articlePath,
'?' ) !==
false && $this->getMethod() ===
'get' ) {
2155 return $this->getConfig()->get( MainConfigNames::Script );
2158 return $this->
getTitle()->getLocalURL();
2172 $this->mAutocomplete = $autocomplete;
2197 foreach ( $this->mFlatFields as $fieldname => $field ) {
2198 if ( $field->needsJSForHtml5FormValidation() ) {
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
static warningBox( $html, $className='')
Return a warning box.
static errorBox( $html, $heading='', $className='')
Return an error box.
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null, $localizer=null, $user=null, $config=null, $relevantTitle=null)
Returns the attributes for the tooltip and access key.
A class containing constants representing the names of configuration variables.
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...
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
isGood()
Returns whether the operation completed and didn't have any error or warnings.
static newGood( $value=null)
Factory function for good results.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
static castFromLinkTarget( $linkTarget)
Same as newFromLinkTarget, but if passed null, returns null.
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Interface for objects which can provide a MediaWiki context on request.