Localisation updates for core and extension messages from translatewiki.net (2010...
[lhc/web/wiklou.git] / includes / HTMLForm.php
index 62aecb0..8bed092 100644 (file)
  *     '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
  */
@@ -92,6 +96,7 @@ class HTMLForm {
        protected $mSubmitText;
        protected $mSubmitTooltip;
        protected $mTitle;
+       protected $mMethod = 'post';
 
        protected $mUseMultipart = false;
        protected $mHiddenFields = array();
@@ -116,15 +121,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;
@@ -166,7 +167,7 @@ class HTMLForm {
         * @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'] ) ) {
@@ -177,6 +178,8 @@ class HTMLForm {
                if ( !$class ) {
                        throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
                }
+               
+               $descriptor['fieldname'] = $fieldname;
 
                $obj = new $class( $descriptor );
 
@@ -208,7 +211,7 @@ class HTMLForm {
                $editToken = $wgRequest->getVal( 'wpEditToken' );
 
                $result = false;
-               if ( $wgUser->matchEditToken( $editToken ) ) {
+               if ( $this->getMethod() != 'post' || $wgUser->matchEditToken( $editToken ) ) {
                        $result = $this->trySubmit();
                }
                return $result;
@@ -318,7 +321,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
         */
@@ -375,7 +378,7 @@ class HTMLForm {
                # Attributes
                $attribs = array(
                        'action'  => $this->getTitle()->getFullURL(),
-                       'method'  => 'post',
+                       'method'  => $this->mMethod,
                        'class'   => 'visualClear',
                        'enctype' => $encType,
                );
@@ -394,8 +397,11 @@ class HTMLForm {
                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', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+                       $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+               }
 
                foreach ( $this->mHiddenFields as $data ) {
                        list( $value, $attribs ) = $data;
@@ -586,6 +592,18 @@ class HTMLForm {
        function getTitle() {
                return $this->mTitle;
        }
+       
+       /**
+        * Set the method used to submit the form
+        * @param $method String
+        */
+       public function setMethod( $method='post' ){
+               $this->mMethod = $method;
+       }
+       
+       public function getMethod(){
+               return $this->mMethod;
+       }
 
        /**
         * TODO: Document
@@ -781,17 +799,17 @@ 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 = 'wp' . $name;
-                       $this->mID = 'mw-input-' . $name;
+                       $this->mName = $params['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'];
@@ -841,7 +859,7 @@ abstract class HTMLFormField {
                        $verticalLabel = true;
                }
 
-               if ( $errors === true || !$wgRequest->wasPosted() ) {
+               if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) {
                        $errors = '';
                } else {
                        $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
@@ -1204,13 +1222,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( 'strval', $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();
        }
@@ -1485,9 +1508,6 @@ 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