r49748 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49747‎ | r49748 | r49749 >
Date:02:58, 23 April 2009
Author:werdna
Status:deferred
Tags:
Comment:
Improve array handling by removing special-case logic for gadgets and searchnamespaces and adding a generic loader for multi-select field types. Includes a minor interface change -- by default, getOption now returns null, not ''.
Modified paths:
  • /branches/preferences-work/extensions/Gadgets/Gadgets.php (modified) (history)
  • /branches/preferences-work/phase3/includes/Preferences.php (modified) (history)
  • /branches/preferences-work/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: branches/preferences-work/extensions/Gadgets/Gadgets.php
@@ -123,20 +123,14 @@
124124
125125 function wfGadgetsGetPreferences( $user, &$preferences ) {
126126 $gadgets = wfLoadGadgetsStructured();
127 -
128127 if (!$gadgets) return true;
129128
130129 wfLoadExtensionMessages( 'Gadgets' );
131130
132131 $options = array_fill_keys( array_keys($gadgets), array() );
133 -
134 - $selectedGadgets = array();
135 -
136132 foreach( $gadgets as $section => $thisSection ) {
137133 foreach( $thisSection as $gname => $code ) {
138134 $options[$section][wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
139 - if ( $user->getOption( "gadget-$gname") )
140 - $selectedGadgets[] = $gname;
141135 }
142136 }
143137
@@ -158,7 +152,6 @@
159153 'section' => 'gadgets',
160154 'label' => ' ',
161155 'prefix' => 'gadget-',
162 - 'default' => $selectedGadgets,
163156 );
164157
165158 return true;
Index: branches/preferences-work/phase3/includes/Preferences.php
@@ -684,7 +684,6 @@
685685
686686 // Searchable namespaces back-compat with old format
687687 $searchableNamespaces = SearchEngine::searchableNamespaces();
688 - $searchDefault = self::loadOldSearchNs( $user );
689688
690689 $nsOptions = array();
691690 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
@@ -702,7 +701,6 @@
703702 'label-message' => 'defaultns',
704703 'options' => $nsOptions,
705704 'section' => 'searchoptions',
706 - 'default' => $searchDefault,
707705 'prefix' => 'searchNs',
708706 );
709707
@@ -735,7 +733,7 @@
736734 ## Prod in defaults from the user
737735 global $wgDefaultUserOptions;
738736 foreach( $defaultPreferences as $name => &$info ) {
739 - $prefFromUser = $user->getOption( $name );
 737+ $prefFromUser = self::getOptionFromUser( $name, $info, $user );
740738 $field = HTMLForm::loadInputFromParameters( $info ); // For validation
741739 $globalDefault = isset($wgDefaultUserOptions[$name])
742740 ? $wgDefaultUserOptions[$name]
@@ -745,7 +743,7 @@
746744 if ( isset($info['default']) ) {
747745 // Already set, no problem
748746 continue;
749 - } elseif ( isset( $user->mOptions[$name] ) && // Make sure we're not just pulling nothing
 747+ } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
750748 $field->validate( $prefFromUser, $user->mOptions ) ) {
751749 $info['default'] = $prefFromUser;
752750 } elseif( $field->validate( $globalDefault, $user->mOptions ) ) {
@@ -758,6 +756,28 @@
759757 return $defaultPreferences;
760758 }
761759
 760+ // Pull option from a user account. Handles stuff like array-type preferences.
 761+ static function getOptionFromUser( $name, $info, $user ) {
 762+ $val = $user->getOption( $name );
 763+
 764+ // Handling for array-type preferences
 765+ if ( ( isset($info['type']) && $info['type'] == 'multiselect' ) ||
 766+ ( isset($info['class']) && $info['class'] == 'HTMLMultiSelectField' ) ) {
 767+
 768+ $options = HTMLFormField::flattenOptions($info['options']);
 769+ $prefix = isset($info['prefix']) ? $info['prefix'] : $name;
 770+ $val = array();
 771+
 772+ foreach( $options as $label => $value ) {
 773+ if ($user->getOption( "$prefix$value" ) ) {
 774+ $val[] = $value;
 775+ }
 776+ }
 777+ }
 778+
 779+ return $val;
 780+ }
 781+
762782 static function generateSkinOptions( $user ) {
763783 global $wgDefaultSkin;
764784 $ret = array();
Index: branches/preferences-work/phase3/includes/User.php
@@ -1869,7 +1869,7 @@
18701870 * @see getBoolOption()
18711871 * @see getIntOption()
18721872 */
1873 - function getOption( $oname, $defaultOverride = '' ) {
 1873+ function getOption( $oname, $defaultOverride = null ) {
18741874 $this->loadOptions();
18751875
18761876 if ( is_null( $this->mOptions ) ) {

Status & tagging log