MediaWiki  master
HTMLForm.php
Go to the documentation of this file.
1 <?php
2 
24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
29 
151 class HTMLForm extends ContextSource {
152  use ProtectedHookAccessorTrait;
153 
155  public static $typeMappings = [
156  'api' => HTMLApiField::class,
157  'text' => HTMLTextField::class,
158  'textwithbutton' => HTMLTextFieldWithButton::class,
159  'textarea' => HTMLTextAreaField::class,
160  'select' => HTMLSelectField::class,
161  'combobox' => HTMLComboboxField::class,
162  'radio' => HTMLRadioField::class,
163  'multiselect' => HTMLMultiSelectField::class,
164  'limitselect' => HTMLSelectLimitField::class,
165  'check' => HTMLCheckField::class,
166  'toggle' => HTMLCheckField::class,
167  'int' => HTMLIntField::class,
168  'file' => HTMLFileField::class,
169  'float' => HTMLFloatField::class,
170  'info' => HTMLInfoField::class,
171  'selectorother' => HTMLSelectOrOtherField::class,
172  'selectandother' => HTMLSelectAndOtherField::class,
173  'namespaceselect' => HTMLSelectNamespace::class,
174  'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
175  'tagfilter' => HTMLTagFilter::class,
176  'sizefilter' => HTMLSizeFilterField::class,
177  'submit' => HTMLSubmitField::class,
178  'hidden' => HTMLHiddenField::class,
179  'edittools' => HTMLEditTools::class,
180  'checkmatrix' => HTMLCheckMatrix::class,
181  'cloner' => HTMLFormFieldCloner::class,
182  'autocompleteselect' => HTMLAutoCompleteSelectField::class,
183  'language' => HTMLSelectLanguageField::class,
184  'date' => HTMLDateTimeField::class,
185  'time' => HTMLDateTimeField::class,
186  'datetime' => HTMLDateTimeField::class,
187  'expiry' => HTMLExpiryField::class,
188  'timezone' => HTMLTimezoneField::class,
189  // HTMLTextField will output the correct type="" attribute automagically.
190  // There are about four zillion other HTML5 input types, like range, but
191  // we don't use those at the moment, so no point in adding all of them.
192  'email' => HTMLTextField::class,
193  'password' => HTMLTextField::class,
194  'url' => HTMLTextField::class,
195  'title' => HTMLTitleTextField::class,
196  'user' => HTMLUserTextField::class,
197  'tagmultiselect' => HTMLTagMultiselectField::class,
198  'usersmultiselect' => HTMLUsersMultiselectField::class,
199  'titlesmultiselect' => HTMLTitlesMultiselectField::class,
200  'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
201  ];
202 
203  public $mFieldData;
204 
205  protected $mMessagePrefix;
206 
208  protected $mFlatFields = [];
209  protected $mFieldTree = [];
210  protected $mShowReset = false;
211  protected $mShowSubmit = true;
213  protected $mSubmitFlags = [ 'primary', 'progressive' ];
214  protected $mShowCancel = false;
215  protected $mCancelTarget;
216 
217  protected $mSubmitCallback;
223 
224  protected $mPre = '';
225  protected $mHeader = '';
226  protected $mFooter = '';
227  protected $mSectionHeaders = [];
228  protected $mSectionFooters = [];
229  protected $mPost = '';
230  protected $mId;
231  protected $mName;
232  protected $mTableId = '';
233 
234  protected $mSubmitID;
235  protected $mSubmitName;
236  protected $mSubmitText;
237  protected $mSubmitTooltip;
238 
239  protected $mFormIdentifier;
241  protected $mTitle;
242  protected $mMethod = 'post';
243  protected $mWasSubmitted = false;
244 
250  protected $mAction = false;
251 
257  protected $mCollapsible = false;
258 
264  protected $mCollapsed = false;
265 
271  protected $mAutocomplete = null;
272 
273  protected $mUseMultipart = false;
278  protected $mHiddenFields = [];
283  protected $mButtons = [];
284 
285  protected $mWrapperLegend = false;
286  protected $mWrapperAttributes = [];
287 
292  protected $mTokenSalt = '';
293 
302  protected $mSubSectionBeforeFields = true;
303 
309  protected $displayFormat = 'table';
310 
316  'table',
317  'div',
318  'raw',
319  'inline',
320  ];
321 
327  'vform',
328  'ooui',
329  ];
330 
335  private $hiddenTitleAddedToForm = false;
336 
350  public static function factory(
351  $displayFormat, $descriptor, IContextSource $context, $messagePrefix = ''
352  ) {
353  switch ( $displayFormat ) {
354  case 'vform':
355  return new VFormHTMLForm( $descriptor, $context, $messagePrefix );
356  case 'ooui':
357  return new OOUIHTMLForm( $descriptor, $context, $messagePrefix );
358  default:
359  $form = new self( $descriptor, $context, $messagePrefix );
360  $form->setDisplayFormat( $displayFormat );
361  return $form;
362  }
363  }
364 
376  public function __construct(
377  $descriptor, IContextSource $context, $messagePrefix = ''
378  ) {
379  $this->setContext( $context );
380  $this->mMessagePrefix = $messagePrefix;
381 
382  // Evil hack for mobile :(
383  if (
384  !$this->getConfig()->get( MainConfigNames::HTMLFormAllowTableFormat )
385  && $this->displayFormat === 'table'
386  ) {
387  $this->displayFormat = 'div';
388  }
389 
390  $this->addFields( $descriptor );
391  }
392 
402  public function addFields( $descriptor ) {
403  $loadedDescriptor = [];
404 
405  foreach ( $descriptor as $fieldname => $info ) {
406 
407  $section = $info['section'] ?? '';
408 
409  if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
410  $this->mUseMultipart = true;
411  }
412 
413  $field = static::loadInputFromParameters( $fieldname, $info, $this );
414 
415  $setSection =& $loadedDescriptor;
416  if ( $section ) {
417  foreach ( explode( '/', $section ) as $newName ) {
418  if ( !isset( $setSection[$newName] ) ) {
419  $setSection[$newName] = [];
420  }
421 
422  $setSection =& $setSection[$newName];
423  }
424  }
425 
426  $setSection[$fieldname] = $field;
427  $this->mFlatFields[$fieldname] = $field;
428  }
429 
430  $this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor );
431 
432  return $this;
433  }
434 
439  public function hasField( $fieldname ) {
440  return isset( $this->mFlatFields[$fieldname] );
441  }
442 
448  public function getField( $fieldname ) {
449  if ( !$this->hasField( $fieldname ) ) {
450  throw new DomainException( __METHOD__ . ': no field named ' . $fieldname );
451  }
452  return $this->mFlatFields[$fieldname];
453  }
454 
465  public function setDisplayFormat( $format ) {
466  if (
467  in_array( $format, $this->availableSubclassDisplayFormats, true ) ||
468  in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true )
469  ) {
470  throw new MWException( 'Cannot change display format after creation, ' .
471  'use HTMLForm::factory() instead' );
472  }
473 
474  if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
475  throw new MWException( 'Display format must be one of ' .
476  print_r(
477  array_merge(
478  $this->availableDisplayFormats,
479  $this->availableSubclassDisplayFormats
480  ),
481  true
482  ) );
483  }
484 
485  // Evil hack for mobile :(
486  if ( !$this->getConfig()->get( MainConfigNames::HTMLFormAllowTableFormat ) &&
487  $format === 'table' ) {
488  $format = 'div';
489  }
490 
491  $this->displayFormat = $format;
492 
493  return $this;
494  }
495 
501  public function getDisplayFormat() {
502  return $this->displayFormat;
503  }
504 
522  public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
523  if ( isset( $descriptor['class'] ) ) {
524  $class = $descriptor['class'];
525  } elseif ( isset( $descriptor['type'] ) ) {
526  $class = static::$typeMappings[$descriptor['type']];
527  $descriptor['class'] = $class;
528  } else {
529  $class = null;
530  }
531 
532  if ( !$class ) {
533  throw new MWException( "Descriptor with no class for $fieldname: "
534  . print_r( $descriptor, true ) );
535  }
536 
537  return $class;
538  }
539 
553  public static function loadInputFromParameters( $fieldname, $descriptor,
554  HTMLForm $parent = null
555  ) {
556  $class = static::getClassFromDescriptor( $fieldname, $descriptor );
557 
558  $descriptor['fieldname'] = $fieldname;
559  if ( $parent ) {
560  $descriptor['parent'] = $parent;
561  }
562 
563  # @todo This will throw a fatal error whenever someone try to use
564  # 'class' to feed a CSS class instead of 'cssclass'. Would be
565  # great to avoid the fatal error and show a nice error.
566  return new $class( $descriptor );
567  }
568 
578  public function prepareForm() {
579  # Load data from the request.
580  if (
581  $this->mFormIdentifier === null ||
582  $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
583  ) {
584  $this->loadFieldData();
585  } else {
586  $this->mFieldData = [];
587  }
588 
589  return $this;
590  }
591 
596  public function tryAuthorizedSubmit() {
597  $result = false;
598 
599  if ( $this->mFormIdentifier === null ) {
600  $identOkay = true;
601  } else {
602  $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier;
603  }
604 
605  $tokenOkay = false;
606  if ( $this->getMethod() !== 'post' ) {
607  $tokenOkay = true; // no session check needed
608  } elseif ( $this->getRequest()->wasPosted() ) {
609  $editToken = $this->getRequest()->getVal( 'wpEditToken' );
610  if ( $this->getUser()->isRegistered() || $editToken !== null ) {
611  // Session tokens for logged-out users have no security value.
612  // However, if the user gave one, check it in order to give a nice
613  // "session expired" error instead of "permission denied" or such.
614  $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
615  } else {
616  $tokenOkay = true;
617  }
618  }
619 
620  if ( $tokenOkay && $identOkay ) {
621  $this->mWasSubmitted = true;
622  $result = $this->trySubmit();
623  }
624 
625  return $result;
626  }
627 
635  public function show() {
636  $this->prepareForm();
637 
638  $result = $this->tryAuthorizedSubmit();
639  if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
640  return $result;
641  }
642 
643  $this->displayForm( $result );
644 
645  return false;
646  }
647 
653  public function showAlways() {
654  $this->prepareForm();
655 
656  $result = $this->tryAuthorizedSubmit();
657 
658  $this->displayForm( $result );
659 
660  return $result;
661  }
662 
675  public function trySubmit() {
676  $valid = true;
677  $hoistedErrors = Status::newGood();
678  if ( $this->mValidationErrorMessage ) {
679  foreach ( $this->mValidationErrorMessage as $error ) {
680  $hoistedErrors->fatal( ...$error );
681  }
682  } else {
683  $hoistedErrors->fatal( 'htmlform-invalid-input' );
684  }
685 
686  $this->mWasSubmitted = true;
687 
688  # Check for cancelled submission
689  foreach ( $this->mFlatFields as $fieldname => $field ) {
690  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
691  continue;
692  }
693  if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
694  $this->mWasSubmitted = false;
695  return false;
696  }
697  }
698 
699  # Check for validation
700  $hasNonDefault = false;
701  foreach ( $this->mFlatFields as $fieldname => $field ) {
702  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
703  continue;
704  }
705  $hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault();
706  if ( $field->isDisabled( $this->mFieldData ) ) {
707  continue;
708  }
709  $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
710  if ( $res !== true ) {
711  $valid = false;
712  if ( $res !== false && !$field->canDisplayErrors() ) {
713  if ( is_string( $res ) ) {
714  $hoistedErrors->fatal( 'rawmessage', $res );
715  } else {
716  $hoistedErrors->fatal( $res );
717  }
718  }
719  }
720  }
721 
722  if ( !$valid ) {
723  // Treat as not submitted if got nothing from the user on GET forms.
724  if ( !$hasNonDefault && $this->getMethod() === 'get' &&
725  ( $this->mFormIdentifier === null ||
726  $this->getRequest()->getCheck( 'wpFormIdentifier' ) )
727  ) {
728  $this->mWasSubmitted = false;
729  return false;
730  }
731  return $hoistedErrors;
732  }
733 
734  $callback = $this->mSubmitCallback;
735  if ( !is_callable( $callback ) ) {
736  throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
737  'setSubmitCallback() to set one.' );
738  }
739 
740  $data = $this->filterDataForSubmit( $this->mFieldData );
741 
742  $res = call_user_func( $callback, $data, $this );
743  if ( $res === false ) {
744  $this->mWasSubmitted = false;
745  } elseif ( $res instanceof StatusValue ) {
746  // DWIM - callbacks are not supposed to return a StatusValue but it's easy to mix up.
747  $res = Status::wrap( $res );
748  }
749 
750  return $res;
751  }
752 
764  public function wasSubmitted() {
765  return $this->mWasSubmitted;
766  }
767 
778  public function setSubmitCallback( $cb ) {
779  $this->mSubmitCallback = $cb;
780 
781  return $this;
782  }
783 
793  public function setValidationErrorMessage( $msg ) {
794  $this->mValidationErrorMessage = $msg;
795 
796  return $this;
797  }
798 
807  public function setIntro( $msg ) {
808  return $this->setPreHtml( $msg );
809  }
810 
819  public function setPreHtml( $html ) {
820  $this->mPre = $html;
821 
822  return $this;
823  }
824 
833  public function addPreHtml( $html ) {
834  $this->mPre .= $html;
835 
836  return $this;
837  }
838 
845  public function getPreHtml() {
846  return $this->mPre;
847  }
848 
857  public function setPreText( $msg ) {
858  return $this->setPreHtml( $msg );
859  }
860 
869  public function addPreText( $msg ) {
870  return $this->addPreHtml( $msg );
871  }
872 
880  public function getPreText() {
881  return $this->getPreHtml();
882  }
883 
893  public function addHeaderHtml( $html, $section = null ) {
894  if ( $section === null ) {
895  $this->mHeader .= $html;
896  } else {
897  if ( !isset( $this->mSectionHeaders[$section] ) ) {
898  $this->mSectionHeaders[$section] = '';
899  }
900  $this->mSectionHeaders[$section] .= $html;
901  }
902 
903  return $this;
904  }
905 
915  public function setHeaderHtml( $html, $section = null ) {
916  if ( $section === null ) {
917  $this->mHeader = $html;
918  } else {
919  $this->mSectionHeaders[$section] = $html;
920  }
921 
922  return $this;
923  }
924 
933  public function getHeaderHtml( $section = null ) {
934  if ( $section === null ) {
935  return $this->mHeader;
936  } else {
937  return $this->mSectionHeaders[$section] ?? '';
938  }
939  }
940 
950  public function addHeaderText( $msg, $section = null ) {
951  return $this->addHeaderHtml( $msg, $section );
952  }
953 
964  public function setHeaderText( $msg, $section = null ) {
965  return $this->setHeaderHtml( $msg, $section );
966  }
967 
977  public function getHeaderText( $section = null ) {
978  return $this->getHeaderHtml( $section );
979  }
980 
990  public function addFooterHtml( $html, $section = null ) {
991  if ( $section === null ) {
992  $this->mFooter .= $html;
993  } else {
994  if ( !isset( $this->mSectionFooters[$section] ) ) {
995  $this->mSectionFooters[$section] = '';
996  }
997  $this->mSectionFooters[$section] .= $html;
998  }
999 
1000  return $this;
1001  }
1002 
1012  public function setFooterHtml( $html, $section = null ) {
1013  if ( $section === null ) {
1014  $this->mFooter = $html;
1015  } else {
1016  $this->mSectionFooters[$section] = $html;
1017  }
1018 
1019  return $this;
1020  }
1021 
1029  public function getFooterHtml( $section = null ) {
1030  if ( $section === null ) {
1031  return $this->mFooter;
1032  } else {
1033  return $this->mSectionFooters[$section] ?? '';
1034  }
1035  }
1036 
1046  public function addFooterText( $msg, $section = null ) {
1047  return $this->addFooterHtml( $msg, $section );
1048  }
1049 
1060  public function setFooterText( $msg, $section = null ) {
1061  return $this->setFooterHtml( $msg, $section );
1062  }
1063 
1072  public function getFooterText( $section = null ) {
1073  return $this->getFooterHtml( $section );
1074  }
1075 
1084  public function addPostHtml( $html ) {
1085  $this->mPost .= $html;
1086 
1087  return $this;
1088  }
1089 
1098  public function setPostHtml( $html ) {
1099  $this->mPost = $html;
1100 
1101  return $this;
1102  }
1103 
1110  public function getPostHtml() {
1111  return $this->mPost;
1112  }
1113 
1122  public function addPostText( $msg ) {
1123  return $this->addPostHtml( $msg );
1124  }
1125 
1134  public function setPostText( $msg ) {
1135  return $this->setPostHtml( $msg );
1136  }
1137 
1147  public function addHiddenField( $name, $value, array $attribs = [] ) {
1148  $attribs += [ 'name' => $name ];
1149  $this->mHiddenFields[] = [ $value, $attribs ];
1150 
1151  return $this;
1152  }
1153 
1164  public function addHiddenFields( array $fields ) {
1165  foreach ( $fields as $name => $value ) {
1166  $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
1167  }
1168 
1169  return $this;
1170  }
1171 
1195  public function addButton( $data ) {
1196  if ( !is_array( $data ) ) {
1197  $args = func_get_args();
1198  if ( count( $args ) < 2 || count( $args ) > 4 ) {
1199  throw new InvalidArgumentException(
1200  'Incorrect number of arguments for deprecated calling style'
1201  );
1202  }
1203  $data = [
1204  'name' => $args[0],
1205  'value' => $args[1],
1206  'id' => $args[2] ?? null,
1207  'attribs' => $args[3] ?? null,
1208  ];
1209  } else {
1210  if ( !isset( $data['name'] ) ) {
1211  throw new InvalidArgumentException( 'A name is required' );
1212  }
1213  if ( !isset( $data['value'] ) ) {
1214  throw new InvalidArgumentException( 'A value is required' );
1215  }
1216  }
1217  $this->mButtons[] = $data + [
1218  'id' => null,
1219  'attribs' => null,
1220  'flags' => null,
1221  'framed' => true,
1222  ];
1223 
1224  return $this;
1225  }
1226 
1236  public function setTokenSalt( $salt ) {
1237  $this->mTokenSalt = $salt;
1238 
1239  return $this;
1240  }
1241 
1256  public function displayForm( $submitResult ) {
1257  $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1258  }
1259 
1264  private function getHiddenTitle(): string {
1265  if ( $this->hiddenTitleAddedToForm ) {
1266  return '';
1267  }
1268 
1269  $html = '';
1270  if ( $this->getMethod() === 'post' ||
1271  $this->getAction() === $this->getConfig()->get( MainConfigNames::Script )
1272  ) {
1273  $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1274  }
1275  $this->hiddenTitleAddedToForm = true;
1276  return $html;
1277  }
1278 
1289  public function getHTML( $submitResult ) {
1290  # For good measure (it is the default)
1291  $this->getOutput()->setPreventClickjacking( true );
1292  $this->getOutput()->addModules( 'mediawiki.htmlform' );
1293  $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1294 
1295  if ( $this->mCollapsible ) {
1296  // Preload jquery.makeCollapsible for mediawiki.htmlform
1297  $this->getOutput()->addModules( 'jquery.makeCollapsible' );
1298  }
1299 
1300  $html = ''
1301  . $this->getErrorsOrWarnings( $submitResult, 'error' )
1302  . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1303  . $this->getHeaderText()
1304  . $this->getHiddenTitle()
1305  . $this->getBody()
1306  . $this->getHiddenFields()
1307  . $this->getButtons()
1308  . $this->getFooterText();
1309 
1310  $html = $this->wrapForm( $html );
1311 
1312  return '' . $this->mPre . $html . $this->mPost;
1313  }
1314 
1322  public function setCollapsibleOptions( $collapsedByDefault = false ) {
1323  $this->mCollapsible = true;
1324  $this->mCollapsed = $collapsedByDefault;
1325  return $this;
1326  }
1327 
1333  protected function getFormAttributes() {
1334  # Use multipart/form-data
1335  $encType = $this->mUseMultipart
1336  ? 'multipart/form-data'
1337  : 'application/x-www-form-urlencoded';
1338  # Attributes
1339  $attribs = [
1340  'class' => 'mw-htmlform',
1341  'action' => $this->getAction(),
1342  'method' => $this->getMethod(),
1343  'enctype' => $encType,
1344  ];
1345  if ( $this->mId ) {
1346  $attribs['id'] = $this->mId;
1347  }
1348  if ( is_string( $this->mAutocomplete ) ) {
1349  $attribs['autocomplete'] = $this->mAutocomplete;
1350  }
1351  if ( $this->mName ) {
1352  $attribs['name'] = $this->mName;
1353  }
1354  if ( $this->needsJSForHtml5FormValidation() ) {
1355  $attribs['novalidate'] = true;
1356  }
1357  return $attribs;
1358  }
1359 
1368  public function wrapForm( $html ) {
1369  # Include a <fieldset> wrapper for style, if requested.
1370  if ( $this->mWrapperLegend !== false ) {
1371  $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
1372  $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1373  }
1374 
1375  return Html::rawElement(
1376  'form',
1377  $this->getFormAttributes(),
1378  $html
1379  );
1380  }
1381 
1386  public function getHiddenFields() {
1387  $html = '';
1388 
1389  // add the title as a hidden file if it hasn't been added yet and if it is necessary
1390  // added for backward compatibility with the previous version of this public method
1391  $html .= $this->getHiddenTitle();
1392 
1393  if ( $this->mFormIdentifier !== null ) {
1394  $html .= Html::hidden(
1395  'wpFormIdentifier',
1396  $this->mFormIdentifier
1397  ) . "\n";
1398  }
1399  if ( $this->getMethod() === 'post' ) {
1400  $html .= Html::hidden(
1401  'wpEditToken',
1402  $this->getUser()->getEditToken( $this->mTokenSalt ),
1403  [ 'id' => 'wpEditToken' ]
1404  ) . "\n";
1405  }
1406 
1407  foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1408  $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1409  }
1410 
1411  return $html;
1412  }
1413 
1419  public function getButtons() {
1420  $buttons = '';
1421  $useMediaWikiUIEverywhere =
1422  $this->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
1423 
1424  if ( $this->mShowSubmit ) {
1425  $attribs = [];
1426 
1427  if ( isset( $this->mSubmitID ) ) {
1428  $attribs['id'] = $this->mSubmitID;
1429  }
1430 
1431  if ( isset( $this->mSubmitName ) ) {
1432  $attribs['name'] = $this->mSubmitName;
1433  }
1434 
1435  if ( isset( $this->mSubmitTooltip ) ) {
1436  $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1437  }
1438 
1439  $attribs['class'] = [ 'mw-htmlform-submit' ];
1440 
1441  if ( $useMediaWikiUIEverywhere ) {
1442  foreach ( $this->mSubmitFlags as $flag ) {
1443  $attribs['class'][] = 'mw-ui-' . $flag;
1444  }
1445  $attribs['class'][] = 'mw-ui-button';
1446  }
1447 
1448  $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1449  }
1450 
1451  if ( $this->mShowReset ) {
1452  $buttons .= Html::element(
1453  'input',
1454  [
1455  'type' => 'reset',
1456  'value' => $this->msg( 'htmlform-reset' )->text(),
1457  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1458  ]
1459  ) . "\n";
1460  }
1461 
1462  if ( $this->mShowCancel ) {
1463  $target = $this->getCancelTargetURL();
1464  $buttons .= Html::element(
1465  'a',
1466  [
1467  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1468  'href' => $target,
1469  ],
1470  $this->msg( 'cancel' )->text()
1471  ) . "\n";
1472  }
1473 
1474  foreach ( $this->mButtons as $button ) {
1475  $attrs = [
1476  'type' => 'submit',
1477  'name' => $button['name'],
1478  'value' => $button['value']
1479  ];
1480 
1481  if ( isset( $button['label-message'] ) ) {
1482  $label = $this->getMessage( $button['label-message'] )->parse();
1483  } elseif ( isset( $button['label'] ) ) {
1484  $label = htmlspecialchars( $button['label'] );
1485  } elseif ( isset( $button['label-raw'] ) ) {
1486  $label = $button['label-raw'];
1487  } else {
1488  $label = htmlspecialchars( $button['value'] );
1489  }
1490 
1491  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1492  if ( $button['attribs'] ) {
1493  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1494  $attrs += $button['attribs'];
1495  }
1496 
1497  if ( isset( $button['id'] ) ) {
1498  $attrs['id'] = $button['id'];
1499  }
1500 
1501  if ( $useMediaWikiUIEverywhere ) {
1502  $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
1503  $attrs['class'][] = 'mw-ui-button';
1504  }
1505 
1506  $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
1507  }
1508 
1509  if ( !$buttons ) {
1510  return '';
1511  }
1512 
1513  return Html::rawElement( 'span',
1514  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1515  }
1516 
1522  public function getBody() {
1523  return $this->displaySection( $this->mFieldTree, $this->mTableId );
1524  }
1525 
1535  public function getErrorsOrWarnings( $elements, $elementsType ) {
1536  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1537  throw new DomainException( $elementsType . ' is not a valid type.' );
1538  }
1539  $elementstr = false;
1540  if ( $elements instanceof Status ) {
1541  [ $errorStatus, $warningStatus ] = $elements->splitByErrorType();
1542  $status = $elementsType === 'error' ? $errorStatus : $warningStatus;
1543  if ( $status->isGood() ) {
1544  $elementstr = '';
1545  } else {
1546  $elementstr = $status
1547  ->getMessage()
1548  ->setContext( $this )
1549  ->setInterfaceMessageFlag( true )
1550  ->parse();
1551  }
1552  } elseif ( $elementsType === 'error' ) {
1553  if ( is_array( $elements ) ) {
1554  $elementstr = $this->formatErrors( $elements );
1555  } elseif ( $elements && $elements !== true ) {
1556  $elementstr = (string)$elements;
1557  }
1558  }
1559 
1560  if ( !$elementstr ) {
1561  return '';
1562  } elseif ( $elementsType === 'error' ) {
1563  return Html::errorBox( $elementstr );
1564  } else { // $elementsType can only be 'warning'
1565  return Html::warningBox( $elementstr );
1566  }
1567  }
1568 
1576  public function formatErrors( $errors ) {
1577  $errorstr = '';
1578 
1579  foreach ( $errors as $error ) {
1580  $errorstr .= Html::rawElement(
1581  'li',
1582  [],
1583  $this->getMessage( $error )->parse()
1584  );
1585  }
1586 
1587  $errorstr = Html::rawElement( 'ul', [], $errorstr );
1588 
1589  return $errorstr;
1590  }
1591 
1599  public function setSubmitText( $t ) {
1600  $this->mSubmitText = $t;
1601 
1602  return $this;
1603  }
1604 
1611  public function setSubmitDestructive() {
1612  $this->mSubmitFlags = [ 'destructive', 'primary' ];
1613 
1614  return $this;
1615  }
1616 
1625  public function setSubmitTextMsg( $msg ) {
1626  if ( !$msg instanceof Message ) {
1627  $msg = $this->msg( $msg );
1628  }
1629  $this->setSubmitText( $msg->text() );
1630 
1631  return $this;
1632  }
1633 
1638  public function getSubmitText() {
1639  return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text();
1640  }
1641 
1647  public function setSubmitName( $name ) {
1648  $this->mSubmitName = $name;
1649 
1650  return $this;
1651  }
1652 
1658  public function setSubmitTooltip( $name ) {
1659  $this->mSubmitTooltip = $name;
1660 
1661  return $this;
1662  }
1663 
1672  public function setSubmitID( $t ) {
1673  $this->mSubmitID = $t;
1674 
1675  return $this;
1676  }
1677 
1693  public function setFormIdentifier( $ident ) {
1694  $this->mFormIdentifier = $ident;
1695 
1696  return $this;
1697  }
1698 
1709  public function suppressDefaultSubmit( $suppressSubmit = true ) {
1710  $this->mShowSubmit = !$suppressSubmit;
1711 
1712  return $this;
1713  }
1714 
1721  public function showCancel( $show = true ) {
1722  $this->mShowCancel = $show;
1723  return $this;
1724  }
1725 
1732  public function setCancelTarget( $target ) {
1733  if ( $target instanceof PageReference ) {
1734  $target = TitleValue::castPageToLinkTarget( $target );
1735  }
1736 
1737  $this->mCancelTarget = $target;
1738  return $this;
1739  }
1740 
1745  protected function getCancelTargetURL() {
1746  if ( is_string( $this->mCancelTarget ) ) {
1747  return $this->mCancelTarget;
1748  } else {
1749  // TODO: use a service to get the local URL for a LinkTarget, see T282283
1750  $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1751  return $target->getLocalURL();
1752  }
1753  }
1754 
1764  public function setTableId( $id ) {
1765  $this->mTableId = $id;
1766 
1767  return $this;
1768  }
1769 
1775  public function setId( $id ) {
1776  $this->mId = $id;
1777 
1778  return $this;
1779  }
1780 
1785  public function setName( $name ) {
1786  $this->mName = $name;
1787 
1788  return $this;
1789  }
1790 
1802  public function setWrapperLegend( $legend ) {
1803  $this->mWrapperLegend = $legend;
1804 
1805  return $this;
1806  }
1807 
1815  public function setWrapperAttributes( $attributes ) {
1816  $this->mWrapperAttributes = $attributes;
1817 
1818  return $this;
1819  }
1820 
1830  public function setWrapperLegendMsg( $msg ) {
1831  if ( !$msg instanceof Message ) {
1832  $msg = $this->msg( $msg );
1833  }
1834  $this->setWrapperLegend( $msg->text() );
1835 
1836  return $this;
1837  }
1838 
1848  public function setMessagePrefix( $p ) {
1849  $this->mMessagePrefix = $p;
1850 
1851  return $this;
1852  }
1853 
1861  public function setTitle( $t ) {
1862  // TODO: make mTitle a PageReference when we have a better way to get URLs, see T282283.
1863  $this->mTitle = Title::castFromPageReference( $t );
1864 
1865  return $this;
1866  }
1867 
1871  public function getTitle() {
1872  return $this->mTitle ?: $this->getContext()->getTitle();
1873  }
1874 
1882  public function setMethod( $method = 'post' ) {
1883  $this->mMethod = strtolower( $method );
1884 
1885  return $this;
1886  }
1887 
1891  public function getMethod() {
1892  return $this->mMethod;
1893  }
1894 
1905  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1906  return Xml::fieldset( $legend, $section, $attributes ) . "\n";
1907  }
1908 
1926  public function displaySection( $fields,
1927  $sectionName = '',
1928  $fieldsetIDPrefix = '',
1929  &$hasUserVisibleFields = false
1930  ) {
1931  if ( $this->mFieldData === null ) {
1932  throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1933  . 'You probably called displayForm() without calling prepareForm() first.' );
1934  }
1935 
1936  $displayFormat = $this->getDisplayFormat();
1937 
1938  $html = [];
1939  $subsectionHtml = '';
1940  $hasLabel = false;
1941 
1942  // Conveniently, PHP method names are case-insensitive.
1943  // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1944  $getFieldHtmlMethod = $displayFormat === 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
1945 
1946  foreach ( $fields as $key => $value ) {
1947  if ( $value instanceof HTMLFormField ) {
1948  $v = array_key_exists( $key, $this->mFieldData )
1949  ? $this->mFieldData[$key]
1950  : $value->getDefault();
1951 
1952  $retval = $value->$getFieldHtmlMethod( $v ?? '' );
1953 
1954  // check, if the form field should be added to
1955  // the output.
1956  if ( $value->hasVisibleOutput() ) {
1957  $html[] = $retval;
1958 
1959  $labelValue = trim( $value->getLabel() );
1960  if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
1961  $hasLabel = true;
1962  }
1963 
1964  $hasUserVisibleFields = true;
1965  }
1966  } elseif ( is_array( $value ) ) {
1967  $subsectionHasVisibleFields = false;
1968  $section =
1969  $this->displaySection( $value,
1970  "mw-htmlform-$key",
1971  "$fieldsetIDPrefix$key-",
1972  $subsectionHasVisibleFields );
1973 
1974  if ( $subsectionHasVisibleFields === true ) {
1975  // Display the section with various niceties.
1976  $hasUserVisibleFields = true;
1977 
1978  $legend = $this->getLegend( $key );
1979 
1980  $section = $this->getHeaderText( $key ) .
1981  $section .
1982  $this->getFooterText( $key );
1983 
1984  $attributes = [];
1985  if ( $fieldsetIDPrefix ) {
1986  $attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1987  }
1988  $subsectionHtml .= $this->wrapFieldSetSection(
1989  $legend, $section, $attributes, $fields === $this->mFieldTree
1990  );
1991  } else {
1992  // Just return the inputs, nothing fancy.
1993  $subsectionHtml .= $section;
1994  }
1995  }
1996  }
1997 
1998  $html = $this->formatSection( $html, $sectionName, $hasLabel );
1999 
2000  if ( $subsectionHtml ) {
2001  if ( $this->mSubSectionBeforeFields ) {
2002  return $subsectionHtml . "\n" . $html;
2003  } else {
2004  return $html . "\n" . $subsectionHtml;
2005  }
2006  } else {
2007  return $html;
2008  }
2009  }
2010 
2019  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
2020  if ( !$fieldsHtml ) {
2021  // Do not generate any wrappers for empty sections. Sections may be empty if they only have
2022  // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
2023  return '';
2024  }
2025 
2026  $displayFormat = $this->getDisplayFormat();
2027  $html = implode( '', $fieldsHtml );
2028 
2029  if ( $displayFormat === 'raw' ) {
2030  return $html;
2031  }
2032 
2033  $classes = [];
2034 
2035  if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
2036  $classes[] = 'mw-htmlform-nolabel';
2037  }
2038 
2039  $attribs = [ 'class' => $classes ];
2040 
2041  if ( $sectionName ) {
2042  $attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
2043  }
2044 
2045  if ( $displayFormat === 'table' ) {
2046  return Html::rawElement( 'table',
2047  $attribs,
2048  Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
2049  } elseif ( $displayFormat === 'inline' ) {
2050  return Html::rawElement( 'span', $attribs, "\n$html\n" );
2051  } else {
2052  return Html::rawElement( 'div', $attribs, "\n$html\n" );
2053  }
2054  }
2055 
2059  public function loadData() {
2060  $this->prepareForm();
2061  }
2062 
2066  protected function loadFieldData() {
2067  $fieldData = [];
2068  $request = $this->getRequest();
2069 
2070  foreach ( $this->mFlatFields as $fieldname => $field ) {
2071  if ( $field->skipLoadData( $request ) ) {
2072  continue;
2073  }
2074  if ( $field->mParams['disabled'] ?? false ) {
2075  $fieldData[$fieldname] = $field->getDefault();
2076  } else {
2077  $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2078  }
2079  }
2080 
2081  // Reset to default for fields that are supposed to be disabled.
2082  // FIXME: Handle dependency chains, fields that a field checks on may need a reset too.
2083  foreach ( $fieldData as $name => &$value ) {
2084  $field = $this->mFlatFields[$name];
2085  if ( $field->isDisabled( $fieldData ) ) {
2086  $value = $field->getDefault();
2087  }
2088  }
2089 
2090  # Filter data.
2091  foreach ( $fieldData as $name => &$value ) {
2092  $field = $this->mFlatFields[$name];
2093  $value = $field->filter( $value, $fieldData );
2094  }
2095 
2096  $this->mFieldData = $fieldData;
2097  }
2098 
2106  public function suppressReset( $suppressReset = true ) {
2107  $this->mShowReset = !$suppressReset;
2108 
2109  return $this;
2110  }
2111 
2122  public function filterDataForSubmit( $data ) {
2123  return $data;
2124  }
2125 
2135  public function getLegend( $key ) {
2136  return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text();
2137  }
2138 
2149  public function setAction( $action ) {
2150  $this->mAction = $action;
2151 
2152  return $this;
2153  }
2154 
2162  public function getAction() {
2163  // If an action is already provided, return it
2164  if ( $this->mAction !== false ) {
2165  return $this->mAction;
2166  }
2167 
2168  $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2169  // Check whether we are in GET mode and the ArticlePath contains a "?"
2170  // meaning that getLocalURL() would return something like "index.php?title=...".
2171  // As browser remove the query string before submitting GET forms,
2172  // it means that the title would be lost. In such case use script path instead
2173  // and put title in an hidden field (see getHiddenFields()).
2174  if ( str_contains( $articlePath, '?' ) && $this->getMethod() === 'get' ) {
2175  return $this->getConfig()->get( MainConfigNames::Script );
2176  }
2177 
2178  return $this->getTitle()->getLocalURL();
2179  }
2180 
2191  public function setAutocomplete( $autocomplete ) {
2192  $this->mAutocomplete = $autocomplete;
2193 
2194  return $this;
2195  }
2196 
2203  protected function getMessage( $value ) {
2204  return Message::newFromSpecifier( $value )->setContext( $this );
2205  }
2206 
2216  public function needsJSForHtml5FormValidation() {
2217  foreach ( $this->mFlatFields as $field ) {
2218  if ( $field->needsJSForHtml5FormValidation() ) {
2219  return true;
2220  }
2221  }
2222  return false;
2223  }
2224 }
getUser()
getContext()
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
The parent class to generate form fields.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:151
needsJSForHtml5FormValidation()
Whether this form, with its current fields, requires the user agent to have JavaScript enabled for th...
Definition: HTMLForm.php:2216
setSubmitCallback( $cb)
Set a callback to a function to do something with the form once it's been successfully validated.
Definition: HTMLForm.php:778
setHeaderText( $msg, $section=null)
Set header text, inside the form.
Definition: HTMLForm.php:964
$mSubmitCallback
Definition: HTMLForm.php:217
string false $mAction
Form action URL.
Definition: HTMLForm.php:250
displayForm( $submitResult)
Display the form (sending to the context's OutputPage object), with an appropriate error message or s...
Definition: HTMLForm.php:1256
$mWasSubmitted
Definition: HTMLForm.php:243
string null $mAutocomplete
Form attribute autocomplete.
Definition: HTMLForm.php:271
setAction( $action)
Set the value for the action attribute of the form.
Definition: HTMLForm.php:2149
string array $mTokenSalt
Salt for the edit token.
Definition: HTMLForm.php:292
setFooterHtml( $html, $section=null)
Set footer HTML, inside the form.
Definition: HTMLForm.php:1012
string[] $mSubmitFlags
Definition: HTMLForm.php:213
addPreHtml( $html)
Add HTML to introductory message.
Definition: HTMLForm.php:833
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1638
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1882
setValidationErrorMessage( $msg)
Set a message to display on a validation error.
Definition: HTMLForm.php:793
getCancelTargetURL()
Definition: HTMLForm.php:1745
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:2203
getHeaderHtml( $section=null)
Get header HTML.
Definition: HTMLForm.php:933
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:893
setAutocomplete( $autocomplete)
Set the value for the autocomplete attribute of the form.
Definition: HTMLForm.php:2191
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
Definition: HTMLForm.php:553
setSubmitName( $name)
Definition: HTMLForm.php:1647
setTitle( $t)
Set the title for form submission.
Definition: HTMLForm.php:1861
static getClassFromDescriptor( $fieldname, &$descriptor)
Get the HTMLFormField subclass for this descriptor.
Definition: HTMLForm.php:522
array $availableSubclassDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:326
string $displayFormat
Format in which to display form.
Definition: HTMLForm.php:309
addPostHtml( $html)
Add HTML to the end of the display.
Definition: HTMLForm.php:1084
bool $mSubSectionBeforeFields
If true, sections that contain both fields and subsections will render their subsections before their...
Definition: HTMLForm.php:302
$mUseMultipart
Definition: HTMLForm.php:273
setTableId( $id)
Set the id of the <table> or outermost <div> element.
Definition: HTMLForm.php:1764
getHTML( $submitResult)
Returns the raw HTML generated by the form.
Definition: HTMLForm.php:1289
getLegend( $key)
Get a string to go in the "<legend>" of a section fieldset.
Definition: HTMLForm.php:2135
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1625
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.
Definition: HTMLForm.php:1905
$mWrapperLegend
Definition: HTMLForm.php:285
filterDataForSubmit( $data)
Overload this if you want to apply special filtration routines to the form as a whole,...
Definition: HTMLForm.php:2122
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1830
setId( $id)
Definition: HTMLForm.php:1775
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1802
formatErrors( $errors)
Format a stack of error messages into a single HTML string.
Definition: HTMLForm.php:1576
setDisplayFormat( $format)
Set format in which to display the form.
Definition: HTMLForm.php:465
addButton( $data)
Add a button to the form.
Definition: HTMLForm.php:1195
addFooterHtml( $html, $section=null)
Add footer HTML, inside the form.
Definition: HTMLForm.php:990
setPreHtml( $html)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:819
getPreHtml()
Get the introductory message HTML.
Definition: HTMLForm.php:845
setPostHtml( $html)
Set HTML at the end of the display.
Definition: HTMLForm.php:1098
getPreText()
Get the introductory message HTML.
Definition: HTMLForm.php:880
static string[] $typeMappings
A mapping of 'type' inputs onto standard HTMLFormField subclasses.
Definition: HTMLForm.php:155
getHiddenFields()
Get the hidden fields that should go inside the form.
Definition: HTMLForm.php:1386
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
Definition: HTMLForm.php:1611
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1709
setSubmitID( $t)
Set the id for the submit button.
Definition: HTMLForm.php:1672
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:501
$mCancelTarget
Definition: HTMLForm.php:215
getAction()
Get the value for the action attribute of the form.
Definition: HTMLForm.php:2162
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition: HTMLForm.php:635
hasField( $fieldname)
Definition: HTMLForm.php:439
$mWrapperAttributes
Definition: HTMLForm.php:286
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition: HTMLForm.php:1046
addPostText( $msg)
Add text to the end of the display.
Definition: HTMLForm.php:1122
$mSectionHeaders
Definition: HTMLForm.php:227
getFooterText( $section=null)
Get footer text.
Definition: HTMLForm.php:1072
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:950
setWrapperAttributes( $attributes)
For internal use only.
Definition: HTMLForm.php:1815
prepareForm()
Prepare form for submission.
Definition: HTMLForm.php:578
array[] $mValidationErrorMessage
Definition: HTMLForm.php:222
setCollapsibleOptions( $collapsedByDefault=false)
Enable collapsible mode, and set whether the form is collapsed by default.
Definition: HTMLForm.php:1322
wasSubmitted()
Test whether the form was considered to have been submitted or not, i.e.
Definition: HTMLForm.php:764
getHeaderText( $section=null)
Get header text.
Definition: HTMLForm.php:977
$mMessagePrefix
Definition: HTMLForm.php:205
array[] $mButtons
Definition: HTMLForm.php:283
$mFormIdentifier
Definition: HTMLForm.php:239
$mSubmitTooltip
Definition: HTMLForm.php:237
tryAuthorizedSubmit()
Try submitting, with edit token check first.
Definition: HTMLForm.php:596
setSubmitTooltip( $name)
Definition: HTMLForm.php:1658
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1926
array[] $mHiddenFields
Definition: HTMLForm.php:278
setPreText( $msg)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:857
bool $mCollapsible
Whether the form can be collapsed.
Definition: HTMLForm.php:257
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:1147
$mSectionFooters
Definition: HTMLForm.php:228
setFooterText( $msg, $section=null)
Set footer text, inside the form.
Definition: HTMLForm.php:1060
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1848
getField( $fieldname)
Definition: HTMLForm.php:448
bool $mCollapsed
Whether the form is collapsed by default.
Definition: HTMLForm.php:264
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Definition: HTMLForm.php:1368
getFormAttributes()
Get HTML attributes for the <form> tag.
Definition: HTMLForm.php:1333
getBody()
Get the whole body of the form.
Definition: HTMLForm.php:1522
showAlways()
Same as self::show with the difference, that the form will be added to the output,...
Definition: HTMLForm.php:653
getFooterHtml( $section=null)
Get footer HTML.
Definition: HTMLForm.php:1029
loadFieldData()
Load data of form fields from the request.
Definition: HTMLForm.php:2066
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition: HTMLForm.php:915
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1236
__construct( $descriptor, IContextSource $context, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
Definition: HTMLForm.php:376
addPreText( $msg)
Add HTML to introductory message.
Definition: HTMLForm.php:869
setFormIdentifier( $ident)
Set an internal identifier for this form.
Definition: HTMLForm.php:1693
setName( $name)
Definition: HTMLForm.php:1785
suppressReset( $suppressReset=true)
Stop a reset button being shown for this form.
Definition: HTMLForm.php:2106
setPostText( $msg)
Set text at the end of the display.
Definition: HTMLForm.php:1134
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: HTMLForm.php:2019
setCancelTarget( $target)
Sets the target where the user is redirected to after clicking cancel.
Definition: HTMLForm.php:1732
array $availableDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:315
showCancel( $show=true)
Show a cancel button (or prevent it).
Definition: HTMLForm.php:1721
Title null $mTitle
Definition: HTMLForm.php:241
setSubmitText( $t)
Set the text for the submit button.
Definition: HTMLForm.php:1599
setIntro( $msg)
Set the introductory message, overwriting any existing message.
Definition: HTMLForm.php:807
getButtons()
Get the submit and (potentially) reset buttons.
Definition: HTMLForm.php:1419
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:350
trySubmit()
Validate all the fields, and call the submission callback function if everything is kosher.
Definition: HTMLForm.php:675
HTMLFormField[] $mFlatFields
Definition: HTMLForm.php:208
getErrorsOrWarnings( $elements, $elementsType)
Returns a formatted list of errors or warnings from the given elements.
Definition: HTMLForm.php:1535
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:1164
getPostHtml()
Get HTML at the end of the display.
Definition: HTMLForm.php:1110
addFields( $descriptor)
Add fields to the form.
Definition: HTMLForm.php:402
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static warningBox( $html, $className='')
Return a warning box.
Definition: Html.php:775
static errorBox( $html, $heading='', $className='')
Return an error box.
Definition: Html.php:788
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:851
MediaWiki exception.
Definition: MWException.php:30
Some internal bits split of from Skin.php.
Definition: Linker.php:65
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.
Definition: Message.php:143
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:425
Compact stacked vertical format for forms, implemented using OOUI widgets.
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...
Definition: Sanitizer.php:946
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:46
isGood()
Returns whether the operation completed and didn't have any error or warnings.
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:46
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:64
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
Definition: TitleValue.php:118
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
Definition: Title.php:335
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:703
static castFromLinkTarget( $linkTarget)
Same as newFromLinkTarget, but if passed null, returns null.
Definition: Title.php:309
Compact stacked vertical format for forms.
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition: Xml.php:466
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition: Xml.php:625
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.