Merge "Fix mime detection of easily-confused-with text/plain formats"
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index 019eb2b..b57b69d 100644 (file)
@@ -117,6 +117,7 @@ class HTMLForm extends ContextSource {
                'hidden' => 'HTMLHiddenField',
                'edittools' => 'HTMLEditTools',
                'checkmatrix' => 'HTMLCheckMatrix',
+               'cloner' => 'HTMLFormFieldCloner',
                // 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.
@@ -155,6 +156,7 @@ class HTMLForm extends ContextSource {
 
        protected $mTitle;
        protected $mMethod = 'post';
+       protected $mWasSubmitted = false;
 
        /**
         * Form action URL. false means we will use the URL to set Title
@@ -302,15 +304,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.
         *
@@ -411,6 +404,7 @@ class HTMLForm extends ContextSource {
                }
 
                if ( $submit ) {
+                       $this->mWasSubmitted = true;
                        $result = $this->trySubmit();
                }
 
@@ -440,11 +434,27 @@ 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;
+
+               # Check for cancelled submission
+               foreach ( $this->mFlatFields as $fieldname => $field ) {
+                       if ( !empty( $field->mParams['nodata'] ) ) {
+                               continue;
+                       }
+                       if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
+                               $this->mWasSubmitted = false;
+                               return false;
+                       }
+               }
+
                # Check for validation
                foreach ( $this->mFlatFields as $fieldname => $field ) {
                        if ( !empty( $field->mParams['nodata'] ) ) {
@@ -470,18 +480,35 @@ class HTMLForm extends ContextSource {
                $data = $this->filterDataForSubmit( $this->mFieldData );
 
                $res = call_user_func( $callback, $data, $this );
+               if ( $res === false ) {
+                       $this->mWasSubmitted = false;
+               }
 
                return $res;
        }
 
+       /**
+        * Test whether the form was considered to have been submitted or not, i.e.
+        * whether the last call to tryAuthorizedSubmit or trySubmit returned
+        * non-false.
+        *
+        * This will return false until HTMLForm::tryAuthorizedSubmit or
+        * HTMLForm::trySubmit is called.
+        *
+        * @since 1.23
+        * @return bool
+        */
+       function wasSubmitted() {
+               return $this->mWasSubmitted;
+       }
+
        /**
         * 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)
         */
@@ -549,7 +576,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)
         */
@@ -571,7 +598,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)
         */
@@ -589,7 +616,7 @@ 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|null $section The section to add the footer text to
         *
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
@@ -611,7 +638,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)
         */
@@ -709,7 +736,7 @@ 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
         */
@@ -720,7 +747,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
         */