MediaWiki  master
HTMLForm.php
Go to the documentation of this file.
1 <?php
2 
24 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
27 
149 class HTMLForm extends ContextSource {
150  use ProtectedHookAccessorTrait;
151 
153  public static $typeMappings = [
154  'api' => HTMLApiField::class,
155  'text' => HTMLTextField::class,
156  'textwithbutton' => HTMLTextFieldWithButton::class,
157  'textarea' => HTMLTextAreaField::class,
158  'select' => HTMLSelectField::class,
159  'combobox' => HTMLComboboxField::class,
160  'radio' => HTMLRadioField::class,
161  'multiselect' => HTMLMultiSelectField::class,
162  'limitselect' => HTMLSelectLimitField::class,
163  'check' => HTMLCheckField::class,
164  'toggle' => HTMLCheckField::class,
165  'int' => HTMLIntField::class,
166  'file' => HTMLFileField::class,
167  'float' => HTMLFloatField::class,
168  'info' => HTMLInfoField::class,
169  'selectorother' => HTMLSelectOrOtherField::class,
170  'selectandother' => HTMLSelectAndOtherField::class,
171  'namespaceselect' => HTMLSelectNamespace::class,
172  'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
173  'tagfilter' => HTMLTagFilter::class,
174  'sizefilter' => HTMLSizeFilterField::class,
175  'submit' => HTMLSubmitField::class,
176  'hidden' => HTMLHiddenField::class,
177  'edittools' => HTMLEditTools::class,
178  'checkmatrix' => HTMLCheckMatrix::class,
179  'cloner' => HTMLFormFieldCloner::class,
180  'autocompleteselect' => HTMLAutoCompleteSelectField::class,
181  'language' => HTMLSelectLanguageField::class,
182  'date' => HTMLDateTimeField::class,
183  'time' => HTMLDateTimeField::class,
184  'datetime' => HTMLDateTimeField::class,
185  'expiry' => HTMLExpiryField::class,
186  // HTMLTextField will output the correct type="" attribute automagically.
187  // There are about four zillion other HTML5 input types, like range, but
188  // we don't use those at the moment, so no point in adding all of them.
189  'email' => HTMLTextField::class,
190  'password' => HTMLTextField::class,
191  'url' => HTMLTextField::class,
192  'title' => HTMLTitleTextField::class,
193  'user' => HTMLUserTextField::class,
194  'tagmultiselect' => HTMLTagMultiselectField::class,
195  'usersmultiselect' => HTMLUsersMultiselectField::class,
196  'titlesmultiselect' => HTMLTitlesMultiselectField::class,
197  'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
198  ];
199 
200  public $mFieldData;
201 
202  protected $mMessagePrefix;
203 
205  protected $mFlatFields = [];
206  protected $mFieldTree = [];
207  protected $mShowReset = false;
208  protected $mShowSubmit = true;
210  protected $mSubmitFlags = [ 'primary', 'progressive' ];
211  protected $mShowCancel = false;
212  protected $mCancelTarget;
213 
214  protected $mSubmitCallback;
216 
217  protected $mPre = '';
218  protected $mHeader = '';
219  protected $mFooter = '';
220  protected $mSectionHeaders = [];
221  protected $mSectionFooters = [];
222  protected $mPost = '';
223  protected $mId;
224  protected $mName;
225  protected $mTableId = '';
226 
227  protected $mSubmitID;
228  protected $mSubmitName;
229  protected $mSubmitText;
230  protected $mSubmitTooltip;
231 
232  protected $mFormIdentifier;
233  protected $mTitle;
234  protected $mMethod = 'post';
235  protected $mWasSubmitted = false;
236 
242  protected $mAction = false;
243 
249  protected $mCollapsible = false;
250 
256  protected $mCollapsed = false;
257 
263  protected $mAutocomplete = null;
264 
265  protected $mUseMultipart = false;
270  protected $mHiddenFields = [];
275  protected $mButtons = [];
276 
277  protected $mWrapperLegend = false;
278  protected $mWrapperAttributes = [];
279 
284  protected $mTokenSalt = '';
285 
294  protected $mSubSectionBeforeFields = true;
295 
301  protected $displayFormat = 'table';
302 
308  'table',
309  'div',
310  'raw',
311  'inline',
312  ];
313 
319  'vform',
320  'ooui',
321  ];
322 
332  public static function factory( $displayFormat, ...$arguments ) {
333  switch ( $displayFormat ) {
334  case 'vform':
335  return new VFormHTMLForm( ...$arguments );
336  case 'ooui':
337  return new OOUIHTMLForm( ...$arguments );
338  default:
339  $form = new self( ...$arguments );
340  $form->setDisplayFormat( $displayFormat );
341  return $form;
342  }
343  }
344 
356  public function __construct( $descriptor, /*IContextSource*/ $context = null,
357  $messagePrefix = ''
358  ) {
359  if ( $context instanceof IContextSource ) {
360  $this->setContext( $context );
361  $this->mTitle = false; // We don't need them to set a title
362  $this->mMessagePrefix = $messagePrefix;
363  } elseif ( $context === null && $messagePrefix !== '' ) {
364  $this->mMessagePrefix = $messagePrefix;
365  } elseif ( is_string( $context ) && $messagePrefix === '' ) {
366  // B/C since 1.18
367  // it's actually $messagePrefix
368  $this->mMessagePrefix = $context;
369  }
370 
371  // Evil hack for mobile :(
372  if (
373  !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
374  && $this->displayFormat === 'table'
375  ) {
376  $this->displayFormat = 'div';
377  }
378 
379  $this->addFields( $descriptor );
380  }
381 
391  public function addFields( $descriptor ) {
392  $loadedDescriptor = [];
393 
394  foreach ( $descriptor as $fieldname => $info ) {
395 
396  $section = $info['section'] ?? '';
397 
398  if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
399  $this->mUseMultipart = true;
400  }
401 
402  $field = static::loadInputFromParameters( $fieldname, $info, $this );
403 
404  $setSection =& $loadedDescriptor;
405  if ( $section ) {
406  foreach ( explode( '/', $section ) as $newName ) {
407  if ( !isset( $setSection[$newName] ) ) {
408  $setSection[$newName] = [];
409  }
410 
411  $setSection =& $setSection[$newName];
412  }
413  }
414 
415  $setSection[$fieldname] = $field;
416  $this->mFlatFields[$fieldname] = $field;
417  }
418 
419  $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
420 
421  return $this;
422  }
423 
428  public function hasField( $fieldname ) {
429  return isset( $this->mFlatFields[$fieldname] );
430  }
431 
437  public function getField( $fieldname ) {
438  if ( !$this->hasField( $fieldname ) ) {
439  throw new DomainException( __METHOD__ . ': no field named ' . $fieldname );
440  }
441  return $this->mFlatFields[$fieldname];
442  }
443 
454  public function setDisplayFormat( $format ) {
455  if (
456  in_array( $format, $this->availableSubclassDisplayFormats, true ) ||
457  in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true )
458  ) {
459  throw new MWException( 'Cannot change display format after creation, ' .
460  'use HTMLForm::factory() instead' );
461  }
462 
463  if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
464  throw new MWException( 'Display format must be one of ' .
465  print_r(
466  array_merge(
467  $this->availableDisplayFormats,
468  $this->availableSubclassDisplayFormats
469  ),
470  true
471  ) );
472  }
473 
474  // Evil hack for mobile :(
475  if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
476  $format = 'div';
477  }
478 
479  $this->displayFormat = $format;
480 
481  return $this;
482  }
483 
489  public function getDisplayFormat() {
490  return $this->displayFormat;
491  }
492 
510  public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
511  if ( isset( $descriptor['class'] ) ) {
512  $class = $descriptor['class'];
513  } elseif ( isset( $descriptor['type'] ) ) {
514  $class = static::$typeMappings[$descriptor['type']];
515  $descriptor['class'] = $class;
516  } else {
517  $class = null;
518  }
519 
520  if ( !$class ) {
521  throw new MWException( "Descriptor with no class for $fieldname: "
522  . print_r( $descriptor, true ) );
523  }
524 
525  return $class;
526  }
527 
540  public static function loadInputFromParameters( $fieldname, $descriptor,
541  HTMLForm $parent = null
542  ) {
543  $class = static::getClassFromDescriptor( $fieldname, $descriptor );
544 
545  $descriptor['fieldname'] = $fieldname;
546  if ( $parent ) {
547  $descriptor['parent'] = $parent;
548  }
549 
550  # @todo This will throw a fatal error whenever someone try to use
551  # 'class' to feed a CSS class instead of 'cssclass'. Would be
552  # great to avoid the fatal error and show a nice error.
553  return new $class( $descriptor );
554  }
555 
565  public function prepareForm() {
566  # Check if we have the info we need
567  if ( !$this->mTitle instanceof PageReference && $this->mTitle !== false ) {
568  throw new MWException( 'You must call setTitle() on an HTMLForm' );
569  }
570 
571  # Load data from the request.
572  if (
573  $this->mFormIdentifier === null ||
574  $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
575  ) {
576  $this->loadData();
577  } else {
578  $this->mFieldData = [];
579  }
580 
581  return $this;
582  }
583 
588  public function tryAuthorizedSubmit() {
589  $result = false;
590 
591  if ( $this->mFormIdentifier === null ) {
592  $identOkay = true;
593  } else {
594  $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier;
595  }
596 
597  $tokenOkay = false;
598  if ( $this->getMethod() !== 'post' ) {
599  $tokenOkay = true; // no session check needed
600  } elseif ( $this->getRequest()->wasPosted() ) {
601  $editToken = $this->getRequest()->getVal( 'wpEditToken' );
602  if ( $this->getUser()->isRegistered() || $editToken !== null ) {
603  // Session tokens for logged-out users have no security value.
604  // However, if the user gave one, check it in order to give a nice
605  // "session expired" error instead of "permission denied" or such.
606  $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
607  } else {
608  $tokenOkay = true;
609  }
610  }
611 
612  if ( $tokenOkay && $identOkay ) {
613  $this->mWasSubmitted = true;
614  $result = $this->trySubmit();
615  }
616 
617  return $result;
618  }
619 
627  public function show() {
628  $this->prepareForm();
629 
630  $result = $this->tryAuthorizedSubmit();
631  if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
632  return $result;
633  }
634 
635  $this->displayForm( $result );
636 
637  return false;
638  }
639 
645  public function showAlways() {
646  $this->prepareForm();
647 
648  $result = $this->tryAuthorizedSubmit();
649 
650  $this->displayForm( $result );
651 
652  return $result;
653  }
654 
667  public function trySubmit() {
668  $valid = true;
669  $hoistedErrors = Status::newGood();
670  if ( $this->mValidationErrorMessage ) {
671  foreach ( $this->mValidationErrorMessage as $error ) {
672  $hoistedErrors->fatal( ...$error );
673  }
674  } else {
675  $hoistedErrors->fatal( 'htmlform-invalid-input' );
676  }
677 
678  $this->mWasSubmitted = true;
679 
680  # Check for cancelled submission
681  foreach ( $this->mFlatFields as $fieldname => $field ) {
682  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
683  continue;
684  }
685  if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
686  $this->mWasSubmitted = false;
687  return false;
688  }
689  }
690 
691  # Check for validation
692  foreach ( $this->mFlatFields as $fieldname => $field ) {
693  if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
694  continue;
695  }
696  if ( $field->isDisabled( $this->mFieldData ) ) {
697  continue;
698  }
699  $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
700  if ( $res !== true ) {
701  $valid = false;
702  if ( $res !== false && !$field->canDisplayErrors() ) {
703  if ( is_string( $res ) ) {
704  $hoistedErrors->fatal( 'rawmessage', $res );
705  } else {
706  $hoistedErrors->fatal( $res );
707  }
708  }
709  }
710  }
711 
712  if ( !$valid ) {
713  return $hoistedErrors;
714  }
715 
716  $callback = $this->mSubmitCallback;
717  if ( !is_callable( $callback ) ) {
718  throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
719  'setSubmitCallback() to set one.' );
720  }
721 
722  $data = $this->filterDataForSubmit( $this->mFieldData );
723 
724  $res = call_user_func( $callback, $data, $this );
725  if ( $res === false ) {
726  $this->mWasSubmitted = false;
727  }
728 
729  return $res;
730  }
731 
743  public function wasSubmitted() {
744  return $this->mWasSubmitted;
745  }
746 
757  public function setSubmitCallback( $cb ) {
758  $this->mSubmitCallback = $cb;
759 
760  return $this;
761  }
762 
771  public function setValidationErrorMessage( $msg ) {
772  $this->mValidationErrorMessage = $msg;
773 
774  return $this;
775  }
776 
785  public function setIntro( $msg ) {
786  return $this->setPreHtml( $msg );
787  }
788 
797  public function setPreHtml( $html ) {
798  $this->mPre = $html;
799 
800  return $this;
801  }
802 
811  public function addPreHtml( $html ) {
812  $this->mPre .= $html;
813 
814  return $this;
815  }
816 
823  public function getPreHtml() {
824  return $this->mPre;
825  }
826 
835  public function setPreText( $msg ) {
836  return $this->setPreHtml( $msg );
837  }
838 
847  public function addPreText( $msg ) {
848  return $this->addPreHtml( $msg );
849  }
850 
858  public function getPreText() {
859  return $this->getPreHtml();
860  }
861 
871  public function addHeaderHtml( $html, $section = null ) {
872  if ( $section === null ) {
873  $this->mHeader .= $html;
874  } else {
875  if ( !isset( $this->mSectionHeaders[$section] ) ) {
876  $this->mSectionHeaders[$section] = '';
877  }
878  $this->mSectionHeaders[$section] .= $html;
879  }
880 
881  return $this;
882  }
883 
893  public function setHeaderHtml( $html, $section = null ) {
894  if ( $section === null ) {
895  $this->mHeader = $html;
896  } else {
897  $this->mSectionHeaders[$section] = $html;
898  }
899 
900  return $this;
901  }
902 
911  public function getHeaderHtml( $section = null ) {
912  if ( $section === null ) {
913  return $this->mHeader;
914  } else {
915  return $this->mSectionHeaders[$section] ?? '';
916  }
917  }
918 
928  public function addHeaderText( $msg, $section = null ) {
929  return $this->addHeaderHtml( $msg, $section );
930  }
931 
942  public function setHeaderText( $msg, $section = null ) {
943  return $this->setHeaderHtml( $msg, $section );
944  }
945 
955  public function getHeaderText( $section = null ) {
956  return $this->getHeaderHtml( $section );
957  }
958 
968  public function addFooterHtml( $html, $section = null ) {
969  if ( $section === null ) {
970  $this->mFooter .= $html;
971  } else {
972  if ( !isset( $this->mSectionFooters[$section] ) ) {
973  $this->mSectionFooters[$section] = '';
974  }
975  $this->mSectionFooters[$section] .= $html;
976  }
977 
978  return $this;
979  }
980 
990  public function setFooterHtml( $html, $section = null ) {
991  if ( $section === null ) {
992  $this->mFooter = $html;
993  } else {
994  $this->mSectionFooters[$section] = $html;
995  }
996 
997  return $this;
998  }
999 
1007  public function getFooterHtml( $section = null ) {
1008  if ( $section === null ) {
1009  return $this->mFooter;
1010  } else {
1011  return $this->mSectionFooters[$section] ?? '';
1012  }
1013  }
1014 
1024  public function addFooterText( $msg, $section = null ) {
1025  return $this->addFooterHtml( $msg, $section );
1026  }
1027 
1038  public function setFooterText( $msg, $section = null ) {
1039  return $this->setFooterHtml( $msg, $section );
1040  }
1041 
1050  public function getFooterText( $section = null ) {
1051  return $this->getFooterHtml( $section );
1052  }
1053 
1062  public function addPostHtml( $html ) {
1063  $this->mPost .= $html;
1064 
1065  return $this;
1066  }
1067 
1076  public function setPostHtml( $html ) {
1077  $this->mPost = $html;
1078 
1079  return $this;
1080  }
1081 
1088  public function getPostHtml() {
1089  return $this->mPost;
1090  }
1091 
1100  public function addPostText( $msg ) {
1101  return $this->addPostHtml( $msg );
1102  }
1103 
1112  public function setPostText( $msg ) {
1113  return $this->setPostHtml( $msg );
1114  }
1115 
1125  public function addHiddenField( $name, $value, array $attribs = [] ) {
1126  $attribs += [ 'name' => $name ];
1127  $this->mHiddenFields[] = [ $value, $attribs ];
1128 
1129  return $this;
1130  }
1131 
1142  public function addHiddenFields( array $fields ) {
1143  foreach ( $fields as $name => $value ) {
1144  $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
1145  }
1146 
1147  return $this;
1148  }
1149 
1173  public function addButton( $data ) {
1174  if ( !is_array( $data ) ) {
1175  $args = func_get_args();
1176  if ( count( $args ) < 2 || count( $args ) > 4 ) {
1177  throw new InvalidArgumentException(
1178  'Incorrect number of arguments for deprecated calling style'
1179  );
1180  }
1181  $data = [
1182  'name' => $args[0],
1183  'value' => $args[1],
1184  'id' => $args[2] ?? null,
1185  'attribs' => $args[3] ?? null,
1186  ];
1187  } else {
1188  if ( !isset( $data['name'] ) ) {
1189  throw new InvalidArgumentException( 'A name is required' );
1190  }
1191  if ( !isset( $data['value'] ) ) {
1192  throw new InvalidArgumentException( 'A value is required' );
1193  }
1194  }
1195  $this->mButtons[] = $data + [
1196  'id' => null,
1197  'attribs' => null,
1198  'flags' => null,
1199  'framed' => true,
1200  ];
1201 
1202  return $this;
1203  }
1204 
1214  public function setTokenSalt( $salt ) {
1215  $this->mTokenSalt = $salt;
1216 
1217  return $this;
1218  }
1219 
1234  public function displayForm( $submitResult ) {
1235  $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1236  }
1237 
1248  public function getHTML( $submitResult ) {
1249  # For good measure (it is the default)
1250  $this->getOutput()->setPreventClickjacking( true );
1251  $this->getOutput()->addModules( 'mediawiki.htmlform' );
1252  $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1253 
1254  if ( $this->mCollapsible ) {
1255  // Preload jquery.makeCollapsible for mediawiki.htmlform
1256  $this->getOutput()->addModules( 'jquery.makeCollapsible' );
1257  }
1258 
1259  $html = ''
1260  . $this->getErrorsOrWarnings( $submitResult, 'error' )
1261  . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1262  . $this->getHeaderText()
1263  . $this->getBody()
1264  . $this->getHiddenFields()
1265  . $this->getButtons()
1266  . $this->getFooterText();
1267 
1268  $html = $this->wrapForm( $html );
1269 
1270  return '' . $this->mPre . $html . $this->mPost;
1271  }
1272 
1280  public function setCollapsibleOptions( $collapsedByDefault = false ) {
1281  $this->mCollapsible = true;
1282  $this->mCollapsed = $collapsedByDefault;
1283  return $this;
1284  }
1285 
1291  protected function getFormAttributes() {
1292  # Use multipart/form-data
1293  $encType = $this->mUseMultipart
1294  ? 'multipart/form-data'
1295  : 'application/x-www-form-urlencoded';
1296  # Attributes
1297  $attribs = [
1298  'class' => 'mw-htmlform',
1299  'action' => $this->getAction(),
1300  'method' => $this->getMethod(),
1301  'enctype' => $encType,
1302  ];
1303  if ( $this->mId ) {
1304  $attribs['id'] = $this->mId;
1305  }
1306  if ( is_string( $this->mAutocomplete ) ) {
1307  $attribs['autocomplete'] = $this->mAutocomplete;
1308  }
1309  if ( $this->mName ) {
1310  $attribs['name'] = $this->mName;
1311  }
1312  if ( $this->needsJSForHtml5FormValidation() ) {
1313  $attribs['novalidate'] = true;
1314  }
1315  return $attribs;
1316  }
1317 
1326  public function wrapForm( $html ) {
1327  # Include a <fieldset> wrapper for style, if requested.
1328  if ( $this->mWrapperLegend !== false ) {
1329  $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
1330  $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1331  }
1332 
1333  return Html::rawElement(
1334  'form',
1335  $this->getFormAttributes(),
1336  $html
1337  );
1338  }
1339 
1344  public function getHiddenFields() {
1345  $html = '';
1346  if ( $this->mFormIdentifier !== null ) {
1347  $html .= Html::hidden(
1348  'wpFormIdentifier',
1349  $this->mFormIdentifier
1350  ) . "\n";
1351  }
1352  if ( $this->getMethod() === 'post' ) {
1353  $html .= Html::hidden(
1354  'wpEditToken',
1355  $this->getUser()->getEditToken( $this->mTokenSalt ),
1356  [ 'id' => 'wpEditToken' ]
1357  ) . "\n";
1358  $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1359  }
1360 
1361  $articlePath = $this->getConfig()->get( 'ArticlePath' );
1362  if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1363  $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1364  }
1365 
1366  foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1367  $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1368  }
1369 
1370  return $html;
1371  }
1372 
1378  public function getButtons() {
1379  $buttons = '';
1380  $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1381 
1382  if ( $this->mShowSubmit ) {
1383  $attribs = [];
1384 
1385  if ( isset( $this->mSubmitID ) ) {
1386  $attribs['id'] = $this->mSubmitID;
1387  }
1388 
1389  if ( isset( $this->mSubmitName ) ) {
1390  $attribs['name'] = $this->mSubmitName;
1391  }
1392 
1393  if ( isset( $this->mSubmitTooltip ) ) {
1394  $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1395  }
1396 
1397  $attribs['class'] = [ 'mw-htmlform-submit' ];
1398 
1399  if ( $useMediaWikiUIEverywhere ) {
1400  foreach ( $this->mSubmitFlags as $flag ) {
1401  $attribs['class'][] = 'mw-ui-' . $flag;
1402  }
1403  $attribs['class'][] = 'mw-ui-button';
1404  }
1405 
1406  $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1407  }
1408 
1409  if ( $this->mShowReset ) {
1410  $buttons .= Html::element(
1411  'input',
1412  [
1413  'type' => 'reset',
1414  'value' => $this->msg( 'htmlform-reset' )->text(),
1415  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1416  ]
1417  ) . "\n";
1418  }
1419 
1420  if ( $this->mShowCancel ) {
1421  $target = $this->getCancelTargetURL();
1422  $buttons .= Html::element(
1423  'a',
1424  [
1425  'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
1426  'href' => $target,
1427  ],
1428  $this->msg( 'cancel' )->text()
1429  ) . "\n";
1430  }
1431 
1432  foreach ( $this->mButtons as $button ) {
1433  $attrs = [
1434  'type' => 'submit',
1435  'name' => $button['name'],
1436  'value' => $button['value']
1437  ];
1438 
1439  if ( isset( $button['label-message'] ) ) {
1440  $label = $this->getMessage( $button['label-message'] )->parse();
1441  } elseif ( isset( $button['label'] ) ) {
1442  $label = htmlspecialchars( $button['label'] );
1443  } elseif ( isset( $button['label-raw'] ) ) {
1444  $label = $button['label-raw'];
1445  } else {
1446  $label = htmlspecialchars( $button['value'] );
1447  }
1448 
1449  if ( $button['attribs'] ) {
1450  $attrs += $button['attribs'];
1451  }
1452 
1453  if ( isset( $button['id'] ) ) {
1454  $attrs['id'] = $button['id'];
1455  }
1456 
1457  if ( $useMediaWikiUIEverywhere ) {
1458  $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
1459  $attrs['class'][] = 'mw-ui-button';
1460  }
1461 
1462  $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
1463  }
1464 
1465  if ( !$buttons ) {
1466  return '';
1467  }
1468 
1469  return Html::rawElement( 'span',
1470  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1471  }
1472 
1478  public function getBody() {
1479  return $this->displaySection( $this->mFieldTree, $this->mTableId );
1480  }
1481 
1491  public function getErrorsOrWarnings( $elements, $elementsType ) {
1492  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1493  throw new DomainException( $elementsType . ' is not a valid type.' );
1494  }
1495  $elementstr = false;
1496  if ( $elements instanceof Status ) {
1497  list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1498  $status = $elementsType === 'error' ? $errorStatus : $warningStatus;
1499  if ( $status->isGood() ) {
1500  $elementstr = '';
1501  } else {
1502  $elementstr = $status
1503  ->getMessage()
1504  ->setContext( $this )
1505  ->setInterfaceMessageFlag( true )
1506  ->parse();
1507  }
1508  } elseif ( $elementsType === 'error' ) {
1509  if ( is_array( $elements ) ) {
1510  $elementstr = $this->formatErrors( $elements );
1511  } elseif ( $elements && $elements !== true ) {
1512  $elementstr = (string)$elements;
1513  }
1514  }
1515 
1516  return $elementstr
1517  ? Html::rawElement( 'div', [ 'class' => $elementsType . 'box' ], $elementstr )
1518  : '';
1519  }
1520 
1528  public function formatErrors( $errors ) {
1529  $errorstr = '';
1530 
1531  foreach ( $errors as $error ) {
1532  $errorstr .= Html::rawElement(
1533  'li',
1534  [],
1535  $this->getMessage( $error )->parse()
1536  );
1537  }
1538 
1539  $errorstr = Html::rawElement( 'ul', [], $errorstr );
1540 
1541  return $errorstr;
1542  }
1543 
1551  public function setSubmitText( $t ) {
1552  $this->mSubmitText = $t;
1553 
1554  return $this;
1555  }
1556 
1563  public function setSubmitDestructive() {
1564  $this->mSubmitFlags = [ 'destructive', 'primary' ];
1565 
1566  return $this;
1567  }
1568 
1577  public function setSubmitTextMsg( $msg ) {
1578  if ( !$msg instanceof Message ) {
1579  $msg = $this->msg( $msg );
1580  }
1581  $this->setSubmitText( $msg->text() );
1582 
1583  return $this;
1584  }
1585 
1590  public function getSubmitText() {
1591  return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text();
1592  }
1593 
1599  public function setSubmitName( $name ) {
1600  $this->mSubmitName = $name;
1601 
1602  return $this;
1603  }
1604 
1610  public function setSubmitTooltip( $name ) {
1611  $this->mSubmitTooltip = $name;
1612 
1613  return $this;
1614  }
1615 
1624  public function setSubmitID( $t ) {
1625  $this->mSubmitID = $t;
1626 
1627  return $this;
1628  }
1629 
1645  public function setFormIdentifier( $ident ) {
1646  $this->mFormIdentifier = $ident;
1647 
1648  return $this;
1649  }
1650 
1661  public function suppressDefaultSubmit( $suppressSubmit = true ) {
1662  $this->mShowSubmit = !$suppressSubmit;
1663 
1664  return $this;
1665  }
1666 
1673  public function showCancel( $show = true ) {
1674  $this->mShowCancel = $show;
1675  return $this;
1676  }
1677 
1684  public function setCancelTarget( $target ) {
1685  if ( $target instanceof PageReference ) {
1686  $target = TitleValue::castPageToLinkTarget( $target );
1687  }
1688 
1689  $this->mCancelTarget = $target;
1690  return $this;
1691  }
1692 
1697  protected function getCancelTargetURL() {
1698  if ( is_string( $this->mCancelTarget ) ) {
1699  return $this->mCancelTarget;
1700  } else {
1701  // TODO: use a service to get the local URL for a LinkTarget, see T282283
1702  $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1703  return $target->getLocalURL();
1704  }
1705  }
1706 
1716  public function setTableId( $id ) {
1717  $this->mTableId = $id;
1718 
1719  return $this;
1720  }
1721 
1727  public function setId( $id ) {
1728  $this->mId = $id;
1729 
1730  return $this;
1731  }
1732 
1737  public function setName( $name ) {
1738  $this->mName = $name;
1739 
1740  return $this;
1741  }
1742 
1754  public function setWrapperLegend( $legend ) {
1755  $this->mWrapperLegend = $legend;
1756 
1757  return $this;
1758  }
1759 
1767  public function setWrapperAttributes( $attributes ) {
1768  $this->mWrapperAttributes = $attributes;
1769 
1770  return $this;
1771  }
1772 
1782  public function setWrapperLegendMsg( $msg ) {
1783  if ( !$msg instanceof Message ) {
1784  $msg = $this->msg( $msg );
1785  }
1786  $this->setWrapperLegend( $msg->text() );
1787 
1788  return $this;
1789  }
1790 
1800  public function setMessagePrefix( $p ) {
1801  $this->mMessagePrefix = $p;
1802 
1803  return $this;
1804  }
1805 
1813  public function setTitle( $t ) {
1814  // TODO: make mTitle a PageReference when we have a better way to get URLs, see T282283.
1815  $this->mTitle = Title::castFromPageReference( $t );
1816 
1817  return $this;
1818  }
1819 
1823  public function getTitle() {
1824  return $this->mTitle ?: $this->getContext()->getTitle();
1825  }
1826 
1834  public function setMethod( $method = 'post' ) {
1835  $this->mMethod = strtolower( $method );
1836 
1837  return $this;
1838  }
1839 
1843  public function getMethod() {
1844  return $this->mMethod;
1845  }
1846 
1857  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1858  return Xml::fieldset( $legend, $section, $attributes ) . "\n";
1859  }
1860 
1878  public function displaySection( $fields,
1879  $sectionName = '',
1880  $fieldsetIDPrefix = '',
1881  &$hasUserVisibleFields = false
1882  ) {
1883  if ( $this->mFieldData === null ) {
1884  throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1885  . 'You probably called displayForm() without calling prepareForm() first.' );
1886  }
1887 
1888  $displayFormat = $this->getDisplayFormat();
1889 
1890  $html = [];
1891  $subsectionHtml = '';
1892  $hasLabel = false;
1893 
1894  // Conveniently, PHP method names are case-insensitive.
1895  // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1896  $getFieldHtmlMethod = $displayFormat === 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
1897 
1898  foreach ( $fields as $key => $value ) {
1899  if ( $value instanceof HTMLFormField ) {
1900  $v = array_key_exists( $key, $this->mFieldData )
1901  ? $this->mFieldData[$key]
1902  : $value->getDefault();
1903 
1904  $retval = $value->$getFieldHtmlMethod( $v );
1905 
1906  // check, if the form field should be added to
1907  // the output.
1908  if ( $value->hasVisibleOutput() ) {
1909  $html[] = $retval;
1910 
1911  $labelValue = trim( $value->getLabel() );
1912  if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
1913  $hasLabel = true;
1914  }
1915 
1916  $hasUserVisibleFields = true;
1917  }
1918  } elseif ( is_array( $value ) ) {
1919  $subsectionHasVisibleFields = false;
1920  $section =
1921  $this->displaySection( $value,
1922  "mw-htmlform-$key",
1923  "$fieldsetIDPrefix$key-",
1924  $subsectionHasVisibleFields );
1925  $legend = null;
1926 
1927  if ( $subsectionHasVisibleFields === true ) {
1928  // Display the section with various niceties.
1929  $hasUserVisibleFields = true;
1930 
1931  $legend = $this->getLegend( $key );
1932 
1933  $section = $this->getHeaderText( $key ) .
1934  $section .
1935  $this->getFooterText( $key );
1936 
1937  $attributes = [];
1938  if ( $fieldsetIDPrefix ) {
1939  $attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1940  }
1941  $subsectionHtml .= $this->wrapFieldSetSection(
1942  $legend, $section, $attributes, $fields === $this->mFieldTree
1943  );
1944  } else {
1945  // Just return the inputs, nothing fancy.
1946  $subsectionHtml .= $section;
1947  }
1948  }
1949  }
1950 
1951  $html = $this->formatSection( $html, $sectionName, $hasLabel );
1952 
1953  if ( $subsectionHtml ) {
1954  if ( $this->mSubSectionBeforeFields ) {
1955  return $subsectionHtml . "\n" . $html;
1956  } else {
1957  return $html . "\n" . $subsectionHtml;
1958  }
1959  } else {
1960  return $html;
1961  }
1962  }
1963 
1972  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1973  if ( !$fieldsHtml ) {
1974  // Do not generate any wrappers for empty sections. Sections may be empty if they only have
1975  // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
1976  return '';
1977  }
1978 
1979  $displayFormat = $this->getDisplayFormat();
1980  $html = implode( '', $fieldsHtml );
1981 
1982  if ( $displayFormat === 'raw' ) {
1983  return $html;
1984  }
1985 
1986  $classes = [];
1987 
1988  if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1989  $classes[] = 'mw-htmlform-nolabel';
1990  }
1991 
1992  $attribs = [ 'class' => $classes ];
1993 
1994  if ( $sectionName ) {
1995  $attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
1996  }
1997 
1998  if ( $displayFormat === 'table' ) {
1999  return Html::rawElement( 'table',
2000  $attribs,
2001  Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
2002  } elseif ( $displayFormat === 'inline' ) {
2003  return Html::rawElement( 'span', $attribs, "\n$html\n" );
2004  } else {
2005  return Html::rawElement( 'div', $attribs, "\n$html\n" );
2006  }
2007  }
2008 
2012  public function loadData() {
2013  $fieldData = [];
2014  $request = $this->getRequest();
2015 
2016  foreach ( $this->mFlatFields as $fieldname => $field ) {
2017  if ( $field->skipLoadData( $request ) ) {
2018  continue;
2019  }
2020  if ( $field->mParams['disabled'] ?? false ) {
2021  $fieldData[$fieldname] = $field->getDefault();
2022  } else {
2023  $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2024  }
2025  }
2026 
2027  // Reset to default for fields that are supposed to be disabled.
2028  // FIXME: Handle dependency chains, fields that a field checks on may need a reset too.
2029  foreach ( $fieldData as $name => &$value ) {
2030  $field = $this->mFlatFields[$name];
2031  if ( $field->isDisabled( $fieldData ) ) {
2032  $value = $field->getDefault();
2033  }
2034  }
2035 
2036  # Filter data.
2037  foreach ( $fieldData as $name => &$value ) {
2038  $field = $this->mFlatFields[$name];
2039  $value = $field->filter( $value, $this->mFlatFields );
2040  }
2041 
2042  $this->mFieldData = $fieldData;
2043  }
2044 
2052  public function suppressReset( $suppressReset = true ) {
2053  $this->mShowReset = !$suppressReset;
2054 
2055  return $this;
2056  }
2057 
2068  public function filterDataForSubmit( $data ) {
2069  return $data;
2070  }
2071 
2081  public function getLegend( $key ) {
2082  return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text();
2083  }
2084 
2095  public function setAction( $action ) {
2096  $this->mAction = $action;
2097 
2098  return $this;
2099  }
2100 
2108  public function getAction() {
2109  // If an action is already provided, return it
2110  if ( $this->mAction !== false ) {
2111  return $this->mAction;
2112  }
2113 
2114  $articlePath = $this->getConfig()->get( 'ArticlePath' );
2115  // Check whether we are in GET mode and the ArticlePath contains a "?"
2116  // meaning that getLocalURL() would return something like "index.php?title=...".
2117  // As browser remove the query string before submitting GET forms,
2118  // it means that the title would be lost. In such case use wfScript() instead
2119  // and put title in an hidden field (see getHiddenFields()).
2120  if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
2121  return wfScript();
2122  }
2123 
2124  return $this->getTitle()->getLocalURL();
2125  }
2126 
2137  public function setAutocomplete( $autocomplete ) {
2138  $this->mAutocomplete = $autocomplete;
2139 
2140  return $this;
2141  }
2142 
2149  protected function getMessage( $value ) {
2150  return Message::newFromSpecifier( $value )->setContext( $this );
2151  }
2152 
2162  public function needsJSForHtml5FormValidation() {
2163  foreach ( $this->mFlatFields as $fieldname => $field ) {
2164  if ( $field->needsJSForHtml5FormValidation() ) {
2165  return true;
2166  }
2167  }
2168  return false;
2169  }
2170 }
HTMLForm\getLegend
getLegend( $key)
Get a string to go in the "<legend>" of a section fieldset.
Definition: HTMLForm.php:2081
HTMLForm\$mTokenSalt
string array $mTokenSalt
Salt for the edit token.
Definition: HTMLForm.php:284
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
HTMLForm\setPostText
setPostText( $msg)
Set text at the end of the display.
Definition: HTMLForm.php:1112
HTMLForm\setAutocomplete
setAutocomplete( $autocomplete)
Set the value for the autocomplete attribute of the form.
Definition: HTMLForm.php:2137
Message\newFromSpecifier
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:399
HTMLForm\$mSubmitText
$mSubmitText
Definition: HTMLForm.php:229
HTMLForm\prepareForm
prepareForm()
Prepare form for submission.
Definition: HTMLForm.php:565
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
HTMLForm\hasField
hasField( $fieldname)
Definition: HTMLForm.php:428
HTMLForm\$mSubSectionBeforeFields
bool $mSubSectionBeforeFields
If true, sections that contain both fields and subsections will render their subsections before their...
Definition: HTMLForm.php:294
HTMLForm\setPreText
setPreText( $msg)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:835
HTMLForm\suppressDefaultSubmit
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1661
HTMLForm\$mSectionHeaders
$mSectionHeaders
Definition: HTMLForm.php:220
HTMLForm\getPreHtml
getPreHtml()
Get the introductory message HTML.
Definition: HTMLForm.php:823
HTMLForm\addPostHtml
addPostHtml( $html)
Add HTML to the end of the display.
Definition: HTMLForm.php:1062
HTMLForm\setSubmitName
setSubmitName( $name)
Definition: HTMLForm.php:1599
HTMLForm\setFooterText
setFooterText( $msg, $section=null)
Set footer text, inside the form.
Definition: HTMLForm.php:1038
HTMLForm\$mFieldTree
$mFieldTree
Definition: HTMLForm.php:206
HTMLForm\setIntro
setIntro( $msg)
Set the introductory message, overwriting any existing message.
Definition: HTMLForm.php:785
HTMLForm\tryAuthorizedSubmit
tryAuthorizedSubmit()
Try submitting, with edit token check first.
Definition: HTMLForm.php:588
HTMLForm\setWrapperAttributes
setWrapperAttributes( $attributes)
For internal use only.
Definition: HTMLForm.php:1767
HTMLForm\setSubmitTooltip
setSubmitTooltip( $name)
Definition: HTMLForm.php:1610
HTMLForm\addHeaderText
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:928
HTMLForm\$mTitle
$mTitle
Definition: HTMLForm.php:233
HTMLForm\addPreHtml
addPreHtml( $html)
Add HTML to introductory message.
Definition: HTMLForm.php:811
HTMLForm\setDisplayFormat
setDisplayFormat( $format)
Set format in which to display the form.
Definition: HTMLForm.php:454
HTMLForm\wrapForm
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Definition: HTMLForm.php:1326
Sanitizer\escapeIdForAttribute
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:811
HTMLForm\getHiddenFields
getHiddenFields()
Get the hidden fields that should go inside the form.
Definition: HTMLForm.php:1344
HTMLForm\$mCollapsed
bool $mCollapsed
Whether the form is collapsed by default.
Definition: HTMLForm.php:256
HTMLForm\getDisplayFormat
getDisplayFormat()
Getter for displayFormat.
Definition: HTMLForm.php:489
HTMLForm\$mWasSubmitted
$mWasSubmitted
Definition: HTMLForm.php:235
HTMLForm\$mFieldData
$mFieldData
Definition: HTMLForm.php:200
HTMLForm\setHeaderText
setHeaderText( $msg, $section=null)
Set header text, inside the form.
Definition: HTMLForm.php:942
HTMLForm\formatErrors
formatErrors( $errors)
Format a stack of error messages into a single HTML string.
Definition: HTMLForm.php:1528
HTMLForm\getClassFromDescriptor
static getClassFromDescriptor( $fieldname, &$descriptor)
Get the HTMLFormField subclass for this descriptor.
Definition: HTMLForm.php:510
HTMLForm\showCancel
showCancel( $show=true)
Show a cancel button (or prevent it).
Definition: HTMLForm.php:1673
HTMLForm\addFields
addFields( $descriptor)
Add fields to the form.
Definition: HTMLForm.php:391
HTMLForm\$mWrapperLegend
$mWrapperLegend
Definition: HTMLForm.php:277
HTMLForm\$mUseMultipart
$mUseMultipart
Definition: HTMLForm.php:265
HTMLForm\$mShowSubmit
$mShowSubmit
Definition: HTMLForm.php:208
HTMLForm\$mAutocomplete
string null $mAutocomplete
Form attribute autocomplete.
Definition: HTMLForm.php:263
HTMLForm\$mCancelTarget
$mCancelTarget
Definition: HTMLForm.php:212
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1214
HTMLForm\$mSubmitTooltip
$mSubmitTooltip
Definition: HTMLForm.php:230
HTMLForm\$displayFormat
string $displayFormat
Format in which to display form.
Definition: HTMLForm.php:301
HTMLForm\show
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition: HTMLForm.php:627
HTMLForm\$mSubmitID
$mSubmitID
Definition: HTMLForm.php:227
HTMLForm\setCancelTarget
setCancelTarget( $target)
Sets the target where the user is redirected to after clicking cancel.
Definition: HTMLForm.php:1684
HTMLForm\getBody
getBody()
Get the whole body of the form.
Definition: HTMLForm.php:1478
HTMLForm\addFooterHtml
addFooterHtml( $html, $section=null)
Add footer HTML, inside the form.
Definition: HTMLForm.php:968
VFormHTMLForm
Compact stacked vertical format for forms.
Definition: VFormHTMLForm.php:29
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:81
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:710
HTMLForm\$mShowReset
$mShowReset
Definition: HTMLForm.php:207
HTMLForm\$mTableId
$mTableId
Definition: HTMLForm.php:225
$res
$res
Definition: testCompression.php:57
HTMLForm\$mSubmitName
$mSubmitName
Definition: HTMLForm.php:228
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
HTMLForm\getAction
getAction()
Get the value for the action attribute of the form.
Definition: HTMLForm.php:2108
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
HTMLForm\setMethod
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1834
Linker\tooltipAndAccesskeyAttribs
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
Definition: Linker.php:2220
HTMLForm\setFooterHtml
setFooterHtml( $html, $section=null)
Set footer HTML, inside the form.
Definition: HTMLForm.php:990
HTMLForm\__construct
__construct( $descriptor, $context=null, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
Definition: HTMLForm.php:356
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
Xml\fieldset
static fieldset( $legend=false, $content=false, $attribs=[])
Shortcut for creating fieldsets.
Definition: Xml.php:624
HTMLForm\setSubmitText
setSubmitText( $t)
Set the text for the submit button.
Definition: HTMLForm.php:1551
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:29
HTMLForm\$mSubmitFlags
string[] $mSubmitFlags
Definition: HTMLForm.php:210
HTMLForm\trySubmit
trySubmit()
Validate all the fields, and call the submission callback function if everything is kosher.
Definition: HTMLForm.php:667
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:122
HTMLForm\setCollapsibleOptions
setCollapsibleOptions( $collapsedByDefault=false)
Enable collapsible mode, and set whether the form is collapsed by default.
Definition: HTMLForm.php:1280
TitleValue\castPageToLinkTarget
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
Definition: TitleValue.php:124
MWException
MediaWiki exception.
Definition: MWException.php:29
HTMLForm\$mAction
bool string $mAction
Form action URL.
Definition: HTMLForm.php:242
HTMLForm\setFormIdentifier
setFormIdentifier( $ident)
Set an internal identifier for this form.
Definition: HTMLForm.php:1645
HTMLForm\getHeaderText
getHeaderText( $section=null)
Get header text.
Definition: HTMLForm.php:955
HTMLForm\addButton
addButton( $data)
Add a button to the form.
Definition: HTMLForm.php:1173
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2268
HTMLForm\addHiddenField
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:1125
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:126
HTMLForm\setSubmitID
setSubmitID( $t)
Set the id for the submit button.
Definition: HTMLForm.php:1624
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:33
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:9
HTMLForm\getPostHtml
getPostHtml()
Get HTML at the end of the display.
Definition: HTMLForm.php:1088
HTMLForm\setPreHtml
setPreHtml( $html)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:797
$args
if( $line===false) $args
Definition: mcc.php:124
HTMLForm\wasSubmitted
wasSubmitted()
Test whether the form was considered to have been submitted or not, i.e.
Definition: HTMLForm.php:743
HTMLForm\$mFormIdentifier
$mFormIdentifier
Definition: HTMLForm.php:232
HTMLForm\formatSection
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: HTMLForm.php:1972
HTMLForm\$mButtons
array[] $mButtons
Definition: HTMLForm.php:275
HTMLForm\setMessagePrefix
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1800
HTMLForm\setSubmitCallback
setSubmitCallback( $cb)
Set a callback to a function to do something with the form once it's been successfully validated.
Definition: HTMLForm.php:757
HTMLForm\getFooterHtml
getFooterHtml( $section=null)
Get footer HTML.
Definition: HTMLForm.php:1007
HTMLForm\$mId
$mId
Definition: HTMLForm.php:223
HTMLForm\$mSectionFooters
$mSectionFooters
Definition: HTMLForm.php:221
HTMLForm\loadInputFromParameters
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
Definition: HTMLForm.php:540
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:63
HTMLForm\$mPost
$mPost
Definition: HTMLForm.php:222
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:197
HTMLForm\getErrorsOrWarnings
getErrorsOrWarnings( $elements, $elementsType)
Returns a formatted list of errors or warnings from the given elements.
Definition: HTMLForm.php:1491
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:834
HTMLForm\$mFooter
$mFooter
Definition: HTMLForm.php:219
HTMLForm\getFormAttributes
getFormAttributes()
Get HTML attributes for the <form> tag.
Definition: HTMLForm.php:1291
HTMLForm\$mMethod
$mMethod
Definition: HTMLForm.php:234
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
HTMLForm\setId
setId( $id)
Definition: HTMLForm.php:1727
HTMLForm\$availableSubclassDisplayFormats
array $availableSubclassDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:318
HTMLForm\$availableDisplayFormats
array $availableDisplayFormats
Available formats in which to display the form.
Definition: HTMLForm.php:307
HTMLForm\getPreText
getPreText()
Get the introductory message HTML.
Definition: HTMLForm.php:858
HTMLForm\$typeMappings
static string[] $typeMappings
A mapping of 'type' inputs onto standard HTMLFormField subclasses.
Definition: HTMLForm.php:153
HTMLForm\setPostHtml
setPostHtml( $html)
Set HTML at the end of the display.
Definition: HTMLForm.php:1076
HTMLForm\getHeaderHtml
getHeaderHtml( $section=null)
Get header HTML.
Definition: HTMLForm.php:911
HTMLForm\setWrapperLegend
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1754
HTMLForm\displaySection
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
Definition: HTMLForm.php:1878
HTMLForm\$mSubmitCallback
$mSubmitCallback
Definition: HTMLForm.php:214
HTMLForm\loadData
loadData()
Construct the form fields from the Descriptor array.
Definition: HTMLForm.php:2012
HTMLForm\showAlways
showAlways()
Same as self::show with the difference, that the form will be added to the output,...
Definition: HTMLForm.php:645
HTMLForm\$mName
$mName
Definition: HTMLForm.php:224
HTMLForm\setSubmitDestructive
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
Definition: HTMLForm.php:1563
HTMLForm\setValidationErrorMessage
setValidationErrorMessage( $msg)
Set a message to display on a validation error.
Definition: HTMLForm.php:771
HTMLForm\$mCollapsible
bool $mCollapsible
Whether the form can be collapsed.
Definition: HTMLForm.php:249
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
HTMLForm\getCancelTargetURL
getCancelTargetURL()
Definition: HTMLForm.php:1697
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1577
HTMLForm\setName
setName( $name)
Definition: HTMLForm.php:1737
HTMLForm\$mPre
$mPre
Definition: HTMLForm.php:217
HTMLForm\getHTML
getHTML( $submitResult)
Returns the raw HTML generated by the form.
Definition: HTMLForm.php:1248
HTMLForm\getSubmitText
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1590
HTMLForm\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:2149
HTMLForm\addHiddenFields
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:1142
HTMLForm\$mShowCancel
$mShowCancel
Definition: HTMLForm.php:211
HTMLForm\$mWrapperAttributes
$mWrapperAttributes
Definition: HTMLForm.php:278
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1782
HTMLForm\$mValidationErrorMessage
$mValidationErrorMessage
Definition: HTMLForm.php:215
HTMLForm\setAction
setAction( $action)
Set the value for the action attribute of the form.
Definition: HTMLForm.php:2095
HTMLForm\$mHeader
$mHeader
Definition: HTMLForm.php:218
HTMLForm\setTitle
setTitle( $t)
Set the title for form submission.
Definition: HTMLForm.php:1813
HTMLForm\setTableId
setTableId( $id)
Set the id of the <table> or outermost <div> element.
Definition: HTMLForm.php:1716
HTMLForm\$mMessagePrefix
$mMessagePrefix
Definition: HTMLForm.php:202
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:213
HTMLForm\displayForm
displayForm( $submitResult)
Display the form (sending to the context's OutputPage object), with an appropriate error message or s...
Definition: HTMLForm.php:1234
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:139
HTMLForm\getTitle
getTitle()
Definition: HTMLForm.php:1823
HTMLForm\filterDataForSubmit
filterDataForSubmit( $data)
Overload this if you want to apply special filtration routines to the form as a whole,...
Definition: HTMLForm.php:2068
HTMLForm\getMethod
getMethod()
Definition: HTMLForm.php:1843
Title\castFromPageReference
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
Definition: Title.php:339
HTMLForm\addHeaderHtml
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:871
HTMLForm\addPreText
addPreText( $msg)
Add HTML to introductory message.
Definition: HTMLForm.php:847
$t
$t
Definition: testCompression.php:74
Title\castFromLinkTarget
static castFromLinkTarget( $linkTarget)
Same as newFromLinkTarget, but if passed null, returns null.
Definition: Title.php:313
HTMLForm\getField
getField( $fieldname)
Definition: HTMLForm.php:437
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:235
HTMLForm\setHeaderHtml
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition: HTMLForm.php:893
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
HTMLForm\$mHiddenFields
array[] $mHiddenFields
Definition: HTMLForm.php:270
HTMLForm\addPostText
addPostText( $msg)
Add text to the end of the display.
Definition: HTMLForm.php:1100
HTMLForm\wrapFieldSetSection
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.
Definition: HTMLForm.php:1857
HTMLForm\suppressReset
suppressReset( $suppressReset=true)
Stop a reset button being shown for this form.
Definition: HTMLForm.php:2052
HTMLForm\getFooterText
getFooterText( $section=null)
Get footer text.
Definition: HTMLForm.php:1050
HTMLForm\$mFlatFields
HTMLFormField[] $mFlatFields
Definition: HTMLForm.php:205
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:332
HTMLForm\getButtons
getButtons()
Get the submit and (potentially) reset buttons.
Definition: HTMLForm.php:1378
HTMLForm\addFooterText
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition: HTMLForm.php:1024
Xml\submitButton
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition: Xml.php:466
HTMLForm\needsJSForHtml5FormValidation
needsJSForHtml5FormValidation()
Whether this form, with its current fields, requires the user agent to have JavaScript enabled for th...
Definition: HTMLForm.php:2162
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:149