MediaWiki  master
HTMLForm.php
Go to the documentation of this file.
1 <?php
2 
24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
31 
155 class HTMLForm extends ContextSource {
156  use ProtectedHookAccessorTrait;
157 
159  public static $typeMappings = [
160  'api' => HTMLApiField::class,
161  'text' => HTMLTextField::class,
162  'textwithbutton' => HTMLTextFieldWithButton::class,
163  'textarea' => HTMLTextAreaField::class,
164  'select' => HTMLSelectField::class,
165  'combobox' => HTMLComboboxField::class,
166  'radio' => HTMLRadioField::class,
167  'multiselect' => HTMLMultiSelectField::class,
168  'limitselect' => HTMLSelectLimitField::class,
169  'check' => HTMLCheckField::class,
170  'toggle' => HTMLCheckField::class,
171  'int' => HTMLIntField::class,
172  'file' => HTMLFileField::class,
173  'float' => HTMLFloatField::class,
174  'info' => HTMLInfoField::class,
175  'selectorother' => HTMLSelectOrOtherField::class,
176  'selectandother' => HTMLSelectAndOtherField::class,
177  'namespaceselect' => HTMLSelectNamespace::class,
178  'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
179  'tagfilter' => HTMLTagFilter::class,
180  'sizefilter' => HTMLSizeFilterField::class,
181  'submit' => HTMLSubmitField::class,
182  'hidden' => HTMLHiddenField::class,
183  'edittools' => HTMLEditTools::class,
184  'checkmatrix' => HTMLCheckMatrix::class,
185  'cloner' => HTMLFormFieldCloner::class,
186  'autocompleteselect' => HTMLAutoCompleteSelectField::class,
187  'language' => HTMLSelectLanguageField::class,
188  'date' => HTMLDateTimeField::class,
189  'time' => HTMLDateTimeField::class,
190  'datetime' => HTMLDateTimeField::class,
191  'expiry' => HTMLExpiryField::class,
192  'timezone' => HTMLTimezoneField::class,
193  // HTMLTextField will output the correct type="" attribute automagically.
194  // There are about four zillion other HTML5 input types, like range, but
195  // we don't use those at the moment, so no point in adding all of them.
196  'email' => HTMLTextField::class,
197  'password' => HTMLTextField::class,
198  'url' => HTMLTextField::class,
199  'title' => HTMLTitleTextField::class,
200  'user' => HTMLUserTextField::class,
201  'tagmultiselect' => HTMLTagMultiselectField::class,
202  'usersmultiselect' => HTMLUsersMultiselectField::class,
203  'titlesmultiselect' => HTMLTitlesMultiselectField::class,
204  'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
205  ];
206 
207  public $mFieldData;
208 
209  protected $mMessagePrefix;
210 
212  protected $mFlatFields = [];
213  protected $mFieldTree = [];
214  protected $mShowReset = false;
215  protected $mShowSubmit = true;
217  protected $mSubmitFlags = [ 'primary', 'progressive' ];
218  protected $mShowCancel = false;
219  protected $mCancelTarget;
220 
221  protected $mSubmitCallback;
227 
228  protected $mPre = '';
229  protected $mHeader = '';
230  protected $mFooter = '';
231  protected $mSectionHeaders = [];
232  protected $mSectionFooters = [];
233  protected $mPost = '';
234  protected $mId;
235  protected $mName;
236  protected $mTableId = '';
237 
238  protected $mSubmitID;
239  protected $mSubmitName;
240  protected $mSubmitText;
241  protected $mSubmitTooltip;
242 
243  protected $mFormIdentifier;
245  protected $mTitle;
246  protected $mMethod = 'post';
247  protected $mWasSubmitted = false;
248 
254  protected $mAction = false;
255 
261  protected $mCollapsible = false;
262 
268  protected $mCollapsed = false;
269 
275  protected $mAutocomplete = null;
276 
277  protected $mUseMultipart = false;
282  protected $mHiddenFields = [];
287  protected $mButtons = [];
288 
289  protected $mWrapperLegend = false;
290  protected $mWrapperAttributes = [];
291 
296  protected $mTokenSalt = '';
297 
306  protected $mSubSectionBeforeFields = true;
307 
313  protected $displayFormat = 'table';
314 
320  'table',
321  'div',
322  'raw',
323  'inline',
324  ];
325 
331  'vform',
332  'ooui',
333  ];
334 
339  private $hiddenTitleAddedToForm = false;
340 
354  public static function factory(
355  $displayFormat, $descriptor, IContextSource $context, $messagePrefix = ''
356  ) {
357  switch ( $displayFormat ) {
358  case 'vform':
359  return new VFormHTMLForm( $descriptor, $context, $messagePrefix );
360  case 'ooui':
361  return new OOUIHTMLForm( $descriptor, $context, $messagePrefix );
362  default:
363  $form = new self( $descriptor, $context, $messagePrefix );
364  $form->setDisplayFormat( $displayFormat );
365  return $form;
366  }
367  }
368 
380  public function __construct(
381  $descriptor, IContextSource $context, $messagePrefix = ''
382  ) {
383  $this->setContext( $context );
384  $this->mMessagePrefix = $messagePrefix;
385 
386  // Evil hack for mobile :(
387  if (
388  !$this->getConfig()->get( MainConfigNames::HTMLFormAllowTableFormat )
389  && $this->displayFormat === 'table'
390  ) {
391  $this->displayFormat = 'div';
392  }
393 
394  $this->addFields( $descriptor );
395  }
396 
406  public function addFields( $descriptor ) {
407  $loadedDescriptor = [];
408 
409  foreach ( $descriptor as $fieldname => $info ) {
410 
411  $section = $info['section'] ?? '';
412 
413  if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
414  $this->mUseMultipart = true;
415  }
416 
417  $field = static::loadInputFromParameters( $fieldname, $info, $this );
418 
419  $setSection =& $loadedDescriptor;
420  if ( $section ) {
421  foreach ( explode( '/', $section ) as $newName ) {
422  if ( !isset( $setSection[$newName] ) ) {
423  $setSection[$newName] = [];
424  }
425 
426  $setSection =& $setSection[$newName];
427  }
428  }
429 
430  $setSection[$fieldname] = $field;
431  $this->mFlatFields[$fieldname] = $field;
432  }
433 
434  $this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor );
435 
436  return $this;
437  }
438 
443  public function hasField( $fieldname ) {
444  return isset( $this->mFlatFields[$fieldname] );
445  }
446 
452  public function getField( $fieldname ) {
453  if ( !$this->hasField( $fieldname ) ) {
454  throw new DomainException( __METHOD__ . ': no field named ' . $fieldname );
455  }
456  return $this->mFlatFields[$fieldname];
457  }
458 
469  public function setDisplayFormat( $format ) {
470  if (
471  in_array( $format, $this->availableSubclassDisplayFormats, true ) ||
472  in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true )
473  ) {
474  throw new MWException( 'Cannot change display format after creation, ' .
475  'use HTMLForm::factory() instead' );
476  }
477 
478  if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
479  throw new MWException( 'Display format must be one of ' .
480  print_r(
481  array_merge(
482  $this->availableDisplayFormats,
483  $this->availableSubclassDisplayFormats
484  ),
485  true
486  ) );
487  }
488 
489  // Evil hack for mobile :(
490  if ( !$this->getConfig()->get( MainConfigNames::HTMLFormAllowTableFormat ) &&
491  $format === 'table' ) {
492  $format = 'div';
493  }
494 
495  $this->displayFormat = $format;
496 
497  return $this;
498  }
499 
505  public function getDisplayFormat() {
506  return $this->displayFormat;
507  }
508 
526  public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
527  if ( isset( $descriptor['class'] ) ) {
528  $class = $descriptor['class'];
529  } elseif ( isset( $descriptor['type'] ) ) {
530  $class = static::$typeMappings[$descriptor['type']];
531  $descriptor['class'] = $class;
532  } else {
533  $class = null;
534  }
535 
536  if ( !$class ) {
537  throw new MWException( "Descriptor with no class for $fieldname: "
538  . print_r( $descriptor, true ) );
539  }
540 
541  return $class;
542  }
543 
557  public static function loadInputFromParameters( $fieldname, $descriptor,
558  HTMLForm $parent = null
559  ) {
560  $class = static::getClassFromDescriptor( $fieldname, $descriptor );
561 
562  $descriptor['fieldname'] = $fieldname;
563  if ( $parent ) {
564  $descriptor['parent'] = $parent;
565  }
566 
567  # @todo This will throw a fatal error whenever someone try to use
568  # 'class' to feed a CSS class instead of 'cssclass'. Would be
569  # great to avoid the fatal error and show a nice error.
570  return new $class( $descriptor );
571  }
572 
582  public function prepareForm() {
583  # Load data from the request.
584  if (
585  $this->mFormIdentifier === null ||
586  $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
587  ) {
588  $this->loadFieldData();
589  } else {
590  $this->mFieldData = [];
591  }
592 
593  return $this;
594  }
595 
600  public function tryAuthorizedSubmit() {
601  $result = false;
602 
603  if ( $this->mFormIdentifier === null ) {
604  $identOkay = true;
605  } else {
606  $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier;
607  }
608 
609  $tokenOkay = false;
610  if ( $this->getMethod() !== 'post' ) {
611  $tokenOkay = true; // no session check needed
612  } elseif ( $this->getRequest()->wasPosted() ) {
613  $editToken = $this->getRequest()->getVal( 'wpEditToken' );
614  if ( $this->getUser()->isRegistered() || $editToken !== null ) {
615  // Session tokens for logged-out users have no security value.
616  // However, if the user gave one, check it in order to give a nice
617  // "session expired" error instead of "permission denied" or such.
618  $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
619  } else {
620  $tokenOkay = true;
621  }
622  }
623 
624  if ( $tokenOkay && $identOkay ) {
625  $this->mWasSubmitted = true;
626  $result = $this->trySubmit();
627  }
628 
629  return $result;
630  }
631 
639  public function show() {
640  $this->prepareForm();
641 
642  $result = $this->tryAuthorizedSubmit();
643  if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
644  return $result;
645  }
646 
647  $this->displayForm( $result );
648 
649  return false;
650  }
651 
657  public function showAlways() {
658  $this->prepareForm();
659 
660  $result = $this->tryAuthorizedSubmit();
661 
662  $this->displayForm( $result );
663 
664  return $result;
665  }
666 
679  public function trySubmit() {
680  $valid = true;
681  $hoistedErrors = Status::newGood();
682  if ( $this->mValidationErrorMessage ) {
683  foreach ( $this->mValidationErrorMessage as $error ) {
684  $hoistedErrors->fatal( ...$error );
685  }
686  } else {
687  $hoistedErrors->fatal( 'htmlform-invalid-input' );
688  }
689 
690  $this->mWasSubmitted = true;
691 
692  # Check for cancelled submission
693  foreach ( $this->mFlatFields as $fieldname => $field ) {
694  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
695  continue;
696  }
697  if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
698  $this->mWasSubmitted = false;
699  return false;
700  }
701  }
702 
703  # Check for validation
704  $hasNonDefault = false;
705  foreach ( $this->mFlatFields as $fieldname => $field ) {
706  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
707  continue;
708  }
709  $hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault();
710  if ( $field->isDisabled( $this->mFieldData ) ) {
711  continue;
712  }
713  $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
714  if ( $res !== true ) {
715  $valid = false;
716  if ( $res !== false && !$field->canDisplayErrors() ) {
717  if ( is_string( $res ) ) {
718  $hoistedErrors->fatal( 'rawmessage', $res );
719  } else {
720  $hoistedErrors->fatal( $res );
721  }
722  }
723  }
724  }
725 
726  if ( !$valid ) {
727  // Treat as not submitted if got nothing from the user on GET forms.
728  if ( !$hasNonDefault && $this->getMethod() === 'get' &&
729  ( $this->mFormIdentifier === null ||
730  $this->getRequest()->getCheck( 'wpFormIdentifier' ) )
731  ) {
732  $this->mWasSubmitted = false;
733  return false;
734  }
735  return $hoistedErrors;
736  }
737 
738  $callback = $this->mSubmitCallback;
739  if ( !is_callable( $callback ) ) {
740  throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
741  'setSubmitCallback() to set one.' );
742  }
743 
744  $data = $this->filterDataForSubmit( $this->mFieldData );
745 
746  $res = call_user_func( $callback, $data, $this );
747  if ( $res === false ) {
748  $this->mWasSubmitted = false;
749  } elseif ( $res instanceof StatusValue ) {
750  // DWIM - callbacks are not supposed to return a StatusValue but it's easy to mix up.
751  $res = Status::wrap( $res );
752  }
753 
754  return $res;
755  }
756 
768  public function wasSubmitted() {
769  return $this->mWasSubmitted;
770  }
771 
782  public function setSubmitCallback( $cb ) {
783  $this->mSubmitCallback = $cb;
784 
785  return $this;
786  }
787 
797  public function setValidationErrorMessage( $msg ) {
798  $this->mValidationErrorMessage = $msg;
799 
800  return $this;
801  }
802 
811  public function setIntro( $msg ) {
812  return $this->setPreHtml( $msg );
813  }
814 
823  public function setPreHtml( $html ) {
824  $this->mPre = $html;
825 
826  return $this;
827  }
828 
837  public function addPreHtml( $html ) {
838  $this->mPre .= $html;
839 
840  return $this;
841  }
842 
849  public function getPreHtml() {
850  return $this->mPre;
851  }
852 
861  public function setPreText( $msg ) {
862  return $this->setPreHtml( $msg );
863  }
864 
873  public function addPreText( $msg ) {
874  return $this->addPreHtml( $msg );
875  }
876 
884  public function getPreText() {
885  return $this->getPreHtml();
886  }
887 
897  public function addHeaderHtml( $html, $section = null ) {
898  if ( $section === null ) {
899  $this->mHeader .= $html;
900  } else {
901  if ( !isset( $this->mSectionHeaders[$section] ) ) {
902  $this->mSectionHeaders[$section] = '';
903  }
904  $this->mSectionHeaders[$section] .= $html;
905  }
906 
907  return $this;
908  }
909 
919  public function setHeaderHtml( $html, $section = null ) {
920  if ( $section === null ) {
921  $this->mHeader = $html;
922  } else {
923  $this->mSectionHeaders[$section] = $html;
924  }
925 
926  return $this;
927  }
928 
937  public function getHeaderHtml( $section = null ) {
938  if ( $section === null ) {
939  return $this->mHeader;
940  } else {
941  return $this->mSectionHeaders[$section] ?? '';
942  }
943  }
944 
954  public function addHeaderText( $msg, $section = null ) {
955  return $this->addHeaderHtml( $msg, $section );
956  }
957 
968  public function setHeaderText( $msg, $section = null ) {
969  return $this->setHeaderHtml( $msg, $section );
970  }
971 
981  public function getHeaderText( $section = null ) {
982  return $this->getHeaderHtml( $section );
983  }
984 
994  public function addFooterHtml( $html, $section = null ) {
995  if ( $section === null ) {
996  $this->mFooter .= $html;
997  } else {
998  if ( !isset( $this->mSectionFooters[$section] ) ) {
999  $this->mSectionFooters[$section] = '';
1000  }
1001  $this->mSectionFooters[$section] .= $html;
1002  }
1003 
1004  return $this;
1005  }
1006 
1016  public function setFooterHtml( $html, $section = null ) {
1017  if ( $section === null ) {
1018  $this->mFooter = $html;
1019  } else {
1020  $this->mSectionFooters[$section] = $html;
1021  }
1022 
1023  return $this;
1024  }
1025 
1033  public function getFooterHtml( $section = null ) {
1034  if ( $section === null ) {
1035  return $this->mFooter;
1036  } else {
1037  return $this->mSectionFooters[$section] ?? '';
1038  }
1039  }
1040 
1050  public function addFooterText( $msg, $section = null ) {
1051  return $this->addFooterHtml( $msg, $section );
1052  }
1053 
1064  public function setFooterText( $msg, $section = null ) {
1065  return $this->setFooterHtml( $msg, $section );
1066  }
1067 
1076  public function getFooterText( $section = null ) {
1077  return $this->getFooterHtml( $section );
1078  }
1079 
1088  public function addPostHtml( $html ) {
1089  $this->mPost .= $html;
1090 
1091  return $this;
1092  }
1093 
1102  public function setPostHtml( $html ) {
1103  $this->mPost = $html;
1104 
1105  return $this;
1106  }
1107 
1114  public function getPostHtml() {
1115  return $this->mPost;
1116  }
1117 
1126  public function addPostText( $msg ) {
1127  return $this->addPostHtml( $msg );
1128  }
1129 
1138  public function setPostText( $msg ) {
1139  return $this->setPostHtml( $msg );
1140  }
1141 
1152  public function addHiddenField( $name, $value, array $attribs = [] ) {
1153  if ( !is_array( $value ) ) {
1154  // Per WebRequest::getVal: Array values are discarded for security reasons.
1155  $attribs += [ 'name' => $name ];
1156  $this->mHiddenFields[] = [ $value, $attribs ];
1157  }
1158 
1159  return $this;
1160  }
1161 
1173  public function addHiddenFields( array $fields ) {
1174  foreach ( $fields as $name => $value ) {
1175  if ( is_array( $value ) ) {
1176  // Per WebRequest::getVal: Array values are discarded for security reasons.
1177  continue;
1178  }
1179  $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
1180  }
1181 
1182  return $this;
1183  }
1184 
1208  public function addButton( $data ) {
1209  if ( !is_array( $data ) ) {
1210  $args = func_get_args();
1211  if ( count( $args ) < 2 || count( $args ) > 4 ) {
1212  throw new InvalidArgumentException(
1213  'Incorrect number of arguments for deprecated calling style'
1214  );
1215  }
1216  $data = [
1217  'name' => $args[0],
1218  'value' => $args[1],
1219  'id' => $args[2] ?? null,
1220  'attribs' => $args[3] ?? null,
1221  ];
1222  } else {
1223  if ( !isset( $data['name'] ) ) {
1224  throw new InvalidArgumentException( 'A name is required' );
1225  }
1226  if ( !isset( $data['value'] ) ) {
1227  throw new InvalidArgumentException( 'A value is required' );
1228  }
1229  }
1230  $this->mButtons[] = $data + [
1231  'id' => null,
1232  'attribs' => null,
1233  'flags' => null,
1234  'framed' => true,
1235  ];
1236 
1237  return $this;
1238  }
1239 
1249  public function setTokenSalt( $salt ) {
1250  $this->mTokenSalt = $salt;
1251 
1252  return $this;
1253  }
1254 
1269  public function displayForm( $submitResult ) {
1270  $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1271  }
1272 
1277  private function getHiddenTitle(): string {
1278  if ( $this->hiddenTitleAddedToForm ) {
1279  return '';
1280  }
1281 
1282  $html = '';
1283  if ( $this->getMethod() === 'post' ||
1284  $this->getAction() === $this->getConfig()->get( MainConfigNames::Script )
1285  ) {
1286  $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1287  }
1288  $this->hiddenTitleAddedToForm = true;
1289  return $html;
1290  }
1291 
1302  public function getHTML( $submitResult ) {
1303  # For good measure (it is the default)
1304  $this->getOutput()->setPreventClickjacking( true );
1305  $this->getOutput()->addModules( 'mediawiki.htmlform' );
1306  $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1307 
1308  if ( $this->mCollapsible ) {
1309  // Preload jquery.makeCollapsible for mediawiki.htmlform
1310  $this->getOutput()->addModules( 'jquery.makeCollapsible' );
1311  }
1312 
1313  $html = ''
1314  . $this->getErrorsOrWarnings( $submitResult, 'error' )
1315  . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1316  . $this->getHeaderText()
1317  . $this->getHiddenTitle()
1318  . $this->getBody()
1319  . $this->getHiddenFields()
1320  . $this->getButtons()
1321  . $this->getFooterText();
1322 
1323  $html = $this->wrapForm( $html );
1324 
1325  return '' . $this->mPre . $html . $this->mPost;
1326  }
1327 
1335  public function setCollapsibleOptions( $collapsedByDefault = false ) {
1336  $this->mCollapsible = true;
1337  $this->mCollapsed = $collapsedByDefault;
1338  return $this;
1339  }
1340 
1346  protected function getFormAttributes() {
1347  # Use multipart/form-data
1348  $encType = $this->mUseMultipart
1349  ? 'multipart/form-data'
1350  : 'application/x-www-form-urlencoded';
1351  # Attributes
1352  $attribs = [
1353  'class' => 'mw-htmlform',
1354  'action' => $this->getAction(),
1355  'method' => $this->getMethod(),
1356  'enctype' => $encType,
1357  ];
1358  if ( $this->mId ) {
1359  $attribs['id'] = $this->mId;
1360  }
1361  if ( is_string( $this->mAutocomplete ) ) {
1362  $attribs['autocomplete'] = $this->mAutocomplete;
1363  }
1364  if ( $this->mName ) {
1365  $attribs['name'] = $this->mName;
1366  }
1367  if ( $this->needsJSForHtml5FormValidation() ) {
1368  $attribs['novalidate'] = true;
1369  }
1370  return $attribs;
1371  }
1372 
1380  public function wrapForm( $html ) {
1381  # Include a <fieldset> wrapper for style, if requested.
1382  if ( $this->mWrapperLegend !== false ) {
1383  $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
1384  $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1385  }
1386 
1387  return Html::rawElement(
1388  'form',
1389  $this->getFormAttributes(),
1390  $html
1391  );
1392  }
1393 
1398  public function getHiddenFields() {
1399  $html = '';
1400 
1401  // add the title as a hidden file if it hasn't been added yet and if it is necessary
1402  // added for backward compatibility with the previous version of this public method
1403  $html .= $this->getHiddenTitle();
1404 
1405  if ( $this->mFormIdentifier !== null ) {
1406  $html .= Html::hidden(
1407  'wpFormIdentifier',
1408  $this->mFormIdentifier
1409  ) . "\n";
1410  }
1411  if ( $this->getMethod() === 'post' ) {
1412  $html .= Html::hidden(
1413  'wpEditToken',
1414  $this->getUser()->getEditToken( $this->mTokenSalt ),
1415  [ 'id' => 'wpEditToken' ]
1416  ) . "\n";
1417  }
1418 
1419  foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1420  $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1421  }
1422 
1423  return $html;
1424  }
1425 
1431  public function getButtons() {
1432  $buttons = '';
1433  $useMediaWikiUIEverywhere =
1434  $this->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
1435 
1436  if ( $this->mShowSubmit ) {
1437  $attribs = [];
1438 
1439  if ( isset( $this->mSubmitID ) ) {
1440  $attribs['id'] = $this->mSubmitID;
1441  }
1442 
1443  if ( isset( $this->mSubmitName ) ) {
1444  $attribs['name'] = $this->mSubmitName;
1445  }
1446 
1447  if ( isset( $this->mSubmitTooltip ) ) {
1448  $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1449  }
1450 
1451  $attribs['class'] = [ 'mw-htmlform-submit' ];
1452 
1453  if ( $useMediaWikiUIEverywhere ) {
1454  foreach ( $this->mSubmitFlags as $flag ) {
1455  $attribs['class'][] = 'mw-ui-' . $flag;
1456  }
1457  $attribs['class'][] = 'mw-ui-button';
1458  }
1459 
1460  $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1461  }
1462 
1463  if ( $this->mShowReset ) {
1464  $buttons .= Html::element(
1465  'input',
1466  [
1467  'type' => 'reset',
1468  'value' => $this->msg( 'htmlform-reset' )->text(),
1469  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1470  ]
1471  ) . "\n";
1472  }
1473 
1474  if ( $this->mShowCancel ) {
1475  $target = $this->getCancelTargetURL();
1476  $buttons .= Html::element(
1477  'a',
1478  [
1479  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1480  'href' => $target,
1481  ],
1482  $this->msg( 'cancel' )->text()
1483  ) . "\n";
1484  }
1485 
1486  foreach ( $this->mButtons as $button ) {
1487  $attrs = [
1488  'type' => 'submit',
1489  'name' => $button['name'],
1490  'value' => $button['value']
1491  ];
1492 
1493  if ( isset( $button['label-message'] ) ) {
1494  $label = $this->getMessage( $button['label-message'] )->parse();
1495  } elseif ( isset( $button['label'] ) ) {
1496  $label = htmlspecialchars( $button['label'] );
1497  } elseif ( isset( $button['label-raw'] ) ) {
1498  $label = $button['label-raw'];
1499  } else {
1500  $label = htmlspecialchars( $button['value'] );
1501  }
1502 
1503  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1504  if ( $button['attribs'] ) {
1505  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1506  $attrs += $button['attribs'];
1507  }
1508 
1509  if ( isset( $button['id'] ) ) {
1510  $attrs['id'] = $button['id'];
1511  }
1512 
1513  if ( $useMediaWikiUIEverywhere ) {
1514  $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
1515  $attrs['class'][] = 'mw-ui-button';
1516  }
1517 
1518  $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
1519  }
1520 
1521  if ( !$buttons ) {
1522  return '';
1523  }
1524 
1525  return Html::rawElement( 'span',
1526  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1527  }
1528 
1534  public function getBody() {
1535  return $this->displaySection( $this->mFieldTree, $this->mTableId );
1536  }
1537 
1547  public function getErrorsOrWarnings( $elements, $elementsType ) {
1548  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1549  throw new DomainException( $elementsType . ' is not a valid type.' );
1550  }
1551  $elementstr = false;
1552  if ( $elements instanceof Status ) {
1553  [ $errorStatus, $warningStatus ] = $elements->splitByErrorType();
1554  $status = $elementsType === 'error' ? $errorStatus : $warningStatus;
1555  if ( $status->isGood() ) {
1556  $elementstr = '';
1557  } else {
1558  $elementstr = $status
1559  ->getMessage()
1560  ->setContext( $this )
1561  ->setInterfaceMessageFlag( true )
1562  ->parse();
1563  }
1564  } elseif ( $elementsType === 'error' ) {
1565  if ( is_array( $elements ) ) {
1566  $elementstr = $this->formatErrors( $elements );
1567  } elseif ( $elements && $elements !== true ) {
1568  $elementstr = (string)$elements;
1569  }
1570  }
1571 
1572  if ( !$elementstr ) {
1573  return '';
1574  } elseif ( $elementsType === 'error' ) {
1575  return Html::errorBox( $elementstr );
1576  } else { // $elementsType can only be 'warning'
1577  return Html::warningBox( $elementstr );
1578  }
1579  }
1580 
1588  public function formatErrors( $errors ) {
1589  $errorstr = '';
1590 
1591  foreach ( $errors as $error ) {
1592  $errorstr .= Html::rawElement(
1593  'li',
1594  [],
1595  $this->getMessage( $error )->parse()
1596  );
1597  }
1598 
1599  $errorstr = Html::rawElement( 'ul', [], $errorstr );
1600 
1601  return $errorstr;
1602  }
1603 
1611  public function setSubmitText( $t ) {
1612  $this->mSubmitText = $t;
1613 
1614  return $this;
1615  }
1616 
1623  public function setSubmitDestructive() {
1624  $this->mSubmitFlags = [ 'destructive', 'primary' ];
1625 
1626  return $this;
1627  }
1628 
1637  public function setSubmitTextMsg( $msg ) {
1638  if ( !$msg instanceof Message ) {
1639  $msg = $this->msg( $msg );
1640  }
1641  $this->setSubmitText( $msg->text() );
1642 
1643  return $this;
1644  }
1645 
1650  public function getSubmitText() {
1651  return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text();
1652  }
1653 
1659  public function setSubmitName( $name ) {
1660  $this->mSubmitName = $name;
1661 
1662  return $this;
1663  }
1664 
1670  public function setSubmitTooltip( $name ) {
1671  $this->mSubmitTooltip = $name;
1672 
1673  return $this;
1674  }
1675 
1684  public function setSubmitID( $t ) {
1685  $this->mSubmitID = $t;
1686 
1687  return $this;
1688  }
1689 
1705  public function setFormIdentifier( $ident ) {
1706  $this->mFormIdentifier = $ident;
1707 
1708  return $this;
1709  }
1710 
1721  public function suppressDefaultSubmit( $suppressSubmit = true ) {
1722  $this->mShowSubmit = !$suppressSubmit;
1723 
1724  return $this;
1725  }
1726 
1733  public function showCancel( $show = true ) {
1734  $this->mShowCancel = $show;
1735  return $this;
1736  }
1737 
1744  public function setCancelTarget( $target ) {
1745  if ( $target instanceof PageReference ) {
1746  $target = TitleValue::castPageToLinkTarget( $target );
1747  }
1748 
1749  $this->mCancelTarget = $target;
1750  return $this;
1751  }
1752 
1757  protected function getCancelTargetURL() {
1758  if ( is_string( $this->mCancelTarget ) ) {
1759  return $this->mCancelTarget;
1760  } else {
1761  // TODO: use a service to get the local URL for a LinkTarget, see T282283
1762  $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1763  return $target->getLocalURL();
1764  }
1765  }
1766 
1776  public function setTableId( $id ) {
1777  $this->mTableId = $id;
1778 
1779  return $this;
1780  }
1781 
1787  public function setId( $id ) {
1788  $this->mId = $id;
1789 
1790  return $this;
1791  }
1792 
1797  public function setName( $name ) {
1798  $this->mName = $name;
1799 
1800  return $this;
1801  }
1802 
1814  public function setWrapperLegend( $legend ) {
1815  $this->mWrapperLegend = $legend;
1816 
1817  return $this;
1818  }
1819 
1827  public function setWrapperAttributes( $attributes ) {
1828  $this->mWrapperAttributes = $attributes;
1829 
1830  return $this;
1831  }
1832 
1842  public function setWrapperLegendMsg( $msg ) {
1843  if ( !$msg instanceof Message ) {
1844  $msg = $this->msg( $msg );
1845  }
1846  $this->setWrapperLegend( $msg->text() );
1847 
1848  return $this;
1849  }
1850 
1860  public function setMessagePrefix( $p ) {
1861  $this->mMessagePrefix = $p;
1862 
1863  return $this;
1864  }
1865 
1873  public function setTitle( $t ) {
1874  // TODO: make mTitle a PageReference when we have a better way to get URLs, see T282283.
1875  $this->mTitle = Title::castFromPageReference( $t );
1876 
1877  return $this;
1878  }
1879 
1883  public function getTitle() {
1884  return $this->mTitle ?: $this->getContext()->getTitle();
1885  }
1886 
1894  public function setMethod( $method = 'post' ) {
1895  $this->mMethod = strtolower( $method );
1896 
1897  return $this;
1898  }
1899 
1903  public function getMethod() {
1904  return $this->mMethod;
1905  }
1906 
1917  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1918  return Xml::fieldset( $legend, $section, $attributes ) . "\n";
1919  }
1920 
1938  public function displaySection( $fields,
1939  $sectionName = '',
1940  $fieldsetIDPrefix = '',
1941  &$hasUserVisibleFields = false
1942  ) {
1943  if ( $this->mFieldData === null ) {
1944  throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1945  . 'You probably called displayForm() without calling prepareForm() first.' );
1946  }
1947 
1948  $displayFormat = $this->getDisplayFormat();
1949 
1950  $html = [];
1951  $subsectionHtml = '';
1952  $hasLabel = false;
1953 
1954  // Conveniently, PHP method names are case-insensitive.
1955  // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1956  $getFieldHtmlMethod = $displayFormat === 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
1957 
1958  foreach ( $fields as $key => $value ) {
1959  if ( $value instanceof HTMLFormField ) {
1960  $v = array_key_exists( $key, $this->mFieldData )
1961  ? $this->mFieldData[$key]
1962  : $value->getDefault();
1963 
1964  $retval = $value->$getFieldHtmlMethod( $v ?? '' );
1965 
1966  // check, if the form field should be added to
1967  // the output.
1968  if ( $value->hasVisibleOutput() ) {
1969  $html[] = $retval;
1970 
1971  $labelValue = trim( $value->getLabel() );
1972  if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
1973  $hasLabel = true;
1974  }
1975 
1976  $hasUserVisibleFields = true;
1977  }
1978  } elseif ( is_array( $value ) ) {
1979  $subsectionHasVisibleFields = false;
1980  $section =
1981  $this->displaySection( $value,
1982  "mw-htmlform-$key",
1983  "$fieldsetIDPrefix$key-",
1984  $subsectionHasVisibleFields );
1985 
1986  if ( $subsectionHasVisibleFields === true ) {
1987  // Display the section with various niceties.
1988  $hasUserVisibleFields = true;
1989 
1990  $legend = $this->getLegend( $key );
1991 
1992  $section = $this->getHeaderText( $key ) .
1993  $section .
1994  $this->getFooterText( $key );
1995 
1996  $attributes = [];
1997  if ( $fieldsetIDPrefix ) {
1998  $attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1999  }
2000  $subsectionHtml .= $this->wrapFieldSetSection(
2001  $legend, $section, $attributes, $fields === $this->mFieldTree
2002  );
2003  } else {
2004  // Just return the inputs, nothing fancy.
2005  $subsectionHtml .= $section;
2006  }
2007  }
2008  }
2009 
2010  $html = $this->formatSection( $html, $sectionName, $hasLabel );
2011 
2012  if ( $subsectionHtml ) {
2013  if ( $this->mSubSectionBeforeFields ) {
2014  return $subsectionHtml . "\n" . $html;
2015  } else {
2016  return $html . "\n" . $subsectionHtml;
2017  }
2018  } else {
2019  return $html;
2020  }
2021  }
2022 
2031  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
2032  if ( !$fieldsHtml ) {
2033  // Do not generate any wrappers for empty sections. Sections may be empty if they only have
2034  // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
2035  return '';
2036  }
2037 
2038  $displayFormat = $this->getDisplayFormat();
2039  $html = implode( '', $fieldsHtml );
2040 
2041  if ( $displayFormat === 'raw' ) {
2042  return $html;
2043  }
2044 
2045  $classes = [];
2046 
2047  if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
2048  $classes[] = 'mw-htmlform-nolabel';
2049  }
2050 
2051  $attribs = [ 'class' => $classes ];
2052 
2053  if ( $sectionName ) {
2054  $attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
2055  }
2056 
2057  if ( $displayFormat === 'table' ) {
2058  return Html::rawElement( 'table',
2059  $attribs,
2060  Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
2061  } elseif ( $displayFormat === 'inline' ) {
2062  return Html::rawElement( 'span', $attribs, "\n$html\n" );
2063  } else {
2064  return Html::rawElement( 'div', $attribs, "\n$html\n" );
2065  }
2066  }
2067 
2071  public function loadData() {
2072  $this->prepareForm();
2073  }
2074 
2078  protected function loadFieldData() {
2079  $fieldData = [];
2080  $request = $this->getRequest();
2081 
2082  foreach ( $this->mFlatFields as $fieldname => $field ) {
2083  if ( $field->skipLoadData( $request ) ) {
2084  continue;
2085  }
2086  if ( $field->mParams['disabled'] ?? false ) {
2087  $fieldData[$fieldname] = $field->getDefault();
2088  } else {
2089  $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2090  }
2091  }
2092 
2093  // Reset to default for fields that are supposed to be disabled.
2094  // FIXME: Handle dependency chains, fields that a field checks on may need a reset too.
2095  foreach ( $fieldData as $name => &$value ) {
2096  $field = $this->mFlatFields[$name];
2097  if ( $field->isDisabled( $fieldData ) ) {
2098  $value = $field->getDefault();
2099  }
2100  }
2101 
2102  # Filter data.
2103  foreach ( $fieldData as $name => &$value ) {
2104  $field = $this->mFlatFields[$name];
2105  $value = $field->filter( $value, $fieldData );
2106  }
2107 
2108  $this->mFieldData = $fieldData;
2109  }
2110 
2118  public function suppressReset( $suppressReset = true ) {
2119  $this->mShowReset = !$suppressReset;
2120 
2121  return $this;
2122  }
2123 
2134  public function filterDataForSubmit( $data ) {
2135  return $data;
2136  }
2137 
2147  public function getLegend( $key ) {
2148  return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text();
2149  }
2150 
2161  public function setAction( $action ) {
2162  $this->mAction = $action;
2163 
2164  return $this;
2165  }
2166 
2174  public function getAction() {
2175  // If an action is already provided, return it
2176  if ( $this->mAction !== false ) {
2177  return $this->mAction;
2178  }
2179 
2180  $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2181  // Check whether we are in GET mode and the ArticlePath contains a "?"
2182  // meaning that getLocalURL() would return something like "index.php?title=...".
2183  // As browser remove the query string before submitting GET forms,
2184  // it means that the title would be lost. In such case use script path instead
2185  // and put title in a hidden field (see getHiddenFields()).
2186  if ( str_contains( $articlePath, '?' ) && $this->getMethod() === 'get' ) {
2187  return $this->getConfig()->get( MainConfigNames::Script );
2188  }
2189 
2190  return $this->getTitle()->getLocalURL();
2191  }
2192 
2203  public function setAutocomplete( $autocomplete ) {
2204  $this->mAutocomplete = $autocomplete;
2205 
2206  return $this;
2207  }
2208 
2215  protected function getMessage( $value ) {
2216  return Message::newFromSpecifier( $value )->setContext( $this );
2217  }
2218 
2228  public function needsJSForHtml5FormValidation() {
2229  foreach ( $this->mFlatFields as $field ) {
2230  if ( $field->needsJSForHtml5FormValidation() ) {
2231  return true;
2232  }
2233  }
2234  return false;
2235  }
2236 }
getUser()
getContext()
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
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:155
needsJSForHtml5FormValidation()
Whether this form, with its current fields, requires the user agent to have JavaScript enabled for th...
Definition: HTMLForm.php:2228
setSubmitCallback( $cb)
Set a callback to a function to do something with the form once it's been successfully validated.
Definition: HTMLForm.php:782
setHeaderText( $msg, $section=null)
Set header text, inside the form.
Definition: HTMLForm.php:968
$mSubmitCallback
Definition: HTMLForm.php:221
string false $mAction
Form action URL.
Definition: HTMLForm.php:254
displayForm( $submitResult)
Display the form (sending to the context's OutputPage object), with an appropriate error message or s...
Definition: HTMLForm.php:1269
$mWasSubmitted
Definition: HTMLForm.php:247
string null $mAutocomplete
Form attribute autocomplete.
Definition: HTMLForm.php:275
setAction( $action)
Set the value for the action attribute of the form.
Definition: HTMLForm.php:2161
string array $mTokenSalt
Salt for the edit token.
Definition: HTMLForm.php:296
setFooterHtml( $html, $section=null)
Set footer HTML, inside the form.
Definition: HTMLForm.php:1016
string[] $mSubmitFlags
Definition: HTMLForm.php:217
addPreHtml( $html)
Add HTML to introductory message.
Definition: HTMLForm.php:837
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1650
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1894
setValidationErrorMessage( $msg)
Set a message to display on a validation error.
Definition: HTMLForm.php:797
getCancelTargetURL()
Definition: HTMLForm.php:1757
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:2215
getHeaderHtml( $section=null)
Get header HTML.
Definition: HTMLForm.php:937
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:897
setAutocomplete( $autocomplete)
Set the value for the autocomplete attribute of the form.
Definition: HTMLForm.php:2203
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
Definition: HTMLForm.php:557
setSubmitName( $name)
Definition: HTMLForm.php:1659
setTitle( $t)
Set the title for form submission.
Definition: HTMLForm.php:1873
static getClassFromDescriptor( $fieldname, &$descriptor)
Get the HTMLFormField subclass for this descriptor.
Definition: HTMLForm.php:526
array $availableSubclassDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:330
string $displayFormat
Format in which to display form.
Definition: HTMLForm.php:313
addPostHtml( $html)
Add HTML to the end of the display.
Definition: HTMLForm.php:1088
bool $mSubSectionBeforeFields
If true, sections that contain both fields and subsections will render their subsections before their...
Definition: HTMLForm.php:306
$mUseMultipart
Definition: HTMLForm.php:277
setTableId( $id)
Set the id of the <table> or outermost <div> element.
Definition: HTMLForm.php:1776
getHTML( $submitResult)
Returns the raw HTML generated by the form.
Definition: HTMLForm.php:1302
getLegend( $key)
Get a string to go in the "<legend>" of a section fieldset.
Definition: HTMLForm.php:2147
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1637
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into a user-visible fieldset.
Definition: HTMLForm.php:1917
$mWrapperLegend
Definition: HTMLForm.php:289
filterDataForSubmit( $data)
Overload this if you want to apply special filtration routines to the form as a whole,...
Definition: HTMLForm.php:2134
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1842
setId( $id)
Definition: HTMLForm.php:1787
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1814
formatErrors( $errors)
Format a stack of error messages into a single HTML string.
Definition: HTMLForm.php:1588
setDisplayFormat( $format)
Set format in which to display the form.
Definition: HTMLForm.php:469
addButton( $data)
Add a button to the form.
Definition: HTMLForm.php:1208
addFooterHtml( $html, $section=null)
Add footer HTML, inside the form.
Definition: HTMLForm.php:994
setPreHtml( $html)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:823
getPreHtml()
Get the introductory message HTML.
Definition: HTMLForm.php:849
setPostHtml( $html)
Set HTML at the end of the display.
Definition: HTMLForm.php:1102
getPreText()
Get the introductory message HTML.
Definition: HTMLForm.php:884
static string[] $typeMappings
A mapping of 'type' inputs onto standard HTMLFormField subclasses.
Definition: HTMLForm.php:159
getHiddenFields()
Get the hidden fields that should go inside the form.
Definition: HTMLForm.php:1398
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
Definition: HTMLForm.php:1623
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1721
setSubmitID( $t)
Set the id for the submit button.
Definition: HTMLForm.php:1684
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:505
$mCancelTarget
Definition: HTMLForm.php:219
getAction()
Get the value for the action attribute of the form.
Definition: HTMLForm.php:2174
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition: HTMLForm.php:639
hasField( $fieldname)
Definition: HTMLForm.php:443
$mWrapperAttributes
Definition: HTMLForm.php:290
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition: HTMLForm.php:1050
addPostText( $msg)
Add text to the end of the display.
Definition: HTMLForm.php:1126
$mSectionHeaders
Definition: HTMLForm.php:231
getFooterText( $section=null)
Get footer text.
Definition: HTMLForm.php:1076
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:954
setWrapperAttributes( $attributes)
For internal use only.
Definition: HTMLForm.php:1827
prepareForm()
Prepare form for submission.
Definition: HTMLForm.php:582
array[] $mValidationErrorMessage
Definition: HTMLForm.php:226
setCollapsibleOptions( $collapsedByDefault=false)
Enable collapsible mode, and set whether the form is collapsed by default.
Definition: HTMLForm.php:1335
wasSubmitted()
Test whether the form was considered to have been submitted or not, i.e.
Definition: HTMLForm.php:768
getHeaderText( $section=null)
Get header text.
Definition: HTMLForm.php:981
$mMessagePrefix
Definition: HTMLForm.php:209
array[] $mButtons
Definition: HTMLForm.php:287
$mFormIdentifier
Definition: HTMLForm.php:243
$mSubmitTooltip
Definition: HTMLForm.php:241
tryAuthorizedSubmit()
Try submitting, with edit token check first.
Definition: HTMLForm.php:600
setSubmitTooltip( $name)
Definition: HTMLForm.php:1670
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1938
array[] $mHiddenFields
Definition: HTMLForm.php:282
setPreText( $msg)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:861
bool $mCollapsible
Whether the form can be collapsed.
Definition: HTMLForm.php:261
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output Array values are discarded for security reasons (per WebRequest::get...
Definition: HTMLForm.php:1152
$mSectionFooters
Definition: HTMLForm.php:232
setFooterText( $msg, $section=null)
Set footer text, inside the form.
Definition: HTMLForm.php:1064
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1860
getField( $fieldname)
Definition: HTMLForm.php:452
bool $mCollapsed
Whether the form is collapsed by default.
Definition: HTMLForm.php:268
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Definition: HTMLForm.php:1380
getFormAttributes()
Get HTML attributes for the <form> tag.
Definition: HTMLForm.php:1346
getBody()
Get the whole body of the form.
Definition: HTMLForm.php:1534
showAlways()
Same as self::show with the difference, that the form will be added to the output,...
Definition: HTMLForm.php:657
getFooterHtml( $section=null)
Get footer HTML.
Definition: HTMLForm.php:1033
loadFieldData()
Load data of form fields from the request.
Definition: HTMLForm.php:2078
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition: HTMLForm.php:919
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1249
__construct( $descriptor, IContextSource $context, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
Definition: HTMLForm.php:380
addPreText( $msg)
Add HTML to introductory message.
Definition: HTMLForm.php:873
setFormIdentifier( $ident)
Set an internal identifier for this form.
Definition: HTMLForm.php:1705
setName( $name)
Definition: HTMLForm.php:1797
suppressReset( $suppressReset=true)
Stop a reset button being shown for this form.
Definition: HTMLForm.php:2118
setPostText( $msg)
Set text at the end of the display.
Definition: HTMLForm.php:1138
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: HTMLForm.php:2031
setCancelTarget( $target)
Sets the target where the user is redirected to after clicking cancel.
Definition: HTMLForm.php:1744
array $availableDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:319
showCancel( $show=true)
Show a cancel button (or prevent it).
Definition: HTMLForm.php:1733
Title null $mTitle
Definition: HTMLForm.php:245
setSubmitText( $t)
Set the text for the submit button.
Definition: HTMLForm.php:1611
setIntro( $msg)
Set the introductory message, overwriting any existing message.
Definition: HTMLForm.php:811
getButtons()
Get the submit and (potentially) reset buttons.
Definition: HTMLForm.php:1431
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:354
trySubmit()
Validate all the fields, and call the submission callback function if everything is kosher.
Definition: HTMLForm.php:679
HTMLFormField[] $mFlatFields
Definition: HTMLForm.php:212
getErrorsOrWarnings( $elements, $elementsType)
Returns a formatted list of errors or warnings from the given elements.
Definition: HTMLForm.php:1547
addHiddenFields(array $fields)
Add an array of hidden fields to the output Array values are discarded for security reasons (per WebR...
Definition: HTMLForm.php:1173
getPostHtml()
Get HTML at the end of the display.
Definition: HTMLForm.php:1114
addFields( $descriptor)
Add fields to the form.
Definition: HTMLForm.php:406
MediaWiki exception.
Definition: MWException.php:32
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Some internal bits split of from Skin.php.
Definition: Linker.php:67
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition: Title.php:82
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:144
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:426
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:947
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
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:463
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition: Xml.php:622
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.