Merge "(bug 37180) Removed hard coded parentheses in SpecialVersion.php"
[lhc/web/wiklou.git] / includes / Preferences.php
index b82d662..3eee191 100644 (file)
@@ -1,4 +1,25 @@
 <?php
+/**
+ * Form to edit user perferences.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
 /**
  * We're now using the HTMLForm object with some customisation to generate the
  * Preferences form. This object handles generic submission, CSRF protection,
@@ -24,7 +45,6 @@
  * Once fields have been retrieved and validated, submission logic is handed
  * over to the tryUISubmit static method of this class.
  */
-
 class Preferences {
        static $defaultPreferences = null;
        static $saveFilters = array(
@@ -84,9 +104,9 @@ class Preferences {
                                // Already set, no problem
                                continue;
                        } elseif ( !is_null( $prefFromUser ) && // Make sure we're not just pulling nothing
-                                       $field->validate( $prefFromUser, $user->mOptions ) === true ) {
+                                       $field->validate( $prefFromUser, $user->getOptions() ) === true ) {
                                $info['default'] = $prefFromUser;
-                       } elseif ( $field->validate( $globalDefault, $user->mOptions ) === true ) {
+                       } elseif ( $field->validate( $globalDefault, $user->getOptions() ) === true ) {
                                $info['default'] = $globalDefault;
                        } else {
                                throw new MWException( "Global default '$globalDefault' is invalid for field $name" );
@@ -171,7 +191,7 @@ class Preferences {
                asort( $userGroups );
                asort( $userMembers );
 
-               $lang = $context->getLang();
+               $lang = $context->getLanguage();
 
                $defaultPreferences['usergroups'] = array(
                        'type' => 'info',
@@ -193,14 +213,16 @@ class Preferences {
                );
 
                if ( $user->getRegistration() ) {
+                       $displayUser = $context->getUser();
+                       $userRegistration = $user->getRegistration();
                        $defaultPreferences['registrationdate'] = array(
                                'type' => 'info',
                                'label-message' => 'prefs-registration',
                                'default' => $context->msg(
                                        'prefs-registration-date-time',
-                                       $lang->timeanddate( $user->getRegistration(), true ),
-                                       $lang->date( $user->getRegistration(), true ),
-                                       $lang->time( $user->getRegistration(), true )
+                                       $lang->userTimeAndDate( $userRegistration, $displayUser ),
+                                       $lang->userDate( $userRegistration, $displayUser ),
+                                       $lang->userTime( $userRegistration, $displayUser )
                                )->parse(),
                                'section' => 'personal/info',
                        );
@@ -250,7 +272,7 @@ class Preferences {
                }
 
                // Language
-               $languages = Language::getLanguageNames( false );
+               $languages = Language::fetchLanguageNames( null, 'mw' );
                if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
                        $languages[$wgLanguageCode] = $wgLanguageCode;
                }
@@ -349,31 +371,38 @@ class Preferences {
 
                        $emailAddress = $user->getEmail() ? htmlspecialchars( $user->getEmail() ) : '';
                        if ( $wgAuth->allowPropChange( 'emailaddress' ) ) {
-                               $emailAddress .= $emailAddress == '' ? $link : " ($link)";
+                               $emailAddress .= $emailAddress == '' ? $link : ( ' ' . $context->msg( 'parentheses' )->rawParams( $link )->plain() );
                        }
 
+
                        $defaultPreferences['emailaddress'] = array(
                                'type' => 'info',
                                'raw' => true,
                                'default' => $emailAddress,
                                'label-message' => 'youremail',
                                'section' => 'personal/email',
+                               'help-messages' => $helpMessages,
+                               # 'cssclass' chosen below
                        );
 
                        $disableEmailPrefs = false;
 
+                       $emailauthenticationclass = 'mw-email-not-authenticated';
                        if ( $wgEmailAuthentication ) {
                                if ( $user->getEmail() ) {
                                        if ( $user->getEmailAuthenticationTimestamp() ) {
                                                // date and time are separate parameters to facilitate localisation.
                                                // $time is kept for backward compat reasons.
                                                // 'emailauthenticated' is also used in SpecialConfirmemail.php
-                                               $time = $lang->timeAndDate( $user->getEmailAuthenticationTimestamp(), true );
-                                               $d = $lang->date( $user->getEmailAuthenticationTimestamp(), true );
-                                               $t = $lang->time( $user->getEmailAuthenticationTimestamp(), true );
+                                               $displayUser = $context->getUser();
+                                               $emailTimestamp = $user->getEmailAuthenticationTimestamp();
+                                               $time = $lang->userTimeAndDate( $emailTimestamp, $displayUser );
+                                               $d = $lang->userDate( $emailTimestamp, $displayUser );
+                                               $t = $lang->userTime( $emailTimestamp, $displayUser );
                                                $emailauthenticated = $context->msg( 'emailauthenticated',
                                                        $time, $d, $t )->parse() . '<br />';
                                                $disableEmailPrefs = false;
+                                               $emailauthenticationclass = 'mw-email-authenticated';
                                        } else {
                                                $disableEmailPrefs = true;
                                                $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
@@ -381,10 +410,12 @@ class Preferences {
                                                                SpecialPage::getTitleFor( 'Confirmemail' ),
                                                                $context->msg( 'emailconfirmlink' )->escaped()
                                                        ) . '<br />';
+                                               $emailauthenticationclass="mw-email-not-authenticated";
                                        }
                                } else {
                                        $disableEmailPrefs = true;
                                        $emailauthenticated = $context->msg( 'noemailprefs' )->escaped();
+                                       $emailauthenticationclass = 'mw-email-none';
                                }
 
                                $defaultPreferences['emailauthentication'] = array(
@@ -393,9 +424,11 @@ class Preferences {
                                        'section' => 'personal/email',
                                        'label-message' => 'prefs-emailconfirm-label',
                                        'default' => $emailauthenticated,
+                                       # Apply the same CSS class used on the input to the message:
+                                       'cssclass' => $emailauthenticationclass,
                                );
-
                        }
+                       $defaultPreferences['emailaddress']['cssclass'] = $emailauthenticationclass;
 
                        if ( $wgEnableUserEmail && $user->isAllowed( 'sendemail' ) ) {
                                $defaultPreferences['disablemail'] = array(
@@ -485,7 +518,7 @@ class Preferences {
                        $defaultPreferences['commoncssjs'] = array(
                                'type' => 'info',
                                'raw' => true,
-                               'default' => $context->getLang()->pipeList( $linkTools ),
+                               'default' => $context->getLanguage()->pipeList( $linkTools ),
                                'label-message' => 'prefs-common-css-js',
                                'section' => 'rendering/skin',
                        );
@@ -493,7 +526,7 @@ class Preferences {
 
                $selectedSkin = $user->getOption( 'skin' );
                if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
-                       $settings = array_flip( $context->getLang()->getQuickbarSettings() );
+                       $settings = array_flip( $context->getLanguage()->getQuickbarSettings() );
 
                        $defaultPreferences['quickbar'] = array(
                                'type' => 'radio',
@@ -545,7 +578,7 @@ class Preferences {
 
                // Info
                $now = wfTimestampNow();
-               $lang = $context->getLang();
+               $lang = $context->getLanguage();
                $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
                        $lang->time( $now, true ) );
                $nowserver = $lang->time( $now, false ) .
@@ -634,11 +667,6 @@ class Preferences {
                );
 
                if ( $wgAllowUserCssPrefs ) {
-                       $defaultPreferences['highlightbroken'] = array(
-                               'type' => 'toggle',
-                               'section' => 'rendering/advancedrendering',
-                               'label' => $context->msg( 'tog-highlightbroken' )->text(), // Raw HTML
-                       );
                        $defaultPreferences['showtoc'] = array(
                                'type' => 'toggle',
                                'section' => 'rendering/advancedrendering',
@@ -845,15 +873,18 @@ class Preferences {
         * @param $defaultPreferences
         */
        static function watchlistPreferences( $user, IContextSource $context, &$defaultPreferences ) {
-               global $wgUseRCPatrol, $wgEnableAPI;
+               global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
 
+               $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
+               
                ## Watchlist #####################################
                $defaultPreferences['watchlistdays'] = array(
                        'type' => 'float',
                        'min' => 0,
-                       'max' => 7,
+                       'max' => $watchlistdaysMax,
                        'section' => 'watchlist/displaywatchlist',
-                       'help' => $context->msg( 'prefs-watchlist-days-max' )->escaped(),
+                       'help' => $context->msg( 'prefs-watchlist-days-max' )->numParams(
+                                                $watchlistdaysMax )->text(),
                        'label-message' => 'prefs-watchlist-days',
                );
                $defaultPreferences['wllimit'] = array(
@@ -905,6 +936,7 @@ class Preferences {
 
                if ( $wgEnableAPI ) {
                        # Some random gibberish as a proposed default
+                       // @fixme This should use CryptRand but we may not want to read urandom on every view
                        $hash = sha1( mt_rand() . microtime( true ) );
 
                        $defaultPreferences['watchlisttoken'] = array(
@@ -1082,7 +1114,7 @@ class Preferences {
                                $linkTools[] = Linker::link( $jsPage, $context->msg( 'prefs-custom-js' )->escaped() );
                        }
 
-                       $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLang()->pipeList( $linkTools ) )->text();
+                       $display = $sn . ' ' . $context->msg( 'parentheses', $context->getLanguage()->pipeList( $linkTools ) )->text();
                        $ret[$display] = $skinkey;
                }
 
@@ -1094,7 +1126,8 @@ class Preferences {
         * @return array
         */
        static function getDateOptions( IContextSource $context ) {
-               $dateopts = $context->getLang()->getDatePreferences();
+               $lang = $context->getLanguage();
+               $dateopts = $lang->getDatePreferences();
 
                $ret = array();
 
@@ -1115,7 +1148,7 @@ class Preferences {
                                if ( $key == 'default' ) {
                                        $formatted = $context->msg( 'datedefault' )->escaped();
                                } else {
-                                       $formatted = htmlspecialchars( $context->getLang()->timeanddate( $epoch, false, $key ) );
+                                       $formatted = htmlspecialchars( $lang->timeanddate( $epoch, false, $key ) );
                                }
                                $ret[$formatted] = $key;
                        }
@@ -1186,12 +1219,12 @@ class Preferences {
         * @return string
         */
        static function cleanSignature( $signature, $alldata, $form ) {
-               global $wgParser;
                if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
+                       global $wgParser;
                        $signature = $wgParser->cleanSig( $signature );
                } else {
                        // When no fancy sig used, make sure ~{3,5} get removed.
-                       $signature = $wgParser->cleanSigInSig( $signature );
+                       $signature = Parser::cleanSigInSig( $signature );
                }
 
                return $signature;
@@ -1210,6 +1243,10 @@ class Preferences {
                        $removeKeys = array_flip( $remove );
                        $formDescriptor = array_diff_key( $formDescriptor, $removeKeys );
                }
+
+               /**
+                * @var $htmlForm PreferencesForm
+                */
                $htmlForm = new $formClass( $formDescriptor, $context, 'prefs' );
 
                $htmlForm->setModifiedUser( $user );
@@ -1329,7 +1366,7 @@ class Preferences {
 
        /**
         * @param $formData
-        * @param $form HTMLForm
+        * @param $form PreferencesForm
         * @param $entryPoint string
         * @return bool|Status|string
         */
@@ -1388,6 +1425,7 @@ class Preferences {
 
        /**
         * @param $formData
+        * @param $form PreferencesForm
         * @return Status
         */
        public static function tryUISubmit( $formData, $form ) {
@@ -1414,48 +1452,31 @@ class Preferences {
         * Try to set a user's email address.
         * This does *not* try to validate the address.
         * Caller is responsible for checking $wgAuth.
+        *
+        * @deprecated in 1.20; use User::setEmailWithConfirmation() instead.
         * @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
+               wfDeprecated( __METHOD__, '1.20' );
 
-               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 ) );
-                       }
+               $result = $user->setEmailWithConfirmation( $newaddr );
+               if ( $result->isGood() ) {
+                       return array( true, $result->value );
+               } else {
+                       return array( $result, 'mailerror' );
                }
-
-               return array( true, $info );
        }
 
        /**
+        * @deprecated in 1.19; will be removed in 1.20.
         * @param $user User
         * @return array
         */
        public static function loadOldSearchNs( $user ) {
+               wfDeprecated( __METHOD__, '1.19' );
+
                $searchableNamespaces = SearchEngine::searchableNamespaces();
                // Back compat with old format
                $arr = array();
@@ -1474,13 +1495,19 @@ class Preferences {
 class PreferencesForm extends HTMLForm {
        // Override default value from HTMLForm
        protected $mSubSectionBeforeFields = false;
-       
+
        private $modifiedUser;
 
+       /**
+        * @param $user User
+        */
        public function setModifiedUser( $user ) {
                $this->modifiedUser = $user;
        }
 
+       /**
+        * @return User
+        */
        public function getModifiedUser() {
                if ( $this->modifiedUser === null ) {
                        return $this->getUser();
@@ -1547,16 +1574,20 @@ class PreferencesForm extends HTMLForm {
 
                return $data;
        }
+
        /**
         * Get the whole body of the form.
+        * @return string
         */
        function getBody() {
                return $this->displaySection( $this->mFieldTree, '', 'mw-prefsection-' );
        }
-       
+
        /**
         * Get the <legend> for a given section key. Normally this is the
         * prefs-$key message but we'll allow extensions to override it.
+        * @param $key string
+        * @return string
         */
        function getLegend( $key ) {
                $legend = parent::getLegend( $key );