r49923 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49922‎ | r49923 | r49924 >
Date:02:52, 27 April 2009
Author:werdna
Status:ok
Tags:
Comment:
Split preference data out to separate methods so I can actually find my way around the code
Modified paths:
  • /trunk/phase3/includes/Preferences.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Preferences.php
@@ -14,7 +14,72 @@
1515 global $wgLang, $wgRCMaxAge;
1616
1717 $defaultPreferences = array();
 18+
 19+ self::profilePreferences( $user, $defaultPreferences );
 20+ self::emailPreferences( $user, $defaultPreferences );
 21+ self::skinPreferences( $user, $defaultPreferences );
 22+ self::mathPreferences( $user, $defaultPreferences );
 23+ self::filesPreferences( $user, $defaultPreferences );
 24+ self::datetimePreferences( $user, $defaultPreferences );
 25+ self::renderingPreferences( $user, $defaultPreferences );
 26+ self::editingPreferences( $user, $defaultPreferences );
 27+ self::rcPreferences( $user, $defaultPreferences );
 28+ self::watchlistPreferences( $user, $defaultPreferences );
 29+ self::searchPreferences( $user, $defaultPreferences );
 30+ self::miscPreferences( $user, $defaultPreferences );
 31+
 32+ wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
 33+
 34+ ## Prod in defaults from the user
 35+ global $wgDefaultUserOptions;
 36+ foreach( $defaultPreferences as $name => &$info ) {
 37+ $prefFromUser = self::getOptionFromUser( $name, $info, $user );
 38+ $field = HTMLForm::loadInputFromParameters( $info ); // For validation
 39+ $globalDefault = isset($wgDefaultUserOptions[$name])
 40+ ? $wgDefaultUserOptions[$name]
 41+ : null;
 42+
 43+ // If it validates, set it as the default
 44+ if ( isset($info['default']) ) {
 45+ // Already set, no problem
 46+ continue;
 47+ } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
 48+ $field->validate( $prefFromUser, $user->mOptions ) ) {
 49+ $info['default'] = $prefFromUser;
 50+ } elseif( $field->validate( $globalDefault, $user->mOptions ) ) {
 51+ $info['default'] = $globalDefault;
 52+ }
 53+ }
1854
 55+ self::$defaultPreferences = $defaultPreferences;
 56+
 57+ return $defaultPreferences;
 58+ }
 59+
 60+ // Pull option from a user account. Handles stuff like array-type preferences.
 61+ static function getOptionFromUser( $name, $info, $user ) {
 62+ $val = $user->getOption( $name );
 63+
 64+ // Handling for array-type preferences
 65+ if ( ( isset($info['type']) && $info['type'] == 'multiselect' ) ||
 66+ ( isset($info['class']) && $info['class'] == 'HTMLMultiSelectField' ) ) {
 67+
 68+ $options = HTMLFormField::flattenOptions($info['options']);
 69+ $prefix = isset($info['prefix']) ? $info['prefix'] : $name;
 70+ $val = array();
 71+
 72+ foreach( $options as $label => $value ) {
 73+ if ($user->getOption( "$prefix$value" ) ) {
 74+ $val[] = $value;
 75+ }
 76+ }
 77+ }
 78+
 79+ return $val;
 80+ }
 81+
 82+ static function profilePreferences( $user, &$defaultPreferences ) {
 83+ global $wgLang;
1984 ## User info #####################################
2085 // Information panel
2186 $defaultPreferences['username'] =
@@ -216,8 +281,9 @@
217282 'label-message' => 'tog-rememberpassword',
218283 'section' => 'personal',
219284 );
220 -
221 -
 285+ }
 286+
 287+ static function emailPreferences( $user, &$defaultPreferences ) {
222288 ## Email #######################################
223289 ## Email stuff
224290 global $wgEnableEmail, $wgEnableUserEmail, $wgEmailAuthentication;
@@ -235,6 +301,7 @@
236302 $disableEmailPrefs = false;
237303 } else {
238304 $disableEmailPrefs = true;
 305+ global $wgUser; // wgUser is okay here, it's for display
239306 $skin = $wgUser->getSkin();
240307 $emailauthenticated = wfMsg('emailnotauthenticated').'<br />' .
241308 $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Confirmemail' ),
@@ -297,7 +364,9 @@
298365 'label-message' => 'tog-enotifrevealaddr'
299366 );
300367 }
301 -
 368+ }
 369+
 370+ static function skinPreferences( $user, &$defaultPreferences ) {
302371 ## Skin #####################################
303372 global $wgAllowUserSkin;
304373
@@ -323,7 +392,9 @@
324393 'label-message' => 'qbsettings',
325394 );
326395 }
327 -
 396+ }
 397+
 398+ static function mathPreferences( $user, &$defaultPreferences ) {
328399 ## Math #####################################
329400 global $wgUseTeX;
330401 if ($wgUseTeX) {
@@ -336,7 +407,9 @@
337408 'section' => 'math',
338409 );
339410 }
340 -
 411+ }
 412+
 413+ static function filesPreferences( $user, &$defaultPreferences ) {
341414 ## Files #####################################
342415 $defaultPreferences['imagesize'] =
343416 array(
@@ -352,6 +425,10 @@
353426 'label-message' => 'thumbsize',
354427 'section' => 'files',
355428 );
 429+ }
 430+
 431+ static function datetimePreferences( $user, &$defaultPreferences ) {
 432+ global $wgLang;
356433
357434 ## Date and time #####################################
358435 $dateOptions = self::getDateOptions();
@@ -407,7 +484,9 @@
408485 'default' => $tzSetting,
409486 'section' => 'datetime',
410487 );
411 -
 488+ }
 489+
 490+ static function renderingPreferences( $user, &$defaultPreferences ) {
412491 ## Page Rendering ##############################
413492 $defaultPreferences['underline'] =
414493 array(
@@ -476,7 +555,9 @@
477556 'section' => 'rendering',
478557 'label-message' => 'tog-numberheadings',
479558 );
480 -
 559+ }
 560+
 561+ static function editingPreferences( $user, &$defaultPreferences ) {
481562 ## Editing #####################################
482563 $defaultPreferences['cols'] =
483564 array(
@@ -566,7 +647,10 @@
567648 'section' => 'editing',
568649 'label-message' => 'tog-uselivepreview',
569650 );
570 -
 651+ }
 652+
 653+ static function rcPreferences( $user, &$defaultPreferences ) {
 654+ global $wgRCMaxAge, $wgUseRCPatrol;
571655 ## RecentChanges #####################################
572656 $defaultPreferences['rcdays'] =
573657 array(
@@ -620,7 +704,10 @@
621705 'label-message' => 'tog-shownumberswatching',
622706 );
623707 }
624 -
 708+ }
 709+
 710+ static function watchlistPreferences( $user, &$defaultPreferences ) {
 711+ global $wgUseRCPatrol;
625712 ## Watchlist #####################################
626713 $defaultPreferences['wllimit'] =
627714 array(
@@ -702,6 +789,10 @@
703790 );
704791 }
705792 }
 793+ }
 794+
 795+ static function searchPreferences( $user, &$defaultPreferences ) {
 796+ global $wgContLang;
706797
707798 ## Search #####################################
708799 $defaultPreferences['searchlimit'] =
@@ -757,7 +848,9 @@
758849 'section' => 'searchoptions',
759850 );
760851 }
761 -
 852+ }
 853+
 854+ static function miscPreferences( $user, &$defaultPreferences ) {
762855 ## Misc #####################################
763856 $defaultPreferences['diffonly'] =
764857 array(
@@ -771,57 +864,8 @@
772865 'section' => 'misc',
773866 'label-message' => 'tog-norollbackdiff',
774867 );
775 -
776 - wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) );
777 -
778 - ## Prod in defaults from the user
779 - global $wgDefaultUserOptions;
780 - foreach( $defaultPreferences as $name => &$info ) {
781 - $prefFromUser = self::getOptionFromUser( $name, $info, $user );
782 - $field = HTMLForm::loadInputFromParameters( $info ); // For validation
783 - $globalDefault = isset($wgDefaultUserOptions[$name])
784 - ? $wgDefaultUserOptions[$name]
785 - : null;
786 -
787 - // If it validates, set it as the default
788 - if ( isset($info['default']) ) {
789 - // Already set, no problem
790 - continue;
791 - } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
792 - $field->validate( $prefFromUser, $user->mOptions ) ) {
793 - $info['default'] = $prefFromUser;
794 - } elseif( $field->validate( $globalDefault, $user->mOptions ) ) {
795 - $info['default'] = $globalDefault;
796 - }
797 - }
798 -
799 - self::$defaultPreferences = $defaultPreferences;
800 -
801 - return $defaultPreferences;
802868 }
803869
804 - // Pull option from a user account. Handles stuff like array-type preferences.
805 - static function getOptionFromUser( $name, $info, $user ) {
806 - $val = $user->getOption( $name );
807 -
808 - // Handling for array-type preferences
809 - if ( ( isset($info['type']) && $info['type'] == 'multiselect' ) ||
810 - ( isset($info['class']) && $info['class'] == 'HTMLMultiSelectField' ) ) {
811 -
812 - $options = HTMLFormField::flattenOptions($info['options']);
813 - $prefix = isset($info['prefix']) ? $info['prefix'] : $name;
814 - $val = array();
815 -
816 - foreach( $options as $label => $value ) {
817 - if ($user->getOption( "$prefix$value" ) ) {
818 - $val[] = $value;
819 - }
820 - }
821 - }
822 -
823 - return $val;
824 - }
825 -
826870 static function generateSkinOptions( $user ) {
827871 global $wgDefaultSkin;
828872 $ret = array();

Status & tagging log