Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/86282


Change subject: [WIP] Rip out crappy user preferences code
......................................................................

[WIP] Rip out crappy user preferences code

Hacks upon hacks, replaced with a single smaller hack.

Change-Id: I3140f15586a67e5938e1c6bb658d9025b705e87f
---
M includes/Preferences.php
M includes/WebRequest.php
M tests/phpunit/includes/specials/SpecialPreferencesTest.php
3 files changed, 28 insertions(+), 73 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/82/86282/4

diff --git a/includes/Preferences.php b/includes/Preferences.php
index c9caf4f..9ee8ec4 100644
--- a/includes/Preferences.php
+++ b/includes/Preferences.php
@@ -88,7 +88,7 @@
 
                wfRunHooks( 'GetPreferences', array( $user, 
&$defaultPreferences ) );
 
-               ## Remove preferences that wikis don't want to use
+               // Remove preferences that wikis don't want to use
                global $wgHiddenPrefs;
                foreach ( $wgHiddenPrefs as $pref ) {
                        if ( isset( $defaultPreferences[$pref] ) ) {
@@ -96,86 +96,17 @@
                        }
                }
 
-               ## Make sure that form fields have their parent set. See bug 
41337.
-               $dummyForm = new HTMLForm( array(), $context );
-
+               // Disable options if they can't be changed
                $disable = !$user->isAllowed( 'editmyoptions' );
-
-               ## Prod in defaults from the user
                foreach ( $defaultPreferences as $name => &$info ) {
-                       $prefFromUser = self::getOptionFromUser( $name, $info, 
$user );
                        if ( $disable && !in_array( $name, self::$saveBlacklist 
) ) {
                                $info['disabled'] = 'disabled';
-                       }
-                       $field = HTMLForm::loadInputFromParameters( $name, 
$info ); // For validation
-                       $field->mParent = $dummyForm;
-                       $defaultOptions = User::getDefaultOptions();
-                       $globalDefault = isset( $defaultOptions[$name] )
-                               ? $defaultOptions[$name]
-                               : null;
-
-                       // If it validates, set it as the default
-                       if ( isset( $info['default'] ) ) {
-                               // Already set, no problem
-                               continue;
-                       } elseif ( !is_null( $prefFromUser ) && // Make sure 
we're not just pulling nothing
-                                       $field->validate( $prefFromUser, 
$user->getOptions() ) === true ) {
-                               $info['default'] = $prefFromUser;
-                       } elseif ( $field->validate( $globalDefault, 
$user->getOptions() ) === true ) {
-                               $info['default'] = $globalDefault;
-                       } else {
-                               throw new MWException( "Global default 
'$globalDefault' is invalid for field $name" );
                        }
                }
 
                self::$defaultPreferences = $defaultPreferences;
 
                return $defaultPreferences;
-       }
-
-       /**
-        * Pull option from a user account. Handles stuff like array-type 
preferences.
-        *
-        * @param $name
-        * @param $info
-        * @param $user User
-        * @return array|String
-        */
-       static function getOptionFromUser( $name, $info, $user ) {
-               $val = $user->getOption( $name );
-
-               // Handling for multiselect preferences
-               if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' 
) ||
-                               ( isset( $info['class'] ) && $info['class'] == 
'HTMLMultiSelectField' ) ) {
-                       $options = HTMLFormField::flattenOptions( 
$info['options'] );
-                       $prefix = isset( $info['prefix'] ) ? $info['prefix'] : 
$name;
-                       $val = array();
-
-                       foreach ( $options as $value ) {
-                               if ( $user->getOption( "$prefix$value" ) ) {
-                                       $val[] = $value;
-                               }
-                       }
-               }
-
-               // Handling for checkmatrix preferences
-               if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' 
) ||
-                               ( isset( $info['class'] ) && $info['class'] == 
'HTMLCheckMatrix' ) ) {
-                       $columns = HTMLFormField::flattenOptions( 
$info['columns'] );
-                       $rows = HTMLFormField::flattenOptions( $info['rows'] );
-                       $prefix = isset( $info['prefix'] ) ? $info['prefix'] : 
$name;
-                       $val = array();
-
-                       foreach ( $columns as $column ) {
-                               foreach ( $rows as $row ) {
-                                       if ( $user->getOption( 
"$prefix$column-$row" ) ) {
-                                               $val[] = "$column-$row";
-                                       }
-                               }
-                       }
-               }
-
-               return $val;
        }
 
        /**
@@ -1283,10 +1214,26 @@
                        }
                }
 
+               // The 'wp' is hardcoded in HTMLFormField constructor…
+               // We need to map over the keys and add 'wp' to each
+               $userOptions = $user->getOptions();
+               $params = array_combine(
+                       array_map( function ( $optionName ) {
+                               return 'wp' . $optionName;
+                       }, array_keys( $userOptions ) ),
+                       array_values( $userOptions )
+               );
+
+               $params = $context->getRequest()->getValues() + $params;
+               $req = new DerivativeRequest( $context->getRequest(), $params, 
true );
+               $context->setRequest( $req );
+
                /**
                 * @var $htmlForm PreferencesForm
                 */
                $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' 
);
+               // Load default preferences from the faked request
+               $htmlForm->prepareForm();
 
                $htmlForm->setModifiedUser( $user );
                $htmlForm->setId( 'mw-prefs-form' );
diff --git a/includes/WebRequest.php b/includes/WebRequest.php
index 23eee04..2d167b5 100644
--- a/includes/WebRequest.php
+++ b/includes/WebRequest.php
@@ -1517,4 +1517,8 @@
        public function getIP() {
                return $this->base->getIP();
        }
+
+       public function getRequestURL() {
+               return $this->base->getRequestURL();
+       }
 }
diff --git a/tests/phpunit/includes/specials/SpecialPreferencesTest.php 
b/tests/phpunit/includes/specials/SpecialPreferencesTest.php
index c7a4828..ea648e0 100644
--- a/tests/phpunit/includes/specials/SpecialPreferencesTest.php
+++ b/tests/phpunit/includes/specials/SpecialPreferencesTest.php
@@ -35,8 +35,12 @@
                        ->method( 'getOption' )
                        ->will( $this->returnValueMap( array(
                                array( 'nickname', null, false, 
'superlongnickname' ),
-                       )
-                       ) );
+                       ) ) );
+               $user->expects( $this->any() )
+                       ->method( 'getOptions' )
+                       ->will( $this->returnValueMap( array(
+                               array( array( 'nickname' => 'superlongnickname' 
) ),
+                       ) ) );
 
                # Validate the mock (FIXME should probably be removed)
                $this->assertFalse( $user->isAnon() );

-- 
To view, visit https://gerrit.wikimedia.org/r/86282
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3140f15586a67e5938e1c6bb658d9025b705e87f
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to