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