Reverting r16861; incompatible change to message texts, breaks a lot of toggle displa...
[lhc/web/wiklou.git] / includes / User.php
index 7bc52ab..f6567b9 100644 (file)
@@ -42,9 +42,88 @@ class User {
        var $mSkin;                     //!<
        var $mToken;            //!<
        var $mTouched;          //!<
+       var $mDatePreference; // !<
        var $mVersion;          //!< serialized version
        /**@}} */
 
+       /**
+        * Default user options
+        * To change this array at runtime, use a UserDefaultOptions hook
+        */
+       static public $mDefaultOptions = array( 
+               'quickbar'              => 1,
+               'underline'             => 2,
+               'cols'                  => 80,
+               'rows'                  => 25,
+               'searchlimit'           => 20,
+               'contextlines'          => 5,
+               'contextchars'          => 50,
+               'skin'                  => false,
+               'math'                  => 1,
+               'rcdays'                => 7,
+               'rclimit'               => 50,
+               'wllimit'               => 250,
+               'highlightbroken'       => 1,
+               'stubthreshold'         => 0,
+               'previewontop'          => 1,
+               'editsection'           => 1,
+               'editsectiononrightclick'=> 0,
+               'showtoc'               => 1,
+               'showtoolbar'           => 1,
+               'date'                  => 'default',
+               'imagesize'             => 2,
+               'thumbsize'             => 2,
+               'rememberpassword'      => 0,
+               'enotifwatchlistpages'  => 0,
+               'enotifusertalkpages'   => 1,
+               'enotifminoredits'      => 0,
+               'enotifrevealaddr'      => 0,
+               'shownumberswatching'   => 1,
+               'fancysig'              => 0,
+               'externaleditor'        => 0,
+               'externaldiff'          => 0,
+               'showjumplinks'         => 1,
+               'numberheadings'        => 0,
+               'uselivepreview'        => 0,
+               'watchlistdays'         => 3.0,
+       );
+
+       static public $mToggles = array(
+               'highlightbroken',
+               'justify',
+               'hideminor',
+               'extendwatchlist',
+               'usenewrc',
+               'numberheadings',
+               'showtoolbar',
+               'editondblclick',
+               'editsection',
+               'editsectiononrightclick',
+               'showtoc',
+               'rememberpassword',
+               'editwidth',
+               'watchcreations',
+               'watchdefault',
+               'minordefault',
+               'previewontop',
+               'previewonfirst',
+               'nocache',
+               'enotifwatchlistpages',
+               'enotifusertalkpages',
+               'enotifminoredits',
+               'enotifrevealaddr',
+               'shownumberswatching',
+               'fancysig',
+               'externaleditor',
+               'externaldiff',
+               'showjumplinks',
+               'uselivepreview',
+               'autopatrol',
+               'forceeditsummary',
+               'watchlisthideown',
+               'watchlisthidebots',
+       );              
+
        /** Constructor using User:loadDefaults() */
        function User() {
                $this->loadDefaults();
@@ -256,6 +335,48 @@ class User {
                
                return true;
        }
+       
+       /**
+        * Usernames which fail to pass this function will be blocked
+        * from user login and new account registrations, but may be used
+        * internally by batch processes.
+        *
+        * If an account already exists in this form, login will be blocked
+        * by a failure to pass this function.
+        *
+        * @param string $name
+        * @return bool
+        */
+       static function isUsableName( $name ) {
+               global $wgReservedUsernames;
+               return
+                       // Must be a usable username, obviously ;)
+                       self::isValidUserName( $name ) &&
+                       
+                       // Certain names may be reserved for batch processes.
+                       !in_array( $name, $wgReservedUsernames );
+       }
+       
+       /**
+        * Usernames which fail to pass this function will be blocked
+        * from new account registrations, but may be used internally
+        * either by batch processes or by user accounts which have
+        * already been created.
+        *
+        * Additional character blacklisting may be added here
+        * rather than in isValidUserName() to avoid disrupting
+        * existing accounts.
+        *
+        * @param string $name
+        * @return bool
+        */
+       static function isCreatableName( $name ) {
+               return
+                       self::isUsableName( $name ) &&
+                       
+                       // Registration-time character blacklisting...
+                       strpos( $name, '@' ) === false;
+       }
 
        /**
         * Is the input a valid password?
@@ -347,11 +468,9 @@ class User {
                $this->mPassword = $this->mNewpassword = '';
                $this->mRights = array();
                $this->mGroups = array();
-               $this->mOptions = User::getDefaultOptions();
+               $this->mOptions = null;
+               $this->mDatePreference = null;
 
-               foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
-                       $this->mOptions['searchNs'.$nsnum] = $val;
-               }
                unset( $this->mSkin );
                $this->mDataLoaded = false;
                $this->mBlockedby = -1; # Unset
@@ -379,19 +498,23 @@ class User {
         * @private
         */
        function getDefaultOptions() {
+               global $wgNamespacesToBeSearchedDefault;
                /**
                 * Site defaults will override the global/language defaults
                 */
-               global $wgContLang, $wgDefaultUserOptions;
-               $defOpt = $wgDefaultUserOptions + $wgContLang->getDefaultUserOptions();
+               global $wgContLang;
+               $defOpt = self::$mDefaultOptions + $wgContLang->getDefaultUserOptionOverrides();
 
                /**
                 * default language setting
                 */
-               $variant = $wgContLang->getPreferredVariant();
+               $variant = $wgContLang->getPreferredVariant( false );
                $defOpt['variant'] = $variant;
                $defOpt['language'] = $variant;
 
+               foreach( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
+                       $defOpt['searchNs'.$nsnum] = $val;
+               }
                return $defOpt;
        }
 
@@ -412,6 +535,18 @@ class User {
                }
        }
 
+       /**
+        * Get a list of user toggle names
+        * @return array
+        */
+       static function getToggles() {
+               global $wgContLang;
+               $extraToggles = array();
+               wfRunHooks( 'UserToggles', array( &$extraToggles ) );
+               return array_merge( self::$mToggles, $extraToggles, $wgContLang->getExtraUserToggles() );
+       }
+
+
        /**
         * Get blocking information
         * @private
@@ -450,7 +585,6 @@ class User {
                }
 
                # Proxy blocking
-               # FIXME ? proxyunbannable is to deprecate the old isSysop()
                if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
 
                        # Local list
@@ -531,7 +665,7 @@ class User {
                                return false;
                }
                
-               global $wgMemc, $wgDBname, $wgRateLimitLog;
+               global $wgMemc, $wgRateLimitLog;
                $fname = 'User::pingLimiter';
                wfProfileIn( $fname );
 
@@ -541,15 +675,15 @@ class User {
                $ip = wfGetIP();
 
                if( isset( $limits['anon'] ) && $id == 0 ) {
-                       $keys["$wgDBname:limiter:$action:anon"] = $limits['anon'];
+                       $keys[wfMemcKey( 'limiter', $action, 'anon' )] = $limits['anon'];
                }
 
                if( isset( $limits['user'] ) && $id != 0 ) {
-                       $keys["$wgDBname:limiter:$action:user:$id"] = $limits['user'];
+                       $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['user'];
                }
                if( $this->isNewbie() ) {
                        if( isset( $limits['newbie'] ) && $id != 0 ) {
-                               $keys["$wgDBname:limiter:$action:user:$id"] = $limits['newbie'];
+                               $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['newbie'];
                        }
                        if( isset( $limits['ip'] ) ) {
                                $keys["mediawiki:limiter:$action:ip:$ip"] = $limits['ip'];
@@ -569,7 +703,7 @@ class User {
                                if( $count > $max ) {
                                        wfDebug( "$fname: tripped! $key at $count $summary\n" );
                                        if( $wgRateLimitLog ) {
-                                               @error_log( wfTimestamp( TS_MW ) . ' ' . $wgDBname . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
+                                               @error_log( wfTimestamp( TS_MW ) . ' ' . wfWikiID() . ': ' . $this->getName() . " tripped $key at $count $summary\n", 3, $wgRateLimitLog );
                                        }
                                        $triggered = true;
                                } else {
@@ -638,19 +772,10 @@ class User {
 
        /**
         * Initialise php session
+        * @deprecated use wfSetupSession()
         */
        function SetupSession() {
-               global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
-               if( $wgSessionsInMemcached ) {
-                       require_once( 'MemcachedSessions.php' );
-               } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
-                       # If it's left on 'user' or another setting from another
-                       # application, it will end up failing. Try to recover.
-                       ini_set ( 'session.save_handler', 'files' );
-               }
-               session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
-               session_cache_limiter( 'private, must-revalidate' );
-               @session_start();
+               wfSetupSession();
        }
 
        /**
@@ -658,7 +783,7 @@ class User {
         * @static
         */
        function loadFromSession() {
-               global $wgMemc, $wgDBname, $wgCookiePrefix;
+               global $wgMemc, $wgCookiePrefix;
 
                if ( isset( $_SESSION['wsUserID'] ) ) {
                        if ( 0 != $_SESSION['wsUserID'] ) {
@@ -682,7 +807,7 @@ class User {
                }
 
                $passwordCorrect = FALSE;
-               $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
+               $user = $wgMemc->get( $key = wfMemcKey( 'user', 'id', $sId ) );
                if( !is_object( $user ) || $user->mVersion < MW_USER_VERSION ) {
                        # Expire old serialized objects; they may be corrupt.
                        $user = false;
@@ -833,8 +958,8 @@ class User {
                        # Check memcached separately for anons, who have no
                        # entire User object stored in there.
                        if( !$this->mId ) {
-                               global $wgDBname, $wgMemc;
-                               $key = "$wgDBname:newtalk:ip:" . $this->getName();
+                               global $wgMemc;
+                               $key = wfMemcKey( 'newtalk', 'ip', $this->getName() );
                                $newtalk = $wgMemc->get( $key );
                                if( is_integer( $newtalk ) ) {
                                        $this->mNewtalk = (bool)$newtalk;
@@ -854,7 +979,6 @@ class User {
         * Return the talk page(s) this user has new messages on.
         */
        function getNewMessageLinks() {
-       global  $wgDBname;
                $talks = array();
                if (!wfRunHooks('UserRetrieveNewTalks', array(&$this, &$talks)))
                        return $talks;
@@ -863,7 +987,7 @@ class User {
                        return array();
                $up = $this->getUserPage();
                $utp = $up->getTalkPage();
-               return array(array("wiki" => $wgDBname, "link" => $utp->getLocalURL()));
+               return array(array("wiki" => wfWikiID(), "link" => $utp->getLocalURL()));
        }
 
                
@@ -958,8 +1082,8 @@ class User {
                        if( $this->isAnon() ) {
                                // Anons have a separate memcached space, since
                                // user records aren't kept for them.
-                               global $wgDBname, $wgMemc;
-                               $key = "$wgDBname:newtalk:ip:$val";
+                               global $wgMemc;
+                               $key = wfMemcKey( 'newtalk', 'ip', $val );
                                $wgMemc->set( $key, $val ? 1 : 0 );
                        } else {
                                if( $val ) {
@@ -969,16 +1093,49 @@ class User {
                                }
                        }
                        $this->invalidateCache();
-                       $this->saveSettings();
+               }
+       }
+       
+       /**
+        * Generate a current or new-future timestamp to be stored in the
+        * user_touched field when we update things.
+        */
+       private static function newTouchedTimestamp() {
+               global $wgClockSkewFudge;
+               return wfTimestamp( TS_MW, time() + $wgClockSkewFudge );
+       }
+       
+       /**
+        * Clear user data from memcached.
+        * Use after applying fun updates to the database; caller's
+        * responsibility to update user_touched if appropriate.
+        *
+        * Called implicitly from invalidateCache() and saveSettings().
+        */
+       private function clearUserCache() {
+               if( $this->mId ) {
+                       global $wgMemc;
+                       $wgMemc->delete( wfMemcKey( 'user', 'id', $this->mId ) );
                }
        }
 
+       /**
+        * Immediately touch the user data cache for this account.
+        * Updates user_touched field, and removes account data from memcached
+        * for reload on the next hit.
+        */
        function invalidateCache() {
-               global $wgClockSkewFudge;
-               $this->loadFromDatabase();
-               $this->mTouched = wfTimestamp(TS_MW, time() + $wgClockSkewFudge );
-               # Don't forget to save the options after this or
-               # it won't take effect!
+               if( $this->mId ) {
+                       $this->mTouched = self::newTouchedTimestamp();
+                       
+                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw->update( 'user',
+                               array( 'user_touched' => $dbw->timestamp( $this->mTouched ) ),
+                               array( 'user_id' => $this->mId ),
+                               __METHOD__ );
+                       
+                       $this->clearUserCache();
+               }
        }
 
        function validateCache( $timestamp ) {
@@ -1006,7 +1163,7 @@ class User {
 
        # Set the random token (used for persistent authentication)
        function setToken( $token = false ) {
-               global $wgSecretKey, $wgProxyKey, $wgDBname;
+               global $wgSecretKey, $wgProxyKey;
                if ( !$token ) {
                        if ( $wgSecretKey ) {
                                $key = $wgSecretKey;
@@ -1015,7 +1172,7 @@ class User {
                        } else {
                                $key = microtime();
                        }
-                       $this->mToken = md5( $key . mt_rand( 0, 0x7fffffff ) . $wgDBname . $this->mId );
+                       $this->mToken = md5( $key . mt_rand( 0, 0x7fffffff ) . wfWikiID() . $this->mId );
                } else {
                        $this->mToken = $token;
                }
@@ -1063,6 +1220,9 @@ class User {
         */
        function getOption( $oname ) {
                $this->loadFromDatabase();
+               if ( is_null( $this->mOptions ) ) {
+                       $this->mOptions = User::getDefaultOptions();
+               }
                if ( array_key_exists( $oname, $this->mOptions ) ) {
                        return trim( $this->mOptions[$oname] );
                } else {
@@ -1070,6 +1230,23 @@ class User {
                }
        }
 
+       /**
+        * Get the user's date preference, including some important migration for 
+        * old user rows.
+        */
+       function getDatePreference() {
+               if ( is_null( $this->mDatePreference ) ) {
+                       global $wgLang;
+                       $value = $this->getOption( 'date' );
+                       $map = $wgLang->getDatePreferenceMigrationMap();
+                       if ( isset( $map[$value] ) ) {
+                               $value = $map[$value];
+                       }
+                       $this->mDatePreference = $value;
+               }
+               return $this->mDatePreference;
+       }
+
        /**
         * @param string $oname The option to check
         * @return bool False if the option is not selected, true if it is
@@ -1094,6 +1271,9 @@ class User {
 
        function setOption( $oname, $val ) {
                $this->loadFromDatabase();
+               if ( is_null( $this->mOptions ) ) {
+                       $this->mOptions = User::getDefaultOptions();
+               }
                if ( $oname == 'skin' ) {
                        # Clear cached skin, so the new one displays immediately in Special:Preferences
                        unset( $this->mSkin );
@@ -1104,7 +1284,6 @@ class User {
                $val = str_replace( "\r", "\n", $val );
                $val = str_replace( "\n", " ", $val );
                $this->mOptions[$oname] = $val;
-               $this->invalidateCache();
        }
 
        function getRights() {
@@ -1155,7 +1334,6 @@ class User {
                $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups() );
 
                $this->invalidateCache();
-               $this->saveSettings();
        }
 
        /**
@@ -1176,7 +1354,6 @@ class User {
                $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups() );
 
                $this->invalidateCache();
-               $this->saveSettings();
        }
 
 
@@ -1200,30 +1377,6 @@ class User {
                return !$this->isLoggedIn();
        }
 
-       /**
-        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
-        * @deprecated
-        */
-       function isSysop() {
-               throw new MWException( "Call to deprecated (v1.7) User::isSysop() method\n" );
-       }
-
-       /**
-        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
-        * @deprecated
-        */
-       function isDeveloper() {
-               throw new MWException( "Call to deprecated (v1.7) User::isDeveloper() method\n" );
-       }
-
-       /**
-        * Deprecated in 1.6, die in 1.7, to be removed in 1.8
-        * @deprecated
-        */
-       function isBureaucrat() {
-               throw new MWException( "Call to deprecated (v1.7) User::isBureaucrat() method\n" );
-       }
-
        /**
         * Whether the user is a bot
         * @deprecated
@@ -1234,7 +1387,7 @@ class User {
 
        /**
         * Check if user is allowed to access a feature / make an action
-        * @param string $action Action to be checked (see $wgAvailableRights in Defines.php for possible actions).
+        * @param string $action Action to be checked
         * @return boolean True: action is allowed, False: action should not be allowed
         */
        function isAllowed($action='') {
@@ -1373,7 +1526,7 @@ class User {
                        $dbw =& wfGetDB( DB_MASTER );
                        $success = $dbw->update( 'watchlist',
                                array( /* SET */
-                                       'wl_notificationtimestamp' => 0
+                                       'wl_notificationtimestamp' => NULL
                                ), array( /* WHERE */
                                        'wl_user' => $currentUser
                                ), 'UserMailer::clearAll'
@@ -1389,6 +1542,9 @@ class User {
         * @return string Encoding options
         */
        function encodeOptions() {
+               if ( is_null( $this->mOptions ) ) {
+                       $this->mOptions = User::getDefaultOptions();
+               }
                $a = array();
                foreach ( $this->mOptions as $oname => $oval ) {
                        array_push( $a, $oname.'='.$oval );
@@ -1401,6 +1557,9 @@ class User {
         * @private
         */
        function decodeOptions( $str ) {
+               global $wgLang;
+               
+               $this->mOptions = array();
                $a = explode( "\n", $str );
                foreach ( $a as $s ) {
                        if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
@@ -1449,13 +1608,15 @@ class User {
 
        /**
         * Save object settings into database
+        * @fixme Only rarely do all these fields need to be set!
         */
        function saveSettings() {
-               global $wgMemc, $wgDBname;
                $fname = 'User::saveSettings';
 
                if ( wfReadOnly() ) { return; }
                if ( 0 == $this->mId ) { return; }
+               
+               $this->mTouched = self::newTouchedTimestamp();
 
                $dbw =& wfGetDB( DB_MASTER );
                $dbw->update( 'user',
@@ -1473,7 +1634,7 @@ class User {
                                'user_id' => $this->mId
                        ), $fname
                );
-               $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
+               $this->clearUserCache();
        }
 
 
@@ -1586,7 +1747,7 @@ class User {
         * @return string
         */
        function getPageRenderingHash() {
-               global $wgContLang;
+               global $wgContLang, $wgUseDynamicDates;
                if( $this->mHash ){
                        return $this->mHash;
                }
@@ -1596,7 +1757,9 @@ class User {
 
                $confstr =        $this->getOption( 'math' );
                $confstr .= '!' . $this->getOption( 'stubthreshold' );
-               $confstr .= '!' . $this->getOption( 'date' );
+               if ( $wgUseDynamicDates ) {
+                       $confstr .= '!' . $this->getDatePreference();
+               }
                $confstr .= '!' . ($this->getOption( 'numberheadings' ) ? '1' : '');
                $confstr .= '!' . $this->getOption( 'language' );
                $confstr .= '!' . $this->getOption( 'thumbsize' );
@@ -1705,7 +1868,7 @@ class User {
                } elseif ( function_exists( 'iconv' ) ) {
                        # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
                        # Check for this with iconv
-                       $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
+                       $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252//TRANSLIT', $password ) );
                        if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {
                                return true;
                        }
@@ -1911,7 +2074,7 @@ class User {
         * @return array list of permission key names for given groups combined
         * @static
         */
-       function getGroupPermissions( $groups ) {
+       static function getGroupPermissions( $groups ) {
                global $wgGroupPermissions;
                $rights = array();
                foreach( $groups as $group ) {
@@ -1928,10 +2091,10 @@ class User {
         * @return string localized descriptive name for group, if provided
         * @static
         */
-       function getGroupName( $group ) {
+       static function getGroupName( $group ) {
                $key = "group-$group";
                $name = wfMsg( $key );
-               if( $name == '' || $name == "&lt;$key&gt;" ) {
+               if( $name == '' || wfEmptyMsg( $key, $name ) ) {
                        return $group;
                } else {
                        return $name;
@@ -1943,17 +2106,16 @@ class User {
         * @return string localized descriptive name for member of a group, if provided
         * @static
         */
-       function getGroupMember( $group ) {
+       static function getGroupMember( $group ) {
                $key = "group-$group-member";
                $name = wfMsg( $key );
-               if( $name == '' || $name == "&lt;$key&gt;" ) {
+               if( $name == '' || wfEmptyMsg( $key, $name ) ) {
                        return $group;
                } else {
                        return $name;
                }
        }
 
-
        /**
         * Return the set of defined explicit groups.
         * The *, 'user', 'autoconfirmed' and 'emailconfirmed'
@@ -1962,20 +2124,20 @@ class User {
         * @return array
         * @static
         */
-       function getAllGroups() {
+       static function getAllGroups() {
                global $wgGroupPermissions;
                return array_diff(
                        array_keys( $wgGroupPermissions ),
                        array( '*', 'user', 'autoconfirmed', 'emailconfirmed' ) );
        }
-       
+
        /**
         * Get the title of a page describing a particular group
         *
         * @param $group Name of the group
         * @return mixed
         */
-       function getGroupPage( $group ) {
+       static function getGroupPage( $group ) {
                $page = wfMsgForContent( 'grouppage-' . $group );
                if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
                        $title = Title::newFromText( $page );
@@ -1984,8 +2146,47 @@ class User {
                }
                return false;
        }
-       
-       
+
+       /**
+        * Create a link to the group in HTML, if available
+        *
+        * @param $group Name of the group
+        * @param $text The text of the link
+        * @return mixed
+        */
+       static function makeGroupLinkHTML( $group, $text = '' ) {
+               if( $text == '' ) {
+                       $text = self::getGroupName( $group );
+               }
+               $title = self::getGroupPage( $group );
+               if( $title ) {
+                       global $wgUser;
+                       $sk = $wgUser->getSkin();
+                       return $sk->makeLinkObj( $title, $text );
+               } else {
+                       return $text;
+               }
+       }
+
+       /**
+        * Create a link to the group in Wikitext, if available
+        *
+        * @param $group Name of the group
+        * @param $text The text of the link (by default, the name of the group)
+        * @return mixed
+        */
+       static function makeGroupLinkWiki( $group, $text = '' ) {
+               if( $text == '' ) {
+                       $text = self::getGroupName( $group );
+               }
+               $title = self::getGroupPage( $group );
+               if( $title ) {
+                       $page = $title->getPrefixedText();
+                       return "[[$page|$text]]";
+               } else {
+                       return $text;
+               }
+       }
 }
 
 ?>