Misc EOL w/s and spaces-instead-of-tabs fixes. One day I'll get around to nagging...
[lhc/web/wiklou.git] / includes / HTMLForm.php
index d1a5438..ec038c9 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'.
  *     'required'            -- passed through to the object, indicating that it
  *                              is a required field.
  *     'size'                -- the length of text fields
  *     'validation-callback' -- a function name to give you the chance
  *                              to impose extra validation on the field input.
  *                              @see HTMLForm::validate()
+ *     'name'                -- By default, the 'name' attribute of the input field
+ *                              is "wp{$fieldname}".  If you want a different name
+ *                              (eg one without the "wp" prefix), specify it here and
+ *                              it will be used without modification.
  *
  * TODO: Document 'section' / 'subsection' stuff
  */
 class HTMLForm {
-       static $jsAdded = false;
 
        # A mapping of 'type' inputs onto standard HTMLFormField subclasses
        static $typeMappings = array(
@@ -61,6 +68,7 @@ class HTMLForm {
                'float' => 'HTMLFloatField',
                'info' => 'HTMLInfoField',
                'selectorother' => 'HTMLSelectOrOtherField',
+               'selectandother' => 'HTMLSelectAndOtherField',
                'submit' => 'HTMLSubmitField',
                'hidden' => 'HTMLHiddenField',
                'edittools' => 'HTMLEditTools',
@@ -84,6 +92,8 @@ class HTMLForm {
        protected $mPre = '';
        protected $mHeader = '';
        protected $mFooter = '';
+       protected $mSectionHeaders = array();
+       protected $mSectionFooters = array();
        protected $mPost = '';
        protected $mId;
 
@@ -91,6 +101,8 @@ class HTMLForm {
        protected $mSubmitName;
        protected $mSubmitText;
        protected $mSubmitTooltip;
+
+       protected $mContext; // <! RequestContext
        protected $mTitle;
        protected $mMethod = 'post';
 
@@ -103,10 +115,22 @@ class HTMLForm {
        /**
         * Build a new HTMLForm from an array of field attributes
         * @param $descriptor Array of Field constructs, as described above
+        * @param $context RequestContext available since 1.18, will become compulsory in 1.19.
+        *     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, /*RequestContext*/ $context = null, $messagePrefix = '' ) {
+               if( $context instanceof RequestContext ){
+                       $this->mContext = $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();
@@ -117,15 +141,11 @@ class HTMLForm {
                                ? $info['section']
                                : '';
 
-                       $info['name'] = isset( $info['name'] )
-                               ? $info['name']
-                               : $fieldname;
-
                        if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
                                $this->mUseMultipart = true;
                        }
 
-                       $field = self::loadInputFromParameters( $info );
+                       $field = self::loadInputFromParameters( $fieldname, $info );
                        $field->mParent = $this;
 
                        $setSection =& $loadedDescriptor;
@@ -153,32 +173,31 @@ class HTMLForm {
        /**
         * Add the HTMLForm-specific JavaScript, if it hasn't been
         * done already.
+        * @deprecated since 1.18 load modules with ResourceLoader instead
         */
-       static function addJS() {
-               if ( self::$jsAdded ) return;
-
-               global $wgOut;
-
-               $wgOut->addModules( 'mediawiki.legacy.htmlform' );
-       }
+       static function addJS() { }
 
        /**
         * Initialise a new Object for the field
         * @param $descriptor input Descriptor, as described above
         * @return HTMLFormField subclass
         */
-       static function loadInputFromParameters( $descriptor ) {
+       static function loadInputFromParameters( $fieldname, $descriptor ) {
                if ( isset( $descriptor['class'] ) ) {
                        $class = $descriptor['class'];
                } elseif ( isset( $descriptor['type'] ) ) {
                        $class = self::$typeMappings[$descriptor['type']];
                        $descriptor['class'] = $class;
+               } else {
+                       $class = null;
                }
 
                if ( !$class ) {
                        throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
                }
 
+               $descriptor['fieldname'] = $fieldname;
+
                $obj = new $class( $descriptor );
 
                return $obj;
@@ -189,27 +208,23 @@ 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" );
                }
 
-               // FIXME shouldn't this be closer to displayForm() ?
-               self::addJS();
-
                # Load data from the request.
                $this->loadData();
        }
 
        /**
         * Try submitting, with edit token check first
-        * @return Status|boolean 
+        * @return Status|boolean
         */
        function tryAuthorizedSubmit() {
-               global $wgUser, $wgRequest;
-               $editToken = $wgRequest->getVal( 'wpEditToken' );
+               $editToken = $this->getRequest()->getVal( 'wpEditToken' );
 
                $result = false;
-               if ( $wgUser->matchEditToken( $editToken ) ) {
+               if ( $this->getMethod() != 'post' || $this->getUser()->matchEditToken( $editToken ) ) {
                        $result = $this->trySubmit();
                }
                return $result;
@@ -303,13 +318,31 @@ class HTMLForm {
         * Add header text, inside the form.
         * @param $msg String complete text of message to display
         */
-       function addHeaderText( $msg ) { $this->mHeader .= $msg; }
+       function addHeaderText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mHeader .= $msg;
+               } else {
+                       if ( !isset( $this->mSectionHeaders[$section] ) ) {
+                               $this->mSectionHeaders[$section] = '';
+                       }
+                       $this->mSectionHeaders[$section] .= $msg;
+               }
+       }
 
        /**
         * Add footer text, inside the form.
         * @param $msg String complete text of message to display
         */
-       function addFooterText( $msg ) { $this->mFooter .= $msg; }
+       function addFooterText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mFooter .= $msg;
+               } else {
+                       if ( !isset( $this->mSectionFooters[$section] ) ) {
+                               $this->mSectionFooters[$section] = '';
+                       }
+                       $this->mSectionFooters[$section] .= $msg;
+               }
+       }
 
        /**
         * Add text to the end of the display.
@@ -319,7 +352,7 @@ class HTMLForm {
 
        /**
         * Add a hidden field to the output
-        * @param $name String field name
+        * @param $name String field name.  This will be used exactly as entered
         * @param $value String field value
         * @param $attribs Array
         */
@@ -338,7 +371,9 @@ class HTMLForm {
         * @param $submitResult Mixed output from HTMLForm::trySubmit()
         */
        function displayForm( $submitResult ) {
-               global $wgOut;
+               # For good measure (it is the default)
+               $this->getOutput()->preventClickjacking();
+               $this->getOutput()->addModules( 'mediawiki.htmlform' );
 
                $html = ''
                        . $this->getErrors( $submitResult )
@@ -351,7 +386,7 @@ class HTMLForm {
 
                $html = $this->wrapForm( $html );
 
-               $wgOut->addHTML( ''
+               $this->getOutput()->addHTML( ''
                        . $this->mPre
                        . $html
                        . $this->mPost
@@ -392,11 +427,11 @@ class HTMLForm {
         * @return String HTML.
         */
        function getHiddenFields() {
-               global $wgUser;
-
                $html = '';
-               $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
-               $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+               if( $this->getMethod() == 'post' ){
+                       $html .= Html::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+                       $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+               }
 
                foreach ( $this->mHiddenFields as $data ) {
                        list( $value, $attribs ) = $data;
@@ -423,8 +458,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';
@@ -471,13 +505,16 @@ 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;
-                       $errorstr = $wgOut->parse( $errors->getWikiText() );
+                       if ( $errors->isOK() ) {
+                               $errorstr = '';
+                       } else {
+                               $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
+                       }
                } elseif ( is_array( $errors ) ) {
                        $errorstr = $this->formatErrors( $errors );
                } else {
@@ -494,7 +531,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 ) {
@@ -507,7 +544,7 @@ class HTMLForm {
 
                        $errorstr .= Html::rawElement(
                                'li',
-                               null,
+                               array(),
                                wfMsgExt( $msg, array( 'parseinline' ), $error )
                        );
                }
@@ -545,7 +582,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;
@@ -585,9 +623,41 @@ class HTMLForm {
         * @return Title
         */
        function getTitle() {
-               return $this->mTitle;
+               return $this->mTitle === false
+                       ? $this->getContext()->title
+                       : $this->mTitle;
+       }
+
+       /**
+        * @return RequestContext
+        */
+       public function getContext(){
+               return $this->mContext instanceof RequestContext
+                       ? $this->mContext
+                       : RequestContext::getMain();
+       }
+
+       /**
+        * @return OutputPage
+        */
+       public function getOutput(){
+               return $this->getContext()->output;
+       }
+
+       /**
+        * @return WebRequest
+        */
+       public function getRequest(){
+               return $this->getContext()->request;
        }
-       
+
+       /**
+        * @return User
+        */
+       public function getUser(){
+               return $this->getContext()->user;
+       }
+
        /**
         * Set the method used to submit the form
         * @param $method String
@@ -595,7 +665,7 @@ class HTMLForm {
        public function setMethod( $method='post' ){
                $this->mMethod = $method;
        }
-       
+
        public function getMethod(){
                return $this->mMethod;
        }
@@ -620,7 +690,13 @@ class HTMLForm {
                                        $hasLeftColumn = true;
                        } elseif ( is_array( $value ) ) {
                                $section = $this->displaySection( $value, $key );
-                               $legend = wfMsg( "{$this->mMessagePrefix}-$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";
                        }
                }
@@ -649,8 +725,6 @@ class HTMLForm {
         * Construct the form fields from the Descriptor array
         */
        function loadData() {
-               global $wgRequest;
-
                $fieldData = array();
 
                foreach ( $this->mFlatFields as $fieldname => $field ) {
@@ -659,7 +733,7 @@ class HTMLForm {
                        } elseif ( !empty( $field->mParams['disabled'] ) ) {
                                $fieldData[$fieldname] = $field->getDefault();
                        } else {
-                               $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
+                               $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
                        }
                }
 
@@ -691,6 +765,16 @@ class HTMLForm {
        function filterDataForSubmit( $data ) {
                return $data;
        }
+
+       /**
+        * Get a string to go in the <legend> of a section fieldset.  Override this if you
+        * want something more complicated
+        * @param $key String
+        * @return String
+        */
+       public function getLegend( $key ) {
+               return wfMsg( "{$this->mMessagePrefix}-$key" );
+       }
 }
 
 /**
@@ -707,6 +791,10 @@ abstract class HTMLFormField {
        protected $mID;
        protected $mClass = '';
        protected $mDefault;
+
+       /**
+        * @var HTMLForm
+        */
        public $mParent;
 
        /**
@@ -794,18 +882,18 @@ abstract class HTMLFormField {
                        $this->mLabel = $params['label'];
                }
 
+               $this->mName = "wp{$params['fieldname']}";
                if ( isset( $params['name'] ) ) {
-                       $name = $params['name'];
-                       $validName = Sanitizer::escapeId( $name );
-
-                       if ( $name != $validName ) {
-                               throw new MWException( "Invalid name '$name' passed to " . __METHOD__ );
-                       }
+                       $this->mName = $params['name'];
+               }
 
-                       $this->mName = 'wp' . $name;
-                       $this->mID = 'mw-input-' . $name;
+               $validName = Sanitizer::escapeId( $this->mName );
+               if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
+                       throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
                }
 
+               $this->mID = "mw-input-{$this->mName}";
+
                if ( isset( $params['default'] ) ) {
                        $this->mDefault = $params['default'];
                }
@@ -842,22 +930,23 @@ abstract class HTMLFormField {
         */
        function getTableRow( $value ) {
                # Check for invalid data.
-               global $wgRequest;
 
                $errors = $this->validate( $value, $this->mParent->mFieldData );
-               
+
                $cellAttributes = array();
                $verticalLabel = false;
-               
+
                if ( !empty($this->mParams['vertical-label']) ) {
                        $cellAttributes['colspan'] = 2;
                        $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 {
-                       $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
+                       $errors = self::formatErrors( $errors );
+                       $errorClass = 'mw-htmlform-invalid-input';
                }
 
                $label = $this->getLabelHtml( $cellAttributes );
@@ -866,29 +955,37 @@ abstract class HTMLFormField {
                        array( 'class' => 'mw-input' ) + $cellAttributes,
                        $this->getInputHTML( $value ) . "\n$errors"
                );
-               
+
                $fieldType = get_class( $this );
-               
-               if ($verticalLabel) {
+
+               if ( $verticalLabel ) {
                        $html = Html::rawElement( 'tr',
                                array( 'class' => 'mw-htmlform-vertical-label' ), $label );
                        $html .= Html::rawElement( 'tr',
-                               array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
+                               array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
                                $field );
                } else {
                        $html = Html::rawElement( 'tr',
-                               array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass}" ),
+                               array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
                                $label . $field );
                }
 
                $helptext = null;
 
                if ( isset( $this->mParams['help-message'] ) ) {
-                       $msg = $this->mParams['help-message'];
-                       $helptext = wfMsgExt( $msg, 'parseinline' );
-                       if ( wfEmptyMsg( $msg, $helptext ) ) {
-                               # 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 $name ) {
+                               $msg = wfMessage( $name );
+                               if( $msg->exists() ) {
+                                       $helptext .= $msg->parse(); // append message
+                               }
                        }
                } elseif ( isset( $this->mParams['help'] ) ) {
                        $helptext = $this->mParams['help'];
@@ -938,10 +1035,7 @@ 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'] );
        }
 
        /**
@@ -964,6 +1058,35 @@ abstract class HTMLFormField {
 
                return $flatOpts;
        }
+
+       /**
+        * Formats one or more errors as accepted by field validation-callback.
+        * @param $errors String|Message|Array of strings or Message instances
+        * @return String html
+        * @since 1.18
+        */
+       protected static function formatErrors( $errors ) {
+               if ( is_array( $errors ) && count( $errors ) === 1 ) {
+                       $errors = array_shift( $errors );
+               }
+
+               if ( is_array( $errors ) ) {
+                       $lines = array();
+                       foreach ( $errors as $error ) {
+                               if ( $error instanceof Message ) {
+                                       $lines[] = Html::rawElement( 'li', array(), $error->parse() );
+                               } else {
+                                       $lines[] = Html::rawElement( 'li', array(), $error );
+                               }
+                       }
+                       return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
+               } else {
+                       if ( $errors instanceof Message ) {
+                               $errors = $errors->parse();
+                       }
+                       return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
+               }
+       }
 }
 
 class HTMLTextField extends HTMLFormField {
@@ -1086,11 +1209,11 @@ class HTMLFloatField extends HTMLTextField {
                if ( $p !== true ) {
                        return $p;
                }
-               
+
                $value = trim( $value );
 
                # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
-               # with the addition that a leading '+' sign is ok. 
+               # with the addition that a leading '+' sign is ok.
                if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
                        return wfMsgExt( 'htmlform-float-invalid', 'parse' );
                }
@@ -1129,8 +1252,8 @@ class HTMLIntField extends HTMLFloatField {
                }
 
                # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
-               # with the addition that a leading '+' sign is ok. Note that leading zeros 
-               # are fine, and will be left in the input, which is useful for things like 
+               # with the addition that a leading '+' sign is ok. Note that leading zeros
+               # are fine, and will be left in the input, which is useful for things like
                # phone numbers when you know that they are integers (the HTML5 type=tel
                # input does not require its value to be numeric).  If you want a tidier
                # value to, eg, save in the DB, clean it up with intval().
@@ -1171,6 +1294,10 @@ class HTMLCheckField extends HTMLFormField {
                return '&#160;';
        }
 
+       /**
+        * @param  $request WebRequest
+        * @return String
+        */
        function loadDataFromRequest( $request ) {
                $invert = false;
                if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
@@ -1178,7 +1305,7 @@ class HTMLCheckField extends HTMLFormField {
                }
 
                // GetCheck won't work like we want for checks.
-               if ( $request->getCheck( 'wpEditToken' ) ) {
+               if ( $request->getCheck( 'wpEditToken' ) || $this->mParent->getMethod() != 'post' ) {
                        // XOR has the following truth table, which is what we want
                        // INVERT VALUE | OUTPUT
                        // true   true  | false
@@ -1215,15 +1342,20 @@ 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.
-               $options = array_map( 'strval', $this->mParams['options'] );
+               foreach( $this->mParams['options'] as &$opt ){
+                       if( is_int( $opt ) ){
+                               $opt = strval( $opt );
+                       }
+               }
+               unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
 
                if ( !empty( $this->mParams['disabled'] ) ) {
                        $select->setAttribute( 'disabled', 'disabled' );
                }
 
-               $select->addOptions( $options );
+               $select->addOptions( $this->mParams['options'] );
 
                return $select->getHTML();
        }
@@ -1293,6 +1425,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 );
@@ -1312,6 +1448,14 @@ class HTMLSelectOrOtherField extends HTMLTextField {
  * Multi-select field
  */
 class HTMLMultiSelectField extends HTMLFormField {
+
+       public function __construct( $params ){
+               parent::__construct( $params );
+               if( isset( $params['flatlist'] ) ){
+                       $this->mClass .= ' mw-htmlform-multiselect-flatlist';
+               }
+       }
+
        function validate( $value, $alldata ) {
                $p = parent::validate( $value, $alldata );
 
@@ -1357,31 +1501,41 @@ class HTMLMultiSelectField extends HTMLFormField {
                        } else {
                                $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
 
-                               $checkbox = Xml::check( 
-                                       $this->mName . '[]', 
+                               $checkbox = Xml::check(
+                                       $this->mName . '[]',
                                        in_array( $info, $value, true ),
                                        $attribs + $thisAttribs );
                                $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
 
-                               $html .= $checkbox . '<br />';
+                               $html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-multiselect-item' ), $checkbox );
                        }
                }
 
                return $html;
        }
 
+       /**
+        * @param  $request WebRequest
+        * @return String
+        */
        function loadDataFromRequest( $request ) {
-               # won't work with getCheck
-               if ( $request->getCheck( 'wpEditToken' ) ) {
-                       $arr = $request->getArray( $this->mName );
-
-                       if ( !$arr ) {
-                               $arr = array();
+               if ( $this->mParent->getMethod() == 'post' ) {
+                       if( $request->wasPosted() ){
+                               # Checkboxes are just not added to the request arrays if they're not checked,
+                               # so it's perfectly possible for there not to be an entry at all
+                               return $request->getArray( $this->mName, array() );
+                       } else {
+                               # That's ok, the user has not yet submitted the form, so show the defaults
+                               return $this->getDefault();
                        }
-
-                       return $arr;
                } else {
-                       return $this->getDefault();
+                       # This is the impossible case: if we look at $_GET and see no data for our
+                       # 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.
+                       # @todo FIXME...
+                       return $request->getArray( $this->mName, array() );
                }
        }
 
@@ -1398,6 +1552,167 @@ 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>
+ *     * 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.
+ * @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'] );
+               } else {
+                       $params['other'] = wfMsg( 'htmlform-selectorother-other' );
+               }
+
+               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'] ) );
+               } else {
+                       # Sulk
+                       throw new MWException( 'HTMLSelectAndOtherField called without any options' );
+               }
+               $this->mFlatOptions = self::flattenOptions( $params['options'] );
+
+               parent::__construct( $params );
+       }
+
+       /**
+        * Build a drop-down box from a textual list.
+        * @param $string String message text
+        * @param $otherName String name of "other reason" option
+        * @return Array
+        * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
+        */
+       public static function parseMessage( $string, $otherName=null ) {
+               if( $otherName === null ){
+                       $otherName = wfMsg( 'htmlform-selectorother-other' );
+               }
+
+               $optgroup = false;
+               $options = array( $otherName => 'other' );
+
+               foreach ( explode( "\n", $string ) as $option ) {
+                       $value = trim( $option );
+                       if ( $value == '' ) {
+                               continue;
+                       } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
+                               # A new group is starting...
+                               $value = trim( substr( $value, 1 ) );
+                               $optgroup = $value;
+                       } 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];
+                               } else {
+                                       $options[$optgroup][$parts[1]] = $parts[0];
+                               }
+                       } else {
+                               # groupless reason list
+                               $optgroup = false;
+                               $parts = array_map( 'trim', explode( '|', $option, 2 ) );
+                               if( count( $parts ) === 1 ){
+                                       $parts[1] = $parts[0];
+                               }
+                               $options[$parts[1]] = $parts[0];
+                       }
+               }
+
+               return $options;
+       }
+
+       function getInputHTML( $value ) {
+               $select = parent::getInputHTML( $value[1] );
+
+               $textAttribs = array(
+                       'id' => $this->mID . '-other',
+                       'size' => $this->getSize(),
+               );
+
+               foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
+                       if ( isset( $this->mParams[$param] ) ) {
+                               $textAttribs[$param] = '';
+                       }
+               }
+
+               $textbox = Html::input(
+                       $this->mName . '-other',
+                       $value[2],
+                       'text',
+                       $textAttribs
+               );
+
+               return "$select<br />\n$textbox";
+       }
+
+       /**
+        * @param  $request WebRequest
+        * @return Array( <overall message>, <select value>, <text field value> )
+        */
+       function loadDataFromRequest( $request ) {
+               if ( $request->getCheck( $this->mName ) ) {
+
+                       $list = $request->getText( $this->mName );
+                       $text = $request->getText( $this->mName . '-other' );
+
+                       if ( $list == 'other' ) {
+                               $final = $text;
+                       } elseif( !in_array( $list, $this->mFlatOptions ) ){
+                               # User has spoofed the select form to give an option which wasn't
+                               # in the original offer.  Sulk...
+                               $final = $text;
+                       } elseif( $text == '' ) {
+                               $final = $list;
+                       } else {
+                               $final = $list . wfMsgForContent( 'colon-separator' ) . $text;
+                       }
+
+               } else {
+                       $final = $this->getDefault();
+                       $list = $text = '';
+               }
+               return array( $final, $list, $text );
+       }
+
+       function getSize() {
+               return isset( $this->mParams['size'] )
+                       ? $this->mParams['size']
+                       : 45;
+       }
+
+       function validate( $value, $alldata ) {
+               # HTMLSelectField forces $value to be one of the options in the select
+               # field, which is not useful here.  But we do want the validation further up
+               # the chain
+               $p = parent::validate( $value[1], $alldata );
+
+               if ( $p !== true ) {
+                       return $p;
+               }
+
+               if( isset( $this->mParams['required'] ) && $value[1] === '' ){
+                       return wfMsgExt( 'htmlform-required', 'parseinline' );
+               }
+
+               return true;
+       }
+}
+
 /**
  * Radio checkbox fields.
  */
@@ -1498,10 +1813,7 @@ class HTMLInfoField extends HTMLFormField {
 class HTMLHiddenField extends HTMLFormField {
        public function __construct( $params ) {
                parent::__construct( $params );
-               # forcing the 'wp' prefix on hidden field names
-               # is undesirable
-               $this->mName = substr( $this->mName, 2 );
-               
+
                # Per HTML5 spec, hidden fields cannot be 'required'
                # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
                unset( $this->mParams['required'] );
@@ -1550,7 +1862,7 @@ class HTMLSubmitField extends HTMLFormField {
        protected function needsLabel() {
                return false;
        }
-       
+
        /**
         * Button cannot be invalid
         */
@@ -1565,11 +1877,20 @@ class HTMLEditTools extends HTMLFormField {
        }
 
        public function getTableRow( $value ) {
-               return "<tr><td></td><td class=\"mw-input\">"
+               if ( empty( $this->mParams['message'] ) ) {
+                       $msg = wfMessage( 'edittools' );
+               } else {
+                       $msg = wfMessage( $this->mParams['message'] );
+                       if ( $msg->isDisabled() ) {
+                               $msg = wfMessage( 'edittools' );
+                       }
+               }
+               $msg->inContentLanguage();
+
+
+               return '<tr><td></td><td class="mw-input">'
                        . '<div class="mw-editTools">'
-                       . wfMsgExt( empty( $this->mParams['message'] )
-                               ? 'edittools' : $this->mParams['message'],
-                               array( 'parse', 'content' ) )
+                       . $msg->parseAsBlock()
                        . "</div></td></tr>\n";
        }
 }