Create a user.groups module in ResourceLoader, which bundles a CSS and JS page for...
[lhc/web/wiklou.git] / includes / HTMLForm.php
1 <?php
2 /**
3 * Object handling generic submission, CSRF protection, layout and
4 * other logic for UI forms. in a reusable manner.
5 *
6 * In order to generate the form, the HTMLForm object takes an array
7 * structure detailing the form fields available. Each element of the
8 * array is a basic property-list, including the type of field, the
9 * label it is to be given in the form, callbacks for validation and
10 * 'filtering', and other pertinent information.
11 *
12 * Field types are implemented as subclasses of the generic HTMLFormField
13 * object, and typically implement at least getInputHTML, which generates
14 * the HTML for the input field to be placed in the table.
15 *
16 * The constructor input is an associative array of $fieldname => $info,
17 * where $info is an Associative Array with any of the following:
18 *
19 * 'class' -- the subclass of HTMLFormField that will be used
20 * to create the object. *NOT* the CSS class!
21 * 'type' -- roughly translates into the <select> type attribute.
22 * if 'class' is not specified, this is used as a map
23 * through HTMLForm::$typeMappings to get the class name.
24 * 'default' -- default value when the form is displayed
25 * 'id' -- HTML id attribute
26 * 'cssclass' -- CSS class
27 * 'options' -- varies according to the specific object.
28 * 'label-message' -- message key for a message to use as the label.
29 * can be an array of msg key and then parameters to
30 * the message.
31 * 'label' -- alternatively, a raw text message. Overridden by
32 * label-message
33 * 'help-message' -- message key for a message to use as a help text.
34 * can be an array of msg key and then parameters to
35 * the message.
36 * Overwrites 'help-messages'.
37 * 'help-messages' -- array of message key. As above, each item can
38 * be an array of msg key and then parameters.
39 * Overwrites 'help-message'.
40 * 'required' -- passed through to the object, indicating that it
41 * is a required field.
42 * 'size' -- the length of text fields
43 * 'filter-callback -- a function name to give you the chance to
44 * massage the inputted value before it's processed.
45 * @see HTMLForm::filter()
46 * 'validation-callback' -- a function name to give you the chance
47 * to impose extra validation on the field input.
48 * @see HTMLForm::validate()
49 * 'name' -- By default, the 'name' attribute of the input field
50 * is "wp{$fieldname}". If you want a different name
51 * (eg one without the "wp" prefix), specify it here and
52 * it will be used without modification.
53 *
54 * TODO: Document 'section' / 'subsection' stuff
55 */
56 class HTMLForm {
57 static $jsAdded = false;
58
59 # A mapping of 'type' inputs onto standard HTMLFormField subclasses
60 static $typeMappings = array(
61 'text' => 'HTMLTextField',
62 'textarea' => 'HTMLTextAreaField',
63 'select' => 'HTMLSelectField',
64 'radio' => 'HTMLRadioField',
65 'multiselect' => 'HTMLMultiSelectField',
66 'check' => 'HTMLCheckField',
67 'toggle' => 'HTMLCheckField',
68 'int' => 'HTMLIntField',
69 'float' => 'HTMLFloatField',
70 'info' => 'HTMLInfoField',
71 'selectorother' => 'HTMLSelectOrOtherField',
72 'submit' => 'HTMLSubmitField',
73 'hidden' => 'HTMLHiddenField',
74 'edittools' => 'HTMLEditTools',
75
76 # HTMLTextField will output the correct type="" attribute automagically.
77 # There are about four zillion other HTML5 input types, like url, but
78 # we don't use those at the moment, so no point in adding all of them.
79 'email' => 'HTMLTextField',
80 'password' => 'HTMLTextField',
81 );
82
83 protected $mMessagePrefix;
84 protected $mFlatFields;
85 protected $mFieldTree;
86 protected $mShowReset = false;
87 public $mFieldData;
88
89 protected $mSubmitCallback;
90 protected $mValidationErrorMessage;
91
92 protected $mPre = '';
93 protected $mHeader = '';
94 protected $mFooter = '';
95 protected $mSectionHeaders = array();
96 protected $mSectionFooters = array();
97 protected $mPost = '';
98 protected $mId;
99
100 protected $mSubmitID;
101 protected $mSubmitName;
102 protected $mSubmitText;
103 protected $mSubmitTooltip;
104 protected $mTitle;
105 protected $mMethod = 'post';
106
107 protected $mUseMultipart = false;
108 protected $mHiddenFields = array();
109 protected $mButtons = array();
110
111 protected $mWrapperLegend = false;
112 protected $mTokenAction = 'Edit';
113
114 /**
115 * Build a new HTMLForm from an array of field attributes
116 * @param $descriptor Array of Field constructs, as described above
117 * @param $messagePrefix String a prefix to go in front of default messages
118 */
119 public function __construct( $descriptor, $messagePrefix = '' ) {
120 $this->mMessagePrefix = $messagePrefix;
121
122 // Expand out into a tree.
123 $loadedDescriptor = array();
124 $this->mFlatFields = array();
125
126 foreach ( $descriptor as $fieldname => $info ) {
127 $section = isset( $info['section'] )
128 ? $info['section']
129 : '';
130
131 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
132 $this->mUseMultipart = true;
133 }
134
135 $field = self::loadInputFromParameters( $fieldname, $info );
136 $field->mParent = $this;
137
138 $setSection =& $loadedDescriptor;
139 if ( $section ) {
140 $sectionParts = explode( '/', $section );
141
142 while ( count( $sectionParts ) ) {
143 $newName = array_shift( $sectionParts );
144
145 if ( !isset( $setSection[$newName] ) ) {
146 $setSection[$newName] = array();
147 }
148
149 $setSection =& $setSection[$newName];
150 }
151 }
152
153 $setSection[$fieldname] = $field;
154 $this->mFlatFields[$fieldname] = $field;
155 }
156
157 $this->mFieldTree = $loadedDescriptor;
158 }
159
160 /**
161 * Add the HTMLForm-specific JavaScript, if it hasn't been
162 * done already.
163 */
164 static function addJS() {
165 if ( self::$jsAdded ) return;
166
167 global $wgOut;
168
169 $wgOut->addModules( 'mediawiki.legacy.htmlform' );
170 }
171
172 /**
173 * Initialise a new Object for the field
174 * @param $descriptor input Descriptor, as described above
175 * @return HTMLFormField subclass
176 */
177 static function loadInputFromParameters( $fieldname, $descriptor ) {
178 if ( isset( $descriptor['class'] ) ) {
179 $class = $descriptor['class'];
180 } elseif ( isset( $descriptor['type'] ) ) {
181 $class = self::$typeMappings[$descriptor['type']];
182 $descriptor['class'] = $class;
183 }
184
185 if ( !$class ) {
186 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
187 }
188
189 $descriptor['fieldname'] = $fieldname;
190
191 $obj = new $class( $descriptor );
192
193 return $obj;
194 }
195
196 /**
197 * Prepare form for submission
198 */
199 function prepareForm() {
200 # Check if we have the info we need
201 if ( ! $this->mTitle ) {
202 throw new MWException( "You must call setTitle() on an HTMLForm" );
203 }
204
205 // FIXME shouldn't this be closer to displayForm() ?
206 self::addJS();
207
208 # Load data from the request.
209 $this->loadData();
210 }
211
212 /**
213 * Try submitting, with edit token check first
214 * @return Status|boolean
215 */
216 function tryAuthorizedSubmit() {
217 global $wgUser, $wgRequest;
218 $editToken = $wgRequest->getVal( 'wpEditToken' );
219
220 $result = false;
221 # FIXME
222 if ( $wgRequest->wasPosted() ){#&& $this->getMethod() != 'post' || $wgUser->matchEditToken( $editToken ) ) {
223 $result = $this->trySubmit();
224 }
225 return $result;
226 }
227
228 /**
229 * The here's-one-I-made-earlier option: do the submission if
230 * posted, or display the form with or without funky valiation
231 * errors
232 * @return Bool or Status whether submission was successful.
233 */
234 function show() {
235 $this->prepareForm();
236
237 $result = $this->tryAuthorizedSubmit();
238 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){
239 return $result;
240 }
241
242 $this->displayForm( $result );
243 return false;
244 }
245
246 /**
247 * Validate all the fields, and call the submision callback
248 * function if everything is kosher.
249 * @return Mixed Bool true == Successful submission, Bool false
250 * == No submission attempted, anything else == Error to
251 * display.
252 */
253 function trySubmit() {
254 # Check the session tokens
255 # FIXME
256 if ( false && !Token::match( null, $this->mTokenAction ) ) {
257 return array( 'sessionfailure' );
258 }
259 # Check for validation
260 foreach ( $this->mFlatFields as $fieldname => $field ) {
261 if ( !empty( $field->mParams['nodata'] ) ) {
262 continue;
263 }
264 if ( $field->validate(
265 $this->mFieldData[$fieldname],
266 $this->mFieldData )
267 !== true
268 ) {
269 return isset( $this->mValidationErrorMessage )
270 ? $this->mValidationErrorMessage
271 : array( 'htmlform-invalid-input' );
272 }
273 }
274
275 $callback = $this->mSubmitCallback;
276
277 $data = $this->filterDataForSubmit( $this->mFieldData );
278
279 $res = call_user_func( $callback, $data );
280
281 return $res;
282 }
283
284 /**
285 * Set a callback to a function to do something with the form
286 * once it's been successfully validated.
287 * @param $cb String function name. The function will be passed
288 * the output from HTMLForm::filterDataForSubmit, and must
289 * return Bool true on success, Bool false if no submission
290 * was attempted, or String HTML output to display on error.
291 */
292 function setSubmitCallback( $cb ) {
293 $this->mSubmitCallback = $cb;
294 }
295
296 /**
297 * Set a message to display on a validation error.
298 * @param $msg Mixed String or Array of valid inputs to wfMsgExt()
299 * (so each entry can be either a String or Array)
300 */
301 function setValidationErrorMessage( $msg ) {
302 $this->mValidationErrorMessage = $msg;
303 }
304
305 /**
306 * Set the introductory message, overwriting any existing message.
307 * @param $msg String complete text of message to display
308 */
309 function setIntro( $msg ) { $this->mPre = $msg; }
310
311 /**
312 * Add introductory text.
313 * @param $msg String complete text of message to display
314 */
315 function addPreText( $msg ) { $this->mPre .= $msg; }
316
317 /**
318 * Add header text, inside the form.
319 * @param $msg String complete text of message to display
320 */
321 function addHeaderText( $msg, $section = null ) {
322 if ( is_null( $section ) ) {
323 $this->mHeader .= $msg;
324 } else {
325 if ( !isset( $this->mSectionHeaders[$section] ) ) {
326 $this->mSectionHeaders[$section] = '';
327 }
328 $this->mSectionHeaders[$section] .= $msg;
329 }
330 }
331
332 /**
333 * Add footer text, inside the form.
334 * @param $msg String complete text of message to display
335 */
336 function addFooterText( $msg, $section = null ) {
337 if ( is_null( $section ) ) {
338 $this->mFooter .= $msg;
339 } else {
340 if ( !isset( $this->mSectionFooters[$section] ) ) {
341 $this->mSectionFooters[$section] = '';
342 }
343 $this->mSectionFooters[$section] .= $msg;
344 }
345 }
346
347 /**
348 * Add text to the end of the display.
349 * @param $msg String complete text of message to display
350 */
351 function addPostText( $msg ) { $this->mPost .= $msg; }
352
353 /**
354 * Add a hidden field to the output
355 * @param $name String field name. This will be used exactly as entered
356 * @param $value String field value
357 * @param $attribs Array
358 */
359 public function addHiddenField( $name, $value, $attribs = array() ) {
360 $attribs += array( 'name' => $name );
361 $this->mHiddenFields[] = array( $value, $attribs );
362 }
363
364 public function addButton( $name, $value, $id = null, $attribs = null ) {
365 $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
366 }
367
368 /**
369 * Display the form (sending to wgOut), with an appropriate error
370 * message or stack of messages, and any validation errors, etc.
371 * @param $submitResult Mixed output from HTMLForm::trySubmit()
372 */
373 function displayForm( $submitResult ) {
374 global $wgOut;
375
376 # For good measure (it is the default)
377 $wgOut->preventClickjacking();
378
379 $html = ''
380 . $this->getErrors( $submitResult )
381 . $this->mHeader
382 . $this->getBody()
383 . $this->getHiddenFields()
384 . $this->getButtons()
385 . $this->mFooter
386 ;
387
388 $html = $this->wrapForm( $html );
389
390 $wgOut->addHTML( ''
391 . $this->mPre
392 . $html
393 . $this->mPost
394 );
395 }
396
397 /**
398 * Wrap the form innards in an actual <form> element
399 * @param $html String HTML contents to wrap.
400 * @return String wrapped HTML.
401 */
402 function wrapForm( $html ) {
403
404 # Include a <fieldset> wrapper for style, if requested.
405 if ( $this->mWrapperLegend !== false ) {
406 $html = Xml::fieldset( $this->mWrapperLegend, $html );
407 }
408 # Use multipart/form-data
409 $encType = $this->mUseMultipart
410 ? 'multipart/form-data'
411 : 'application/x-www-form-urlencoded';
412 # Attributes
413 $attribs = array(
414 'action' => $this->getTitle()->getFullURL(),
415 'method' => $this->mMethod,
416 'class' => 'visualClear',
417 'enctype' => $encType,
418 );
419 if ( !empty( $this->mId ) ) {
420 $attribs['id'] = $this->mId;
421 }
422
423 return Html::rawElement( 'form', $attribs, $html );
424 }
425
426 /**
427 * Get the hidden fields that should go inside the form.
428 * @return String HTML.
429 */
430 function getHiddenFields() {
431 global $wgUser;
432
433 $html = '';
434 if( $this->getMethod() == 'post' ){
435 # FIXME
436 $token = new Token( $this->mTokenAction );
437 $html .= Html::hidden(
438 "wp{$this->mTokenAction}Token",
439 $token->set(),
440 array( 'id' => 'wpEditToken' )
441 ) . "\n";
442 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
443 }
444
445 foreach ( $this->mHiddenFields as $data ) {
446 list( $value, $attribs ) = $data;
447 $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
448 }
449
450 return $html;
451 }
452
453 /**
454 * Get the submit and (potentially) reset buttons.
455 * @return String HTML.
456 */
457 function getButtons() {
458 $html = '';
459 $attribs = array();
460
461 if ( isset( $this->mSubmitID ) ) {
462 $attribs['id'] = $this->mSubmitID;
463 }
464
465 if ( isset( $this->mSubmitName ) ) {
466 $attribs['name'] = $this->mSubmitName;
467 }
468
469 if ( isset( $this->mSubmitTooltip ) ) {
470 global $wgUser;
471 $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
472 }
473
474 $attribs['class'] = 'mw-htmlform-submit';
475
476 $html .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
477
478 if ( $this->mShowReset ) {
479 $html .= Html::element(
480 'input',
481 array(
482 'type' => 'reset',
483 'value' => wfMsg( 'htmlform-reset' )
484 )
485 ) . "\n";
486 }
487
488 foreach ( $this->mButtons as $button ) {
489 $attrs = array(
490 'type' => 'submit',
491 'name' => $button['name'],
492 'value' => $button['value']
493 );
494
495 if ( $button['attribs'] ) {
496 $attrs += $button['attribs'];
497 }
498
499 if ( isset( $button['id'] ) ) {
500 $attrs['id'] = $button['id'];
501 }
502
503 $html .= Html::element( 'input', $attrs );
504 }
505
506 return $html;
507 }
508
509 /**
510 * Get the whole body of the form.
511 */
512 function getBody() {
513 return $this->displaySection( $this->mFieldTree );
514 }
515
516 /**
517 * Format and display an error message stack.
518 * @param $errors Mixed String or Array of message keys
519 * @return String
520 */
521 function getErrors( $errors ) {
522 if ( $errors instanceof Status ) {
523 global $wgOut;
524 if ( $errors->isOK() ) {
525 $errorstr = '';
526 } else {
527 $errorstr = $wgOut->parse( $errors->getWikiText() );
528 }
529 } elseif ( is_array( $errors ) ) {
530 $errorstr = $this->formatErrors( $errors );
531 } else {
532 $errorstr = $errors;
533 }
534
535 return $errorstr
536 ? Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
537 : '';
538 }
539
540 /**
541 * Format a stack of error messages into a single HTML string
542 * @param $errors Array of message keys/values
543 * @return String HTML, a <ul> list of errors
544 */
545 static function formatErrors( $errors ) {
546 $errorstr = '';
547
548 foreach ( $errors as $error ) {
549 if ( is_array( $error ) ) {
550 $msg = array_shift( $error );
551 } else {
552 $msg = $error;
553 $error = array();
554 }
555
556 $errorstr .= Html::rawElement(
557 'li',
558 null,
559 wfMsgExt( $msg, array( 'parseinline' ), $error )
560 );
561 }
562
563 $errorstr = Html::rawElement( 'ul', array(), $errorstr );
564
565 return $errorstr;
566 }
567
568 /**
569 * Set the text for the submit button
570 * @param $t String plaintext.
571 */
572 function setSubmitText( $t ) {
573 $this->mSubmitText = $t;
574 }
575
576 /**
577 * Get the text for the submit button, either customised or a default.
578 * @return unknown_type
579 */
580 function getSubmitText() {
581 return $this->mSubmitText
582 ? $this->mSubmitText
583 : wfMsg( 'htmlform-submit' );
584 }
585
586 public function setSubmitName( $name ) {
587 $this->mSubmitName = $name;
588 }
589
590 public function setSubmitTooltip( $name ) {
591 $this->mSubmitTooltip = $name;
592 }
593
594
595 /**
596 * Set the id for the submit button.
597 * @param $t String. FIXME: Integrity is *not* validated
598 */
599 function setSubmitID( $t ) {
600 $this->mSubmitID = $t;
601 }
602
603 public function setId( $id ) {
604 $this->mId = $id;
605 }
606 /**
607 * Prompt the whole form to be wrapped in a <fieldset>, with
608 * this text as its <legend> element.
609 * @param $legend String HTML to go inside the <legend> element.
610 * Will be escaped
611 */
612 public function setWrapperLegend( $legend ) { $this->mWrapperLegend = $legend; }
613
614 /**
615 * Set the prefix for various default messages
616 * TODO: currently only used for the <fieldset> legend on forms
617 * with multiple sections; should be used elsewhre?
618 * @param $p String
619 */
620 function setMessagePrefix( $p ) {
621 $this->mMessagePrefix = $p;
622 }
623 /**
624 * If you want to protect the form from CSRF by a token other than
625 * the usual wsEditToken, set something here.
626 * @see Token::set()
627 * @param $a
628 */
629 function setTokenAction( $a ){
630 $this->mTokenAction = ucfirst( $a );
631 }
632
633 /**
634 * Set the title for form submission
635 * @param $t Title of page the form is on/should be posted to
636 */
637 function setTitle( $t ) {
638 $this->mTitle = $t;
639 }
640
641 /**
642 * Get the title
643 * @return Title
644 */
645 function getTitle() {
646 return $this->mTitle;
647 }
648
649 /**
650 * Set the method used to submit the form
651 * @param $method String
652 */
653 public function setMethod( $method='post' ){
654 $this->mMethod = $method;
655 }
656
657 public function getMethod(){
658 return $this->mMethod;
659 }
660
661 /**
662 * TODO: Document
663 * @param $fields
664 */
665 function displaySection( $fields, $sectionName = '' ) {
666 $tableHtml = '';
667 $subsectionHtml = '';
668 $hasLeftColumn = false;
669
670 foreach ( $fields as $key => $value ) {
671 if ( is_object( $value ) ) {
672 $v = empty( $value->mParams['nodata'] )
673 ? $this->mFieldData[$key]
674 : $value->getDefault();
675 $tableHtml .= $value->getTableRow( $v );
676
677 if ( $value->getLabel() != '&#160;' )
678 $hasLeftColumn = true;
679 } elseif ( is_array( $value ) ) {
680 $section = $this->displaySection( $value, $key );
681 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
682 if ( isset( $this->mSectionHeaders[$key] ) ) {
683 $section = $this->mSectionHeaders[$key] . $section;
684 }
685 if ( isset( $this->mSectionFooters[$key] ) ) {
686 $section .= $this->mSectionFooters[$key];
687 }
688 $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
689 }
690 }
691
692 $classes = array();
693
694 if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist
695 $classes[] = 'mw-htmlform-nolabel';
696 }
697
698 $attribs = array(
699 'class' => implode( ' ', $classes ),
700 );
701
702 if ( $sectionName ) {
703 $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" );
704 }
705
706 $tableHtml = Html::rawElement( 'table', $attribs,
707 Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
708
709 return $subsectionHtml . "\n" . $tableHtml;
710 }
711
712 /**
713 * Construct the form fields from the Descriptor array
714 */
715 function loadData() {
716 global $wgRequest;
717
718 $fieldData = array();
719
720 foreach ( $this->mFlatFields as $fieldname => $field ) {
721 if ( !empty( $field->mParams['nodata'] ) ) {
722 continue;
723 } elseif ( !empty( $field->mParams['disabled'] ) ) {
724 $fieldData[$fieldname] = $field->getDefault();
725 } else {
726 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
727 }
728 }
729
730 # Filter data.
731 foreach ( $fieldData as $name => &$value ) {
732 $field = $this->mFlatFields[$name];
733 $value = $field->filter( $value, $this->mFlatFields );
734 }
735
736 $this->mFieldData = $fieldData;
737 }
738
739 /**
740 * Stop a reset button being shown for this form
741 * @param $suppressReset Bool set to false to re-enable the
742 * button again
743 */
744 function suppressReset( $suppressReset = true ) {
745 $this->mShowReset = !$suppressReset;
746 }
747
748 /**
749 * Overload this if you want to apply special filtration routines
750 * to the form as a whole, after it's submitted but before it's
751 * processed.
752 * @param $data
753 * @return unknown_type
754 */
755 function filterDataForSubmit( $data ) {
756 return $data;
757 }
758 }
759
760 /**
761 * The parent class to generate form fields. Any field type should
762 * be a subclass of this.
763 */
764 abstract class HTMLFormField {
765
766 protected $mValidationCallback;
767 protected $mFilterCallback;
768 protected $mName;
769 public $mParams;
770 protected $mLabel; # String label. Set on construction
771 protected $mID;
772 protected $mClass = '';
773 protected $mDefault;
774 public $mParent;
775
776 /**
777 * This function must be implemented to return the HTML to generate
778 * the input object itself. It should not implement the surrounding
779 * table cells/rows, or labels/help messages.
780 * @param $value String the value to set the input to; eg a default
781 * text for a text input.
782 * @return String valid HTML.
783 */
784 abstract function getInputHTML( $value );
785
786 /**
787 * Override this function to add specific validation checks on the
788 * field input. Don't forget to call parent::validate() to ensure
789 * that the user-defined callback mValidationCallback is still run
790 * @param $value String the value the field was submitted with
791 * @param $alldata Array the data collected from the form
792 * @return Mixed Bool true on success, or String error to display.
793 */
794 function validate( $value, $alldata ) {
795 if ( isset( $this->mValidationCallback ) ) {
796 return call_user_func( $this->mValidationCallback, $value, $alldata );
797 }
798
799 if ( isset( $this->mParams['required'] ) && $value === '' ) {
800 return wfMsgExt( 'htmlform-required', 'parseinline' );
801 }
802
803 return true;
804 }
805
806 function filter( $value, $alldata ) {
807 if ( isset( $this->mFilterCallback ) ) {
808 $value = call_user_func( $this->mFilterCallback, $value, $alldata );
809 }
810
811 return $value;
812 }
813
814 /**
815 * Should this field have a label, or is there no input element with the
816 * appropriate id for the label to point to?
817 *
818 * @return bool True to output a label, false to suppress
819 */
820 protected function needsLabel() {
821 return true;
822 }
823
824 /**
825 * Get the value that this input has been set to from a posted form,
826 * or the input's default value if it has not been set.
827 * @param $request WebRequest
828 * @return String the value
829 */
830 function loadDataFromRequest( $request ) {
831 if ( $request->getCheck( $this->mName ) ) {
832 return $request->getText( $this->mName );
833 } else {
834 return $this->getDefault();
835 }
836 }
837
838 /**
839 * Initialise the object
840 * @param $params Associative Array. See HTMLForm doc for syntax.
841 */
842 function __construct( $params ) {
843 $this->mParams = $params;
844
845 # Generate the label from a message, if possible
846 if ( isset( $params['label-message'] ) ) {
847 $msgInfo = $params['label-message'];
848
849 if ( is_array( $msgInfo ) ) {
850 $msg = array_shift( $msgInfo );
851 } else {
852 $msg = $msgInfo;
853 $msgInfo = array();
854 }
855
856 $this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
857 } elseif ( isset( $params['label'] ) ) {
858 $this->mLabel = $params['label'];
859 }
860
861 $this->mName = "wp{$params['fieldname']}";
862 if ( isset( $params['name'] ) ) {
863 $this->mName = $params['name'];
864 }
865
866 $validName = Sanitizer::escapeId( $this->mName );
867 if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
868 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
869 }
870
871 $this->mID = "mw-input-{$this->mName}";
872
873 if ( isset( $params['default'] ) ) {
874 $this->mDefault = $params['default'];
875 }
876
877 if ( isset( $params['id'] ) ) {
878 $id = $params['id'];
879 $validId = Sanitizer::escapeId( $id );
880
881 if ( $id != $validId ) {
882 throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
883 }
884
885 $this->mID = $id;
886 }
887
888 if ( isset( $params['cssclass'] ) ) {
889 $this->mClass = $params['cssclass'];
890 }
891
892 if ( isset( $params['validation-callback'] ) ) {
893 $this->mValidationCallback = $params['validation-callback'];
894 }
895
896 if ( isset( $params['filter-callback'] ) ) {
897 $this->mFilterCallback = $params['filter-callback'];
898 }
899 }
900
901 /**
902 * Get the complete table row for the input, including help text,
903 * labels, and whatever.
904 * @param $value String the value to set the input to.
905 * @return String complete HTML table row.
906 */
907 function getTableRow( $value ) {
908 # Check for invalid data.
909 global $wgRequest;
910
911 $errors = $this->validate( $value, $this->mParent->mFieldData );
912
913 $cellAttributes = array();
914 $verticalLabel = false;
915
916 if ( !empty($this->mParams['vertical-label']) ) {
917 $cellAttributes['colspan'] = 2;
918 $verticalLabel = true;
919 }
920
921 if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
922 $errors = '';
923 } else {
924 $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
925 }
926
927 $label = $this->getLabelHtml( $cellAttributes );
928 $field = Html::rawElement(
929 'td',
930 array( 'class' => 'mw-input' ) + $cellAttributes,
931 $this->getInputHTML( $value ) . "\n$errors"
932 );
933
934 $fieldType = get_class( $this );
935
936 if ($verticalLabel) {
937 $html = Html::rawElement( 'tr',
938 array( 'class' => 'mw-htmlform-vertical-label' ), $label );
939 $html .= Html::rawElement( 'tr',
940 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
941 $field );
942 } else {
943 $html = Html::rawElement( 'tr',
944 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
945 $label . $field );
946 }
947
948 $helptext = null;
949
950 if ( isset( $this->mParams['help-message'] ) ) {
951 $msg = $this->mParams['help-message'];
952 $helptext = wfMsgExt( $msg, 'parseinline' );
953 if ( wfEmptyMsg( $msg, $helptext ) ) {
954 # Never mind
955 $helptext = null;
956 }
957 } elseif ( isset( $this->mParams['help-messages'] ) ) {
958 # help-message can be passed a message key (string) or an array containing
959 # a message key and additional parameters. This makes it impossible to pass
960 # an array of message key
961 foreach( $this->mParams['help-messages'] as $msg ) {
962 $candidate = wfMsgExt( $msg, 'parseinline' );
963 if( wfEmptyMsg( $msg ) ) {
964 $candidate = null;
965 }
966 $helptext .= $candidate; // append message
967 }
968 } elseif ( isset( $this->mParams['help'] ) ) {
969 $helptext = $this->mParams['help'];
970 }
971
972 if ( !is_null( $helptext ) ) {
973 $row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
974 $helptext );
975 $row = Html::rawElement( 'tr', array(), $row );
976 $html .= "$row\n";
977 }
978
979 return $html;
980 }
981
982 function getLabel() {
983 return $this->mLabel;
984 }
985 function getLabelHtml( $cellAttributes = array() ) {
986 # Don't output a for= attribute for labels with no associated input.
987 # Kind of hacky here, possibly we don't want these to be <label>s at all.
988 $for = array();
989
990 if ( $this->needsLabel() ) {
991 $for['for'] = $this->mID;
992 }
993
994 return Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes,
995 Html::rawElement( 'label', $for, $this->getLabel() )
996 );
997 }
998
999 function getDefault() {
1000 if ( isset( $this->mDefault ) ) {
1001 return $this->mDefault;
1002 } else {
1003 return null;
1004 }
1005 }
1006
1007 /**
1008 * Returns the attributes required for the tooltip and accesskey.
1009 *
1010 * @return array Attributes
1011 */
1012 public function getTooltipAndAccessKey() {
1013 if ( empty( $this->mParams['tooltip'] ) ) {
1014 return array();
1015 }
1016
1017 global $wgUser;
1018
1019 return $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] );
1020 }
1021
1022 /**
1023 * flatten an array of options to a single array, for instance,
1024 * a set of <options> inside <optgroups>.
1025 * @param $options Associative Array with values either Strings
1026 * or Arrays
1027 * @return Array flattened input
1028 */
1029 public static function flattenOptions( $options ) {
1030 $flatOpts = array();
1031
1032 foreach ( $options as $value ) {
1033 if ( is_array( $value ) ) {
1034 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1035 } else {
1036 $flatOpts[] = $value;
1037 }
1038 }
1039
1040 return $flatOpts;
1041 }
1042 }
1043
1044 class HTMLTextField extends HTMLFormField {
1045 function getSize() {
1046 return isset( $this->mParams['size'] )
1047 ? $this->mParams['size']
1048 : 45;
1049 }
1050
1051 function getInputHTML( $value ) {
1052 $attribs = array(
1053 'id' => $this->mID,
1054 'name' => $this->mName,
1055 'size' => $this->getSize(),
1056 'value' => $value,
1057 ) + $this->getTooltipAndAccessKey();
1058
1059 if ( isset( $this->mParams['maxlength'] ) ) {
1060 $attribs['maxlength'] = $this->mParams['maxlength'];
1061 }
1062
1063 if ( !empty( $this->mParams['disabled'] ) ) {
1064 $attribs['disabled'] = 'disabled';
1065 }
1066
1067 # TODO: Enforce pattern, step, required, readonly on the server side as
1068 # well
1069 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
1070 'placeholder' ) as $param ) {
1071 if ( isset( $this->mParams[$param] ) ) {
1072 $attribs[$param] = $this->mParams[$param];
1073 }
1074 }
1075
1076 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
1077 if ( isset( $this->mParams[$param] ) ) {
1078 $attribs[$param] = '';
1079 }
1080 }
1081
1082 # Implement tiny differences between some field variants
1083 # here, rather than creating a new class for each one which
1084 # is essentially just a clone of this one.
1085 if ( isset( $this->mParams['type'] ) ) {
1086 switch ( $this->mParams['type'] ) {
1087 case 'email':
1088 $attribs['type'] = 'email';
1089 break;
1090 case 'int':
1091 $attribs['type'] = 'number';
1092 break;
1093 case 'float':
1094 $attribs['type'] = 'number';
1095 $attribs['step'] = 'any';
1096 break;
1097 # Pass through
1098 case 'password':
1099 case 'file':
1100 $attribs['type'] = $this->mParams['type'];
1101 break;
1102 }
1103 }
1104
1105 return Html::element( 'input', $attribs );
1106 }
1107 }
1108 class HTMLTextAreaField extends HTMLFormField {
1109 function getCols() {
1110 return isset( $this->mParams['cols'] )
1111 ? $this->mParams['cols']
1112 : 80;
1113 }
1114
1115 function getRows() {
1116 return isset( $this->mParams['rows'] )
1117 ? $this->mParams['rows']
1118 : 25;
1119 }
1120
1121 function getInputHTML( $value ) {
1122 $attribs = array(
1123 'id' => $this->mID,
1124 'name' => $this->mName,
1125 'cols' => $this->getCols(),
1126 'rows' => $this->getRows(),
1127 ) + $this->getTooltipAndAccessKey();
1128
1129
1130 if ( !empty( $this->mParams['disabled'] ) ) {
1131 $attribs['disabled'] = 'disabled';
1132 }
1133
1134 if ( !empty( $this->mParams['readonly'] ) ) {
1135 $attribs['readonly'] = 'readonly';
1136 }
1137
1138 foreach ( array( 'required', 'autofocus' ) as $param ) {
1139 if ( isset( $this->mParams[$param] ) ) {
1140 $attribs[$param] = '';
1141 }
1142 }
1143
1144 return Html::element( 'textarea', $attribs, $value );
1145 }
1146 }
1147
1148 /**
1149 * A field that will contain a numeric value
1150 */
1151 class HTMLFloatField extends HTMLTextField {
1152 function getSize() {
1153 return isset( $this->mParams['size'] )
1154 ? $this->mParams['size']
1155 : 20;
1156 }
1157
1158 function validate( $value, $alldata ) {
1159 $p = parent::validate( $value, $alldata );
1160
1161 if ( $p !== true ) {
1162 return $p;
1163 }
1164
1165 $value = trim( $value );
1166
1167 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
1168 # with the addition that a leading '+' sign is ok.
1169 if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
1170 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
1171 }
1172
1173 # The "int" part of these message names is rather confusing.
1174 # They make equal sense for all numbers.
1175 if ( isset( $this->mParams['min'] ) ) {
1176 $min = $this->mParams['min'];
1177
1178 if ( $min > $value ) {
1179 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
1180 }
1181 }
1182
1183 if ( isset( $this->mParams['max'] ) ) {
1184 $max = $this->mParams['max'];
1185
1186 if ( $max < $value ) {
1187 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
1188 }
1189 }
1190
1191 return true;
1192 }
1193 }
1194
1195 /**
1196 * A field that must contain a number
1197 */
1198 class HTMLIntField extends HTMLFloatField {
1199 function validate( $value, $alldata ) {
1200 $p = parent::validate( $value, $alldata );
1201
1202 if ( $p !== true ) {
1203 return $p;
1204 }
1205
1206 # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
1207 # with the addition that a leading '+' sign is ok. Note that leading zeros
1208 # are fine, and will be left in the input, which is useful for things like
1209 # phone numbers when you know that they are integers (the HTML5 type=tel
1210 # input does not require its value to be numeric). If you want a tidier
1211 # value to, eg, save in the DB, clean it up with intval().
1212 if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) )
1213 ) {
1214 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
1215 }
1216
1217 return true;
1218 }
1219 }
1220
1221 /**
1222 * A checkbox field
1223 */
1224 class HTMLCheckField extends HTMLFormField {
1225 function getInputHTML( $value ) {
1226 if ( !empty( $this->mParams['invert'] ) ) {
1227 $value = !$value;
1228 }
1229
1230 $attr = $this->getTooltipAndAccessKey();
1231 $attr['id'] = $this->mID;
1232
1233 if ( !empty( $this->mParams['disabled'] ) ) {
1234 $attr['disabled'] = 'disabled';
1235 }
1236
1237 return Xml::check( $this->mName, $value, $attr ) . '&#160;' .
1238 Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
1239 }
1240
1241 /**
1242 * For a checkbox, the label goes on the right hand side, and is
1243 * added in getInputHTML(), rather than HTMLFormField::getRow()
1244 */
1245 function getLabel() {
1246 return '&#160;';
1247 }
1248
1249 function loadDataFromRequest( $request ) {
1250 $invert = false;
1251 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
1252 $invert = true;
1253 }
1254
1255 // GetCheck won't work like we want for checks.
1256 if ( $request->getCheck( 'wpEditToken' ) ) {
1257 // XOR has the following truth table, which is what we want
1258 // INVERT VALUE | OUTPUT
1259 // true true | false
1260 // false true | true
1261 // false false | false
1262 // true false | true
1263 return $request->getBool( $this->mName ) xor $invert;
1264 } else {
1265 return $this->getDefault();
1266 }
1267 }
1268 }
1269
1270 /**
1271 * A select dropdown field. Basically a wrapper for Xmlselect class
1272 */
1273 class HTMLSelectField extends HTMLFormField {
1274 function validate( $value, $alldata ) {
1275 $p = parent::validate( $value, $alldata );
1276
1277 if ( $p !== true ) {
1278 return $p;
1279 }
1280
1281 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1282
1283 if ( in_array( $value, $validOptions ) )
1284 return true;
1285 else
1286 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1287 }
1288
1289 function getInputHTML( $value ) {
1290 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
1291
1292 # If one of the options' 'name' is int(0), it is automatically selected.
1293 # because PHP sucks and things int(0) == 'some string'.
1294 # Working around this by forcing all of them to strings.
1295 foreach( $this->mParams['options'] as $key => &$opt ){
1296 if( is_int( $opt ) ){
1297 $opt = strval( $opt );
1298 }
1299 }
1300 unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
1301
1302 if ( !empty( $this->mParams['disabled'] ) ) {
1303 $select->setAttribute( 'disabled', 'disabled' );
1304 }
1305
1306 $select->addOptions( $this->mParams['options'] );
1307
1308 return $select->getHTML();
1309 }
1310 }
1311
1312 /**
1313 * Select dropdown field, with an additional "other" textbox.
1314 */
1315 class HTMLSelectOrOtherField extends HTMLTextField {
1316 static $jsAdded = false;
1317
1318 function __construct( $params ) {
1319 if ( !in_array( 'other', $params['options'], true ) ) {
1320 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
1321 }
1322
1323 parent::__construct( $params );
1324 }
1325
1326 static function forceToStringRecursive( $array ) {
1327 if ( is_array( $array ) ) {
1328 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
1329 } else {
1330 return strval( $array );
1331 }
1332 }
1333
1334 function getInputHTML( $value ) {
1335 $valInSelect = false;
1336
1337 if ( $value !== false ) {
1338 $valInSelect = in_array(
1339 $value,
1340 HTMLFormField::flattenOptions( $this->mParams['options'] )
1341 );
1342 }
1343
1344 $selected = $valInSelect ? $value : 'other';
1345
1346 $opts = self::forceToStringRecursive( $this->mParams['options'] );
1347
1348 $select = new XmlSelect( $this->mName, $this->mID, $selected );
1349 $select->addOptions( $opts );
1350
1351 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
1352
1353 $tbAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize() );
1354
1355 if ( !empty( $this->mParams['disabled'] ) ) {
1356 $select->setAttribute( 'disabled', 'disabled' );
1357 $tbAttribs['disabled'] = 'disabled';
1358 }
1359
1360 $select = $select->getHTML();
1361
1362 if ( isset( $this->mParams['maxlength'] ) ) {
1363 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
1364 }
1365
1366 $textbox = Html::input(
1367 $this->mName . '-other',
1368 $valInSelect ? '' : $value,
1369 'text',
1370 $tbAttribs
1371 );
1372
1373 return "$select<br />\n$textbox";
1374 }
1375
1376 function loadDataFromRequest( $request ) {
1377 if ( $request->getCheck( $this->mName ) ) {
1378 $val = $request->getText( $this->mName );
1379
1380 if ( $val == 'other' ) {
1381 $val = $request->getText( $this->mName . '-other' );
1382 }
1383
1384 return $val;
1385 } else {
1386 return $this->getDefault();
1387 }
1388 }
1389 }
1390
1391 /**
1392 * Multi-select field
1393 */
1394 class HTMLMultiSelectField extends HTMLFormField {
1395 function validate( $value, $alldata ) {
1396 $p = parent::validate( $value, $alldata );
1397
1398 if ( $p !== true ) {
1399 return $p;
1400 }
1401
1402 if ( !is_array( $value ) ) {
1403 return false;
1404 }
1405
1406 # If all options are valid, array_intersect of the valid options
1407 # and the provided options will return the provided options.
1408 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1409
1410 $validValues = array_intersect( $value, $validOptions );
1411 if ( count( $validValues ) == count( $value ) ) {
1412 return true;
1413 } else {
1414 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1415 }
1416 }
1417
1418 function getInputHTML( $value ) {
1419 $html = $this->formatOptions( $this->mParams['options'], $value );
1420
1421 return $html;
1422 }
1423
1424 function formatOptions( $options, $value ) {
1425 $html = '';
1426
1427 $attribs = array();
1428
1429 if ( !empty( $this->mParams['disabled'] ) ) {
1430 $attribs['disabled'] = 'disabled';
1431 }
1432
1433 foreach ( $options as $label => $info ) {
1434 if ( is_array( $info ) ) {
1435 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
1436 $html .= $this->formatOptions( $info, $value );
1437 } else {
1438 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
1439
1440 $checkbox = Xml::check(
1441 $this->mName . '[]',
1442 in_array( $info, $value, true ),
1443 $attribs + $thisAttribs );
1444 $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
1445
1446 $html .= $checkbox . '<br />';
1447 }
1448 }
1449
1450 return $html;
1451 }
1452
1453 function loadDataFromRequest( $request ) {
1454 # won't work with getCheck
1455 if ( $request->getCheck( 'wpEditToken' ) ) {
1456 $arr = $request->getArray( $this->mName );
1457
1458 if ( !$arr ) {
1459 $arr = array();
1460 }
1461
1462 return $arr;
1463 } else {
1464 return $this->getDefault();
1465 }
1466 }
1467
1468 function getDefault() {
1469 if ( isset( $this->mDefault ) ) {
1470 return $this->mDefault;
1471 } else {
1472 return array();
1473 }
1474 }
1475
1476 protected function needsLabel() {
1477 return false;
1478 }
1479 }
1480
1481 /**
1482 * Radio checkbox fields.
1483 */
1484 class HTMLRadioField extends HTMLFormField {
1485 function validate( $value, $alldata ) {
1486 $p = parent::validate( $value, $alldata );
1487
1488 if ( $p !== true ) {
1489 return $p;
1490 }
1491
1492 if ( !is_string( $value ) && !is_int( $value ) ) {
1493 return false;
1494 }
1495
1496 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
1497
1498 if ( in_array( $value, $validOptions ) ) {
1499 return true;
1500 } else {
1501 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
1502 }
1503 }
1504
1505 /**
1506 * This returns a block of all the radio options, in one cell.
1507 * @see includes/HTMLFormField#getInputHTML()
1508 */
1509 function getInputHTML( $value ) {
1510 $html = $this->formatOptions( $this->mParams['options'], $value );
1511
1512 return $html;
1513 }
1514
1515 function formatOptions( $options, $value ) {
1516 $html = '';
1517
1518 $attribs = array();
1519 if ( !empty( $this->mParams['disabled'] ) ) {
1520 $attribs['disabled'] = 'disabled';
1521 }
1522
1523 # TODO: should this produce an unordered list perhaps?
1524 foreach ( $options as $label => $info ) {
1525 if ( is_array( $info ) ) {
1526 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
1527 $html .= $this->formatOptions( $info, $value );
1528 } else {
1529 $id = Sanitizer::escapeId( $this->mID . "-$info" );
1530 $html .= Xml::radio(
1531 $this->mName,
1532 $info,
1533 $info == $value,
1534 $attribs + array( 'id' => $id )
1535 );
1536 $html .= '&#160;' .
1537 Html::rawElement( 'label', array( 'for' => $id ), $label );
1538
1539 $html .= "<br />\n";
1540 }
1541 }
1542
1543 return $html;
1544 }
1545
1546 protected function needsLabel() {
1547 return false;
1548 }
1549 }
1550
1551 /**
1552 * An information field (text blob), not a proper input.
1553 */
1554 class HTMLInfoField extends HTMLFormField {
1555 function __construct( $info ) {
1556 $info['nodata'] = true;
1557
1558 parent::__construct( $info );
1559 }
1560
1561 function getInputHTML( $value ) {
1562 return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
1563 }
1564
1565 function getTableRow( $value ) {
1566 if ( !empty( $this->mParams['rawrow'] ) ) {
1567 return $value;
1568 }
1569
1570 return parent::getTableRow( $value );
1571 }
1572
1573 protected function needsLabel() {
1574 return false;
1575 }
1576 }
1577
1578 class HTMLHiddenField extends HTMLFormField {
1579 public function __construct( $params ) {
1580 parent::__construct( $params );
1581
1582 # Per HTML5 spec, hidden fields cannot be 'required'
1583 # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
1584 unset( $this->mParams['required'] );
1585 }
1586
1587 public function getTableRow( $value ) {
1588 $params = array();
1589 if ( $this->mID ) {
1590 $params['id'] = $this->mID;
1591 }
1592
1593 $this->mParent->addHiddenField(
1594 $this->mName,
1595 $this->mDefault,
1596 $params
1597 );
1598
1599 return '';
1600 }
1601
1602 public function getInputHTML( $value ) { return ''; }
1603 }
1604
1605 /**
1606 * Add a submit button inline in the form (as opposed to
1607 * HTMLForm::addButton(), which will add it at the end).
1608 */
1609 class HTMLSubmitField extends HTMLFormField {
1610
1611 function __construct( $info ) {
1612 $info['nodata'] = true;
1613 parent::__construct( $info );
1614 }
1615
1616 function getInputHTML( $value ) {
1617 return Xml::submitButton(
1618 $value,
1619 array(
1620 'class' => 'mw-htmlform-submit',
1621 'name' => $this->mName,
1622 'id' => $this->mID,
1623 )
1624 );
1625 }
1626
1627 protected function needsLabel() {
1628 return false;
1629 }
1630
1631 /**
1632 * Button cannot be invalid
1633 */
1634 public function validate( $value, $alldata ){
1635 return true;
1636 }
1637 }
1638
1639 class HTMLEditTools extends HTMLFormField {
1640 public function getInputHTML( $value ) {
1641 return '';
1642 }
1643
1644 public function getTableRow( $value ) {
1645 if ( empty( $this->mParams['message'] ) ) {
1646 $msg = wfMessage( 'edittools' );
1647 } else {
1648 $msg = wfMessage( $this->mParams['message'] );
1649 if ( $msg->isDisabled() ) {
1650 $msg = wfMessage( 'edittools' );
1651 }
1652 }
1653 $msg->inContentLanguage();
1654
1655
1656 return '<tr><td></td><td class="mw-input">'
1657 . '<div class="mw-editTools">'
1658 . $msg->parseAsBlock()
1659 . "</div></td></tr>\n";
1660 }
1661 }