revert r82283, loads of unrelated changes
[lhc/web/wiklou.git] / includes / HTMLForm.php
index fbb17d4..2472bc2 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
@@ -88,6 +92,8 @@ class HTMLForm {
        protected $mPre = '';
        protected $mHeader = '';
        protected $mFooter = '';
+       protected $mSectionHeaders = array();
+       protected $mSectionFooters = array();
        protected $mPost = '';
        protected $mId;
 
@@ -305,13 +311,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.
@@ -342,6 +366,9 @@ class HTMLForm {
        function displayForm( $submitResult ) {
                global $wgOut;
 
+               # For good measure (it is the default)
+               $wgOut->preventClickjacking();
+
                $html = ''
                        . $this->getErrors( $submitResult )
                        . $this->mHeader
@@ -482,7 +509,11 @@ class HTMLForm {
        function getErrors( $errors ) {
                if ( $errors instanceof Status ) {
                        global $wgOut;
-                       $errorstr = $wgOut->parse( $errors->getWikiText() );
+                       if ( $errors->isOK() ) {
+                               $errorstr = '';
+                       } else {
+                               $errorstr = $wgOut->parse( $errors->getWikiText() );
+                       }
                } elseif ( is_array( $errors ) ) {
                        $errorstr = $this->formatErrors( $errors );
                } else {
@@ -626,7 +657,13 @@ class HTMLForm {
                        } elseif ( is_array( $value ) ) {
                                $section = $this->displaySection( $value, $key );
                                $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
-                               $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
+                               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";                                   
                        }
                }
 
@@ -895,6 +932,17 @@ abstract class HTMLFormField {
                                # Never mind
                                $helptext = null;
                        }
+               } 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;
+                               }
+                               $helptext .= $candidate; // append message
+                       }       
                } elseif ( isset( $this->mParams['help'] ) ) {
                        $helptext = $this->mParams['help'];
                }
@@ -1222,16 +1270,18 @@ class HTMLSelectField extends HTMLFormField {
                # If one of the options' 'name' is int(0), it is automatically selected.
                # because PHP sucks and things int(0) == 'some string'.
                # Working around this by forcing all of them to strings.
-               $options = array_map( 
-                       create_function('$opt', 'return is_int($opt) ? strval($opt) : $opt;'),
-                       $this->mParams['options'] 
-               );
+               foreach( $this->mParams['options'] as $key => &$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();
        }
@@ -1570,11 +1620,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";
        }
 }