Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / Preferences.php
index 5df8b54..a955b6f 100644 (file)
@@ -131,7 +131,7 @@ class Preferences {
         * @return void
         */
        static function profilePreferences( $user, &$defaultPreferences ) {
-               global $wgLang, $wgUser;
+               global $wgLang;
                ## User info #####################################
                // Information panel
                $defaultPreferences['username'] = array(
@@ -224,7 +224,7 @@ class Preferences {
                );
 
                if ( $wgAuth->allowPasswordChange() ) {
-                       $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'ChangePassword' ),
+                       $link = Linker::link( SpecialPage::getTitleFor( 'ChangePassword' ),
                                wfMsgHtml( 'prefs-resetpass' ), array(),
                                array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
 
@@ -276,13 +276,9 @@ class Preferences {
                if ( !$wgDisableLangConversion ) {
                        $variants = $wgContLang->getVariants();
 
-                       $languages = Language::getLanguageNames( true );
                        foreach ( $variants as $v ) {
                                $v = str_replace( '_', '-', strtolower( $v ) );
-                               if ( array_key_exists( $v, $languages ) ) {
-                                       // If it doesn't have a name, we'll pretend it doesn't exist
-                                       $variantArray[$v] = $languages[$v];
-                               }
+                               $variantArray[$v] = $wgContLang->getVariantname( $v, false );
                        }
 
                        $options = array();
@@ -297,6 +293,7 @@ class Preferences {
                                        'type' => 'select',
                                        'options' => $options,
                                        'section' => 'personal/i18n',
+                                       'help-message' => 'prefs-help-variant',
                                );
                        }
                }
@@ -314,7 +311,7 @@ class Preferences {
 
                // show a preview of the old signature first
                $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title, $user, new ParserOptions );
-               $oldsigHTML = $wgOut->parseInline( $oldsigWikiText );
+               $oldsigHTML = $wgOut->parseInline( $oldsigWikiText, true, true );
                $defaultPreferences['oldsig'] = array(
                        'type' => 'info',
                        'raw' => true,
@@ -353,13 +350,20 @@ class Preferences {
                                $helpMessages[] = 'prefs-help-email-others';
                        }
 
+                       $link = Linker::link(
+                               SpecialPage::getTitleFor( 'ChangeEmail' ),
+                               wfMsgHtml( $user->getEmail() ? 'prefs-changeemail' : 'prefs-setemail' ),
+                               array(),
+                               array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
+
                        $defaultPreferences['emailaddress'] = array(
-                               'type' => $wgAuth->allowPropChange( 'emailaddress' ) ? 'email' : 'info',
-                               'default' => $user->getEmail(),
-                               'section' => 'personal/email',
+                               'type' => 'info',
+                               'raw' => true,
+                               'default' => $user->getEmail()
+                                       ? htmlspecialchars( $user->getEmail() ) . " ($link)"
+                                       : $link,
                                'label-message' => 'youremail',
-                               'help-messages' => $helpMessages,
-                               'validation-callback' => array( 'Preferences', 'validateEmail' ),
+                               'section' => 'personal/email',
                        );
 
                        global $wgEmailAuthentication;
@@ -382,14 +386,10 @@ class Preferences {
                                                $disableEmailPrefs = false;
                                        } else {
                                                $disableEmailPrefs = true;
-                                               $skin = $wgUser->getSkin();
                                                $emailauthenticated = wfMsgExt( 'emailnotauthenticated', 'parseinline' ) . '<br />' .
-                                                       $skin->link(
+                                                       Linker::linkKnown(
                                                                SpecialPage::getTitleFor( 'Confirmemail' ),
-                                                               wfMsg( 'emailconfirmlink' ),
-                                                               array(),
-                                                               array(),
-                                                               array( 'known', 'noclasses' )
+                                                               wfMsg( 'emailconfirmlink' )
                                                        ) . '<br />';
                                        }
                                } else {
@@ -482,17 +482,16 @@ class Preferences {
                # This code is basically copied from generateSkinOptions().  It'd
                # be nice to somehow merge this back in there to avoid redundancy.
                if ( $wgAllowUserCss || $wgAllowUserJs ) {
-                       $sk = $user->getSkin();
                        $linkTools = array();
 
                        if ( $wgAllowUserCss ) {
                                $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.css' );
-                               $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
+                               $linkTools[] = Linker::link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
                        }
 
                        if ( $wgAllowUserJs ) {
                                $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/common.js' );
-                               $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
+                               $linkTools[] = Linker::link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
                        }
 
                        $defaultPreferences['commoncssjs'] = array(
@@ -786,7 +785,7 @@ class Preferences {
         * @param $defaultPreferences Array
         */
        static function rcPreferences( $user, &$defaultPreferences ) {
-               global $wgRCMaxAge, $wgUseRCPatrol, $wgLang;
+               global $wgRCMaxAge, $wgLang;
 
                ## RecentChanges #####################################
                $defaultPreferences['rcdays'] = array(
@@ -1058,7 +1057,6 @@ class Preferences {
                        }
                }
                asort( $validSkinNames );
-               $sk = $user->getSkin();
 
                foreach ( $validSkinNames as $skinkey => $sn ) {
                        $linkTools = array();
@@ -1075,12 +1073,12 @@ class Preferences {
                        # Create links to user CSS/JS pages
                        if ( $wgAllowUserCss ) {
                                $cssPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.css' );
-                               $linkTools[] = $sk->link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
+                               $linkTools[] = Linker::link( $cssPage, wfMsgHtml( 'prefs-custom-css' ) );
                        }
 
                        if ( $wgAllowUserJs ) {
                                $jsPage = Title::makeTitleSafe( NS_USER, $user->getName() . '/' . $skinkey . '.js' );
-                               $linkTools[] = $sk->link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
+                               $linkTools[] = Linker::link( $jsPage, wfMsgHtml( 'prefs-custom-js' ) );
                        }
 
                        $display = $sn . ' ' . wfMsg( 'parentheses', $wgLang->pipeList( $linkTools ) );
@@ -1214,12 +1212,13 @@ class Preferences {
 
        /**
         * @param $user User
+        * @param $context IContextSource
         * @param $formClass string
         * @return HtmlForm
         */
-       static function getFormObject( $user, $formClass = 'PreferencesForm' ) {
+       static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm' ) {
                $formDescriptor = Preferences::getPreferences( $user );
-               $htmlForm = new $formClass( $formDescriptor, 'prefs' );
+               $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
 
                $htmlForm->setId( 'mw-prefs-form' );
                $htmlForm->setSubmitText( wfMsg( 'saveprefs' ) );
@@ -1243,7 +1242,7 @@ class Preferences {
                if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
                        $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $wgLocaltimezone );
                } else {
-                       $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
+                       $tzstring = sprintf( '%+03d:%02d', floor( $wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
                        $server_tz_msg = wfMsg( 'timezoneuseserverdefault', $tzstring );
                }
                $opt[$server_tz_msg] = "System|$wgLocalTZoffset";
@@ -1342,7 +1341,7 @@ class Preferences {
         * @return bool|Status|string
         */
        static function tryFormSubmit( $formData, $entryPoint = 'internal' ) {
-               global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
+               global $wgUser;
 
                $result = true;
 
@@ -1360,34 +1359,6 @@ class Preferences {
                        'emailaddress',
                );
 
-               if ( $wgEnableEmail ) {
-                       $newaddr = $formData['emailaddress'];
-                       $oldaddr = $wgUser->getEmail();
-                       if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
-                               # the user has supplied a new email address on the login page
-                               # new behaviour: set this new emailaddr from login-page into user database record
-                               $wgUser->setEmail( $newaddr );
-                               # but flag as "dirty" = unauthenticated
-                               $wgUser->invalidateEmail();
-                               if ( $wgEmailAuthentication ) {
-                                       # Mail a temporary password to the dirty address.
-                                       # User can come back through the confirmation URL to re-enable email.
-                                       $type = $oldaddr != '' ? 'changed' : 'set';
-                                       $result = $wgUser->sendConfirmationMail( $type );
-                                       if ( !$result->isGood() ) {
-                                               return htmlspecialchars( $result->getWikiText( 'mailerror' ) );
-                                       } elseif ( $entryPoint == 'ui' ) {
-                                               $result = 'eauth';
-                                       }
-                               }
-                       } else {
-                               $wgUser->setEmail( $newaddr );
-                       }
-                       if ( $oldaddr != $newaddr ) {
-                               wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldaddr, $newaddr ) );
-                       }
-               }
-
                // Fortunately, the realname field is MUCH simpler
                global $wgHiddenPrefs;
                if ( !in_array( 'realname', $wgHiddenPrefs ) ) {
@@ -1447,6 +1418,46 @@ class Preferences {
                return Status::newGood();
        }
 
+       /*
+        * Try to set a user's email address.
+        * This does *not* try to validate the address.
+        * @param $user User
+        * @param $newaddr string New email address
+        * @return Array (true on success or Status on failure, info string)
+        */
+       public static function trySetUserEmail( User $user, $newaddr ) {
+               global $wgEnableEmail, $wgEmailAuthentication;
+               $info = ''; // none
+
+               if ( $wgEnableEmail ) {
+                       $oldaddr = $user->getEmail();
+                       if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
+                               # The user has supplied a new email address on the login page
+                               # new behaviour: set this new emailaddr from login-page into user database record
+                               $user->setEmail( $newaddr );
+                               # But flag as "dirty" = unauthenticated
+                               $user->invalidateEmail();
+                               if ( $wgEmailAuthentication ) {
+                                       # Mail a temporary password to the dirty address.
+                                       # User can come back through the confirmation URL to re-enable email.
+                                       $type = $oldaddr != '' ? 'changed' : 'set';
+                                       $result = $user->sendConfirmationMail( $type );
+                                       if ( !$result->isGood() ) {
+                                               return array( $result, 'mailerror' );
+                                       }
+                                       $info = 'eauth';
+                               }
+                       } else {
+                               $user->setEmail( $newaddr );
+                       }
+                       if ( $oldaddr != $newaddr ) {
+                               wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
+                       }
+               }
+
+               return array( true, $info );
+       }
+
        /**
         * @param $user User
         * @return array
@@ -1485,12 +1496,9 @@ class PreferencesForm extends HTMLForm {
        function getButtons() {
                $html = parent::getButtons();
 
-               global $wgUser;
-
-               $sk = $wgUser->getSkin();
                $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
 
-               $html .= "\n" . $sk->link( $t, wfMsgHtml( 'restoreprefs' ) );
+               $html .= "\n" . Linker::link( $t, wfMsgHtml( 'restoreprefs' ) );
 
                $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html );
 
@@ -1520,4 +1528,10 @@ class PreferencesForm extends HTMLForm {
 
                return $data;
        }
+       /**
+        * Get the whole body of the form.
+        */
+       function getBody() {
+               return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
+       }
 }