Whitespace fixes
[lhc/web/wiklou.git] / includes / Preferences.php
index 87dc83d..1331181 100644 (file)
@@ -29,20 +29,28 @@ 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' ),
+                       'searchlimit' => array( 'Preferences', 'filterIntval' ),
        );
 
+       /**
+        * @throws MWException
+        * @param $user User
+        * @return array|null
+        */
        static function getPreferences( $user ) {
-               if ( self::$defaultPreferences )
+               if ( self::$defaultPreferences ) {
                        return self::$defaultPreferences;
-
-               global $wgRCMaxAge;
+               }
 
                $defaultPreferences = array();
 
                self::profilePreferences( $user, $defaultPreferences );
                self::skinPreferences( $user, $defaultPreferences );
                self::filesPreferences( $user, $defaultPreferences );
-               self::mathPreferences( $user, $defaultPreferences );
                self::datetimePreferences( $user, $defaultPreferences );
                self::renderingPreferences( $user, $defaultPreferences );
                self::editingPreferences( $user, $defaultPreferences );
@@ -62,10 +70,9 @@ class Preferences {
                }
 
                ## Prod in defaults from the user
-               global $wgDefaultUserOptions;
                foreach ( $defaultPreferences as $name => &$info ) {
                        $prefFromUser = self::getOptionFromUser( $name, $info, $user );
-                       $field = HTMLForm::loadInputFromParameters( $info ); // For validation
+                       $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation
                        $defaultOptions = User::getDefaultOptions();
                        $globalDefault = isset( $defaultOptions[$name] )
                                ? $defaultOptions[$name]
@@ -90,7 +97,14 @@ class Preferences {
                return $defaultPreferences;
        }
 
-       // Pull option from a user account. Handles stuff like array-type preferences.
+       /**
+        * Pull option from a user account. Handles stuff like array-type preferences.
+        *
+        * @param $name
+        * @param $info
+        * @param $user User
+        * @return array|String
+        */
        static function getOptionFromUser( $name, $info, $user ) {
                $val = $user->getOption( $name );
 
@@ -101,7 +115,7 @@ class Preferences {
                        $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
                        $val = array();
 
-                       foreach ( $options as $label => $value ) {
+                       foreach ( $options as $value ) {
                                if ( $user->getOption( "$prefix$value" ) ) {
                                        $val[] = $value;
                                }
@@ -111,6 +125,11 @@ class Preferences {
                return $val;
        }
 
+       /**
+        * @param $user User
+        * @param $defaultPreferences
+        * @return void
+        */
        static function profilePreferences( $user, &$defaultPreferences ) {
                global $wgLang, $wgUser;
                ## User info #####################################
@@ -205,7 +224,7 @@ class Preferences {
                );
 
                if ( $wgAuth->allowPasswordChange() ) {
-                       $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'Resetpass' ),
+                       $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'ChangePassword' ),
                                wfMsgHtml( 'prefs-resetpass' ), array(),
                                array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' ) ) );
 
@@ -231,10 +250,10 @@ class Preferences {
                }
 
                // Language
-               global $wgContLanguageCode;
-               $languages = array_reverse( Language::getLanguageNames( false ) );
-               if ( !array_key_exists( $wgContLanguageCode, $languages ) ) {
-                       $languages[$wgContLanguageCode] = $wgContLanguageCode;
+               global $wgLanguageCode;
+               $languages = Language::getLanguageNames( false );
+               if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
+                       $languages[$wgLanguageCode] = $wgLanguageCode;
                }
                ksort( $languages );
 
@@ -294,7 +313,7 @@ class Preferences {
                global $wgMaxSigChars, $wgOut, $wgParser;
 
                // show a preview of the old signature first
-               $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title , $user, new ParserOptions );
+               $oldsigWikiText = $wgParser->preSaveTransform( "~~~", new Title, $user, new ParserOptions );
                $oldsigHTML = $wgOut->parseInline( $oldsigWikiText );
                $defaultPreferences['oldsig'] = array(
                        'type' => 'info',
@@ -323,19 +342,27 @@ class Preferences {
                global $wgEnableEmail;
                if ( $wgEnableEmail ) {
                        global $wgEmailConfirmToEdit;
+                       global $wgEnableUserEmail;
+
+                       $helpMessages[] = $wgEmailConfirmToEdit
+                                       ? 'prefs-help-email-required'
+                                       : 'prefs-help-email' ;
+
+                       if( $wgEnableUserEmail ) {
+                               // additional messages when users can send email to each other
+                               $helpMessages[] = 'prefs-help-email-others';
+                       }
 
                        $defaultPreferences['emailaddress'] = array(
                                'type' => $wgAuth->allowPropChange( 'emailaddress' ) ? 'email' : 'info',
                                'default' => $user->getEmail(),
                                'section' => 'personal/email',
                                'label-message' => 'youremail',
-                               'help-message' => $wgEmailConfirmToEdit
-                                       ? 'prefs-help-email-required'
-                                       : 'prefs-help-email',
+                               'help-messages' => $helpMessages,
                                'validation-callback' => array( 'Preferences', 'validateEmail' ),
                        );
 
-                       global $wgEnableUserEmail, $wgEmailAuthentication;
+                       global $wgEmailAuthentication;
 
                        $disableEmailPrefs = false;
 
@@ -435,6 +462,11 @@ class Preferences {
                }
        }
 
+       /**
+        * @param $user User
+        * @param $defaultPreferences
+        * @return void
+        */
        static function skinPreferences( $user, &$defaultPreferences ) {
                ## Skin #####################################
                global $wgLang, $wgAllowUserCss, $wgAllowUserJs;
@@ -474,8 +506,6 @@ class Preferences {
 
                $selectedSkin = $user->getOption( 'skin' );
                if ( in_array( $selectedSkin, array( 'cologneblue', 'standard' ) ) ) {
-                       global $wgLang;
-
                        $settings = array_flip( $wgLang->getQuickbarSettings() );
 
                        $defaultPreferences['quickbar'] = array(
@@ -487,19 +517,6 @@ class Preferences {
                }
        }
 
-       static function mathPreferences( $user, &$defaultPreferences ) {
-               ## Math #####################################
-               global $wgUseTeX, $wgLang;
-               if ( $wgUseTeX ) {
-                       $defaultPreferences['math'] = array(
-                               'type' => 'radio',
-                               'options' => array_flip( array_map( 'wfMsgHtml', $wgLang->getMathNames() ) ),
-                               'label' => ' ',
-                               'section' => 'rendering/math',
-                       );
-               }
-       }
-
        static function filesPreferences( $user, &$defaultPreferences ) {
                ## Files #####################################
                $defaultPreferences['imagesize'] = array(
@@ -516,6 +533,11 @@ class Preferences {
                );
        }
 
+       /**
+        * @param $user User
+        * @param $defaultPreferences
+        * @return void
+        */
        static function datetimePreferences( $user, &$defaultPreferences ) {
                global $wgLang;
 
@@ -531,10 +553,11 @@ class Preferences {
                }
 
                // Info
+               $now = wfTimestampNow();
                $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ),
-                       $wgLang->time( $now = wfTimestampNow(), true ) );
+                       $wgLang->time( $now, true ) );
                $nowserver = $wgLang->time( $now, false ) .
-                       Xml::hidden( 'wpServerTime', substr( $now, 8, 2 ) * 60 + substr( $now, 10, 2 ) );
+                       Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) );
 
                $defaultPreferences['nowserver'] = array(
                        'type' => 'info',
@@ -588,8 +611,8 @@ class Preferences {
                        );
                }
 
-               $stubThresholdValues = array( 0, 50, 100, 500, 1000, 2000, 5000, 10000 );
-               $stubThresholdOptions = array();
+               $stubThresholdValues = array( 50, 100, 500, 1000, 2000, 5000, 10000 );
+               $stubThresholdOptions = array( wfMsg( 'stub-threshold-disabled' ) => 0 );
                foreach ( $stubThresholdValues as $value ) {
                        $stubThresholdOptions[wfMsg( 'size-bytes', $value )] = $value;
                }
@@ -735,7 +758,7 @@ class Preferences {
                        'label-message' => 'tog-forceeditsummary',
                );
 
-                               
+
                $defaultPreferences['uselivepreview'] = array(
                        'type' => 'toggle',
                        'section' => 'editing/advancedediting',
@@ -750,7 +773,7 @@ class Preferences {
                $defaultPreferences['rcdays'] = array(
                        'type' => 'float',
                        'label-message' => 'recentchangesdays',
-                       'section' => 'rc/display',
+                       'section' => 'rc/displayrc',
                        'min' => 1,
                        'max' => ceil( $wgRCMaxAge / ( 3600 * 24 ) ),
                        'help' => wfMsgExt(
@@ -763,7 +786,7 @@ class Preferences {
                        'type' => 'int',
                        'label-message' => 'recentchangescount',
                        'help-message' => 'prefs-help-recentchangescount',
-                       'section' => 'rc/display',
+                       'section' => 'rc/displayrc',
                );
                $defaultPreferences['usenewrc'] = array(
                        'type' => 'toggle',
@@ -799,6 +822,11 @@ class Preferences {
                }
        }
 
+       /**
+        * @param $user User
+        * @param $defaultPreferences
+        * @return void
+        */
        static function watchlistPreferences( $user, &$defaultPreferences ) {
                global $wgUseRCPatrol, $wgEnableAPI;
 
@@ -807,7 +835,7 @@ class Preferences {
                        'type' => 'float',
                        'min' => 0,
                        'max' => 7,
-                       'section' => 'watchlist/display',
+                       'section' => 'watchlist/displaywatchlist',
                        'help' => wfMsgHtml( 'prefs-watchlist-days-max' ),
                        'label-message' => 'prefs-watchlist-days',
                );
@@ -817,7 +845,7 @@ class Preferences {
                        'max' => 1000,
                        'label-message' => 'prefs-watchlist-edits',
                        'help' => wfMsgHtml( 'prefs-watchlist-edits-max' ),
-                       'section' => 'watchlist/display',
+                       'section' => 'watchlist/displaywatchlist',
                );
                $defaultPreferences['extendwatchlist'] = array(
                        'type' => 'toggle',
@@ -899,19 +927,7 @@ class Preferences {
                $defaultPreferences['searchlimit'] = array(
                        'type' => 'int',
                        'label-message' => 'resultsperpage',
-                       'section' => 'searchoptions/display',
-                       'min' => 0,
-               );
-               $defaultPreferences['contextlines'] = array(
-                       'type' => 'int',
-                       'label-message' => 'contextlines',
-                       'section' => 'searchoptions/display',
-                       'min' => 0,
-               );
-               $defaultPreferences['contextchars'] = array(
-                       'type' => 'int',
-                       'label-message' => 'contextchars',
-                       'section' => 'searchoptions/display',
+                       'section' => 'searchoptions/displaysearchoptions',
                        'min' => 0,
                );
 
@@ -920,16 +936,16 @@ class Preferences {
                        $defaultPreferences['disablesuggest'] = array(
                                'type' => 'toggle',
                                'label-message' => 'mwsuggest-disable',
-                               'section' => 'searchoptions/display',
+                               'section' => 'searchoptions/displaysearchoptions',
                        );
                }
-               
+
                global $wgVectorUseSimpleSearch;
                if ( $wgVectorUseSimpleSearch ) {
                        $defaultPreferences['vector-simplesearch'] = array(
                                'type' => 'toggle',
                                'label-message' => 'vector-simplesearch-preference',
-                               'section' => 'searchoptions/display'
+                               'section' => 'searchoptions/displaysearchoptions'
                        );
                }
 
@@ -939,9 +955,6 @@ class Preferences {
                        'section' => 'searchoptions/advancedsearchoptions',
                );
 
-               // Searchable namespaces back-compat with old format
-               $searchableNamespaces = SearchEngine::searchableNamespaces();
-
                $nsOptions = array();
 
                foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
@@ -996,7 +1009,7 @@ class Preferences {
        }
 
        /**
-        * @param $user The User object
+        * @param $user User The User object
         * @return Array: text/links to display as key; $skinkey as value
         */
        static function generateSkinOptions( $user ) {
@@ -1013,10 +1026,9 @@ 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 ) {
-                       $msgName = "skinname-{$skinkey}";
-                       $localisedSkinName = wfMsg( $msgName );
-                       if ( !wfEmptyMsg( $msgName, $localisedSkinName ) ) {
-                               $skinname = htmlspecialchars( $localisedSkinName );
+                       $msg = wfMessage( "skinname-{$skinkey}" );
+                       if ( $msg->exists() ) {
+                               $skinname = htmlspecialchars( $msg->text() );
                        }
                }
                asort( $validSkinNames );
@@ -1070,7 +1082,6 @@ class Preferences {
                                $wgDefaultUserOptions['date'] = 'default';
                        }
 
-                       $idCnt = 0;
                        $epoch = wfTimestampNow();
                        foreach ( $dateopts as $key ) {
                                if ( $key == 'default' ) {
@@ -1118,7 +1129,8 @@ class Preferences {
                                        $wgLang->formatNum( $wgMaxSigChars )
                                )
                        );
-               } elseif ( !empty( $alldata['fancysig'] ) &&
+               } elseif ( isset( $alldata['fancysig'] ) &&
+                               $alldata['fancysig'] &&
                                false === $wgParser->validateSig( $signature ) ) {
                        return Xml::element( 'span', array( 'class' => 'error' ), wfMsg( 'badsig' ) );
                } else {
@@ -1128,7 +1140,7 @@ class Preferences {
 
        static function cleanSignature( $signature, $alldata ) {
                global $wgParser;
-               if ( $alldata['fancysig'] ) {
+               if ( isset( $alldata['fancysig'] ) && $alldata['fancysig'] ) {
                        $signature = $wgParser->cleanSig( $signature );
                } else {
                        // When no fancy sig used, make sure ~{3,5} get removed.
@@ -1139,7 +1151,7 @@ class Preferences {
        }
 
        static function validateEmail( $email, $alldata ) {
-               if ( $email && !User::isValidEmailAddr( $email ) ) {
+               if ( $email && !Sanitizer::validateEmail( $email ) ) {
                        return wfMsgExt( 'invalidemailaddress', 'parseinline' );
                }
 
@@ -1150,10 +1162,11 @@ class Preferences {
                return true;
        }
 
-       static function getFormObject( $user ) {
+       static function getFormObject( $user, $formClass = 'PreferencesForm' ) {
                $formDescriptor = Preferences::getPreferences( $user );
-               $htmlForm = new PreferencesForm( $formDescriptor, 'prefs' );
+               $htmlForm = new $formClass( $formDescriptor, 'prefs' );
 
+               $htmlForm->setId( 'mw-prefs-form' );
                $htmlForm->setSubmitText( wfMsg( 'saveprefs' ) );
                # Used message keys: 'accesskey-preferences-save', 'tooltip-preferences-save'
                $htmlForm->setSubmitTooltip( 'preferences-save' );
@@ -1220,6 +1233,10 @@ class Preferences {
                return $opt;
        }
 
+       static function filterIntval( $value, $alldata ){
+               return intval( $value );
+       }
+
        static function filterTimezoneInput( $tz, $alldata ) {
                $data = explode( '|', $tz, 3 );
                switch ( $data[0] ) {
@@ -1228,7 +1245,6 @@ class Preferences {
                                return $tz;
                        default:
                                $data = explode( ':', $tz, 2 );
-                               $minDiff = 0;
                                if ( count( $data ) == 2 ) {
                                        $data[0] = intval( $data[0] );
                                        $data[1] = intval( $data[1] );
@@ -1277,9 +1293,10 @@ class Preferences {
                                if ( $wgEmailAuthentication ) {
                                        # Mail a temporary password to the dirty address.
                                        # User can come back through the confirmation URL to re-enable email.
-                                       $result = $wgUser->sendConfirmationMail( $oldaddr != '' );
-                                       if ( WikiError::isError( $result ) ) {
-                                               return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
+                                       $type = $oldaddr != '' ? 'changed' : 'set';
+                                       $result = $wgUser->sendConfirmationMail( $type );
+                                       if ( !$result->isGood() ) {
+                                               return htmlspecialchars( $result->getWikiText( 'mailerror' ) );
                                        } elseif ( $entryPoint == 'ui' ) {
                                                $result = 'eauth';
                                        }
@@ -1303,6 +1320,16 @@ class Preferences {
                        unset( $formData[$b] );
                }
 
+               # If users have saved a value for a preference which has subsequently been disabled
+               # via $wgHiddenPrefs, we don't want to destroy that setting in case the preference
+               # is subsequently re-enabled
+               # TODO: maintenance script to actually delete these
+               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 );
+               }
+
                //  Keeps old preferences from interfering due to back-compat
                //  code, etc.
                $wgUser->resetOptions();
@@ -1334,9 +1361,13 @@ class Preferences {
                        $wgOut->redirect( $url );
                }
 
-               return true;
+               return Status::newGood();
        }
 
+       /**
+        * @param $user User
+        * @return array
+        */
        public static function loadOldSearchNs( $user ) {
                $searchableNamespaces = SearchEngine::searchableNamespaces();
                // Back compat with old format