Revert to arbitrarily old point before initial remote branch creation to help clean up
[lhc/web/wiklou.git] / includes / Preferences.php
index 14168a3..9ec3b64 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,
  * 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(
                        'timecorrection' => array( 'Preferences', 'filterTimezoneInput' ),
+                       'cols' => array( 'Preferences', 'filterIntval' ),
                        'rows' => array( 'Preferences', 'filterIntval' ),
                        'rclimit' => array( 'Preferences', 'filterIntval' ),
                        'wllimit' => array( 'Preferences', 'filterIntval' ),
@@ -38,10 +59,10 @@ class Preferences {
        /**
         * @throws MWException
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @return array|null
         */
-       public static function getPreferences( $user, ContextSource $context ) {
+       static function getPreferences( $user, IContextSource $context ) {
                if ( self::$defaultPreferences ) {
                        return self::$defaultPreferences;
                }
@@ -83,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" );
@@ -127,11 +148,11 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences
         * @return void
         */
-       static function profilePreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function profilePreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgAuth, $wgContLang, $wgParser, $wgCookieExpiration, $wgLanguageCode,
                        $wgDisableTitleConversion, $wgDisableLangConversion, $wgMaxSigChars,
                        $wgEnableEmail, $wgEmailConfirmToEdit, $wgEnableUserEmail, $wgEmailAuthentication,
@@ -251,7 +272,7 @@ class Preferences {
                }
 
                // Language
-               $languages = Language::getLanguageNames( false );
+               $languages = Language::fetchLanguageNames( null, 'mw' );
                if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
                        $languages[$wgLanguageCode] = $wgLanguageCode;
                }
@@ -353,16 +374,20 @@ class Preferences {
                                $emailAddress .= $emailAddress == '' ? $link : " ($link)";
                        }
 
+
                        $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() ) {
@@ -377,6 +402,7 @@ class Preferences {
                                                $emailauthenticated = $context->msg( 'emailauthenticated',
                                                        $time, $d, $t )->parse() . '<br />';
                                                $disableEmailPrefs = false;
+                                               $emailauthenticationclass = 'mw-email-authenticated';
                                        } else {
                                                $disableEmailPrefs = true;
                                                $emailauthenticated = $context->msg( 'emailnotauthenticated' )->parse() . '<br />' .
@@ -384,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(
@@ -396,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(
@@ -454,11 +484,11 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences
         * @return void
         */
-       static function skinPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function skinPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                ## Skin #####################################
                global $wgAllowUserCss, $wgAllowUserJs;
 
@@ -509,10 +539,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function filesPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function filesPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                ## Files #####################################
                $defaultPreferences['imagesize'] = array(
                        'type' => 'select',
@@ -530,11 +560,11 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences
         * @return void
         */
-       static function datetimePreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function datetimePreferences( $user, IContextSource $context, &$defaultPreferences ) {
                ## Date and time #####################################
                $dateOptions = self::getDateOptions( $context );
                if ( $dateOptions ) {
@@ -603,10 +633,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function renderingPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function renderingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                ## Page Rendering ##############################
                global $wgAllowUserCssPrefs;
                if ( $wgAllowUserCssPrefs ) {
@@ -637,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',
@@ -681,14 +706,20 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function editingPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function editingPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgUseExternalEditor, $wgAllowUserCssPrefs;
 
                ## Editing #####################################
-               
+               $defaultPreferences['cols'] = array(
+                       'type' => 'int',
+                       'label-message' => 'columns',
+                       'section' => 'editing/textboxsize',
+                       'min' => 4,
+                       'max' => 1000,
+               );
                $defaultPreferences['rows'] = array(
                        'type' => 'int',
                        'label-message' => 'rows',
@@ -781,10 +812,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function rcPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function rcPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgRCMaxAge, $wgRCShowWatchingUsers;
 
                ## RecentChanges #####################################
@@ -838,10 +869,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences
         */
-       static function watchlistPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function watchlistPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgUseRCPatrol, $wgEnableAPI, $wgRCMaxAge;
 
                $watchlistdaysMax = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
@@ -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(
@@ -939,10 +971,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function searchPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function searchPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgContLang, $wgEnableMWSuggest, $wgVectorUseSimpleSearch;
 
                ## Search #####################################
@@ -1003,10 +1035,10 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $defaultPreferences Array
         */
-       static function miscPreferences( $user, ContextSource $context, &$defaultPreferences ) {
+       static function miscPreferences( $user, IContextSource $context, &$defaultPreferences ) {
                global $wgContLang;
 
                ## Misc #####################################
@@ -1035,10 +1067,10 @@ class Preferences {
 
        /**
         * @param $user User The User object
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @return Array: text/links to display as key; $skinkey as value
         */
-       static function generateSkinOptions( $user, ContextSource $context ) {
+       static function generateSkinOptions( $user, IContextSource $context ) {
                global $wgDefaultSkin, $wgAllowUserCss, $wgAllowUserJs;
                $ret = array();
 
@@ -1090,10 +1122,10 @@ class Preferences {
        }
 
        /**
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @return array
         */
-       static function getDateOptions( ContextSource $context ) {
+       static function getDateOptions( IContextSource $context ) {
                $lang = $context->getLanguage();
                $dateopts = $lang->getDatePreferences();
 
@@ -1125,10 +1157,10 @@ class Preferences {
        }
 
        /**
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @return array
         */
-       static function getImageSizes( ContextSource $context ) {
+       static function getImageSizes( IContextSource $context ) {
                global $wgImageLimits;
 
                $ret = array();
@@ -1143,10 +1175,10 @@ class Preferences {
        }
 
        /**
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @return array
         */
-       static function getThumbSizes( ContextSource $context ) {
+       static function getThumbSizes( IContextSource $context ) {
                global $wgThumbLimits;
 
                $ret = array();
@@ -1200,12 +1232,12 @@ class Preferences {
 
        /**
         * @param $user User
-        * @param $context ContextSource
+        * @param $context IContextSource
         * @param $formClass string
         * @param $remove Array: array of items to remove
         * @return HtmlForm
         */
-       static function getFormObject( $user, ContextSource $context, $formClass = 'PreferencesForm', array $remove = array() ) {
+       static function getFormObject( $user, IContextSource $context, $formClass = 'PreferencesForm', array $remove = array() ) {
                $formDescriptor = Preferences::getPreferences( $user, $context );
                if ( count( $remove ) ) {
                        $removeKeys = array_flip( $remove );
@@ -1229,10 +1261,9 @@ class Preferences {
        }
 
        /**
-        * @param $context ContextSource
         * @return array
         */
-       static function getTimezoneOptions( ContextSource $context ) {
+       static function getTimezoneOptions( IContextSource $context ) {
                $opt = array();
 
                global $wgLocalTZoffset, $wgLocaltimezone;
@@ -1421,46 +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 );
-                               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';
-                               }
-                       } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
-                               $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();