From b6008e9086ee790dc3456bece511e95b112d37a0 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Sun, 9 Oct 2011 11:43:06 +0000 Subject: [PATCH] * Use the context when building the preferences form descriptor * Pass the HTMLForm object to the submit callback so that it can have a context and also access to the form itself (see below). This makes Preferences::tryUISubmit() and Prefrences::tryFormSubmit() signatures incompatible. The only extension calling these is EditUser and I will fix in my next commit. * Added two extension possibilites (for EditUser): PreferencesForm::getExtraSuccessRedirectParameters() to modify the URL parameters when redirecting after successful save and the fourthparameter to Prefrences::getFormObject() to be able to remove some items from the descriptor * Pass the user being modified to the HTMLForm to use it to save the preferences instead of doing this unconditionally on $wgUser (to remove one hack in EditUser preferences) --- includes/HTMLForm.php | 2 +- includes/Preferences.php | 364 ++++++++++++++++++++------------------- 2 files changed, 191 insertions(+), 175 deletions(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 63a5d5966e..c30a77b3b1 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -277,7 +277,7 @@ class HTMLForm { $data = $this->filterDataForSubmit( $this->mFieldData ); - $res = call_user_func( $callback, $data ); + $res = call_user_func( $callback, $data, $this ); return $res; } diff --git a/includes/Preferences.php b/includes/Preferences.php index ad8e82caa9..186aa0c619 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -39,25 +39,26 @@ class Preferences { /** * @throws MWException * @param $user User + * @param $context IContextSource * @return array|null */ - static function getPreferences( $user ) { + static function getPreferences( $user, IContextSource $context ) { if ( self::$defaultPreferences ) { return self::$defaultPreferences; } $defaultPreferences = array(); - self::profilePreferences( $user, $defaultPreferences ); - self::skinPreferences( $user, $defaultPreferences ); - self::filesPreferences( $user, $defaultPreferences ); - self::datetimePreferences( $user, $defaultPreferences ); - self::renderingPreferences( $user, $defaultPreferences ); - self::editingPreferences( $user, $defaultPreferences ); - self::rcPreferences( $user, $defaultPreferences ); - self::watchlistPreferences( $user, $defaultPreferences ); - self::searchPreferences( $user, $defaultPreferences ); - self::miscPreferences( $user, $defaultPreferences ); + self::profilePreferences( $user, $context, $defaultPreferences ); + self::skinPreferences( $user, $context, $defaultPreferences ); + self::filesPreferences( $user, $context, $defaultPreferences ); + self::datetimePreferences( $user, $context, $defaultPreferences ); + self::renderingPreferences( $user, $context, $defaultPreferences ); + self::editingPreferences( $user, $context, $defaultPreferences ); + self::rcPreferences( $user, $context, $defaultPreferences ); + self::watchlistPreferences( $user, $context, $defaultPreferences ); + self::searchPreferences( $user, $context, $defaultPreferences ); + self::miscPreferences( $user, $context, $defaultPreferences ); wfRunHooks( 'GetPreferences', array( $user, &$defaultPreferences ) ); @@ -127,11 +128,16 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences * @return void */ - static function profilePreferences( $user, &$defaultPreferences ) { - global $wgLang; + static function profilePreferences( $user, IContextSource $context, &$defaultPreferences ) { + global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode, + $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars, + $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication, + $wgEnotifWatchlist, $wgEnotifUserTalk, $wgEnotifRevealEditorAddress; + ## User info ##################################### // Information panel $defaultPreferences['username'] = array( @@ -165,17 +171,16 @@ class Preferences { asort( $userGroups ); asort( $userMembers ); + $lang = $context->getLang(); + $defaultPreferences['usergroups'] = array( 'type' => 'info', - 'label' => wfMsgExt( - 'prefs-memberingroups', 'parseinline', - $wgLang->formatNum( count( $userGroups ) ) - ), - 'default' => wfMsgExt( - 'prefs-memberingroups-type', array(), - $wgLang->commaList( $userGroups ), - $wgLang->commaList( $userMembers ) - ), + 'label' => $context->msg( 'prefs-memberingroups' )->numParams( + count( $userGroups ) )->parse(), + 'default' => $context->msg( 'prefs-memberingroups-type', + $lang->commaList( $userGroups ), + $lang->commaList( $userMembers ) + )->plain(), 'raw' => true, 'section' => 'personal/info', ); @@ -183,7 +188,7 @@ class Preferences { $defaultPreferences['editcount'] = array( 'type' => 'info', 'label-message' => 'prefs-edits', - 'default' => $wgLang->formatNum( $user->getEditCount() ), + 'default' => $lang->formatNum( $user->getEditCount() ), 'section' => 'personal/info', ); @@ -191,18 +196,17 @@ class Preferences { $defaultPreferences['registrationdate'] = array( 'type' => 'info', 'label-message' => 'prefs-registration', - 'default' => wfMsgExt( - 'prefs-registration-date-time', 'parsemag', - $wgLang->timeanddate( $user->getRegistration(), true ), - $wgLang->date( $user->getRegistration(), true ), - $wgLang->time( $user->getRegistration(), true ) - ), + 'default' => $context->msg( + 'prefs-registration-date-time', + $lang->timeanddate( $user->getRegistration(), true ), + $lang->date( $user->getRegistration(), true ), + $lang->time( $user->getRegistration(), true ) + )->parse(), 'section' => 'personal/info', ); } // Actually changeable stuff - global $wgAuth; $defaultPreferences['realname'] = array( 'type' => $wgAuth->allowPropChange( 'realname' ) ? 'text' : 'info', 'default' => $user->getRealName(), @@ -215,9 +219,9 @@ class Preferences { 'type' => 'select', 'section' => 'personal/info', 'options' => array( - wfMsg( 'gender-male' ) => 'male', - wfMsg( 'gender-female' ) => 'female', - wfMsg( 'gender-unknown' ) => 'unknown', + $context->msg( 'gender-male' )->text() => 'male', + $context->msg( 'gender-female' )->text() => 'female', + $context->msg( 'gender-unknown' )->text() => 'unknown', ), 'label-message' => 'yourgender', 'help-message' => 'prefs-help-gender', @@ -225,7 +229,7 @@ class Preferences { if ( $wgAuth->allowPasswordChange() ) { $link = Linker::link( SpecialPage::getTitleFor( 'ChangePassword' ), - wfMsgHtml( 'prefs-resetpass' ), array(), + $context->msg( 'prefs-resetpass' )->escaped(), array(), array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) ); $defaultPreferences['password'] = array( @@ -236,21 +240,16 @@ class Preferences { 'section' => 'personal/info', ); } - global $wgCookieExpiration; if ( $wgCookieExpiration > 0 ) { $defaultPreferences['rememberpassword'] = array( 'type' => 'toggle', - 'label' => wfMsgExt( - 'tog-rememberpassword', - array( 'parsemag' ), - $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) - ), + 'label' => $context->msg( 'tog-rememberpassword' )->numParams( + ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(), 'section' => 'personal/info', ); } // Language - global $wgLanguageCode; $languages = Language::getLanguageNames( false ); if ( !array_key_exists( $wgLanguageCode, $languages ) ) { $languages[$wgLanguageCode] = $wgLanguageCode; @@ -269,8 +268,6 @@ class Preferences { 'label-message' => 'yourlanguage', ); - global $wgContLang, $wgDisableLangConversion; - global $wgDisableTitleConversion; /* see if there are multiple language variants to choose from*/ $variantArray = array(); if ( !$wgDisableLangConversion ) { @@ -300,18 +297,16 @@ class Preferences { if ( count( $variantArray ) > 1 && !$wgDisableLangConversion && !$wgDisableTitleConversion ) { $defaultPreferences['noconvertlink'] = - array( + array( 'type' => 'toggle', 'section' => 'personal/i18n', 'label-message' => 'tog-noconvertlink', ); } - global $wgMaxSigChars, $wgOut, $wgParser; - // show a preview of the old signature first - $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title, $user, new ParserOptions ); - $oldsigHTML = $wgOut->parseInline( $oldsigWikiText, true, true ); + $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, new ParserOptions ); + $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true ); $defaultPreferences['oldsig'] = array( 'type' => 'info', 'raw' => true, @@ -336,11 +331,7 @@ class Preferences { ## Email stuff - global $wgEnableEmail; if ( $wgEnableEmail ) { - global $wgEmailConfirmToEdit; - global $wgEnableUserEmail; - $helpMessages[] = $wgEmailConfirmToEdit ? 'prefs-help-email-required' : 'prefs-help-email' ; @@ -352,7 +343,7 @@ class Preferences { $link = Linker::link( SpecialPage::getTitleFor( 'ChangeEmail' ), - wfMsgHtml( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' ), + $context->msg( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' )->escaped(), array(), array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) ); @@ -369,8 +360,6 @@ class Preferences { 'section' => 'personal/email', ); - global $wgEmailAuthentication; - $disableEmailPrefs = false; if ( $wgEmailAuthentication ) { @@ -379,25 +368,23 @@ class Preferences { // date and time are separate parameters to facilitate localisation. // $time is kept for backward compat reasons. // 'emailauthenticated' is also used in SpecialConfirmemail.php - $time = $wgLang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true ); - $d = $wgLang->date( $user->getEmailAuthenticationTimestamp(), true ); - $t = $wgLang->time( $user->getEmailAuthenticationTimestamp(), true ); - $emailauthenticated = wfMsgExt( - 'emailauthenticated', 'parseinline', - array( $time, $d, $t ) - ) . '
'; + $time = $lang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true ); + $d = $lang->date( $user->getEmailAuthenticationTimestamp(), true ); + $t = $lang->time( $user->getEmailAuthenticationTimestamp(), true ); + $emailauthenticated = $context->msg( 'emailauthenticated', + $time, $d, $t )->parse() . '
'; $disableEmailPrefs = false; } else { $disableEmailPrefs = true; - $emailauthenticated = wfMsgExt( 'emailnotauthenticated', 'parseinline' ) . '
' . + $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '
' . Linker::linkKnown( SpecialPage::getTitleFor( 'Confirmemail' ), - wfMsg( 'emailconfirmlink' ) + $context->msg( 'emailconfirmlink' )->escaped() ) . '
'; } } else { $disableEmailPrefs = true; - $emailauthenticated = wfMsgHtml( 'noemailprefs' ); + $emailauthenticated = $context->msg( 'noemailprefs' )->escaped(); } $defaultPreferences['emailauthentication'] = array( @@ -426,7 +413,6 @@ class Preferences { ); } - global $wgEnotifWatchlist; if ( $wgEnotifWatchlist ) { $defaultPreferences['enotifwatchlistpages'] = array( 'type' => 'toggle', @@ -435,7 +421,6 @@ class Preferences { 'disabled' => $disableEmailPrefs, ); } - global $wgEnotifUserTalk; if ( $wgEnotifUserTalk ) { $defaultPreferences['enotifusertalkpages'] = array( 'type' => 'toggle', @@ -452,7 +437,6 @@ class Preferences { 'disabled' => $disableEmailPrefs, ); - global $wgEnotifRevealEditorAddress; if ( $wgEnotifRevealEditorAddress ) { $defaultPreferences['enotifrevealaddr'] = array( 'type' => 'toggle', @@ -467,16 +451,17 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences * @return void */ - static function skinPreferences( $user, &$defaultPreferences ) { + static function skinPreferences( $user, IContextSource $context, &$defaultPreferences ) { ## Skin ##################################### - global $wgLang, $wgAllowUserCss, $wgAllowUserJs; + global $wgAllowUserCss, $wgAllowUserJs; $defaultPreferences['skin'] = array( 'type' => 'radio', - 'options' => self::generateSkinOptions( $user ), + 'options' => self::generateSkinOptions( $user, $context ), 'label' => ' ', 'section' => 'rendering/skin', ); @@ -489,18 +474,18 @@ class Preferences { if ( $wgAllowUserCss ) { $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.css' ); - $linkTools[] = Linker::link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) ); + $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() ); } if ( $wgAllowUserJs ) { $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.js' ); - $linkTools[] = Linker::link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) ); + $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() ); } $defaultPreferences['commoncssjs'] = array( 'type' => 'info', 'raw' => true, - 'default' => $wgLang->pipeList( $linkTools ), + 'default' => $context->getLang()->pipeList( $linkTools ), 'label-message' => 'prefs-common-css-js', 'section' => 'rendering/skin', ); @@ -508,7 +493,7 @@ class Preferences { $selectedSkin = $user->getOption( 'skin' ); if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) { - $settings = array_flip( $wgLang->getQuickbarSettings() ); + $settings = array_flip( $context->getLang()->getQuickbarSettings() ); $defaultPreferences['quickbar'] = array( 'type' => 'radio', @@ -521,19 +506,20 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function filesPreferences( $user, &$defaultPreferences ) { + static function filesPreferences( $user, IContextSource $context, &$defaultPreferences ) { ## Files ##################################### $defaultPreferences['imagesize'] = array( 'type' => 'select', - 'options' => self::getImageSizes(), + 'options' => self::getImageSizes( $context ), 'label-message' => 'imagemaxsize', 'section' => 'rendering/files', ); $defaultPreferences['thumbsize'] = array( 'type' => 'select', - 'options' => self::getThumbSizes(), + 'options' => self::getThumbSizes( $context ), 'label-message' => 'thumbsize', 'section' => 'rendering/files', ); @@ -541,14 +527,13 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences * @return void */ - static function datetimePreferences( $user, &$defaultPreferences ) { - global $wgLang; - + static function datetimePreferences( $user, IContextSource $context, &$defaultPreferences ) { ## Date and time ##################################### - $dateOptions = self::getDateOptions(); + $dateOptions = self::getDateOptions( $context ); if ( $dateOptions ) { $defaultPreferences['date'] = array( 'type' => 'radio', @@ -560,9 +545,10 @@ class Preferences { // Info $now = wfTimestampNow(); + $lang = $context->getLang(); $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ), - $wgLang->time( $now, true ) ); - $nowserver = $wgLang->time( $now, false ) . + $lang->time( $now, true ) ); + $nowserver = $lang->time( $now, false ) . Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) ); $defaultPreferences['nowserver'] = array( @@ -594,7 +580,7 @@ class Preferences { $defaultPreferences['timecorrection'] = array( 'class' => 'HTMLSelectOrOtherField', 'label-message' => 'timezonelegend', - 'options' => self::getTimezoneOptions(), + 'options' => self::getTimezoneOptions( $context ), 'default' => $tzSetting, 'size' => 20, 'section' => 'datetime/timeoffset', @@ -603,18 +589,19 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function renderingPreferences( $user, &$defaultPreferences ) { + static function renderingPreferences( $user, IContextSource $context, &$defaultPreferences ) { ## Page Rendering ############################## global $wgAllowUserCssPrefs; if ( $wgAllowUserCssPrefs ) { $defaultPreferences['underline'] = array( 'type' => 'select', 'options' => array( - wfMsg( 'underline-never' ) => 0, - wfMsg( 'underline-always' ) => 1, - wfMsg( 'underline-default' ) => 2, + $context->msg( 'underline-never' )->text() => 0, + $context->msg( 'underline-always' )->text() => 1, + $context->msg( 'underline-default' )->text() => 2, ), 'label-message' => 'tog-underline', 'section' => 'rendering/advancedrendering', @@ -622,9 +609,9 @@ class Preferences { } $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 ); - $stubThresholdOptions = array( wfMsg( 'stub-threshold-disabled' ) => 0 ); + $stubThresholdOptions = array( $context->msg( 'stub-threshold-disabled' )->text() => 0 ); foreach ( $stubThresholdValues as $value ) { - $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value; + $stubThresholdOptions[$context->msg( 'size-bytes', $value )->text()] = $value; } $defaultPreferences['stubthreshold'] = array( @@ -632,14 +619,14 @@ class Preferences { 'section' => 'rendering/advancedrendering', 'options' => $stubThresholdOptions, 'size' => 20, - 'label' => wfMsg( 'stub-threshold' ), // Raw HTML message. Yay? + 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay? ); if ( $wgAllowUserCssPrefs ) { $defaultPreferences['highlightbroken'] = array( 'type' => 'toggle', 'section' => 'rendering/advancedrendering', - 'label' => wfMsg( 'tog-highlightbroken' ), // Raw HTML + 'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML ); $defaultPreferences['showtoc'] = array( 'type' => 'toggle', @@ -680,9 +667,10 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function editingPreferences( $user, &$defaultPreferences ) { + static function editingPreferences( $user, IContextSource $context, &$defaultPreferences ) { global $wgUseExternalEditor, $wgAllowUserCssPrefs; ## Editing ##################################### @@ -707,10 +695,10 @@ class Preferences { 'section' => 'editing/advancedediting', 'label-message' => 'editfont-style', 'options' => array( - wfMsg( 'editfont-default' ) => 'default', - wfMsg( 'editfont-monospace' ) => 'monospace', - wfMsg( 'editfont-sansserif' ) => 'sans-serif', - wfMsg( 'editfont-serif' ) => 'serif', + $context->msg( 'editfont-default' )->text() => 'default', + $context->msg( 'editfont-monospace' )->text() => 'monospace', + $context->msg( 'editfont-sansserif' )->text() => 'sans-serif', + $context->msg( 'editfont-serif' )->text() => 'serif', ) ); } @@ -785,10 +773,11 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function rcPreferences( $user, &$defaultPreferences ) { - global $wgRCMaxAge, $wgLang; + static function rcPreferences( $user, IContextSource $context, &$defaultPreferences ) { + global $wgRCMaxAge, $wgRCShowWatchingUsers; ## RecentChanges ##################################### $defaultPreferences['rcdays'] = array( @@ -797,11 +786,8 @@ class Preferences { 'section' => 'rc/displayrc', 'min' => 1, 'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ), - 'help' => wfMsgExt( - 'recentchangesdays-max', - array( 'parsemag' ), - $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) - ) + 'help' => $context->msg( 'recentchangesdays-max' )->numParams( + ceil( $wgRCMaxAge / ( 3600 * 24 ) ) )->text() ); $defaultPreferences['rclimit'] = array( 'type' => 'int', @@ -833,7 +819,6 @@ class Preferences { ); } - global $wgRCShowWatchingUsers; if ( $wgRCShowWatchingUsers ) { $defaultPreferences['shownumberswatching'] = array( 'type' => 'toggle', @@ -845,9 +830,10 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences */ - static function watchlistPreferences( $user, &$defaultPreferences ) { + static function watchlistPreferences( $user, IContextSource $context, &$defaultPreferences ) { global $wgUseRCPatrol, $wgEnableAPI; ## Watchlist ##################################### @@ -856,7 +842,7 @@ class Preferences { 'min' => 0, 'max' => 7, 'section' => 'watchlist/displaywatchlist', - 'help' => wfMsgHtml( 'prefs-watchlist-days-max' ), + 'help' => $context->msg( 'prefs-watchlist-days-max' )->escaped(), 'label-message' => 'prefs-watchlist-days', ); $defaultPreferences['wllimit'] = array( @@ -864,7 +850,7 @@ class Preferences { 'min' => 0, 'max' => 1000, 'label-message' => 'prefs-watchlist-edits', - 'help' => wfMsgHtml( 'prefs-watchlist-edits-max' ), + 'help' => $context->msg( 'prefs-watchlist-edits-max' )->escaped(), 'section' => 'watchlist/displaywatchlist', ); $defaultPreferences['extendwatchlist'] = array( @@ -914,7 +900,7 @@ class Preferences { 'type' => 'text', 'section' => 'watchlist/advancedwatchlist', 'label-message' => 'prefs-watchlist-token', - 'help' => wfMsgHtml( 'prefs-help-watchlist-token', $hash ) + 'help' => $context->msg( 'prefs-help-watchlist-token', $hash )->escaped() ); } @@ -942,10 +928,11 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function searchPreferences( $user, &$defaultPreferences ) { - global $wgContLang; + static function searchPreferences( $user, IContextSource $context, &$defaultPreferences ) { + global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch; ## Search ##################################### $defaultPreferences['searchlimit'] = array( @@ -955,7 +942,6 @@ class Preferences { 'min' => 0, ); - global $wgEnableMWSuggest; if ( $wgEnableMWSuggest ) { $defaultPreferences['disablesuggest'] = array( 'type' => 'toggle', @@ -964,7 +950,6 @@ class Preferences { ); } - global $wgVectorUseSimpleSearch; if ( $wgVectorUseSimpleSearch ) { $defaultPreferences['vector-simplesearch'] = array( 'type' => 'toggle', @@ -989,7 +974,7 @@ class Preferences { $displayNs = str_replace( '_', ' ', $name ); if ( !$displayNs ) { - $displayNs = wfMsg( 'blanknamespace' ); + $displayNs = $context->msg( 'blanknamespace' )->text(); } $displayNs = htmlspecialchars( $displayNs ); @@ -1007,9 +992,12 @@ class Preferences { /** * @param $user User + * @param $context IContextSource * @param $defaultPreferences Array */ - static function miscPreferences( $user, &$defaultPreferences ) { + static function miscPreferences( $user, IContextSource $context, &$defaultPreferences ) { + global $wgContLang; + ## Misc ##################################### $defaultPreferences['diffonly'] = array( 'type' => 'toggle', @@ -1023,8 +1011,6 @@ class Preferences { ); // Stuff from Language::getExtraUserToggles() - global $wgContLang; - $toggles = $wgContLang->getExtraUserToggles(); foreach ( $toggles as $toggle ) { @@ -1038,14 +1024,15 @@ class Preferences { /** * @param $user User The User object + * @param $context IContextSource * @return Array: text/links to display as key; $skinkey as value */ - static function generateSkinOptions( $user ) { - global $wgDefaultSkin, $wgLang, $wgAllowUserCss, $wgAllowUserJs; + static function generateSkinOptions( $user, IContextSource $context ) { + global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs; $ret = array(); $mptitle = Title::newMainPage(); - $previewtext = wfMsgHtml( 'skin-preview' ); + $previewtext = $context->msg( 'skin-preview' )->text(); # Only show members of Skin::getSkinNames() rather than # $skinNames (skins is all skin names from Language.php) @@ -1054,7 +1041,7 @@ class Preferences { # Sort by UI skin name. First though need to update validSkinNames as sometimes # the skinkey & UI skinname differ (e.g. "standard" skinkey is "Classic" in the UI). foreach ( $validSkinNames as $skinkey => &$skinname ) { - $msg = wfMessage( "skinname-{$skinkey}" ); + $msg = $context->msg( "skinname-{$skinkey}" ); if ( $msg->exists() ) { $skinname = htmlspecialchars( $msg->text() ); } @@ -1066,7 +1053,7 @@ class Preferences { # Mark the default skin if ( $skinkey == $wgDefaultSkin ) { - $linkTools[] = wfMsgHtml( 'default' ); + $linkTools[] = $context->msg( 'default' )->escaped(); } # Create preview link @@ -1076,15 +1063,15 @@ class Preferences { # Create links to user CSS/JS pages if ( $wgAllowUserCss ) { $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' ); - $linkTools[] = Linker::link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) ); + $linkTools[] = Linker::link( $cssPage, $context->msg( 'prefs-custom-css' )->escaped() ); } if ( $wgAllowUserJs ) { $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' ); - $linkTools[] = Linker::link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) ); + $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() ); } - $display = $sn . ' ' . wfMsg( 'parentheses', $wgLang->pipeList( $linkTools ) ); + $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLang()->pipeList( $linkTools ) )->text(); $ret[$display] = $skinkey; } @@ -1092,11 +1079,11 @@ class Preferences { } /** + * @param $context IContextSource * @return array */ - static function getDateOptions() { - global $wgLang; - $dateopts = $wgLang->getDatePreferences(); + static function getDateOptions( IContextSource $context ) { + $dateopts = $context->getLang()->getDatePreferences(); $ret = array(); @@ -1115,9 +1102,9 @@ class Preferences { $epoch = wfTimestampNow(); foreach ( $dateopts as $key ) { if ( $key == 'default' ) { - $formatted = wfMsgHtml( 'datedefault' ); + $formatted = $context->msg( 'datedefault' )->escaped(); } else { - $formatted = htmlspecialchars( $wgLang->timeanddate( $epoch, false, $key ) ); + $formatted = htmlspecialchars( $context->getLang()->timeanddate( $epoch, false, $key ) ); } $ret[$formatted] = $key; } @@ -1126,15 +1113,17 @@ class Preferences { } /** + * @param $context IContextSource * @return array */ - static function getImageSizes() { + static function getImageSizes( IContextSource $context ) { global $wgImageLimits; $ret = array(); + $pixels = $context->msg( 'unit-pixel' )->text(); foreach ( $wgImageLimits as $index => $limits ) { - $display = "{$limits[0]}×{$limits[1]}" . wfMsg( 'unit-pixel' ); + $display = "{$limits[0]}×{$limits[1]}" . $pixels; $ret[$display] = $index; } @@ -1142,15 +1131,17 @@ class Preferences { } /** + * @param $context IContextSource * @return array */ - static function getThumbSizes() { + static function getThumbSizes( IContextSource $context ) { global $wgThumbLimits; $ret = array(); + $pixels = $context->msg( 'unit-pixel' )->text(); foreach ( $wgThumbLimits as $index => $size ) { - $display = $size . wfMsg( 'unit-pixel' ); + $display = $size . $pixels; $ret[$display] = $index; } @@ -1163,13 +1154,10 @@ class Preferences { * @return bool|string */ static function validateSignature( $signature, $alldata ) { - global $wgParser, $wgMaxSigChars, $wgLang; + global $wgParser, $wgMaxSigChars; if ( mb_strlen( $signature ) > $wgMaxSigChars ) { return Xml::element( 'span', array( 'class' => 'error' ), - wfMsgExt( 'badsiglength', 'parsemag', - $wgLang->formatNum( $wgMaxSigChars ) - ) - ); + wfMessage( 'badsiglength' )->numParams( $wgMaxSigChars )->text() ); } elseif ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] && false === $wgParser->validateSig( $signature ) ) { @@ -1217,17 +1205,22 @@ class Preferences { * @param $user User * @param $context IContextSource * @param $formClass string + * @param $remove Array: array of items to remove * @return HtmlForm */ - static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm' ) { - $formDescriptor = Preferences::getPreferences( $user ); + static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm', array $remove = array() ) { + $formDescriptor = Preferences::getPreferences( $user, $context ); + if ( count( $remove ) ) { + $removeKeys = array_flip( $remove ); + $formDescriptor = array_diff_key( $formDescriptor, $removeKeys ); + } $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' ); + $htmlForm->setModifiedUser( $user ); $htmlForm->setId( 'mw-prefs-form' ); $htmlForm->setSubmitText( wfMsg( 'saveprefs' ) ); # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save' $htmlForm->setSubmitTooltip( 'preferences-save' ); - $htmlForm->setTitle( SpecialPage::getTitleFor( 'Preferences' ) ); $htmlForm->setSubmitID( 'prefsubmit' ); $htmlForm->setSubmitCallback( array( 'Preferences', 'tryFormSubmit' ) ); @@ -1237,20 +1230,20 @@ class Preferences { /** * @return array */ - static function getTimezoneOptions() { + static function getTimezoneOptions( IContextSource $context ) { $opt = array(); global $wgLocalTZoffset, $wgLocaltimezone; // Check that $wgLocalTZoffset is the same as $wgLocaltimezone if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) { - $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $wgLocaltimezone ); + $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $wgLocaltimezone )->text(); } else { $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 ); - $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $tzstring ); + $server_tz_msg = $context->msg( 'timezoneuseserverdefault', $tzstring )->text(); } $opt[$server_tz_msg] = "System|$wgLocalTZoffset"; - $opt[wfMsg( 'timezoneuseoffset' )] = 'other'; - $opt[wfMsg( 'guesstimezone' )] = 'guess'; + $opt[$context->msg( 'timezoneuseoffset' )->text()] = 'other'; + $opt[$context->msg( 'guesstimezone' )->text()] = 'guess'; if ( function_exists( 'timezone_identifiers_list' ) ) { # Read timezone list @@ -1258,16 +1251,16 @@ class Preferences { sort( $tzs ); $tzRegions = array(); - $tzRegions['Africa'] = wfMsg( 'timezoneregion-africa' ); - $tzRegions['America'] = wfMsg( 'timezoneregion-america' ); - $tzRegions['Antarctica'] = wfMsg( 'timezoneregion-antarctica' ); - $tzRegions['Arctic'] = wfMsg( 'timezoneregion-arctic' ); - $tzRegions['Asia'] = wfMsg( 'timezoneregion-asia' ); - $tzRegions['Atlantic'] = wfMsg( 'timezoneregion-atlantic' ); - $tzRegions['Australia'] = wfMsg( 'timezoneregion-australia' ); - $tzRegions['Europe'] = wfMsg( 'timezoneregion-europe' ); - $tzRegions['Indian'] = wfMsg( 'timezoneregion-indian' ); - $tzRegions['Pacific'] = wfMsg( 'timezoneregion-pacific' ); + $tzRegions['Africa'] = $context->msg( 'timezoneregion-africa' )->text(); + $tzRegions['America'] = $context->msg( 'timezoneregion-america' )->text(); + $tzRegions['Antarctica'] = $context->msg( 'timezoneregion-antarctica' )->text(); + $tzRegions['Arctic'] = $context->msg( 'timezoneregion-arctic' )->text(); + $tzRegions['Asia'] = $context->msg( 'timezoneregion-asia' )->text(); + $tzRegions['Atlantic'] = $context->msg( 'timezoneregion-atlantic' )->text(); + $tzRegions['Australia'] = $context->msg( 'timezoneregion-australia' )->text(); + $tzRegions['Europe'] = $context->msg( 'timezoneregion-europe' )->text(); + $tzRegions['Indian'] = $context->msg( 'timezoneregion-indian' )->text(); + $tzRegions['Pacific'] = $context->msg( 'timezoneregion-pacific' )->text(); asort( $tzRegions ); $prefill = array_fill_keys( array_values( $tzRegions ), array() ); @@ -1340,12 +1333,14 @@ class Preferences { /** * @param $formData + * @param $form HTMLForm * @param $entryPoint string * @return bool|Status|string */ - static function tryFormSubmit( $formData, $entryPoint = 'internal' ) { - global $wgUser; + static function tryFormSubmit( $formData, $form, $entryPoint = 'internal' ) { + global $wgHiddenPrefs; + $user = $form->getModifiedUser(); $result = true; // Filter input @@ -1363,10 +1358,9 @@ class Preferences { ); // Fortunately, the realname field is MUCH simpler - global $wgHiddenPrefs; if ( !in_array( 'realname', $wgHiddenPrefs ) ) { $realName = $formData['realname']; - $wgUser->setRealName( $realName ); + $user->setRealName( $realName ); } foreach ( $saveBlacklist as $b ) { @@ -1380,18 +1374,18 @@ class Preferences { foreach( $wgHiddenPrefs as $pref ){ # If the user has not set a non-default value here, the default will be returned # and subsequently discarded - $formData[$pref] = $wgUser->getOption( $pref, null, true ); + $formData[$pref] = $user->getOption( $pref, null, true ); } // Keeps old preferences from interfering due to back-compat // code, etc. - $wgUser->resetOptions(); + $user->resetOptions(); foreach ( $formData as $key => $value ) { - $wgUser->setOption( $key, $value ); + $user->setOption( $key, $value ); } - $wgUser->saveSettings(); + $user->saveSettings(); return $result; } @@ -1400,22 +1394,21 @@ class Preferences { * @param $formData * @return Status */ - public static function tryUISubmit( $formData ) { - $res = self::tryFormSubmit( $formData, 'ui' ); + public static function tryUISubmit( $formData, $form ) { + $res = self::tryFormSubmit( $formData, $form, 'ui' ); if ( $res ) { - $urlOptions = array( 'success' ); + $urlOptions = array( 'success' => 1 ); if ( $res === 'eauth' ) { - $urlOptions[] = 'eauth'; + $urlOptions['eauth'] = 1; } - $queryString = implode( '&', $urlOptions ); + $urlOptions += $form->getExtraSuccessRedirectParameters(); - $url = SpecialPage::getTitleFor( 'Preferences' )->getFullURL( $queryString ); + $url = $form->getTitle()->getFullURL( $urlOptions ); - global $wgOut; - $wgOut->redirect( $url ); + $form->getContext()->getOutput()->redirect( $url ); } return Status::newGood(); @@ -1483,6 +1476,29 @@ class Preferences { /** Some tweaks to allow js prefs to work */ class PreferencesForm extends HTMLForm { + private $modifiedUser; + + public function setModifiedUser( $user ) { + $this->modifiedUser = $user; + } + + public function getModifiedUser() { + if ( $this->modifiedUser === null ) { + return $this->getUser(); + } else { + return $this->modifiedUser; + } + } + + /** + * Get extra parameters for the query string when redirecting after + * successful save. + * + * @return array() + */ + public function getExtraSuccessRedirectParameters() { + return array(); + } /** * @param $html string -- 2.20.1