Merge changes I48dcf831,I8922b80e
[lhc/web/wiklou.git] / includes / HTMLForm.php
index 7adbfc8..e06a934 100644 (file)
@@ -140,6 +140,7 @@ class HTMLForm extends ContextSource {
        protected $mSectionFooters = array();
        protected $mPost = '';
        protected $mId;
+       protected $mTableId = '';
 
        protected $mSubmitID;
        protected $mSubmitName;
@@ -200,12 +201,12 @@ class HTMLForm extends ContextSource {
                        $this->setContext( $context );
                        $this->mTitle = false; // We don't need them to set a title
                        $this->mMessagePrefix = $messagePrefix;
-               } else {
+               } elseif ( is_null( $context ) && $messagePrefix !== '' ) {
+                       $this->mMessagePrefix = $messagePrefix;
+               } elseif ( is_string( $context ) && $messagePrefix === '' ) {
                        // B/C since 1.18
-                       if ( is_string( $context ) && $messagePrefix === '' ) {
-                               // it's actually $messagePrefix
-                               $this->mMessagePrefix = $context;
-                       }
+                       // it's actually $messagePrefix
+                       $this->mMessagePrefix = $context;
                }
 
                // Expand out into a tree.
@@ -587,8 +588,8 @@ class HTMLForm extends ContextSource {
        }
 
        /**
-        * Display the form (sending to $wgOut), with an appropriate error
-        * message or stack of messages, and any validation errors, etc.
+        * Display the form (sending to the context's OutputPage object), with an
+        * appropriate error message or stack of messages, and any validation errors, etc.
         *
         * @attention You should call prepareForm() before calling this function.
         * Moreover, when doing method chaining this should be the very last method
@@ -642,8 +643,8 @@ class HTMLForm extends ContextSource {
                        : 'application/x-www-form-urlencoded';
                # Attributes
                $attribs = array(
-                       'action' => $this->mAction === false ? $this->getTitle()->getFullURL() : $this->mAction,
-                       'method' => $this->mMethod,
+                       'action' => $this->getAction(),
+                       'method' => $this->getMethod(),
                        'class' => 'visualClear',
                        'enctype' => $encType,
                );
@@ -742,7 +743,7 @@ class HTMLForm extends ContextSource {
         * @return String
         */
        function getBody() {
-               return $this->displaySection( $this->mFieldTree );
+               return $this->displaySection( $this->mFieldTree, $this->mTableId );
        }
 
        /**
@@ -871,6 +872,18 @@ class HTMLForm extends ContextSource {
                return $this;
        }
 
+       /**
+        * Set the id of the \<table\> or outermost \<div\> element.
+        *
+        * @since 1.22
+        * @param string $id new value of the id attribute, or "" to remove
+        * @return HTMLForm $this for chaining calls
+        */
+       public function setTableId( $id ) {
+               $this->mTableId = $id;
+               return $this;
+       }
+
        /**
         * @param string $id DOM id for the form
         * @return HTMLForm $this for chaining calls (since 1.20)
@@ -879,6 +892,7 @@ class HTMLForm extends ContextSource {
                $this->mId = $id;
                return $this;
        }
+
        /**
         * Prompt the whole form to be wrapped in a "<fieldset>", with
         * this text as its "<legend>" element.
@@ -977,7 +991,7 @@ class HTMLForm extends ContextSource {
                                        $hasLabel = true;
                                }
                        } elseif ( is_array( $value ) ) {
-                               $section = $this->displaySection( $value, $key, "$fieldsetIDPrefix$key-" );
+                               $section = $this->displaySection( $value, "mw-htmlform-$key", "$fieldsetIDPrefix$key-" );
                                $legend = $this->getLegend( $key );
                                if ( isset( $this->mSectionHeaders[$key] ) ) {
                                        $section = $this->mSectionHeaders[$key] . $section;
@@ -1005,7 +1019,7 @@ class HTMLForm extends ContextSource {
                        );
 
                        if ( $sectionName ) {
-                               $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" );
+                               $attribs['id'] = Sanitizer::escapeId( $sectionName );
                        }
 
                        if ( $displayFormat === 'table' ) {
@@ -1093,6 +1107,33 @@ class HTMLForm extends ContextSource {
                $this->mAction = $action;
                return $this;
        }
+
+       /**
+        * Get the value for the action attribute of the form.
+        *
+        * @since 1.22
+        *
+        * @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 "?"
+               // 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
+               // and put title in an hidden field (see getHiddenFields()).
+               if ( strpos( $wgArticlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
+                       return $wgScript;
+               }
+
+               return $this->getTitle()->getLocalURL();
+       }
 }
 
 /**
@@ -1848,12 +1889,18 @@ class HTMLCheckField extends HTMLFormField {
  * are of the form "columnName-rowName"
  *
  * Options:
- *   columns:           Required list of columns in the matrix.
- *   rows:              Required list of rows in the matrix.
- *   force-options-on:  Accepts array of column-row tags to be displayed as enabled
- *                      but unavailable to change
- *   force-options-off: Accepts array of column-row tags to be displayed as disabled
- *                      but unavailable to change.
+ *   - columns
+ *     - Required list of columns in the matrix.
+ *   - rows
+ *     - Required list of rows in the matrix.
+ *   - force-options-on
+ *     - Accepts array of column-row tags to be displayed as enabled but unavailable to change
+ *   - force-options-off
+ *     - Accepts array of column-row tags to be displayed as disabled but unavailable to change.
+ *   - tooltips
+ *     - Optional array mapping row label to tooltip content
+ *   - tooltip-class
+ *     - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
  */
 class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
 
@@ -1867,7 +1914,7 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
        public function __construct( $params ) {
                $missing = array_diff( self::$requiredParams, array_keys( $params ) );
                if ( $missing ) {
-                       throw HTMLFormFieldRequiredOptionsException::create( $this, $missing );
+                       throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
                }
                parent::__construct( $params );
        }
@@ -1930,8 +1977,21 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
                }
                $tableContents .= Html::rawElement( 'tr', array(), "\n$headerContents\n" );
 
+               $tooltipClass = 'mw-icon-question';
+               if ( isset( $this->mParams['tooltip-class'] ) ) {
+                       $tooltipClass = $this->mParams['tooltip-class'];
+               }
+
                // Build the options matrix
                foreach ( $rows as $rowLabel => $rowTag ) {
+                       // Append tooltip if configured
+                       if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
+                               $tooltipAttribs = array(
+                                       'class' => "mw-htmlform-tooltip $tooltipClass",
+                                       'title' =>  $this->mParams['tooltips'][$rowLabel],
+                               );
+                               $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
+                       }
                        $rowContents = Html::rawElement( 'td', array(), $rowLabel );
                        foreach ( $columns as $columnTag ) {
                                $thisTag = "$columnTag-$rowTag";
@@ -2770,8 +2830,8 @@ interface HTMLNestedFilterable {
 }
 
 class HTMLFormFieldRequiredOptionsException extends MWException {
-       static public function create( HTMLFormField $field, array $missing ) {
-               return new self( sprintf(
+       public function __construct( HTMLFormField $field, array $missing ) {
+               parent::__construct( sprintf(
                        "Form type `%s` expected the following parameters to be set: %s",
                        get_class( $field ),
                        implode( ', ', $missing )