r78452 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78451‎ | r78452 | r78453 >
Date:21:14, 15 December 2010
Author:happy-melon
Status:ok (Comments)
Tags:
Comment:
Clean up the running mess that is r64866, r65040, and then r67277. Implement and document consistent behaviour for all types of fields: if the 'name' parameter is specified, use it exactly as is, otherwise use "wp{$fieldname}". For hidden fields added with addHiddenField(), continue to use either $attrib['name'] or $name, both unaltered.
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)
  • /trunk/phase3/includes/Preferences.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialEmailuser.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUpload.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUploadStash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/HTMLForm.php
@@ -42,6 +42,10 @@
4343 * 'validation-callback' -- a function name to give you the chance
4444 * to impose extra validation on the field input.
4545 * @see HTMLForm::validate()
 46+ * 'name' -- By default, the 'name' attribute of the input field
 47+ * is "wp{$fieldname}". If you want a different name
 48+ * (eg one without the "wp" prefix), specify it here and
 49+ * it will be used without modification.
4650 *
4751 * TODO: Document 'section' / 'subsection' stuff
4852 */
@@ -117,15 +121,11 @@
118122 ? $info['section']
119123 : '';
120124
121 - $info['name'] = isset( $info['name'] )
122 - ? $info['name']
123 - : $fieldname;
124 -
125125 if ( isset( $info['type'] ) && $info['type'] == 'file' ) {
126126 $this->mUseMultipart = true;
127127 }
128128
129 - $field = self::loadInputFromParameters( $info );
 129+ $field = self::loadInputFromParameters( $fieldname, $info );
130130 $field->mParent = $this;
131131
132132 $setSection =& $loadedDescriptor;
@@ -167,7 +167,7 @@
168168 * @param $descriptor input Descriptor, as described above
169169 * @return HTMLFormField subclass
170170 */
171 - static function loadInputFromParameters( $descriptor ) {
 171+ static function loadInputFromParameters( $fieldname, $descriptor ) {
172172 if ( isset( $descriptor['class'] ) ) {
173173 $class = $descriptor['class'];
174174 } elseif ( isset( $descriptor['type'] ) ) {
@@ -178,6 +178,8 @@
179179 if ( !$class ) {
180180 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
181181 }
 182+
 183+ $descriptor['fieldname'] = $fieldname;
182184
183185 $obj = new $class( $descriptor );
184186
@@ -319,7 +321,7 @@
320322
321323 /**
322324 * Add a hidden field to the output
323 - * @param $name String field name
 325+ * @param $name String field name. This will be used exactly as entered
324326 * @param $value String field value
325327 * @param $attribs Array
326328 */
@@ -794,17 +796,17 @@
795797 $this->mLabel = $params['label'];
796798 }
797799
 800+ $this->mName = "wp{$params['fieldname']}";
798801 if ( isset( $params['name'] ) ) {
799 - $name = $params['name'];
800 - $validName = Sanitizer::escapeId( $name );
801 -
802 - if ( $name != $validName ) {
803 - throw new MWException( "Invalid name '$name' passed to " . __METHOD__ );
804 - }
805 -
806 - $this->mName = 'wp' . $name;
807 - $this->mID = 'mw-input-' . $name;
 802+ $this->mName = $params['name'];
808803 }
 804+
 805+ $validName = Sanitizer::escapeId( $this->mName );
 806+ if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
 807+ throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
 808+ }
 809+
 810+ $this->mID = "mw-input-{$this->mName}";
809811
810812 if ( isset( $params['default'] ) ) {
811813 $this->mDefault = $params['default'];
@@ -1498,9 +1500,6 @@
14991501 class HTMLHiddenField extends HTMLFormField {
15001502 public function __construct( $params ) {
15011503 parent::__construct( $params );
1502 - # forcing the 'wp' prefix on hidden field names
1503 - # is undesirable
1504 - $this->mName = substr( $this->mName, 2 );
15051504
15061505 # Per HTML5 spec, hidden fields cannot be 'required'
15071506 # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
Index: trunk/phase3/includes/specials/SpecialUploadStash.php
@@ -329,7 +329,13 @@
330330 // create the form, which will also be used to execute a callback to process incoming form data
331331 // this design is extremely dubious, but supposedly HTMLForm is our standard now?
332332
333 - $form = new HTMLForm( array( 'clear' => array( 'class' => 'HTMLHiddenField', 'default' => true ) ), 'clearStashedUploads' );
 333+ $form = new HTMLForm( array(
 334+ 'Clear' => array(
 335+ 'type' => 'hidden',
 336+ 'default' => true,
 337+ 'name' => 'clear',
 338+ )
 339+ ), 'clearStashedUploads' );
334340 $form->setSubmitCallback( array( __CLASS__, 'tryClearStashedUploads' ) );
335341 $form->setTitle( $this->getTitle() );
336342 $form->addHiddenField( 'clear', true, array( 'type' => 'boolean' ) );
Index: trunk/phase3/includes/specials/SpecialUpload.php
@@ -1033,14 +1033,14 @@
10341034 );
10351035 }
10361036
1037 - $descriptor['wpDestFileWarningAck'] = array(
 1037+ $descriptor['DestFileWarningAck'] = array(
10381038 'type' => 'hidden',
10391039 'id' => 'wpDestFileWarningAck',
10401040 'default' => $this->mDestWarningAck ? '1' : '',
10411041 );
10421042
10431043 if ( $this->mForReUpload ) {
1044 - $descriptor['wpForReUpload'] = array(
 1044+ $descriptor['ForReUpload'] = array(
10451045 'type' => 'hidden',
10461046 'id' => 'wpForReUpload',
10471047 'default' => '1',
Index: trunk/phase3/includes/specials/SpecialEmailuser.php
@@ -57,7 +57,6 @@
5858 'id' => 'mw-emailuser-recipient',
5959 ),
6060 'Target' => array(
61 - 'name' => 'wpTarget',
6261 'type' => 'hidden',
6362 'default' => $this->mTargetObj->getName(),
6463 ),
Index: trunk/phase3/includes/Preferences.php
@@ -67,7 +67,7 @@
6868 ## Prod in defaults from the user
6969 foreach ( $defaultPreferences as $name => &$info ) {
7070 $prefFromUser = self::getOptionFromUser( $name, $info, $user );
71 - $field = HTMLForm::loadInputFromParameters( $info ); // For validation
 71+ $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation
7272 $defaultOptions = User::getDefaultOptions();
7373 $globalDefault = isset( $defaultOptions[$name] )
7474 ? $defaultOptions[$name]

Follow-up revisions

RevisionCommit summaryAuthorDate
r78497Follow-up r78452: if you're going to try and declare an HTMLFormField without...happy-melon17:34, 16 December 2010
r78499Follow-up r78452, r78497: having got my copy of phpunit working, fix test bet...happy-melon18:06, 16 December 2010
r79140MFT HTMLForm saga (r78452, r78454, r78566, r78574)platonides23:10, 28 December 2010
r79758Follow-up r78452: missed a couple of hidden fields in SpecialUploadhappy-melon21:17, 6 January 2011
r79914Release notes for r78452.happy-melon20:23, 9 January 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64866Improvements and fixes to HTMLForm:...happy-melon12:03, 10 April 2010
r65040From r64866, more consistent behaviour of hidden field names. May affect r65...happy-melon21:42, 14 April 2010
r67277Follow-up to r64903; fix field names broken by r65040.happy-melon09:43, 3 June 2010

Comments

#Comment by Happy-melon (talk | contribs)   21:27, 15 December 2010

It would be good to get this little saga cleared up within the space of one release, so tagging 1.17.

#Comment by Platonides (talk | contribs)   15:33, 16 December 2010
1) LicensesTest::testLicenses
 Undefined index: fieldname

 phase3/includes/HTMLForm.php:802
 phase3/includes/Licenses.php:33
 phase3/tests/phpunit/includes/LicensesTest.php:11
 phase3/tests/phpunit/phpunit.php:31
#Comment by Happy-melon (talk | contribs)   18:06, 16 December 2010

Fixed in r78497, r78499.

#Comment by Bryan (talk | contribs)   20:07, 9 January 2011

Per our discussion on IRC, this changes the behaviour of hidden fields and thus needs RELEASE-NOTES.

#Comment by Happy-melon (talk | contribs)   20:24, 9 January 2011

done in r79914

#Comment by Ryan lane (talk | contribs)   19:23, 11 February 2011

I don't understand the reasoning for modifying the parameters to prepend wp. It's very unintuitive, and it causes backwards compatibility problems.

Is these some really compelling reason to do this?

#Comment by Happy-melon (talk | contribs)   23:22, 11 February 2011

While I'd largely agree that prepending 'wp' is universally unintuitive, doing so across the board is a B/C solution, not a problem. Given that premise (that we tend to use the 'wp' prefix on field names), this change is to make the implementation more consistent and intuitive, by treating hidden fields in the same way as other fields. This would be relevant if, for instance, a field type was flipped between a visible input and a hidden field based on some other logic; you would not want the 'wp' prefix to appear, disappear or be duplicated depending on what you set the 'type' to.

Status & tagging log