Merge "mediawiki.action.edit.preview: Combine two the consecutive .push"
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
1 <?php
2
3 /**
4 * HTML form generation and submission handling.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Object handling generic submission, CSRF protection, layout and
26 * other logic for UI forms. in a reusable manner.
27 *
28 * In order to generate the form, the HTMLForm object takes an array
29 * structure detailing the form fields available. Each element of the
30 * array is a basic property-list, including the type of field, the
31 * label it is to be given in the form, callbacks for validation and
32 * 'filtering', and other pertinent information.
33 *
34 * Field types are implemented as subclasses of the generic HTMLFormField
35 * object, and typically implement at least getInputHTML, which generates
36 * the HTML for the input field to be placed in the table.
37 *
38 * You can find extensive documentation on the www.mediawiki.org wiki:
39 * - https://www.mediawiki.org/wiki/HTMLForm
40 * - https://www.mediawiki.org/wiki/HTMLForm/tutorial
41 *
42 * The constructor input is an associative array of $fieldname => $info,
43 * where $info is an Associative Array with any of the following:
44 *
45 * 'class' -- the subclass of HTMLFormField that will be used
46 * to create the object. *NOT* the CSS class!
47 * 'type' -- roughly translates into the <select> type attribute.
48 * if 'class' is not specified, this is used as a map
49 * through HTMLForm::$typeMappings to get the class name.
50 * 'default' -- default value when the form is displayed
51 * 'id' -- HTML id attribute
52 * 'cssclass' -- CSS class
53 * 'csshelpclass' -- CSS class used to style help text
54 * 'dir' -- Direction of the element.
55 * 'options' -- associative array mapping labels to values.
56 * Some field types support multi-level arrays.
57 * 'options-messages' -- associative array mapping message keys to values.
58 * Some field types support multi-level arrays.
59 * 'options-message' -- message key to be parsed to extract the list of
60 * options (like 'ipbreason-dropdown').
61 * 'label-message' -- message key for a message to use as the label.
62 * can be an array of msg key and then parameters to
63 * the message.
64 * 'label' -- alternatively, a raw text message. Overridden by
65 * label-message
66 * 'help' -- message text for a message to use as a help text.
67 * 'help-message' -- message key for a message to use as a help text.
68 * can be an array of msg key and then parameters to
69 * the message.
70 * Overwrites 'help-messages' and 'help'.
71 * 'help-messages' -- array of message key. As above, each item can
72 * be an array of msg key and then parameters.
73 * Overwrites 'help'.
74 * 'required' -- passed through to the object, indicating that it
75 * is a required field.
76 * 'size' -- the length of text fields
77 * 'filter-callback -- a function name to give you the chance to
78 * massage the inputted value before it's processed.
79 * @see HTMLFormField::filter()
80 * 'validation-callback' -- a function name to give you the chance
81 * to impose extra validation on the field input.
82 * @see HTMLFormField::validate()
83 * 'name' -- By default, the 'name' attribute of the input field
84 * is "wp{$fieldname}". If you want a different name
85 * (eg one without the "wp" prefix), specify it here and
86 * it will be used without modification.
87 * 'hide-if' -- expression given as an array stating when the field
88 * should be hidden. The first array value has to be the
89 * expression's logic operator. Supported expressions:
90 * 'NOT'
91 * [ 'NOT', array $expression ]
92 * To hide a field if a given expression is not true.
93 * '==='
94 * [ '===', string $fieldName, string $value ]
95 * To hide a field if another field identified by
96 * $field has the value $value.
97 * '!=='
98 * [ '!==', string $fieldName, string $value ]
99 * Same as [ 'NOT', [ '===', $fieldName, $value ]
100 * 'OR', 'AND', 'NOR', 'NAND'
101 * [ 'XXX', array $expression1, ..., array $expressionN ]
102 * To hide a field if one or more (OR), all (AND),
103 * neither (NOR) or not all (NAND) given expressions
104 * are evaluated as true.
105 * The expressions will be given to a JavaScript frontend
106 * module which will continually update the field's
107 * visibility.
108 *
109 * Since 1.20, you can chain mutators to ease the form generation:
110 * @par Example:
111 * @code
112 * $form = new HTMLForm( $someFields );
113 * $form->setMethod( 'get' )
114 * ->setWrapperLegendMsg( 'message-key' )
115 * ->prepareForm()
116 * ->displayForm( '' );
117 * @endcode
118 * Note that you will have prepareForm and displayForm at the end. Other
119 * methods call done after that would simply not be part of the form :(
120 *
121 * @todo Document 'section' / 'subsection' stuff
122 */
123 class HTMLForm extends ContextSource {
124 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
125 public static $typeMappings = array(
126 'api' => 'HTMLApiField',
127 'text' => 'HTMLTextField',
128 'textwithbutton' => 'HTMLTextFieldWithButton',
129 'textarea' => 'HTMLTextAreaField',
130 'select' => 'HTMLSelectField',
131 'combobox' => 'HTMLComboboxField',
132 'radio' => 'HTMLRadioField',
133 'multiselect' => 'HTMLMultiSelectField',
134 'limitselect' => 'HTMLSelectLimitField',
135 'check' => 'HTMLCheckField',
136 'toggle' => 'HTMLCheckField',
137 'int' => 'HTMLIntField',
138 'float' => 'HTMLFloatField',
139 'info' => 'HTMLInfoField',
140 'selectorother' => 'HTMLSelectOrOtherField',
141 'selectandother' => 'HTMLSelectAndOtherField',
142 'namespaceselect' => 'HTMLSelectNamespace',
143 'namespaceselectwithbutton' => 'HTMLSelectNamespaceWithButton',
144 'tagfilter' => 'HTMLTagFilter',
145 'submit' => 'HTMLSubmitField',
146 'hidden' => 'HTMLHiddenField',
147 'edittools' => 'HTMLEditTools',
148 'checkmatrix' => 'HTMLCheckMatrix',
149 'cloner' => 'HTMLFormFieldCloner',
150 'autocompleteselect' => 'HTMLAutoCompleteSelectField',
151 // HTMLTextField will output the correct type="" attribute automagically.
152 // There are about four zillion other HTML5 input types, like range, but
153 // we don't use those at the moment, so no point in adding all of them.
154 'email' => 'HTMLTextField',
155 'password' => 'HTMLTextField',
156 'url' => 'HTMLTextField',
157 'title' => 'HTMLTitleTextField',
158 'user' => 'HTMLUserTextField',
159 );
160
161 public $mFieldData;
162
163 protected $mMessagePrefix;
164
165 /** @var HTMLFormField[] */
166 protected $mFlatFields;
167
168 protected $mFieldTree;
169 protected $mShowReset = false;
170 protected $mShowSubmit = true;
171 protected $mSubmitFlags = array( 'constructive', 'primary' );
172
173 protected $mSubmitCallback;
174 protected $mValidationErrorMessage;
175
176 protected $mPre = '';
177 protected $mHeader = '';
178 protected $mFooter = '';
179 protected $mSectionHeaders = array();
180 protected $mSectionFooters = array();
181 protected $mPost = '';
182 protected $mId;
183 protected $mTableId = '';
184
185 protected $mSubmitID;
186 protected $mSubmitName;
187 protected $mSubmitText;
188 protected $mSubmitTooltip;
189
190 protected $mTitle;
191 protected $mMethod = 'post';
192 protected $mWasSubmitted = false;
193
194 /**
195 * Form action URL. false means we will use the URL to set Title
196 * @since 1.19
197 * @var bool|string
198 */
199 protected $mAction = false;
200
201 protected $mUseMultipart = false;
202 protected $mHiddenFields = array();
203 protected $mButtons = array();
204
205 protected $mWrapperLegend = false;
206
207 /**
208 * Salt for the edit token.
209 * @var string|array
210 */
211 protected $mTokenSalt = '';
212
213 /**
214 * If true, sections that contain both fields and subsections will
215 * render their subsections before their fields.
216 *
217 * Subclasses may set this to false to render subsections after fields
218 * instead.
219 */
220 protected $mSubSectionBeforeFields = true;
221
222 /**
223 * Format in which to display form. For viable options,
224 * @see $availableDisplayFormats
225 * @var string
226 */
227 protected $displayFormat = 'table';
228
229 /**
230 * Available formats in which to display the form
231 * @var array
232 */
233 protected $availableDisplayFormats = array(
234 'table',
235 'div',
236 'raw',
237 'inline',
238 );
239
240 /**
241 * Available formats in which to display the form
242 * @var array
243 */
244 protected $availableSubclassDisplayFormats = array(
245 'vform',
246 'ooui',
247 );
248
249 /**
250 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
251 *
252 * @param string $displayFormat
253 * @param mixed $arguments... Additional arguments to pass to the constructor.
254 * @return HTMLForm
255 */
256 public static function factory( $displayFormat/*, $arguments...*/ ) {
257 $arguments = func_get_args();
258 array_shift( $arguments );
259
260 switch ( $displayFormat ) {
261 case 'vform':
262 $reflector = new ReflectionClass( 'VFormHTMLForm' );
263 return $reflector->newInstanceArgs( $arguments );
264 case 'ooui':
265 $reflector = new ReflectionClass( 'OOUIHTMLForm' );
266 return $reflector->newInstanceArgs( $arguments );
267 default:
268 $reflector = new ReflectionClass( 'HTMLForm' );
269 $form = $reflector->newInstanceArgs( $arguments );
270 $form->setDisplayFormat( $displayFormat );
271 return $form;
272 }
273 }
274
275 /**
276 * Build a new HTMLForm from an array of field attributes
277 *
278 * @param array $descriptor Array of Field constructs, as described above
279 * @param IContextSource $context Available since 1.18, will become compulsory in 1.18.
280 * Obviates the need to call $form->setTitle()
281 * @param string $messagePrefix A prefix to go in front of default messages
282 */
283 public function __construct( $descriptor, /*IContextSource*/ $context = null,
284 $messagePrefix = ''
285 ) {
286 if ( $context instanceof IContextSource ) {
287 $this->setContext( $context );
288 $this->mTitle = false; // We don't need them to set a title
289 $this->mMessagePrefix = $messagePrefix;
290 } elseif ( is_null( $context ) && $messagePrefix !== '' ) {
291 $this->mMessagePrefix = $messagePrefix;
292 } elseif ( is_string( $context ) && $messagePrefix === '' ) {
293 // B/C since 1.18
294 // it's actually $messagePrefix
295 $this->mMessagePrefix = $context;
296 }
297
298 // Evil hack for mobile :(
299 if (
300 !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
301 && $this->displayFormat === 'table'
302 ) {
303 $this->displayFormat = 'div';
304 }
305
306 // Expand out into a tree.
307 $loadedDescriptor = array();
308 $this->mFlatFields = array();
309
310 foreach ( $descriptor as $fieldname => $info ) {
311 $section = isset( $info['section'] )
312 ? $info['section']
313 : '';
314
315 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
316 $this->mUseMultipart = true;
317 }
318
319 $field = static::loadInputFromParameters( $fieldname, $info, $this );
320
321 $setSection =& $loadedDescriptor;
322 if ( $section ) {
323 $sectionParts = explode( '/', $section );
324
325 while ( count( $sectionParts ) ) {
326 $newName = array_shift( $sectionParts );
327
328 if ( !isset( $setSection[$newName] ) ) {
329 $setSection[$newName] = array();
330 }
331
332 $setSection =& $setSection[$newName];
333 }
334 }
335
336 $setSection[$fieldname] = $field;
337 $this->mFlatFields[$fieldname] = $field;
338 }
339
340 $this->mFieldTree = $loadedDescriptor;
341 }
342
343 /**
344 * Set format in which to display the form
345 *
346 * @param string $format The name of the format to use, must be one of
347 * $this->availableDisplayFormats
348 *
349 * @throws MWException
350 * @since 1.20
351 * @return HTMLForm $this for chaining calls (since 1.20)
352 */
353 public function setDisplayFormat( $format ) {
354 if (
355 in_array( $format, $this->availableSubclassDisplayFormats ) ||
356 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats )
357 ) {
358 throw new MWException( 'Cannot change display format after creation, ' .
359 'use HTMLForm::factory() instead' );
360 }
361
362 if ( !in_array( $format, $this->availableDisplayFormats ) ) {
363 throw new MWException( 'Display format must be one of ' .
364 print_r( $this->availableDisplayFormats, true ) );
365 }
366
367 // Evil hack for mobile :(
368 if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
369 $format = 'div';
370 }
371
372 $this->displayFormat = $format;
373
374 return $this;
375 }
376
377 /**
378 * Getter for displayFormat
379 * @since 1.20
380 * @return string
381 */
382 public function getDisplayFormat() {
383 return $this->displayFormat;
384 }
385
386 /**
387 * Test if displayFormat is 'vform'
388 * @since 1.22
389 * @deprecated since 1.25
390 * @return bool
391 */
392 public function isVForm() {
393 wfDeprecated( __METHOD__, '1.25' );
394 return false;
395 }
396
397 /**
398 * Get the HTMLFormField subclass for this descriptor.
399 *
400 * The descriptor can be passed either 'class' which is the name of
401 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
402 * This makes sure the 'class' is always set, and also is returned by
403 * this function for ease.
404 *
405 * @since 1.23
406 *
407 * @param string $fieldname Name of the field
408 * @param array $descriptor Input Descriptor, as described above
409 *
410 * @throws MWException
411 * @return string Name of a HTMLFormField subclass
412 */
413 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
414 if ( isset( $descriptor['class'] ) ) {
415 $class = $descriptor['class'];
416 } elseif ( isset( $descriptor['type'] ) ) {
417 $class = static::$typeMappings[$descriptor['type']];
418 $descriptor['class'] = $class;
419 } else {
420 $class = null;
421 }
422
423 if ( !$class ) {
424 throw new MWException( "Descriptor with no class for $fieldname: "
425 . print_r( $descriptor, true ) );
426 }
427
428 return $class;
429 }
430
431 /**
432 * Initialise a new Object for the field
433 *
434 * @param string $fieldname Name of the field
435 * @param array $descriptor Input Descriptor, as described above
436 * @param HTMLForm|null $parent Parent instance of HTMLForm
437 *
438 * @throws MWException
439 * @return HTMLFormField Instance of a subclass of HTMLFormField
440 */
441 public static function loadInputFromParameters( $fieldname, $descriptor,
442 HTMLForm $parent = null
443 ) {
444 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
445
446 $descriptor['fieldname'] = $fieldname;
447 if ( $parent ) {
448 $descriptor['parent'] = $parent;
449 }
450
451 # @todo This will throw a fatal error whenever someone try to use
452 # 'class' to feed a CSS class instead of 'cssclass'. Would be
453 # great to avoid the fatal error and show a nice error.
454 $obj = new $class( $descriptor );
455
456 return $obj;
457 }
458
459 /**
460 * Prepare form for submission.
461 *
462 * @attention When doing method chaining, that should be the very last
463 * method call before displayForm().
464 *
465 * @throws MWException
466 * @return HTMLForm $this for chaining calls (since 1.20)
467 */
468 function prepareForm() {
469 # Check if we have the info we need
470 if ( !$this->mTitle instanceof Title && $this->mTitle !== false ) {
471 throw new MWException( "You must call setTitle() on an HTMLForm" );
472 }
473
474 # Load data from the request.
475 $this->loadData();
476
477 return $this;
478 }
479
480 /**
481 * Try submitting, with edit token check first
482 * @return Status|bool
483 */
484 function tryAuthorizedSubmit() {
485 $result = false;
486
487 $submit = false;
488 if ( $this->getMethod() != 'post' ) {
489 $submit = true; // no session check needed
490 } elseif ( $this->getRequest()->wasPosted() ) {
491 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
492 if ( $this->getUser()->isLoggedIn() || $editToken != null ) {
493 // Session tokens for logged-out users have no security value.
494 // However, if the user gave one, check it in order to give a nice
495 // "session expired" error instead of "permission denied" or such.
496 $submit = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
497 } else {
498 $submit = true;
499 }
500 }
501
502 if ( $submit ) {
503 $this->mWasSubmitted = true;
504 $result = $this->trySubmit();
505 }
506
507 return $result;
508 }
509
510 /**
511 * The here's-one-I-made-earlier option: do the submission if
512 * posted, or display the form with or without funky validation
513 * errors
514 * @return bool|Status Whether submission was successful.
515 */
516 function show() {
517 $this->prepareForm();
518
519 $result = $this->tryAuthorizedSubmit();
520 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
521 return $result;
522 }
523
524 $this->displayForm( $result );
525
526 return false;
527 }
528
529 /**
530 * Same as self::show with the difference, that the form will be
531 * added to the output, no matter, if the validation was good or not.
532 * @return bool|Status Whether submission was successful.
533 */
534 function showAlways() {
535 $this->prepareForm();
536
537 $result = $this->tryAuthorizedSubmit();
538
539 $this->displayForm( $result );
540
541 return $result;
542 }
543
544 /**
545 * Validate all the fields, and call the submission callback
546 * function if everything is kosher.
547 * @throws MWException
548 * @return bool|string|array|Status
549 * - Bool true or a good Status object indicates success,
550 * - Bool false indicates no submission was attempted,
551 * - Anything else indicates failure. The value may be a fatal Status
552 * object, an HTML string, or an array of arrays (message keys and
553 * params) or strings (message keys)
554 */
555 function trySubmit() {
556 $valid = true;
557 $hoistedErrors = array();
558 $hoistedErrors[] = isset( $this->mValidationErrorMessage )
559 ? $this->mValidationErrorMessage
560 : array( 'htmlform-invalid-input' );
561
562 $this->mWasSubmitted = true;
563
564 # Check for cancelled submission
565 foreach ( $this->mFlatFields as $fieldname => $field ) {
566 if ( !empty( $field->mParams['nodata'] ) ) {
567 continue;
568 }
569 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
570 $this->mWasSubmitted = false;
571 return false;
572 }
573 }
574
575 # Check for validation
576 foreach ( $this->mFlatFields as $fieldname => $field ) {
577 if ( !empty( $field->mParams['nodata'] ) ) {
578 continue;
579 }
580 if ( $field->isHidden( $this->mFieldData ) ) {
581 continue;
582 }
583 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
584 if ( $res !== true ) {
585 $valid = false;
586 if ( $res !== false && !$field->canDisplayErrors() ) {
587 $hoistedErrors[] = array( 'rawmessage', $res );
588 }
589 }
590 }
591
592 if ( !$valid ) {
593 if ( count( $hoistedErrors ) === 1 ) {
594 $hoistedErrors = $hoistedErrors[0];
595 }
596 return $hoistedErrors;
597 }
598
599 $callback = $this->mSubmitCallback;
600 if ( !is_callable( $callback ) ) {
601 throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
602 'setSubmitCallback() to set one.' );
603 }
604
605 $data = $this->filterDataForSubmit( $this->mFieldData );
606
607 $res = call_user_func( $callback, $data, $this );
608 if ( $res === false ) {
609 $this->mWasSubmitted = false;
610 }
611
612 return $res;
613 }
614
615 /**
616 * Test whether the form was considered to have been submitted or not, i.e.
617 * whether the last call to tryAuthorizedSubmit or trySubmit returned
618 * non-false.
619 *
620 * This will return false until HTMLForm::tryAuthorizedSubmit or
621 * HTMLForm::trySubmit is called.
622 *
623 * @since 1.23
624 * @return bool
625 */
626 function wasSubmitted() {
627 return $this->mWasSubmitted;
628 }
629
630 /**
631 * Set a callback to a function to do something with the form
632 * once it's been successfully validated.
633 *
634 * @param callable $cb The function will be passed the output from
635 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
636 * return as documented for HTMLForm::trySubmit
637 *
638 * @return HTMLForm $this for chaining calls (since 1.20)
639 */
640 function setSubmitCallback( $cb ) {
641 $this->mSubmitCallback = $cb;
642
643 return $this;
644 }
645
646 /**
647 * Set a message to display on a validation error.
648 *
649 * @param string|array $msg String or Array of valid inputs to wfMessage()
650 * (so each entry can be either a String or Array)
651 *
652 * @return HTMLForm $this for chaining calls (since 1.20)
653 */
654 function setValidationErrorMessage( $msg ) {
655 $this->mValidationErrorMessage = $msg;
656
657 return $this;
658 }
659
660 /**
661 * Set the introductory message, overwriting any existing message.
662 *
663 * @param string $msg Complete text of message to display
664 *
665 * @return HTMLForm $this for chaining calls (since 1.20)
666 */
667 function setIntro( $msg ) {
668 $this->setPreText( $msg );
669
670 return $this;
671 }
672
673 /**
674 * Set the introductory message, overwriting any existing message.
675 * @since 1.19
676 *
677 * @param string $msg Complete text of message to display
678 *
679 * @return HTMLForm $this for chaining calls (since 1.20)
680 */
681 function setPreText( $msg ) {
682 $this->mPre = $msg;
683
684 return $this;
685 }
686
687 /**
688 * Add introductory text.
689 *
690 * @param string $msg Complete text of message to display
691 *
692 * @return HTMLForm $this for chaining calls (since 1.20)
693 */
694 function addPreText( $msg ) {
695 $this->mPre .= $msg;
696
697 return $this;
698 }
699
700 /**
701 * Add header text, inside the form.
702 *
703 * @param string $msg Complete text of message to display
704 * @param string|null $section The section to add the header to
705 *
706 * @return HTMLForm $this for chaining calls (since 1.20)
707 */
708 function addHeaderText( $msg, $section = null ) {
709 if ( is_null( $section ) ) {
710 $this->mHeader .= $msg;
711 } else {
712 if ( !isset( $this->mSectionHeaders[$section] ) ) {
713 $this->mSectionHeaders[$section] = '';
714 }
715 $this->mSectionHeaders[$section] .= $msg;
716 }
717
718 return $this;
719 }
720
721 /**
722 * Set header text, inside the form.
723 * @since 1.19
724 *
725 * @param string $msg Complete text of message to display
726 * @param string|null $section The section to add the header to
727 *
728 * @return HTMLForm $this for chaining calls (since 1.20)
729 */
730 function setHeaderText( $msg, $section = null ) {
731 if ( is_null( $section ) ) {
732 $this->mHeader = $msg;
733 } else {
734 $this->mSectionHeaders[$section] = $msg;
735 }
736
737 return $this;
738 }
739
740 /**
741 * Get header text.
742 *
743 * @param string|null $section The section to get the header text for
744 * @since 1.26
745 * @return string
746 */
747 function getHeaderText( $section = null ) {
748 if ( is_null( $section ) ) {
749 return $this->mHeader;
750 } else {
751 return isset( $this->mSectionHeaders[$section] ) ? $this->mSectionHeaders[$section] : '';
752 }
753 }
754
755 /**
756 * Add footer text, inside the form.
757 *
758 * @param string $msg Complete text of message to display
759 * @param string|null $section The section to add the footer text to
760 *
761 * @return HTMLForm $this for chaining calls (since 1.20)
762 */
763 function addFooterText( $msg, $section = null ) {
764 if ( is_null( $section ) ) {
765 $this->mFooter .= $msg;
766 } else {
767 if ( !isset( $this->mSectionFooters[$section] ) ) {
768 $this->mSectionFooters[$section] = '';
769 }
770 $this->mSectionFooters[$section] .= $msg;
771 }
772
773 return $this;
774 }
775
776 /**
777 * Set footer text, inside the form.
778 * @since 1.19
779 *
780 * @param string $msg Complete text of message to display
781 * @param string|null $section The section to add the footer text to
782 *
783 * @return HTMLForm $this for chaining calls (since 1.20)
784 */
785 function setFooterText( $msg, $section = null ) {
786 if ( is_null( $section ) ) {
787 $this->mFooter = $msg;
788 } else {
789 $this->mSectionFooters[$section] = $msg;
790 }
791
792 return $this;
793 }
794
795 /**
796 * Get footer text.
797 *
798 * @param string|null $section The section to get the footer text for
799 * @since 1.26
800 * @return string
801 */
802 function getFooterText( $section = null ) {
803 if ( is_null( $section ) ) {
804 return $this->mFooter;
805 } else {
806 return isset( $this->mSectionFooters[$section] ) ? $this->mSectionFooters[$section] : '';
807 }
808 }
809
810 /**
811 * Add text to the end of the display.
812 *
813 * @param string $msg Complete text of message to display
814 *
815 * @return HTMLForm $this for chaining calls (since 1.20)
816 */
817 function addPostText( $msg ) {
818 $this->mPost .= $msg;
819
820 return $this;
821 }
822
823 /**
824 * Set text at the end of the display.
825 *
826 * @param string $msg Complete text of message to display
827 *
828 * @return HTMLForm $this for chaining calls (since 1.20)
829 */
830 function setPostText( $msg ) {
831 $this->mPost = $msg;
832
833 return $this;
834 }
835
836 /**
837 * Add a hidden field to the output
838 *
839 * @param string $name Field name. This will be used exactly as entered
840 * @param string $value Field value
841 * @param array $attribs
842 *
843 * @return HTMLForm $this for chaining calls (since 1.20)
844 */
845 public function addHiddenField( $name, $value, $attribs = array() ) {
846 $attribs += array( 'name' => $name );
847 $this->mHiddenFields[] = array( $value, $attribs );
848
849 return $this;
850 }
851
852 /**
853 * Add an array of hidden fields to the output
854 *
855 * @since 1.22
856 *
857 * @param array $fields Associative array of fields to add;
858 * mapping names to their values
859 *
860 * @return HTMLForm $this for chaining calls
861 */
862 public function addHiddenFields( array $fields ) {
863 foreach ( $fields as $name => $value ) {
864 $this->mHiddenFields[] = array( $value, array( 'name' => $name ) );
865 }
866
867 return $this;
868 }
869
870 /**
871 * Add a button to the form
872 *
873 * @param string $name Field name.
874 * @param string $value Field value
875 * @param string $id DOM id for the button (default: null)
876 * @param array $attribs
877 *
878 * @return HTMLForm $this for chaining calls (since 1.20)
879 */
880 public function addButton( $name, $value, $id = null, $attribs = null ) {
881 $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
882
883 return $this;
884 }
885
886 /**
887 * Set the salt for the edit token.
888 *
889 * Only useful when the method is "post".
890 *
891 * @since 1.24
892 * @param string|array $salt Salt to use
893 * @return HTMLForm $this For chaining calls
894 */
895 public function setTokenSalt( $salt ) {
896 $this->mTokenSalt = $salt;
897
898 return $this;
899 }
900
901 /**
902 * Display the form (sending to the context's OutputPage object), with an
903 * appropriate error message or stack of messages, and any validation errors, etc.
904 *
905 * @attention You should call prepareForm() before calling this function.
906 * Moreover, when doing method chaining this should be the very last method
907 * call just after prepareForm().
908 *
909 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
910 *
911 * @return void Nothing, should be last call
912 */
913 function displayForm( $submitResult ) {
914 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
915 }
916
917 /**
918 * Returns the raw HTML generated by the form
919 *
920 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
921 *
922 * @return string
923 */
924 function getHTML( $submitResult ) {
925 # For good measure (it is the default)
926 $this->getOutput()->preventClickjacking();
927 $this->getOutput()->addModules( 'mediawiki.htmlform' );
928 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
929
930 $html = ''
931 . $this->getErrors( $submitResult )
932 . $this->getHeaderText()
933 . $this->getBody()
934 . $this->getHiddenFields()
935 . $this->getButtons()
936 . $this->getFooterText();
937
938 $html = $this->wrapForm( $html );
939
940 return '' . $this->mPre . $html . $this->mPost;
941 }
942
943 /**
944 * Get HTML attributes for the `<form>` tag.
945 * @return array
946 */
947 protected function getFormAttributes() {
948 # Use multipart/form-data
949 $encType = $this->mUseMultipart
950 ? 'multipart/form-data'
951 : 'application/x-www-form-urlencoded';
952 # Attributes
953 $attribs = array(
954 'action' => $this->getAction(),
955 'method' => $this->getMethod(),
956 'enctype' => $encType,
957 );
958 if ( !empty( $this->mId ) ) {
959 $attribs['id'] = $this->mId;
960 }
961 return $attribs;
962 }
963
964 /**
965 * Wrap the form innards in an actual "<form>" element
966 *
967 * @param string $html HTML contents to wrap.
968 *
969 * @return string Wrapped HTML.
970 */
971 function wrapForm( $html ) {
972 # Include a <fieldset> wrapper for style, if requested.
973 if ( $this->mWrapperLegend !== false ) {
974 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
975 $html = Xml::fieldset( $legend, $html );
976 }
977
978 return Html::rawElement(
979 'form',
980 $this->getFormAttributes() + array( 'class' => 'visualClear' ),
981 $html
982 );
983 }
984
985 /**
986 * Get the hidden fields that should go inside the form.
987 * @return string HTML.
988 */
989 function getHiddenFields() {
990 $html = '';
991 if ( $this->getMethod() == 'post' ) {
992 $html .= Html::hidden(
993 'wpEditToken',
994 $this->getUser()->getEditToken( $this->mTokenSalt ),
995 array( 'id' => 'wpEditToken' )
996 ) . "\n";
997 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
998 }
999
1000 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1001 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() == 'get' ) {
1002 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1003 }
1004
1005 foreach ( $this->mHiddenFields as $data ) {
1006 list( $value, $attribs ) = $data;
1007 $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1008 }
1009
1010 return $html;
1011 }
1012
1013 /**
1014 * Get the submit and (potentially) reset buttons.
1015 * @return string HTML.
1016 */
1017 function getButtons() {
1018 $buttons = '';
1019 $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1020
1021 if ( $this->mShowSubmit ) {
1022 $attribs = array();
1023
1024 if ( isset( $this->mSubmitID ) ) {
1025 $attribs['id'] = $this->mSubmitID;
1026 }
1027
1028 if ( isset( $this->mSubmitName ) ) {
1029 $attribs['name'] = $this->mSubmitName;
1030 }
1031
1032 if ( isset( $this->mSubmitTooltip ) ) {
1033 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1034 }
1035
1036 $attribs['class'] = array( 'mw-htmlform-submit' );
1037
1038 if ( $useMediaWikiUIEverywhere ) {
1039 foreach ( $this->mSubmitFlags as $flag ) {
1040 array_push( $attribs['class'], 'mw-ui-' . $flag );
1041 }
1042 array_push( $attribs['class'], 'mw-ui-button' );
1043 }
1044
1045 $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1046 }
1047
1048 if ( $this->mShowReset ) {
1049 $buttons .= Html::element(
1050 'input',
1051 array(
1052 'type' => 'reset',
1053 'value' => $this->msg( 'htmlform-reset' )->text(),
1054 'class' => ( $useMediaWikiUIEverywhere ? 'mw-ui-button' : null ),
1055 )
1056 ) . "\n";
1057 }
1058
1059 foreach ( $this->mButtons as $button ) {
1060 $attrs = array(
1061 'type' => 'submit',
1062 'name' => $button['name'],
1063 'value' => $button['value']
1064 );
1065
1066 if ( $button['attribs'] ) {
1067 $attrs += $button['attribs'];
1068 }
1069
1070 if ( isset( $button['id'] ) ) {
1071 $attrs['id'] = $button['id'];
1072 }
1073
1074 if ( $useMediaWikiUIEverywhere ) {
1075 $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : array();
1076 $attrs['class'][] = 'mw-ui-button';
1077 }
1078
1079 $buttons .= Html::element( 'input', $attrs ) . "\n";
1080 }
1081
1082 $html = Html::rawElement( 'span',
1083 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
1084
1085 return $html;
1086 }
1087
1088 /**
1089 * Get the whole body of the form.
1090 * @return string
1091 */
1092 function getBody() {
1093 return $this->displaySection( $this->mFieldTree, $this->mTableId );
1094 }
1095
1096 /**
1097 * Format and display an error message stack.
1098 *
1099 * @param string|array|Status $errors
1100 *
1101 * @return string
1102 */
1103 function getErrors( $errors ) {
1104 if ( $errors instanceof Status ) {
1105 if ( $errors->isOK() ) {
1106 $errorstr = '';
1107 } else {
1108 $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
1109 }
1110 } elseif ( is_array( $errors ) ) {
1111 $errorstr = $this->formatErrors( $errors );
1112 } else {
1113 $errorstr = $errors;
1114 }
1115
1116 return $errorstr
1117 ? Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
1118 : '';
1119 }
1120
1121 /**
1122 * Format a stack of error messages into a single HTML string
1123 *
1124 * @param array $errors Array of message keys/values
1125 *
1126 * @return string HTML, a "<ul>" list of errors
1127 */
1128 public function formatErrors( $errors ) {
1129 $errorstr = '';
1130
1131 foreach ( $errors as $error ) {
1132 if ( is_array( $error ) ) {
1133 $msg = array_shift( $error );
1134 } else {
1135 $msg = $error;
1136 $error = array();
1137 }
1138
1139 $errorstr .= Html::rawElement(
1140 'li',
1141 array(),
1142 $this->msg( $msg, $error )->parse()
1143 );
1144 }
1145
1146 $errorstr = Html::rawElement( 'ul', array(), $errorstr );
1147
1148 return $errorstr;
1149 }
1150
1151 /**
1152 * Set the text for the submit button
1153 *
1154 * @param string $t Plaintext
1155 *
1156 * @return HTMLForm $this for chaining calls (since 1.20)
1157 */
1158 function setSubmitText( $t ) {
1159 $this->mSubmitText = $t;
1160
1161 return $this;
1162 }
1163
1164 /**
1165 * Identify that the submit button in the form has a destructive action
1166 * @since 1.24
1167 */
1168 public function setSubmitDestructive() {
1169 $this->mSubmitFlags = array( 'destructive', 'primary' );
1170
1171 return $this;
1172 }
1173
1174 /**
1175 * Identify that the submit button in the form has a progressive action
1176 * @since 1.25
1177 */
1178 public function setSubmitProgressive() {
1179 $this->mSubmitFlags = array( 'progressive', 'primary' );
1180
1181 return $this;
1182 }
1183
1184 /**
1185 * Set the text for the submit button to a message
1186 * @since 1.19
1187 *
1188 * @param string|Message $msg Message key or Message object
1189 *
1190 * @return HTMLForm $this for chaining calls (since 1.20)
1191 */
1192 public function setSubmitTextMsg( $msg ) {
1193 if ( !$msg instanceof Message ) {
1194 $msg = $this->msg( $msg );
1195 }
1196 $this->setSubmitText( $msg->text() );
1197
1198 return $this;
1199 }
1200
1201 /**
1202 * Get the text for the submit button, either customised or a default.
1203 * @return string
1204 */
1205 function getSubmitText() {
1206 return $this->mSubmitText
1207 ? $this->mSubmitText
1208 : $this->msg( 'htmlform-submit' )->text();
1209 }
1210
1211 /**
1212 * @param string $name Submit button name
1213 *
1214 * @return HTMLForm $this for chaining calls (since 1.20)
1215 */
1216 public function setSubmitName( $name ) {
1217 $this->mSubmitName = $name;
1218
1219 return $this;
1220 }
1221
1222 /**
1223 * @param string $name Tooltip for the submit button
1224 *
1225 * @return HTMLForm $this for chaining calls (since 1.20)
1226 */
1227 public function setSubmitTooltip( $name ) {
1228 $this->mSubmitTooltip = $name;
1229
1230 return $this;
1231 }
1232
1233 /**
1234 * Set the id for the submit button.
1235 *
1236 * @param string $t
1237 *
1238 * @todo FIXME: Integrity of $t is *not* validated
1239 * @return HTMLForm $this for chaining calls (since 1.20)
1240 */
1241 function setSubmitID( $t ) {
1242 $this->mSubmitID = $t;
1243
1244 return $this;
1245 }
1246
1247 /**
1248 * Stop a default submit button being shown for this form. This implies that an
1249 * alternate submit method must be provided manually.
1250 *
1251 * @since 1.22
1252 *
1253 * @param bool $suppressSubmit Set to false to re-enable the button again
1254 *
1255 * @return HTMLForm $this for chaining calls
1256 */
1257 function suppressDefaultSubmit( $suppressSubmit = true ) {
1258 $this->mShowSubmit = !$suppressSubmit;
1259
1260 return $this;
1261 }
1262
1263 /**
1264 * Set the id of the \<table\> or outermost \<div\> element.
1265 *
1266 * @since 1.22
1267 *
1268 * @param string $id New value of the id attribute, or "" to remove
1269 *
1270 * @return HTMLForm $this for chaining calls
1271 */
1272 public function setTableId( $id ) {
1273 $this->mTableId = $id;
1274
1275 return $this;
1276 }
1277
1278 /**
1279 * @param string $id DOM id for the form
1280 *
1281 * @return HTMLForm $this for chaining calls (since 1.20)
1282 */
1283 public function setId( $id ) {
1284 $this->mId = $id;
1285
1286 return $this;
1287 }
1288
1289 /**
1290 * Prompt the whole form to be wrapped in a "<fieldset>", with
1291 * this text as its "<legend>" element.
1292 *
1293 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1294 * If true, a wrapper will be displayed, but no legend.
1295 * If a string, a wrapper will be displayed with that string as a legend.
1296 * The string will be escaped before being output (this doesn't support HTML).
1297 *
1298 * @return HTMLForm $this for chaining calls (since 1.20)
1299 */
1300 public function setWrapperLegend( $legend ) {
1301 $this->mWrapperLegend = $legend;
1302
1303 return $this;
1304 }
1305
1306 /**
1307 * Prompt the whole form to be wrapped in a "<fieldset>", with
1308 * this message as its "<legend>" element.
1309 * @since 1.19
1310 *
1311 * @param string|Message $msg Message key or Message object
1312 *
1313 * @return HTMLForm $this for chaining calls (since 1.20)
1314 */
1315 public function setWrapperLegendMsg( $msg ) {
1316 if ( !$msg instanceof Message ) {
1317 $msg = $this->msg( $msg );
1318 }
1319 $this->setWrapperLegend( $msg->text() );
1320
1321 return $this;
1322 }
1323
1324 /**
1325 * Set the prefix for various default messages
1326 * @todo Currently only used for the "<fieldset>" legend on forms
1327 * with multiple sections; should be used elsewhere?
1328 *
1329 * @param string $p
1330 *
1331 * @return HTMLForm $this for chaining calls (since 1.20)
1332 */
1333 function setMessagePrefix( $p ) {
1334 $this->mMessagePrefix = $p;
1335
1336 return $this;
1337 }
1338
1339 /**
1340 * Set the title for form submission
1341 *
1342 * @param Title $t Title of page the form is on/should be posted to
1343 *
1344 * @return HTMLForm $this for chaining calls (since 1.20)
1345 */
1346 function setTitle( $t ) {
1347 $this->mTitle = $t;
1348
1349 return $this;
1350 }
1351
1352 /**
1353 * Get the title
1354 * @return Title
1355 */
1356 function getTitle() {
1357 return $this->mTitle === false
1358 ? $this->getContext()->getTitle()
1359 : $this->mTitle;
1360 }
1361
1362 /**
1363 * Set the method used to submit the form
1364 *
1365 * @param string $method
1366 *
1367 * @return HTMLForm $this for chaining calls (since 1.20)
1368 */
1369 public function setMethod( $method = 'post' ) {
1370 $this->mMethod = strtolower( $method );
1371
1372 return $this;
1373 }
1374
1375 /**
1376 * @return string Always lowercase
1377 */
1378 public function getMethod() {
1379 return $this->mMethod;
1380 }
1381
1382 /**
1383 * Wraps the given $section into an user-visible fieldset.
1384 *
1385 * @param string $legend Legend text for the fieldset
1386 * @param string $section The section content in plain Html
1387 * @param array $attributes Additional attributes for the fieldset
1388 * @return string The fieldset's Html
1389 */
1390 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
1391 return Xml::fieldset( $legend, $section, $attributes ) . "\n";
1392 }
1393
1394 /**
1395 * @todo Document
1396 *
1397 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
1398 * objects).
1399 * @param string $sectionName ID attribute of the "<table>" tag for this
1400 * section, ignored if empty.
1401 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
1402 * each subsection, ignored if empty.
1403 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
1404 *
1405 * @return string
1406 */
1407 public function displaySection( $fields,
1408 $sectionName = '',
1409 $fieldsetIDPrefix = '',
1410 &$hasUserVisibleFields = false ) {
1411 $displayFormat = $this->getDisplayFormat();
1412
1413 $html = array();
1414 $subsectionHtml = '';
1415 $hasLabel = false;
1416
1417 // Conveniently, PHP method names are case-insensitive.
1418 // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1419 $getFieldHtmlMethod = $displayFormat == 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
1420
1421 foreach ( $fields as $key => $value ) {
1422 if ( $value instanceof HTMLFormField ) {
1423 $v = empty( $value->mParams['nodata'] )
1424 ? $this->mFieldData[$key]
1425 : $value->getDefault();
1426
1427 $retval = $value->$getFieldHtmlMethod( $v );
1428
1429 // check, if the form field should be added to
1430 // the output.
1431 if ( $value->hasVisibleOutput() ) {
1432 $html[] = $retval;
1433
1434 $labelValue = trim( $value->getLabel() );
1435 if ( $labelValue != '&#160;' && $labelValue !== '' ) {
1436 $hasLabel = true;
1437 }
1438
1439 $hasUserVisibleFields = true;
1440 }
1441 } elseif ( is_array( $value ) ) {
1442 $subsectionHasVisibleFields = false;
1443 $section =
1444 $this->displaySection( $value,
1445 "mw-htmlform-$key",
1446 "$fieldsetIDPrefix$key-",
1447 $subsectionHasVisibleFields );
1448 $legend = null;
1449
1450 if ( $subsectionHasVisibleFields === true ) {
1451 // Display the section with various niceties.
1452 $hasUserVisibleFields = true;
1453
1454 $legend = $this->getLegend( $key );
1455
1456 $section = $this->getHeaderText( $key ) .
1457 $section .
1458 $this->getFooterText( $key );
1459
1460 $attributes = array();
1461 if ( $fieldsetIDPrefix ) {
1462 $attributes['id'] = Sanitizer::escapeId( "$fieldsetIDPrefix$key" );
1463 }
1464 $subsectionHtml .= $this->wrapFieldSetSection( $legend, $section, $attributes );
1465 } else {
1466 // Just return the inputs, nothing fancy.
1467 $subsectionHtml .= $section;
1468 }
1469 }
1470 }
1471
1472 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1473
1474 if ( $subsectionHtml ) {
1475 if ( $this->mSubSectionBeforeFields ) {
1476 return $subsectionHtml . "\n" . $html;
1477 } else {
1478 return $html . "\n" . $subsectionHtml;
1479 }
1480 } else {
1481 return $html;
1482 }
1483 }
1484
1485 /**
1486 * Put a form section together from the individual fields' HTML, merging it and wrapping.
1487 * @param array $fieldsHtml
1488 * @param string $sectionName
1489 * @param bool $anyFieldHasLabel
1490 * @return string HTML
1491 */
1492 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1493 $displayFormat = $this->getDisplayFormat();
1494 $html = implode( '', $fieldsHtml );
1495
1496 if ( $displayFormat === 'raw' ) {
1497 return $html;
1498 }
1499
1500 $classes = array();
1501
1502 if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1503 $classes[] = 'mw-htmlform-nolabel';
1504 }
1505
1506 $attribs = array(
1507 'class' => implode( ' ', $classes ),
1508 );
1509
1510 if ( $sectionName ) {
1511 $attribs['id'] = Sanitizer::escapeId( $sectionName );
1512 }
1513
1514 if ( $displayFormat === 'table' ) {
1515 return Html::rawElement( 'table',
1516 $attribs,
1517 Html::rawElement( 'tbody', array(), "\n$html\n" ) ) . "\n";
1518 } elseif ( $displayFormat === 'inline' ) {
1519 return Html::rawElement( 'span', $attribs, "\n$html\n" );
1520 } else {
1521 return Html::rawElement( 'div', $attribs, "\n$html\n" );
1522 }
1523 }
1524
1525 /**
1526 * Construct the form fields from the Descriptor array
1527 */
1528 function loadData() {
1529 $fieldData = array();
1530
1531 foreach ( $this->mFlatFields as $fieldname => $field ) {
1532 if ( !empty( $field->mParams['nodata'] ) ) {
1533 continue;
1534 } elseif ( !empty( $field->mParams['disabled'] ) ) {
1535 $fieldData[$fieldname] = $field->getDefault();
1536 } else {
1537 $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
1538 }
1539 }
1540
1541 # Filter data.
1542 foreach ( $fieldData as $name => &$value ) {
1543 $field = $this->mFlatFields[$name];
1544 $value = $field->filter( $value, $this->mFlatFields );
1545 }
1546
1547 $this->mFieldData = $fieldData;
1548 }
1549
1550 /**
1551 * Stop a reset button being shown for this form
1552 *
1553 * @param bool $suppressReset Set to false to re-enable the button again
1554 *
1555 * @return HTMLForm $this for chaining calls (since 1.20)
1556 */
1557 function suppressReset( $suppressReset = true ) {
1558 $this->mShowReset = !$suppressReset;
1559
1560 return $this;
1561 }
1562
1563 /**
1564 * Overload this if you want to apply special filtration routines
1565 * to the form as a whole, after it's submitted but before it's
1566 * processed.
1567 *
1568 * @param array $data
1569 *
1570 * @return array
1571 */
1572 function filterDataForSubmit( $data ) {
1573 return $data;
1574 }
1575
1576 /**
1577 * Get a string to go in the "<legend>" of a section fieldset.
1578 * Override this if you want something more complicated.
1579 *
1580 * @param string $key
1581 *
1582 * @return string
1583 */
1584 public function getLegend( $key ) {
1585 return $this->msg( "{$this->mMessagePrefix}-$key" )->text();
1586 }
1587
1588 /**
1589 * Set the value for the action attribute of the form.
1590 * When set to false (which is the default state), the set title is used.
1591 *
1592 * @since 1.19
1593 *
1594 * @param string|bool $action
1595 *
1596 * @return HTMLForm $this for chaining calls (since 1.20)
1597 */
1598 public function setAction( $action ) {
1599 $this->mAction = $action;
1600
1601 return $this;
1602 }
1603
1604 /**
1605 * Get the value for the action attribute of the form.
1606 *
1607 * @since 1.22
1608 *
1609 * @return string
1610 */
1611 public function getAction() {
1612 // If an action is alredy provided, return it
1613 if ( $this->mAction !== false ) {
1614 return $this->mAction;
1615 }
1616
1617 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1618 // Check whether we are in GET mode and the ArticlePath contains a "?"
1619 // meaning that getLocalURL() would return something like "index.php?title=...".
1620 // As browser remove the query string before submitting GET forms,
1621 // it means that the title would be lost. In such case use wfScript() instead
1622 // and put title in an hidden field (see getHiddenFields()).
1623 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1624 return wfScript();
1625 }
1626
1627 return $this->getTitle()->getLocalURL();
1628 }
1629 }