From 4603f2802520d60f2b21deacd68682213fc5f650 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 17 Dec 2014 14:19:06 -0800 Subject: [PATCH] Avoid GlobalTitleFail in HTMLFormField::__construct Pass the HTMLForm parent instance in the constructor so context is available when parsing a message. Change-Id: I532c0d95698cbcc57294b9bd2725f33838f393a9 --- includes/Preferences.php | 3 +-- includes/htmlform/HTMLForm.php | 11 ++++++----- includes/htmlform/HTMLFormField.php | 6 +++++- includes/htmlform/HTMLFormFieldCloner.php | 9 +++------ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/includes/Preferences.php b/includes/Preferences.php index 44995aca35..aca6dcbc3f 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -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] diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 62345b8cd4..df805aaa92 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -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 diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 4cf23942ff..861ae4cc15 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -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'] === ' ' ) { // Apparently some things set   directly and in an odd format diff --git a/includes/htmlform/HTMLFormFieldCloner.php b/includes/htmlform/HTMLFormFieldCloner.php index 5dadaf8f7d..d1b7746c1e 100644 --- a/includes/htmlform/HTMLFormFieldCloner.php +++ b/includes/htmlform/HTMLFormFieldCloner.php @@ -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; -- 2.20.1