Apply vform styling to HTMLForm buttons
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index 01f3ab7..c810f68 100644 (file)
@@ -50,6 +50,7 @@
  *    'default'             -- default value when the form is displayed
  *    'id'                  -- HTML id attribute
  *    'cssclass'            -- CSS class
+ *    'csshelpclass'        -- CSS class used to style help text
  *    'options'             -- associative array mapping labels to values.
  *                             Some field types support multi-level arrays.
  *    'options-messages'    -- associative array mapping message keys to values.
@@ -106,6 +107,7 @@ class HTMLForm extends ContextSource {
                'select' => 'HTMLSelectField',
                'radio' => 'HTMLRadioField',
                'multiselect' => 'HTMLMultiSelectField',
+               'limitselect' => 'HTMLSelectLimitField',
                'check' => 'HTMLCheckField',
                'toggle' => 'HTMLCheckField',
                'int' => 'HTMLIntField',
@@ -118,6 +120,7 @@ class HTMLForm extends ContextSource {
                'edittools' => 'HTMLEditTools',
                'checkmatrix' => 'HTMLCheckMatrix',
                'cloner' => 'HTMLFormFieldCloner',
+               'autocompleteselect' => 'HTMLAutoCompleteSelectField',
                // HTMLTextField will output the correct type="" attribute automagically.
                // There are about four zillion other HTML5 input types, like range, but
                // we don't use those at the moment, so no point in adding all of them.
@@ -171,6 +174,12 @@ class HTMLForm extends ContextSource {
 
        protected $mWrapperLegend = false;
 
+       /**
+        * Salt for the edit token.
+        * @var string|array
+        */
+       protected $mTokenSalt = '';
+
        /**
         * If true, sections that contain both fields and subsections will
         * render their subsections before their fields.
@@ -292,7 +301,11 @@ class HTMLForm extends ContextSource {
         * @return string
         */
        public function getDisplayFormat() {
-               return $this->displayFormat;
+               $format = $this->displayFormat;
+               if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
+                       $format = 'div';
+               }
+               return $format;
        }
 
        /**
@@ -304,15 +317,6 @@ class HTMLForm extends ContextSource {
                return $this->displayFormat === 'vform';
        }
 
-       /**
-        * Add the HTMLForm-specific JavaScript, if it hasn't been
-        * done already.
-        * @deprecated since 1.18 load modules with ResourceLoader instead
-        */
-       static function addJS() {
-               wfDeprecated( __METHOD__, '1.18' );
-       }
-
        /**
         * Get the HTMLFormField subclass for this descriptor.
         *
@@ -354,7 +358,7 @@ class HTMLForm extends ContextSource {
         * @param array $descriptor Input Descriptor, as described above
         *
         * @throws MWException
-        * @return HTMLFormField subclass
+        * @return HTMLFormField Instance of a subclass of HTMLFormField
         */
        public static function loadInputFromParameters( $fieldname, $descriptor ) {
                $class = self::getClassFromDescriptor( $fieldname, $descriptor );
@@ -406,7 +410,7 @@ class HTMLForm extends ContextSource {
                                // 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 );
+                               $submit = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
                        } else {
                                $submit = true;
                        }
@@ -443,9 +447,12 @@ class HTMLForm extends ContextSource {
         * Validate all the fields, and call the submission callback
         * function if everything is kosher.
         * @throws MWException
-        * @return mixed Bool true == Successful submission, Bool false
-        *     == No submission attempted, anything else == Error to
-        *     display.
+        * @return bool|string|array|Status
+        *     - Bool true or a good Status object indicates success,
+        *     - Bool false indicates no submission was attempted,
+        *     - Anything else indicates failure. The value may be a fatal Status
+        *       object, an HTML string, or an array of arrays (message keys and
+        *       params) or strings (message keys)
         */
        function trySubmit() {
                $this->mWasSubmitted = true;
@@ -512,10 +519,9 @@ class HTMLForm extends ContextSource {
         * Set a callback to a function to do something with the form
         * once it's been successfully validated.
         *
-        * @param string $cb Function name.  The function will be passed
-        *     the output from HTMLForm::filterDataForSubmit, and must
-        *     return Bool true on success, Bool false if no submission
-        *     was attempted, or String HTML output to display on error.
+        * @param callable $cb The function will be passed the output from
+        *   HTMLForm::filterDataForSubmit and this HTMLForm object, and must
+        *   return as documented for HTMLForm::trySubmit
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -583,7 +589,7 @@ class HTMLForm extends ContextSource {
         * Add header text, inside the form.
         *
         * @param string $msg Complete text of message to display
-        * @param string $section The section to add the header to
+        * @param string|null $section The section to add the header to
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -605,7 +611,7 @@ class HTMLForm extends ContextSource {
         * @since 1.19
         *
         * @param string $msg Complete text of message to display
-        * @param string $section The section to add the header to
+        * @param string|null $section The section to add the header to
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -622,8 +628,8 @@ class HTMLForm extends ContextSource {
        /**
         * Add footer text, inside the form.
         *
-        * @param string $msg complete text of message to display
-        * @param string $section The section to add the footer text to
+        * @param string $msg Complete text of message to display
+        * @param string|null $section The section to add the footer text to
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -645,7 +651,7 @@ class HTMLForm extends ContextSource {
         * @since 1.19
         *
         * @param string $msg Complete text of message to display
-        * @param string $section The section to add the footer text to
+        * @param string|null $section The section to add the footer text to
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -735,6 +741,21 @@ class HTMLForm extends ContextSource {
                return $this;
        }
 
+       /**
+        * Set the salt for the edit token.
+        *
+        * Only useful when the method is "post".
+        *
+        * @since 1.24
+        * @param string|array $salt Salt to use
+        * @return HTMLForm $this For chaining calls
+        */
+       public function setTokenSalt( $salt ) {
+               $this->mTokenSalt = $salt;
+
+               return $this;
+       }
+
        /**
         * Display the form (sending to the context's OutputPage object), with an
         * appropriate error message or stack of messages, and any validation errors, etc.
@@ -743,9 +764,9 @@ class HTMLForm extends ContextSource {
         * Moreover, when doing method chaining this should be the very last method
         * call just after prepareForm().
         *
-        * @param mixed $submitResult Mixed output from HTMLForm::trySubmit()
+        * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
         *
-        * @return Nothing, should be last call
+        * @return void Nothing, should be last call
         */
        function displayForm( $submitResult ) {
                $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
@@ -754,7 +775,7 @@ class HTMLForm extends ContextSource {
        /**
         * Returns the raw HTML generated by the form
         *
-        * @param mixed $submitResult Mixed output from HTMLForm::trySubmit()
+        * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
         *
         * @return string
         */
@@ -824,19 +845,18 @@ class HTMLForm extends ContextSource {
         * @return string HTML.
         */
        function getHiddenFields() {
-               global $wgArticlePath;
-
                $html = '';
                if ( $this->getMethod() == 'post' ) {
                        $html .= Html::hidden(
                                'wpEditToken',
-                               $this->getUser()->getEditToken(),
+                               $this->getUser()->getEditToken( $this->mTokenSalt ),
                                array( 'id' => 'wpEditToken' )
                        ) . "\n";
                        $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
                }
 
-               if ( strpos( $wgArticlePath, '?' ) !== false && $this->getMethod() == 'get' ) {
+               $articlePath = $this->getConfig()->get( 'ArticlePath' );
+               if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() == 'get' ) {
                        $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
                }
 
@@ -854,6 +874,7 @@ class HTMLForm extends ContextSource {
         */
        function getButtons() {
                $buttons = '';
+               $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
 
                if ( $this->mShowSubmit ) {
                        $attribs = array();
@@ -872,15 +893,17 @@ class HTMLForm extends ContextSource {
 
                        $attribs['class'] = array( 'mw-htmlform-submit' );
 
+                       if ( $this->isVForm() || $useMediaWikiUIEverywhere ) {
+                               array_push( $attribs['class'], 'mw-ui-button', 'mw-ui-constructive' );
+                       }
+
                        if ( $this->isVForm() ) {
                                // mw-ui-block is necessary because the buttons aren't necessarily in an
                                // immediate child div of the vform.
                                // @todo Let client specify if the primary submit button is progressive or destructive
                                array_push(
                                        $attribs['class'],
-                                       'mw-ui-button',
                                        'mw-ui-big',
-                                       'mw-ui-constructive',
                                        'mw-ui-block'
                                );
                        }
@@ -913,6 +936,17 @@ class HTMLForm extends ContextSource {
                                $attrs['id'] = $button['id'];
                        }
 
+                       if ( $this->isVForm() || $useMediaWikiUIEverywhere ) {
+                               if ( isset( $attrs['class'] ) ) {
+                                       $attrs['class'] .= ' mw-ui-button';
+                               } else {
+                                       $attrs['class'] = 'mw-ui-button';
+                               }
+                               if ( $this->isVForm() ) {
+                                       $attrs['class'] .= ' mw-ui-big mw-ui-block';
+                               }
+                       }
+
                        $buttons .= Html::element( 'input', $attrs ) . "\n";
                }
 
@@ -965,7 +999,7 @@ class HTMLForm extends ContextSource {
        /**
         * Format a stack of error messages into a single HTML string
         *
-        * @param array $errors of message keys/values
+        * @param array $errors Array of message keys/values
         *
         * @return string HTML, a "<ul>" list of errors
         */
@@ -995,7 +1029,7 @@ class HTMLForm extends ContextSource {
        /**
         * Set the text for the submit button
         *
-        * @param string $t plaintext.
+        * @param string $t Plaintext
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -1009,12 +1043,15 @@ class HTMLForm extends ContextSource {
         * Set the text for the submit button to a message
         * @since 1.19
         *
-        * @param string $msg Message key
+        * @param string|Message $msg Message key or Message object
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
        public function setSubmitTextMsg( $msg ) {
-               $this->setSubmitText( $this->msg( $msg )->text() );
+               if ( !$msg instanceof Message ) {
+                       $msg = $this->msg( $msg );
+               }
+               $this->setSubmitText( $msg->text() );
 
                return $this;
        }
@@ -1128,12 +1165,15 @@ class HTMLForm extends ContextSource {
         * this message as its "<legend>" element.
         * @since 1.19
         *
-        * @param string $msg Message key
+        * @param string|Message $msg Message key or Message object
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
        public function setWrapperLegendMsg( $msg ) {
-               $this->setWrapperLegend( $this->msg( $msg )->text() );
+               if ( !$msg instanceof Message ) {
+                       $msg = $this->msg( $msg );
+               }
+               $this->setWrapperLegend( $msg->text() );
 
                return $this;
        }
@@ -1224,6 +1264,9 @@ class HTMLForm extends ContextSource {
                                // Close enough to a div.
                                $getFieldHtmlMethod = 'getDiv';
                                break;
+                       case 'div':
+                               $getFieldHtmlMethod = 'getDiv';
+                               break;
                        default:
                                $getFieldHtmlMethod = 'get' . ucfirst( $displayFormat );
                }
@@ -1355,7 +1398,7 @@ class HTMLForm extends ContextSource {
         *
         * @param array $data
         *
-        * @return
+        * @return array
         */
        function filterDataForSubmit( $data ) {
                return $data;
@@ -1397,20 +1440,19 @@ class HTMLForm extends ContextSource {
         * @return string
         */
        public function getAction() {
-               global $wgScript, $wgArticlePath;
-
                // If an action is alredy provided, return it
                if ( $this->mAction !== false ) {
                        return $this->mAction;
                }
 
-               // Check whether we are in GET mode and $wgArticlePath contains a "?"
+               $articlePath = $this->getConfig()->get( 'ArticlePath' );
+               // Check whether we are in GET mode and the ArticlePath contains a "?"
                // meaning that getLocalURL() would return something like "index.php?title=...".
                // As browser remove the query string before submitting GET forms,
-               // it means that the title would be lost. In such case use $wgScript instead
+               // it means that the title would be lost. In such case use wfScript() instead
                // and put title in an hidden field (see getHiddenFields()).
-               if ( strpos( $wgArticlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
-                       return $wgScript;
+               if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
+                       return wfScript();
                }
 
                return $this->getTitle()->getLocalURL();