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