(bug 34289) user.options CSS loaded twice. Fixed by splitting off the CSS part of...
[lhc/web/wiklou.git] / includes / HTMLForm.php
index 377783b..7326bf5 100644 (file)
  *     'help-message'        -- message key for a message to use as a help text.
  *                              can be an array of msg key and then parameters to
  *                              the message.
- *                           Overwrites 'help-messages'.
- *  'help-messages'       -- array of message key. As above, each item can
- *                           be an array of msg key and then parameters.
- *                           Overwrites 'help-message'.
+ *                              Overwrites 'help-messages'.
+ *     'help-messages'       -- array of message key. As above, each item can
+ *                              be an array of msg key and then parameters.
+ *                              Overwrites 'help-message'.
  *     'required'            -- passed through to the object, indicating that it
  *                              is a required field.
  *     'size'                -- the length of text fields
@@ -53,9 +53,9 @@
  *
  * TODO: Document 'section' / 'subsection' stuff
  */
-class HTMLForm {
+class HTMLForm extends ContextSource {
 
-       # A mapping of 'type' inputs onto standard HTMLFormField subclasses
+       // A mapping of 'type' inputs onto standard HTMLFormField subclasses
        static $typeMappings = array(
                'text' => 'HTMLTextField',
                'textarea' => 'HTMLTextAreaField',
@@ -73,15 +73,18 @@ class HTMLForm {
                'hidden' => 'HTMLHiddenField',
                'edittools' => 'HTMLEditTools',
 
-               # HTMLTextField will output the correct type="" attribute automagically.
-               # There are about four zillion other HTML5 input types, like url, but
-               # we don't use those at the moment, so no point in adding all of them.
+               // HTMLTextField will output the correct type="" attribute automagically.
+               // There are about four zillion other HTML5 input types, like url, but
+               // we don't use those at the moment, so no point in adding all of them.
                'email' => 'HTMLTextField',
                'password' => 'HTMLTextField',
        );
 
        protected $mMessagePrefix;
+
+       /** @var HTMLFormField[] */
        protected $mFlatFields;
+
        protected $mFieldTree;
        protected $mShowReset = false;
        public $mFieldData;
@@ -101,22 +104,51 @@ class HTMLForm {
        protected $mSubmitName;
        protected $mSubmitText;
        protected $mSubmitTooltip;
+
        protected $mTitle;
        protected $mMethod = 'post';
 
+       /**
+        * Form action URL. false means we will use the URL to set Title
+        * @since 1.19
+        * @var false|string
+        */
+       protected $mAction = false;
+
        protected $mUseMultipart = false;
        protected $mHiddenFields = array();
        protected $mButtons = array();
 
        protected $mWrapperLegend = false;
+       
+       /**
+        * If true, sections that contain both fields and subsections will
+        * render their subsections before their fields.
+        * 
+        * Subclasses may set this to false to render subsections after fields
+        * instead.
+        */
+       protected $mSubSectionBeforeFields = true;
 
        /**
         * Build a new HTMLForm from an array of field attributes
         * @param $descriptor Array of Field constructs, as described above
+        * @param $context IContextSource available since 1.18, will become compulsory in 1.18.
+        *     Obviates the need to call $form->setTitle()
         * @param $messagePrefix String a prefix to go in front of default messages
         */
-       public function __construct( $descriptor, $messagePrefix = '' ) {
-               $this->mMessagePrefix = $messagePrefix;
+       public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
+               if( $context instanceof IContextSource ){
+                       $this->setContext( $context );
+                       $this->mTitle = false; // We don't need them to set a title
+                       $this->mMessagePrefix = $messagePrefix;
+               } else {
+                       // B/C since 1.18
+                       if( is_string( $context ) && $messagePrefix === '' ){
+                               // it's actually $messagePrefix
+                               $this->mMessagePrefix = $context;
+                       }
+               }
 
                // Expand out into a tree.
                $loadedDescriptor = array();
@@ -161,11 +193,12 @@ class HTMLForm {
         * done already.
         * @deprecated since 1.18 load modules with ResourceLoader instead
         */
-       static function addJS() { }
+       static function addJS() { wfDeprecated( __METHOD__, '1.18' ); }
 
        /**
         * Initialise a new Object for the field
-        * @param $descriptor input Descriptor, as described above
+        * @param $fieldname string
+        * @param $descriptor string input Descriptor, as described above
         * @return HTMLFormField subclass
         */
        static function loadInputFromParameters( $fieldname, $descriptor ) {
@@ -174,6 +207,8 @@ class HTMLForm {
                } elseif ( isset( $descriptor['type'] ) ) {
                        $class = self::$typeMappings[$descriptor['type']];
                        $descriptor['class'] = $class;
+               } else {
+                       $class = null;
                }
 
                if ( !$class ) {
@@ -192,7 +227,7 @@ class HTMLForm {
         */
        function prepareForm() {
                # Check if we have the info we need
-               if ( ! $this->mTitle ) {
+               if ( !$this->mTitle instanceof Title && $this->mTitle !== false ) {
                        throw new MWException( "You must call setTitle() on an HTMLForm" );
                }
 
@@ -205,13 +240,27 @@ class HTMLForm {
         * @return Status|boolean
         */
        function tryAuthorizedSubmit() {
-               global $wgUser, $wgRequest;
-               $editToken = $wgRequest->getVal( 'wpEditToken' );
-
                $result = false;
-               if ( $this->getMethod() != 'post' || $wgUser->matchEditToken( $editToken ) ) {
+
+               $submit = false;
+               if ( $this->getMethod() != 'post' ) {
+                       $submit = true; // no session check needed
+               } elseif ( $this->getRequest()->wasPosted() ) {
+                       $editToken = $this->getRequest()->getVal( 'wpEditToken' );
+                       if ( $this->getUser()->isLoggedIn() || $editToken != null ) {
+                               // Session tokens for logged-out users have no security value.
+                               // However, if the user gave one, check it in order to give a nice 
+                               // "session expired" error instead of "permission denied" or such.
+                               $submit = $this->getUser()->matchEditToken( $editToken );
+                       } else {
+                               $submit = true;
+                       }
+               }
+
+               if ( $submit ) {
                        $result = $this->trySubmit();
                }
+
                return $result;
        }
 
@@ -261,7 +310,7 @@ class HTMLForm {
 
                $data = $this->filterDataForSubmit( $this->mFieldData );
 
-               $res = call_user_func( $callback, $data );
+               $res = call_user_func( $callback, $data, $this );
 
                return $res;
        }
@@ -291,7 +340,16 @@ class HTMLForm {
         * Set the introductory message, overwriting any existing message.
         * @param $msg String complete text of message to display
         */
-       function setIntro( $msg ) { $this->mPre = $msg; }
+       function setIntro( $msg ) {
+               $this->setPreText( $msg );
+       }
+
+       /**
+        * Set the introductory message, overwriting any existing message.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        */
+       function setPreText( $msg ) { $this->mPre = $msg; }
 
        /**
         * Add introductory text.
@@ -302,10 +360,11 @@ class HTMLForm {
        /**
         * Add header text, inside the form.
         * @param $msg String complete text of message to display
+        * @param $section string The section to add the header to
         */
-       function addHeaderText( $msg, $section = null ) { 
+       function addHeaderText( $msg, $section = null ) {
                if ( is_null( $section ) ) {
-                       $this->mHeader .= $msg; 
+                       $this->mHeader .= $msg;
                } else {
                        if ( !isset( $this->mSectionHeaders[$section] ) ) {
                                $this->mSectionHeaders[$section] = '';
@@ -314,18 +373,47 @@ class HTMLForm {
                }
        }
 
+       /**
+        * Set header text, inside the form.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        * @param $section The section to add the header to
+        */
+       function setHeaderText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mHeader = $msg;
+               } else {
+                       $this->mSectionHeaders[$section] = $msg;
+               }
+       }
+
        /**
         * Add footer text, inside the form.
         * @param $msg String complete text of message to display
+        * @param $section string The section to add the footer text to
         */
-       function addFooterText( $msg, $section = null ) { 
+       function addFooterText( $msg, $section = null ) {
                if ( is_null( $section ) ) {
-                       $this->mFooter .= $msg; 
+                       $this->mFooter .= $msg;
                } else {
                        if ( !isset( $this->mSectionFooters[$section] ) ) {
                                $this->mSectionFooters[$section] = '';
                        }
-                       $this->mSectionFooters[$section] .= $msg;                       
+                       $this->mSectionFooters[$section] .= $msg;
+               }
+       }
+
+       /**
+        * Set footer text, inside the form.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        * @param $section string The section to add the footer text to
+        */
+       function setFooterText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mFooter = $msg;
+               } else {
+                       $this->mSectionFooters[$section] = $msg;
                }
        }
 
@@ -335,6 +423,12 @@ class HTMLForm {
         */
        function addPostText( $msg ) { $this->mPost .= $msg; }
 
+       /**
+        * Set text at the end of the display.
+        * @param $msg String complete text of message to display
+        */
+       function setPostText( $msg ) { $this->mPost = $msg; }
+
        /**
         * Add a hidden field to the output
         * @param $name String field name.  This will be used exactly as entered
@@ -351,16 +445,23 @@ class HTMLForm {
        }
 
        /**
-        * Display the form (sending to wgOut), with an appropriate error
+        * Display the form (sending to $wgOut), with an appropriate error
         * message or stack of messages, and any validation errors, etc.
         * @param $submitResult Mixed output from HTMLForm::trySubmit()
         */
        function displayForm( $submitResult ) {
-               global $wgOut;
+               $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
+       }
 
+       /**
+        * Returns the raw HTML generated by the form
+        * @param $submitResult Mixed output from HTMLForm::trySubmit()
+        * @return string
+        */
+       function getHTML( $submitResult ) {
                # For good measure (it is the default)
-               $wgOut->preventClickjacking();
-               $wgOut->addModules( 'mediawiki.htmlform' );
+               $this->getOutput()->preventClickjacking();
+               $this->getOutput()->addModules( 'mediawiki.htmlform' );
 
                $html = ''
                        . $this->getErrors( $submitResult )
@@ -373,11 +474,7 @@ class HTMLForm {
 
                $html = $this->wrapForm( $html );
 
-               $wgOut->addHTML( ''
-                       . $this->mPre
-                       . $html
-                       . $this->mPost
-               );
+               return '' . $this->mPre . $html . $this->mPost;
        }
 
        /**
@@ -397,7 +494,7 @@ class HTMLForm {
                        : 'application/x-www-form-urlencoded';
                # Attributes
                $attribs = array(
-                       'action'  => $this->getTitle()->getFullURL(),
+                       'action'  => $this->mAction === false ? $this->getTitle()->getFullURL() : $this->mAction,
                        'method'  => $this->mMethod,
                        'class'   => 'visualClear',
                        'enctype' => $encType,
@@ -414,11 +511,15 @@ class HTMLForm {
         * @return String HTML.
         */
        function getHiddenFields() {
-               global $wgUser;
+               global $wgUsePathInfo;
 
                $html = '';
                if( $this->getMethod() == 'post' ){
-                       $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+                       $html .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+                       $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+               }
+
+               if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
                        $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
                }
 
@@ -447,8 +548,7 @@ class HTMLForm {
                }
 
                if ( isset( $this->mSubmitTooltip ) ) {
-                       global $wgUser;
-                       $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
+                       $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
                }
 
                $attribs['class'] = 'mw-htmlform-submit';
@@ -488,6 +588,7 @@ class HTMLForm {
 
        /**
         * Get the whole body of the form.
+        * @return String
         */
        function getBody() {
                return $this->displaySection( $this->mFieldTree );
@@ -495,16 +596,15 @@ class HTMLForm {
 
        /**
         * Format and display an error message stack.
-        * @param $errors Mixed String or Array of message keys
+        * @param $errors String|Array|Status
         * @return String
         */
        function getErrors( $errors ) {
                if ( $errors instanceof Status ) {
-                       global $wgOut;
                        if ( $errors->isOK() ) {
                                $errorstr = '';
                        } else {
-                               $errorstr = $wgOut->parse( $errors->getWikiText() );
+                               $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
                        }
                } elseif ( is_array( $errors ) ) {
                        $errorstr = $this->formatErrors( $errors );
@@ -522,7 +622,7 @@ class HTMLForm {
         * @param $errors Array of message keys/values
         * @return String HTML, a <ul> list of errors
         */
-       static function formatErrors( $errors ) {
+       public static function formatErrors( $errors ) {
                $errorstr = '';
 
                foreach ( $errors as $error ) {
@@ -535,7 +635,7 @@ class HTMLForm {
 
                        $errorstr .= Html::rawElement(
                                'li',
-                               null,
+                               array(),
                                wfMsgExt( $msg, array( 'parseinline' ), $error )
                        );
                }
@@ -553,6 +653,15 @@ class HTMLForm {
                $this->mSubmitText = $t;
        }
 
+       /**
+        * Set the text for the submit button to a message
+        * @since 1.19
+        * @param $msg String message key
+        */
+       public function setSubmitTextMsg( $msg ) {
+               return $this->setSubmitText( $this->msg( $msg )->escaped() );
+       }
+
        /**
         * Get the text for the submit button, either customised or a default.
         * @return unknown_type
@@ -573,7 +682,8 @@ class HTMLForm {
 
        /**
         * Set the id for the submit button.
-        * @param $t String.  FIXME: Integrity is *not* validated
+        * @param $t String.
+        * @todo FIXME: Integrity of $t is *not* validated
         */
        function setSubmitID( $t ) {
                $this->mSubmitID = $t;
@@ -590,6 +700,16 @@ class HTMLForm {
         */
        public function setWrapperLegend( $legend ) { $this->mWrapperLegend = $legend; }
 
+       /**
+        * Prompt the whole form to be wrapped in a <fieldset>, with
+        * this message as its <legend> element.
+        * @since 1.19
+        * @param $msg String message key
+        */
+       public function setWrapperLegendMsg( $msg ) {
+               return $this->setWrapperLegend( $this->msg( $msg )->escaped() );
+       }
+
        /**
         * Set the prefix for various default messages
         * TODO: currently only used for the <fieldset> legend on forms
@@ -613,7 +733,9 @@ class HTMLForm {
         * @return Title
         */
        function getTitle() {
-               return $this->mTitle;
+               return $this->mTitle === false
+                       ? $this->getContext()->getTitle()
+                       : $this->mTitle;
        }
 
        /**
@@ -630,9 +752,12 @@ class HTMLForm {
 
        /**
         * TODO: Document
-        * @param $fields
+        * @param $fields array[]|HTMLFormField[] array of fields (either arrays or objects)
+        * @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
+        * @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
+        * @return String
         */
-       function displaySection( $fields, $sectionName = '' ) {
+       function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
                $tableHtml = '';
                $subsectionHtml = '';
                $hasLeftColumn = false;
@@ -644,18 +769,23 @@ class HTMLForm {
                                        : $value->getDefault();
                                $tableHtml .= $value->getTableRow( $v );
 
-                               if ( $value->getLabel() != '&#160;' )
+                               if ( $value->getLabel() != '&#160;' ) {
                                        $hasLeftColumn = true;
+                               }
                        } elseif ( is_array( $value ) ) {
                                $section = $this->displaySection( $value, $key );
                                $legend = $this->getLegend( $key );
                                if ( isset( $this->mSectionHeaders[$key] ) ) {
                                        $section = $this->mSectionHeaders[$key] . $section;
-                               } 
+                               }
                                if ( isset( $this->mSectionFooters[$key] ) ) {
                                        $section .= $this->mSectionFooters[$key];
                                }
-                               $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";                                   
+                               $attributes = array();
+                               if ( $fieldsetIDPrefix ) {
+                                       $attributes['id'] = Sanitizer::escapeId( "$fieldsetIDPrefix$key" );
+                               }
+                               $subsectionHtml .= Xml::fieldset( $legend, $section, $attributes ) . "\n";
                        }
                }
 
@@ -676,15 +806,17 @@ class HTMLForm {
                $tableHtml = Html::rawElement( 'table', $attribs,
                        Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
 
-               return $subsectionHtml . "\n" . $tableHtml;
+               if ( $this->mSubSectionBeforeFields ) {
+                       return $subsectionHtml . "\n" . $tableHtml;
+               } else {
+                       return $tableHtml . "\n" . $subsectionHtml;
+               }
        }
 
        /**
         * Construct the form fields from the Descriptor array
         */
        function loadData() {
-               global $wgRequest;
-
                $fieldData = array();
 
                foreach ( $this->mFlatFields as $fieldname => $field ) {
@@ -693,7 +825,7 @@ class HTMLForm {
                        } elseif ( !empty( $field->mParams['disabled'] ) ) {
                                $fieldData[$fieldname] = $field->getDefault();
                        } else {
-                               $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
+                               $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
                        }
                }
 
@@ -735,6 +867,19 @@ class HTMLForm {
        public function getLegend( $key ) {
                return wfMsg( "{$this->mMessagePrefix}-$key" );
        }
+
+       /**
+        * Set the value for the action attribute of the form.
+        * When set to false (which is the default state), the set title is used.
+        *
+        * @since 1.19
+        *
+        * @param string|false $action
+        */
+       public function setAction( $action ) {
+               $this->mAction = $action;
+       }
+
 }
 
 /**
@@ -751,6 +896,10 @@ abstract class HTMLFormField {
        protected $mID;
        protected $mClass = '';
        protected $mDefault;
+
+       /**
+        * @var HTMLForm
+        */
        public $mParent;
 
        /**
@@ -772,20 +921,20 @@ abstract class HTMLFormField {
         * @return Mixed Bool true on success, or String error to display.
         */
        function validate( $value, $alldata ) {
-               if ( isset( $this->mValidationCallback ) ) {
-                       return call_user_func( $this->mValidationCallback, $value, $alldata );
-               }
-
                if ( isset( $this->mParams['required'] ) && $value === '' ) {
                        return wfMsgExt( 'htmlform-required', 'parseinline' );
                }
 
+               if ( isset( $this->mValidationCallback ) ) {
+                       return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
+               }
+
                return true;
        }
 
        function filter( $value, $alldata ) {
                if ( isset( $this->mFilterCallback ) ) {
-                       $value = call_user_func( $this->mFilterCallback, $value, $alldata );
+                       $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
                }
 
                return $value;
@@ -817,7 +966,7 @@ abstract class HTMLFormField {
 
        /**
         * Initialise the object
-        * @param $params Associative Array. See HTMLForm doc for syntax.
+        * @param $params array Associative Array. See HTMLForm doc for syntax.
         */
        function __construct( $params ) {
                $this->mParams = $params;
@@ -876,6 +1025,10 @@ abstract class HTMLFormField {
                if ( isset( $params['filter-callback'] ) ) {
                        $this->mFilterCallback = $params['filter-callback'];
                }
+
+               if ( isset( $params['flatlist'] ) ){
+                       $this->mClass .= ' mw-htmlform-flatlist';
+               }
        }
 
        /**
@@ -886,7 +1039,6 @@ abstract class HTMLFormField {
         */
        function getTableRow( $value ) {
                # Check for invalid data.
-               global $wgRequest;
 
                $errors = $this->validate( $value, $this->mParent->mFieldData );
 
@@ -898,7 +1050,7 @@ abstract class HTMLFormField {
                        $verticalLabel = true;
                }
 
-               if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
+               if ( $errors === true || ( !$this->mParent->getRequest()->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
                        $errors = '';
                        $errorClass = '';
                } else {
@@ -930,23 +1082,20 @@ abstract class HTMLFormField {
                $helptext = null;
 
                if ( isset( $this->mParams['help-message'] ) ) {
-                       $msg = $this->mParams['help-message'];
-                       $helptext = wfMsgExt( $msg, 'parseinline' );
-                       if ( wfEmptyMsg( $msg ) ) {
-                               # Never mind
-                               $helptext = null;
+                       $msg = wfMessage( $this->mParams['help-message'] );
+                       if ( $msg->exists() ) {
+                               $helptext = $msg->parse();
                        }
                } elseif ( isset( $this->mParams['help-messages'] ) ) {
                        # help-message can be passed a message key (string) or an array containing
                        # a message key and additional parameters. This makes it impossible to pass
                        # an array of message key
-                       foreach( $this->mParams['help-messages'] as $msg ) {
-                               $candidate = wfMsgExt( $msg, 'parseinline' );
-                               if( wfEmptyMsg( $msg ) ) {
-                                       $candidate = null;
+                       foreach( $this->mParams['help-messages'] as $name ) {
+                               $msg = wfMessage( $name );
+                               if( $msg->exists() ) {
+                                       $helptext .= $msg->parse(); // append message
                                }
-                               $helptext .= $candidate; // append message
-                       }       
+                       }
                } elseif ( isset( $this->mParams['help'] ) ) {
                        $helptext = $this->mParams['help'];
                }
@@ -995,16 +1144,13 @@ abstract class HTMLFormField {
                if ( empty( $this->mParams['tooltip'] ) ) {
                        return array();
                }
-
-               global $wgUser;
-
-               return $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] );
+               return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
        }
 
        /**
         * flatten an array of options to a single array, for instance,
         * a set of <options> inside <optgroups>.
-        * @param $options Associative Array with values either Strings
+        * @param $options array Associative Array with values either Strings
         *       or Arrays
         * @return Array flattened input
         */
@@ -1067,6 +1213,10 @@ class HTMLTextField extends HTMLFormField {
                        'value' => $value,
                ) + $this->getTooltipAndAccessKey();
 
+               if ( $this->mClass !== '' ) {
+                       $attribs['class'] = $this->mClass;
+               }
+               
                if ( isset( $this->mParams['maxlength'] ) ) {
                        $attribs['maxlength'] = $this->mParams['maxlength'];
                }
@@ -1137,7 +1287,10 @@ class HTMLTextAreaField extends HTMLFormField {
                        'rows' => $this->getRows(),
                ) + $this->getTooltipAndAccessKey();
 
-
+               if ( $this->mClass !== '' ) {
+                       $attribs['class'] = $this->mClass;
+               }
+               
                if ( !empty( $this->mParams['disabled'] ) ) {
                        $attribs['disabled'] = 'disabled';
                }
@@ -1244,6 +1397,10 @@ class HTMLCheckField extends HTMLFormField {
                if ( !empty( $this->mParams['disabled'] ) ) {
                        $attr['disabled'] = 'disabled';
                }
+               
+               if ( $this->mClass !== '' ) {
+                       $attr['class'] = $this->mClass;
+               }
 
                return Xml::check( $this->mName, $value, $attr ) . '&#160;' .
                        Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
@@ -1252,11 +1409,16 @@ class HTMLCheckField extends HTMLFormField {
        /**
         * For a checkbox, the label goes on the right hand side, and is
         * added in getInputHTML(), rather than HTMLFormField::getRow()
+        * @return String
         */
        function getLabel() {
                return '&#160;';
        }
 
+       /**
+        * @param  $request WebRequest
+        * @return String
+        */
        function loadDataFromRequest( $request ) {
                $invert = false;
                if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
@@ -1264,7 +1426,10 @@ class HTMLCheckField extends HTMLFormField {
                }
 
                // GetCheck won't work like we want for checks.
-               if ( $request->getCheck( 'wpEditToken' ) || $this->mParent->getMethod() != 'post' ) {
+               // Fetch the value in either one of the two following case:
+               // - we have a valid token (form got posted or GET forged by the user)
+               // - checkbox name has a value (false or true), ie is not null
+               if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName )!== null ) {
                        // XOR has the following truth table, which is what we want
                        // INVERT VALUE | OUTPUT
                        // true   true  | false
@@ -1301,9 +1466,9 @@ class HTMLSelectField extends HTMLFormField {
                $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
 
                # If one of the options' 'name' is int(0), it is automatically selected.
-               # because PHP sucks and things int(0) == 'some string'.
+               # because PHP sucks and thinks int(0) == 'some string'.
                # Working around this by forcing all of them to strings.
-               foreach( $this->mParams['options'] as $key => &$opt ){
+               foreach( $this->mParams['options'] as &$opt ){
                        if( is_int( $opt ) ){
                                $opt = strval( $opt );
                        }
@@ -1313,6 +1478,10 @@ class HTMLSelectField extends HTMLFormField {
                if ( !empty( $this->mParams['disabled'] ) ) {
                        $select->setAttribute( 'disabled', 'disabled' );
                }
+               
+               if ( $this->mClass !== '' ) {
+                       $select->setAttribute( 'class', $this->mClass );
+               }
 
                $select->addOptions( $this->mParams['options'] );
 
@@ -1328,7 +1497,8 @@ class HTMLSelectOrOtherField extends HTMLTextField {
 
        function __construct( $params ) {
                if ( !in_array( 'other', $params['options'], true ) ) {
-                       $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
+                       $msg = isset( $params['other'] ) ? $params['other'] : wfMsg( 'htmlform-selectorother-other' );
+                       $params['options'][$msg] = 'other';
                }
 
                parent::__construct( $params );
@@ -1373,6 +1543,10 @@ class HTMLSelectOrOtherField extends HTMLTextField {
                if ( isset( $this->mParams['maxlength'] ) ) {
                        $tbAttribs['maxlength'] = $this->mParams['maxlength'];
                }
+               
+               if ( $this->mClass !== '' ) {
+                       $tbAttribs['class'] = $this->mClass;
+               }
 
                $textbox = Html::input(
                        $this->mName . '-other',
@@ -1384,6 +1558,10 @@ class HTMLSelectOrOtherField extends HTMLTextField {
                return "$select<br />\n$textbox";
        }
 
+       /**
+        * @param  $request WebRequest
+        * @return String
+        */
        function loadDataFromRequest( $request ) {
                if ( $request->getCheck( $this->mName ) ) {
                        $val = $request->getText( $this->mName );
@@ -1403,6 +1581,7 @@ class HTMLSelectOrOtherField extends HTMLTextField {
  * Multi-select field
  */
 class HTMLMultiSelectField extends HTMLFormField {
+
        function validate( $value, $alldata ) {
                $p = parent::validate( $value, $alldata );
 
@@ -1454,13 +1633,17 @@ class HTMLMultiSelectField extends HTMLFormField {
                                        $attribs + $thisAttribs );
                                $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
 
-                               $html .= Html::rawElement( 'div', array( 'class' => 'mw-htmlform-multiselect-item' ), $checkbox );
+                               $html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $checkbox );
                        }
                }
 
                return $html;
        }
 
+       /**
+        * @param  $request WebRequest
+        * @return String
+        */
        function loadDataFromRequest( $request ) {
                if ( $this->mParent->getMethod() == 'post' ) {
                        if( $request->wasPosted() ){
@@ -1476,7 +1659,8 @@ class HTMLMultiSelectField extends HTMLFormField {
                        # field, is it because the user has not yet submitted the form, or that they
                        # have submitted it with all the options unchecked? We will have to assume the
                        # latter, which basically means that you can't specify 'positive' defaults
-                       # for GET forms.  FIXME...
+                       # for GET forms.
+                       # @todo FIXME...
                        return $request->getArray( $this->mName, array() );
                }
        }
@@ -1497,29 +1681,31 @@ class HTMLMultiSelectField extends HTMLFormField {
 /**
  * Double field with a dropdown list constructed from a system message in the format
  *     * Optgroup header
- *     ** <option value>|<option name>
- *     ** <option value == option name>
+ *     ** <option value>
  *     * New Optgroup header
  * Plus a text field underneath for an additional reason.  The 'value' of the field is
  * ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
  * select dropdown.
- * FIXME: If made 'required', only the text field should be compulsory.
+ * @todo FIXME: If made 'required', only the text field should be compulsory.
  */
 class HTMLSelectAndOtherField extends HTMLSelectField {
 
        function __construct( $params ) {
                if ( array_key_exists( 'other', $params ) ) {
                } elseif( array_key_exists( 'other-message', $params ) ){
-                       $params['other'] = wfMsg( $params['other-message'] );
+                       $params['other'] = wfMessage( $params['other-message'] )->plain();
                } else {
-                       $params['other'] = wfMsg( 'htmlform-selectorother-other' );
+                       $params['other'] = null;
                }
 
                if ( array_key_exists( 'options', $params ) ) {
                        # Options array already specified
                } elseif( array_key_exists( 'options-message', $params ) ){
                        # Generate options array from a system message
-                       $params['options'] = self::parseMessage( wfMsg( $params['options-message'], $params['other'] ) );
+                       $params['options'] = self::parseMessage(
+                               wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
+                               $params['other']
+                       );
                } else {
                        # Sulk
                        throw new MWException( 'HTMLSelectAndOtherField called without any options' );
@@ -1538,7 +1724,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
         */
        public static function parseMessage( $string, $otherName=null ) {
                if( $otherName === null ){
-                       $otherName = wfMsg( 'htmlform-selectorother-other' );
+                       $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
                }
 
                $optgroup = false;
@@ -1555,23 +1741,15 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        } elseif ( substr( $value, 0, 2) == '**' ) {
                                # groupmember
                                $opt = trim( substr( $value, 2 ) );
-                               $parts = array_map( 'trim', explode( '|', $opt, 2 ) );
-                               if( count( $parts ) === 1 ){
-                                       $parts[1] = $parts[0];
-                               }
                                if( $optgroup === false ){
-                                       $options[$parts[1]] = $parts[0];
+                                       $options[$opt] = $opt;
                                } else {
-                                       $options[$optgroup][$parts[1]] = $parts[0];
+                                       $options[$optgroup][$opt] = $opt;
                                }
                        } else {
                                # groupless reason list
                                $optgroup = false;
-                               $parts = array_map( 'trim', explode( '|', $opt, 2 ) );
-                               if( count( $parts ) === 1 ){
-                                       $parts[1] = $parts[0];
-                               }
-                               $options[$parts[1]] = $parts[0];
+                               $options[$option] = $option;
                        }
                }
 
@@ -1585,6 +1763,10 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        'id' => $this->mID . '-other',
                        'size' => $this->getSize(),
                );
+               
+               if ( $this->mClass !== '' ) {
+                       $textAttribs['class'] = $this->mClass;
+               }
 
                foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
                        if ( isset( $this->mParams[$param] ) ) {
@@ -1626,7 +1808,17 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
 
                } else {
                        $final = $this->getDefault();
-                       $list = $text = '';
+
+                       $list = 'other';
+                       $text = $final;
+                       foreach ( $this->mFlatOptions as $option ) {
+                               $match = $option . wfMsgForContent( 'colon-separator' );
+                               if( strpos( $text, $match ) === 0 ) {
+                                       $list = $option;
+                                       $text = substr( $text, strlen( $match ) );
+                                       break;
+                               }
+                       }
                }
                return array( $final, $list, $text );
        }
@@ -1659,6 +1851,8 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
  * Radio checkbox fields.
  */
 class HTMLRadioField extends HTMLFormField {
+
+
        function validate( $value, $alldata ) {
                $p = parent::validate( $value, $alldata );
 
@@ -1682,6 +1876,8 @@ class HTMLRadioField extends HTMLFormField {
        /**
         * This returns a block of all the radio options, in one cell.
         * @see includes/HTMLFormField#getInputHTML()
+        * @param $value String
+        * @return String
         */
        function getInputHTML( $value ) {
                $html = $this->formatOptions( $this->mParams['options'], $value );
@@ -1704,16 +1900,16 @@ class HTMLRadioField extends HTMLFormField {
                                $html .= $this->formatOptions( $info, $value );
                        } else {
                                $id = Sanitizer::escapeId( $this->mID . "-$info" );
-                               $html .= Xml::radio(
+                               $radio = Xml::radio(
                                        $this->mName,
                                        $info,
                                        $info == $value,
                                        $attribs + array( 'id' => $id )
                                );
-                               $html .= '&#160;' .
+                               $radio .= '&#160;' .
                                                Html::rawElement( 'label', array( 'for' => $id ), $label );
 
-                               $html .= "<br />\n";
+                               $html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $radio );
                        }
                }
 
@@ -1794,7 +1990,7 @@ class HTMLSubmitField extends HTMLFormField {
                return Xml::submitButton(
                        $value,
                        array(
-                               'class' => 'mw-htmlform-submit',
+                               'class' => 'mw-htmlform-submit ' . $this->mClass,
                                'name' => $this->mName,
                                'id' => $this->mID,
                        )
@@ -1807,6 +2003,9 @@ class HTMLSubmitField extends HTMLFormField {
 
        /**
         * Button cannot be invalid
+        * @param $value String
+        * @param $alldata Array
+        * @return Bool
         */
        public function validate( $value, $alldata ){
                return true;
@@ -1828,8 +2027,8 @@ class HTMLEditTools extends HTMLFormField {
                        }
                }
                $msg->inContentLanguage();
-               
-               
+
+
                return '<tr><td></td><td class="mw-input">'
                        . '<div class="mw-editTools">'
                        . $msg->parseAsBlock()