Avoid GlobalTitleFail in HTMLFormField::__construct
authorKunal Mehta <legoktm@gmail.com>
Wed, 17 Dec 2014 22:19:06 +0000 (14:19 -0800)
committerKunal Mehta <legoktm@gmail.com>
Wed, 17 Dec 2014 22:20:14 +0000 (14:20 -0800)
Pass the HTMLForm parent instance in the constructor so context
is available when parsing a message.

Change-Id: I532c0d95698cbcc57294b9bd2725f33838f393a9

includes/Preferences.php
includes/htmlform/HTMLForm.php
includes/htmlform/HTMLFormField.php
includes/htmlform/HTMLFormFieldCloner.php

index 44995ac..aca6dcb 100644 (file)
@@ -130,8 +130,7 @@ class Preferences {
                        if ( $disable && !in_array( $name, self::$saveBlacklist ) ) {
                                $info['disabled'] = 'disabled';
                        }
-                       $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation
-                       $field->mParent = $dummyForm;
+                       $field = HTMLForm::loadInputFromParameters( $name, $info, $dummyForm ); // For validation
                        $defaultOptions = User::getDefaultOptions();
                        $globalDefault = isset( $defaultOptions[$name] )
                                ? $defaultOptions[$name]
index 62345b8..df805aa 100644 (file)
@@ -246,10 +246,7 @@ class HTMLForm extends ContextSource {
                                $this->mUseMultipart = true;
                        }
 
-                       $field = self::loadInputFromParameters( $fieldname, $info );
-                       // FIXME During field's construct, the parent form isn't available!
-                       // could add a 'parent' name-value to $info, could add a third parameter.
-                       $field->mParent = $this;
+                       $field = self::loadInputFromParameters( $fieldname, $info, $this );
 
                        // vform gets too much space if empty labels generate HTML.
                        if ( $this->isVForm() ) {
@@ -359,14 +356,18 @@ class HTMLForm extends ContextSource {
         *
         * @param string $fieldname Name of the field
         * @param array $descriptor Input Descriptor, as described above
+        * @param HTMLForm|null $parent Parent instance of HTMLForm
         *
         * @throws MWException
         * @return HTMLFormField Instance of a subclass of HTMLFormField
         */
-       public static function loadInputFromParameters( $fieldname, $descriptor ) {
+       public static function loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent = null ) {
                $class = self::getClassFromDescriptor( $fieldname, $descriptor );
 
                $descriptor['fieldname'] = $fieldname;
+               if ( $parent ) {
+                       $descriptor['parent'] = $parent;
+               }
 
                # @todo This will throw a fatal error whenever someone try to use
                # 'class' to feed a CSS class instead of 'cssclass'. Would be
index 4cf2394..861ae4c 100644 (file)
@@ -343,6 +343,10 @@ abstract class HTMLFormField {
        function __construct( $params ) {
                $this->mParams = $params;
 
+               if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
+                       $this->mParent = $params['parent'];
+               }
+
                # Generate the label from a message, if possible
                if ( isset( $params['label-message'] ) ) {
                        $msgInfo = $params['label-message'];
@@ -354,7 +358,7 @@ abstract class HTMLFormField {
                                $msgInfo = array();
                        }
 
-                       $this->mLabel = wfMessage( $msg, $msgInfo )->parse();
+                       $this->mLabel = $this->msg( $msg, $msgInfo )->parse();
                } elseif ( isset( $params['label'] ) ) {
                        if ( $params['label'] === '&#160;' ) {
                                // Apparently some things set &nbsp directly and in an odd format
index 5dadaf8..d1b7746 100644 (file)
@@ -96,8 +96,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                        } else {
                                $info['id'] = Sanitizer::escapeId( "{$this->mID}--$key--$fieldname" );
                        }
-                       $field = HTMLForm::loadInputFromParameters( $name, $info );
-                       $field->mParent = $this->mParent;
+                       $field = HTMLForm::loadInputFromParameters( $name, $info, $this->mParent );
                        $fields[$fieldname] = $field;
                }
                return $fields;
@@ -310,8 +309,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                                'id' => Sanitizer::escapeId( "{$this->mID}--$key--delete" ),
                                'cssclass' => 'mw-htmlform-cloner-delete-button',
                                'default' => $this->msg( $label )->text(),
-                       ) );
-                       $field->mParent = $this->mParent;
+                       ), $this->mParent );
                        $v = $field->getDefault();
 
                        if ( $displayFormat === 'table' ) {
@@ -383,8 +381,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                        'id' => Sanitizer::escapeId( "{$this->mID}--create" ),
                        'cssclass' => 'mw-htmlform-cloner-create-button',
                        'default' => $this->msg( $label )->text(),
-               ) );
-               $field->mParent = $this->mParent;
+               ), $this->mParent );
                $html .= $field->getInputHTML( $field->getDefault() );
 
                return $html;