Revert r35178 and normalize User's getID() and setID() methods to prettier getId...
[lhc/web/wiklou.git] / includes / User.php
index 8e33705..3eee728 100644 (file)
@@ -1,21 +1,21 @@
 <?php
 /**
  * See user.txt
- *
+ * @file
  */
 
 # Number of characters in user_token field
 define( 'USER_TOKEN_LENGTH', 32 );
 
 # Serialized record version
-define( 'MW_USER_VERSION', 5 );
+define( 'MW_USER_VERSION', 6 );
 
 # Some punctuation to prevent editing from broken text-mangling proxies.
 define( 'EDIT_TOKEN_SUFFIX', '+\\' );
 
 /**
  * Thrown by User::setPassword() on error
- * @addtogroup Exception
+ * @ingroup Exception
  */
 class PasswordError extends MWException {
        // NOP
@@ -34,8 +34,8 @@ class PasswordError extends MWException {
 class User {
 
        /**
-        * A list of default user toggles, i.e. boolean user preferences that are 
-        * displayed by Special:Preferences as checkboxes. This list can be 
+        * A list of default user toggles, i.e. boolean user preferences that are
+        * displayed by Special:Preferences as checkboxes. This list can be
         * extended via the UserToggles hook or $wgContLang->getExtraUserToggles().
         */
        static public $mToggles = array(
@@ -81,7 +81,7 @@ class User {
 
        /**
         * List of member variables which are saved to the shared cache (memcached).
-        * Any operation which changes the corresponding database fields must 
+        * Any operation which changes the corresponding database fields must
         * call a cache-clearing function.
         */
        static $mCacheVars = array(
@@ -105,11 +105,57 @@ class User {
                'mGroups',
        );
 
+       /**
+        * Core rights
+        * Each of these should have a corresponding message of the form "right-$right"
+        */
+       static $mCoreRights = array(
+               'apihighlimits',
+               'autoconfirmed',
+               'autopatrol',
+               'bigdelete',
+               'block',
+               'blockemail',
+               'bot',
+               'browsearchive',
+               'createaccount',
+               'createpage',
+               'createtalk',
+               'delete',
+               'deletedhistory',
+               'edit',
+               'editinterface',
+               'editusercssjs',
+               'import',
+               'importupload',
+               'ipblock-exempt',
+               'markbotedits',
+               'minoredit',
+               'move',
+               'nominornewtalk',
+               'patrol',
+               'protect',
+               'proxyunbannable',
+               'purge',
+               'read',
+               'reupload',
+               'reupload-shared',
+               'rollback',
+               'suppressredirect',
+               'trackback',
+               'undelete',
+               'unwatchedpages',
+               'upload',
+               'upload_by_url',
+               'userrights',
+       );
+       static $mAllRights = false;
+
        /**
         * The cache variable declarations
         */
-       var $mId, $mName, $mRealName, $mPassword, $mNewpassword, $mNewpassTime, 
-               $mEmail, $mOptions, $mTouched, $mToken, $mEmailAuthenticated, 
+       var $mId, $mName, $mRealName, $mPassword, $mNewpassword, $mNewpassTime,
+               $mEmail, $mOptions, $mTouched, $mToken, $mEmailAuthenticated,
                $mEmailToken, $mEmailTokenExpires, $mRegistration, $mGroups;
 
        /**
@@ -134,7 +180,7 @@ class User {
        var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mSkin, $mRights,
                $mBlockreason, $mBlock, $mEffectiveGroups;
 
-       /** 
+       /**
         * Lightweight constructor for anonymous user
         * Use the User::newFrom* factory functions for other kinds of users
         */
@@ -189,7 +235,7 @@ class User {
                if ( $this->mId == 0 ) {
                        $this->loadDefaults();
                        return false;
-               } 
+               }
 
                # Try cache
                $key = wfMemcKey( 'user', 'id', $this->mId );
@@ -198,7 +244,7 @@ class User {
                        # Object is expired, load from DB
                        $data = false;
                }
-               
+
                if ( !$data ) {
                        wfDebug( "Cache miss for user {$this->mId}\n" );
                        # Load from DB
@@ -206,7 +252,6 @@ class User {
                                # Can't load from ID, user is anonymous
                                return false;
                        }
-
                        $this->saveToCache();
                } else {
                        wfDebug( "Got user {$this->mId} from cache\n" );
@@ -223,6 +268,7 @@ class User {
         */
        function saveToCache() {
                $this->load();
+               $this->loadGroups();
                if ( $this->isAnon() ) {
                        // Anonymous users are uncached
                        return;
@@ -241,16 +287,16 @@ class User {
         * Static factory method for creation from username.
         *
         * This is slightly less efficient than newFromId(), so use newFromId() if
-        * you have both an ID and a name handy. 
+        * you have both an ID and a name handy.
         *
         * @param string $name Username, validated by Title:newFromText()
-        * @param mixed $validate Validate username. Takes the same parameters as 
-        *    User::getCanonicalName(), except that true is accepted as an alias 
+        * @param mixed $validate Validate username. Takes the same parameters as
+        *    User::getCanonicalName(), except that true is accepted as an alias
         *    for 'valid', for BC.
-        * 
-        * @return User object, or null if the username is invalid. If the username 
+        *
+        * @return User object, or null if the username is invalid. If the username
         *    is not present in the database, the result will be a user object with
-        *    a name, zero user ID and default settings. 
+        *    a name, zero user ID and default settings.
         * @static
         */
        static function newFromName( $name, $validate = 'valid' ) {
@@ -299,7 +345,7 @@ class User {
                        return null;
                }
        }
-       
+
        /**
         * Create a new user object using data from session or cookies. If the
         * login credentials are invalid, the result is an anonymous user.
@@ -313,6 +359,16 @@ class User {
                return $user;
        }
 
+       /**
+        * Create a new user object from a user row.
+        * The row should have all fields from the user table in it.
+        */
+       static function newFromRow( $row ) {
+               $user = new User;
+               $user->loadFromRow( $row );
+               return $user;
+       }
+
        /**
         * Get username given an id.
         * @param integer $id Database user id
@@ -362,9 +418,9 @@ class User {
         *
         * This function exists for username validation, in order to reject
         * usernames which are similar in form to IP addresses. Strings such
-        * as 300.300.300.300 will return true because it looks like an IP 
+        * as 300.300.300.300 will return true because it looks like an IP
         * address, despite not being strictly valid.
-        * 
+        *
         * We match \d{1,3}\.\d{1,3}\.\d{1,3}\.xxx as an anonymous IP
         * address because the usemod software would "cloak" anonymous IP
         * addresses like this, if we allowed accounts like this to be created
@@ -388,8 +444,8 @@ class User {
         * Check if $name is an IPv6 IP.
         */
        static function isIPv6($name) {
-               /* 
-                * if it has any non-valid characters, it can't be a valid IPv6  
+               /*
+                * if it has any non-valid characters, it can't be a valid IPv6
                 * address.
                 */
                if (preg_match("/[^:a-fA-F0-9]/", $name))
@@ -440,7 +496,7 @@ class User {
                                ": '$name' invalid due to ambiguous prefixes" );
                        return false;
                }
-               
+
                // Check an additional blacklist of troublemaker characters.
                // Should these be merged into the title char list?
                $unicodeBlacklist = '/[' .
@@ -456,10 +512,10 @@ class User {
                                ": '$name' invalid due to blacklisted characters" );
                        return false;
                }
-               
+
                return true;
        }
-       
+
        /**
         * Usernames which fail to pass this function will be blocked
         * from user login and new account registrations, but may be used
@@ -476,11 +532,11 @@ class User {
                return
                        // Must be a valid 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
@@ -497,7 +553,7 @@ class User {
        static function isCreatableName( $name ) {
                return
                        self::isUsableName( $name ) &&
-                       
+
                        // Registration-time character blacklisting...
                        strpos( $name, '@' ) === false;
        }
@@ -516,7 +572,7 @@ class User {
                        return $result;
                if( $result === false )
                        return false;
-                       
+
                // Password needs to be long enough, and can't be the same as the username
                return strlen( $password ) >= $wgMinimalPasswordLength
                        && $wgContLang->lc( $password ) !== $wgContLang->lc( $this->mName );
@@ -544,7 +600,7 @@ class User {
        }
 
        /**
-        * Given unvalidated user input, return a canonical username, or false if 
+        * Given unvalidated user input, return a canonical username, or false if
         * the username is invalid.
         * @param string $name
         * @param mixed $validate Type of validation to use:
@@ -603,7 +659,7 @@ class User {
         * Count the number of edits of a user
         *
         * It should not be static and some day should be merged as proper member function / deprecated -- domas
-        * 
+        *
         * @param int $uid The user ID to check
         * @return int
         * @static
@@ -660,7 +716,7 @@ class User {
        }
 
        /**
-        * Set cached properties to default. Note: this no longer clears 
+        * Set cached properties to default. Note: this no longer clears
         * uncached lazy-initialised properties. The constructor does that instead.
         *
         * @private
@@ -693,13 +749,13 @@ class User {
 
                wfProfileOut( __METHOD__ );
        }
-       
+
        /**
         * Initialise php session
         * @deprecated use wfSetupSession()
         */
        function SetupSession() {
-               trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+               wfDeprecated( __METHOD__ );
                wfSetupSession();
        }
 
@@ -711,6 +767,12 @@ class User {
        private function loadFromSession() {
                global $wgMemc, $wgCookiePrefix;
 
+               $result = null;
+               wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
+               if ( $result !== null ) {
+                       return $result;
+               }
+
                if ( isset( $_SESSION['wsUserID'] ) ) {
                        if ( 0 != $_SESSION['wsUserID'] ) {
                                $sId = $_SESSION['wsUserID'];
@@ -741,7 +803,7 @@ class User {
                        # Not a valid ID, loadFromId has switched the object to anon for us
                        return false;
                }
-               
+
                if ( isset( $_SESSION['wsToken'] ) ) {
                        $passwordCorrect = $_SESSION['wsToken'] == $this->mToken;
                        $from = 'session';
@@ -765,11 +827,11 @@ class User {
                        return false;
                }
        }
-       
+
        /**
         * Load user and user_group data from the database
         * $this->mId must be set, this is how the user is identified.
-        * 
+        *
         * @return true if the user exists, false if the user is anonymous
         * @private
         */
@@ -788,23 +850,50 @@ class User {
 
                if ( $s !== false ) {
                        # Initialise user table data
-                       $this->mName = $s->user_name;
-                       $this->mRealName = $s->user_real_name;
-                       $this->mPassword = $s->user_password;
-                       $this->mNewpassword = $s->user_newpassword;
-                       $this->mNewpassTime = wfTimestampOrNull( TS_MW, $s->user_newpass_time );
-                       $this->mEmail = $s->user_email;
-                       $this->decodeOptions( $s->user_options );
-                       $this->mTouched = wfTimestamp(TS_MW,$s->user_touched);
-                       $this->mToken = $s->user_token;
-                       $this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $s->user_email_authenticated );
-                       $this->mEmailToken = $s->user_email_token;
-                       $this->mEmailTokenExpires = wfTimestampOrNull( TS_MW, $s->user_email_token_expires );
-                       $this->mRegistration = wfTimestampOrNull( TS_MW, $s->user_registration );
-                       $this->mEditCount = $s->user_editcount; 
+                       $this->loadFromRow( $s );
+                       $this->mGroups = null; // deferred
                        $this->getEditCount(); // revalidation for nulls
+                       return true;
+               } else {
+                       # Invalid user_id
+                       $this->mId = 0;
+                       $this->loadDefaults();
+                       return false;
+               }
+       }
 
-                       # Load group data
+       /**
+        * Initialise the user object from a row from the user table
+        */
+       function loadFromRow( $row ) {
+               $this->mDataLoaded = true;
+
+               if ( isset( $row->user_id ) ) {
+                       $this->mId = $row->user_id;
+               }
+               $this->mName = $row->user_name;
+               $this->mRealName = $row->user_real_name;
+               $this->mPassword = $row->user_password;
+               $this->mNewpassword = $row->user_newpassword;
+               $this->mNewpassTime = wfTimestampOrNull( TS_MW, $row->user_newpass_time );
+               $this->mEmail = $row->user_email;
+               $this->decodeOptions( $row->user_options );
+               $this->mTouched = wfTimestamp(TS_MW,$row->user_touched);
+               $this->mToken = $row->user_token;
+               $this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $row->user_email_authenticated );
+               $this->mEmailToken = $row->user_email_token;
+               $this->mEmailTokenExpires = wfTimestampOrNull( TS_MW, $row->user_email_token_expires );
+               $this->mRegistration = wfTimestampOrNull( TS_MW, $row->user_registration );
+               $this->mEditCount = $row->user_editcount; 
+       }
+
+       /**
+        * Load the groups from the database if they aren't already loaded
+        * @private
+        */
+       function loadGroups() {
+               if ( is_null( $this->mGroups ) ) {
+                       $dbr = wfGetDB( DB_MASTER );
                        $res = $dbr->select( 'user_groups',
                                array( 'ug_group' ),
                                array( 'ug_user' => $this->mId ),
@@ -813,19 +902,13 @@ class User {
                        while( $row = $dbr->fetchObject( $res ) ) {
                                $this->mGroups[] = $row->ug_group;
                        }
-                       return true;
-               } else {
-                       # Invalid user_id
-                       $this->mId = 0;
-                       $this->loadDefaults();
-                       return false;
                }
        }
 
        /**
-        * Clear various cached data stored in this object. 
-        * @param string $reloadFrom Reload user and user_groups table data from a 
-        *   given source. May be "name", "id", "defaults", "session" or false for 
+        * Clear various cached data stored in this object.
+        * @param string $reloadFrom Reload user and user_groups table data from a
+        *   given source. May be "name", "id", "defaults", "session" or false for
         *   no reload.
         */
        function clearInstanceCache( $reloadFrom = false ) {
@@ -919,7 +1002,14 @@ class User {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": checking...\n" );
 
-               $this->mBlockedby = 0; 
+               // Initialize data...
+               // Otherwise something ends up stomping on $this->mBlockedby when
+               // things get lazy-loaded later, causing false positive block hits
+               // due to -1 !== 0. Probably session-related... Nothing should be
+               // overwriting mBlockedby, surely?
+               $this->load();
+               
+               $this->mBlockedby = 0;
                $this->mHideName = 0;
                $ip = wfGetIP();
 
@@ -980,7 +1070,7 @@ class User {
 
                $found = false;
                $host = '';
-
+               // FIXME: IPv6 ???
                $m = array();
                if ( preg_match( '/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $m ) ) {
                        # Make hostname
@@ -1048,13 +1138,14 @@ class User {
                $keys = array();
                $id = $this->getId();
                $ip = wfGetIP();
+               $userLimit = false;
 
                if( isset( $limits['anon'] ) && $id == 0 ) {
                        $keys[wfMemcKey( 'limiter', $action, 'anon' )] = $limits['anon'];
                }
 
                if( isset( $limits['user'] ) && $id != 0 ) {
-                       $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['user'];
+                       $userLimit = $limits['user'];
                }
                if( $this->isNewbie() ) {
                        if( isset( $limits['newbie'] ) && $id != 0 ) {
@@ -1069,6 +1160,20 @@ class User {
                                $keys["mediawiki:limiter:$action:subnet:$subnet"] = $limits['subnet'];
                        }
                }
+               // Check for group-specific permissions
+               // If more than one group applies, use the group with the highest limit
+               foreach ( $this->getGroups() as $group ) {
+                       if ( isset( $limits[$group] ) ) {
+                               if ( $userLimit === false || $limits[$group] > $userLimit ) {
+                                       $userLimit = $limits[$group];
+                               }
+                       }
+               }
+               // Set the user limit key
+               if ( $userLimit !== false ) {
+                       wfDebug( __METHOD__.": effective user limit: $userLimit\n" );
+                       $keys[ wfMemcKey( 'limiter', $action, 'user', $id ) ] = $userLimit;
+               }
 
                $triggered = false;
                foreach( $keys as $key => $limit ) {
@@ -1147,7 +1252,7 @@ class User {
        /**
         * Get the user ID. Returns 0 if the user is anonymous or nonexistent.
         */
-       function getID() {
+       function getId() {
                if( $this->mId === null and $this->mName !== null
                and User::isIP( $this->mName ) ) {
                        // Special case, we know the user is anonymous
@@ -1161,10 +1266,8 @@ class User {
 
        /**
         * Set the user and reload all fields according to that ID
-        * @deprecated use User::newFromId()
         */
-       function setID( $v ) {
-               trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+       function setId( $v ) {
                $this->mId = $v;
                $this->clearInstanceCache( 'id' );
        }
@@ -1187,12 +1290,12 @@ class User {
        }
 
        /**
-        * Set the user name. 
+        * Set the user name.
         *
-        * This does not reload fields from the database according to the given 
+        * This does not reload fields from the database according to the given
         * name. Rather, it is used to create a temporary "nonexistent user" for
-        * later addition to the database. It can also be used to set the IP 
-        * address for an anonymous user to something other than the current 
+        * later addition to the database. It can also be used to set the IP
+        * address for an anonymous user to something other than the current
         * remote IP.
         *
         * User::newFromName() has rougly the same function, when the named user
@@ -1256,11 +1359,11 @@ class User {
                return array(array("wiki" => wfWikiID(), "link" => $utp->getLocalURL()));
        }
 
-               
+
        /**
-        * Perform a user_newtalk check, uncached. 
+        * Perform a user_newtalk check, uncached.
         * Use getNewtalk for a cached check.
-        * 
+        *
         * @param string $field
         * @param mixed $id
         * @param bool $fromMaster True to fetch from the master, false for a slave
@@ -1356,7 +1459,7 @@ class User {
                        $this->invalidateCache();
                }
        }
-       
+
        /**
         * Generate a current or new-future timestamp to be stored in the
         * user_touched field when we update things.
@@ -1365,7 +1468,7 @@ class User {
                global $wgClockSkewFudge;
                return wfTimestamp( TS_MW, time() + $wgClockSkewFudge );
        }
-       
+
        /**
         * Clear user data from memcached.
         * Use after applying fun updates to the database; caller's
@@ -1389,13 +1492,13 @@ class User {
                $this->load();
                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->clearSharedCache();
                }
        }
@@ -1433,12 +1536,12 @@ class User {
         */
        function setPassword( $str ) {
                global $wgAuth;
-               
+
                if( $str !== null ) {
                        if( !$wgAuth->allowPasswordChange() ) {
                                throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
                        }
-               
+
                        if( !$this->isValidPassword( $str ) ) {
                                global $wgMinimalPasswordLength;
                                throw new PasswordError( wfMsg( 'passwordtooshort',
@@ -1449,7 +1552,7 @@ class User {
                if( !$wgAuth->setPassword( $this, $str ) ) {
                        throw new PasswordError( wfMsg( 'externaldberror' ) );
                }
-               
+
                $this->setInternalPassword( $str );
 
                return true;
@@ -1464,7 +1567,7 @@ class User {
        function setInternalPassword( $str ) {
                $this->load();
                $this->setToken();
-               
+
                if( $str === null ) {
                        // Save an invalid hash...
                        $this->mPassword = '';
@@ -1526,20 +1629,23 @@ class User {
                $expiry = wfTimestamp( TS_UNIX, $this->mNewpassTime ) + $wgPasswordReminderResendTime * 3600;
                return time() < $expiry;
        }
-       
+
        function getEmail() {
                $this->load();
+               wfRunHooks( 'UserGetEmail', array( $this, &$this->mEmail ) );
                return $this->mEmail;
        }
 
        function getEmailAuthenticationTimestamp() {
                $this->load();
+               wfRunHooks( 'UserGetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
                return $this->mEmailAuthenticated;
        }
 
        function setEmail( $str ) {
                $this->load();
                $this->mEmail = $str;
+               wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
        }
 
        function getRealName() {
@@ -1575,7 +1681,7 @@ class User {
        }
 
        /**
-        * Get the user's date preference, including some important migration for 
+        * Get the user's date preference, including some important migration for
         * old user rows.
         */
        function getDatePreference() {
@@ -1598,7 +1704,7 @@ class User {
        function getBoolOption( $oname ) {
                return (bool)$this->getOption( $oname );
        }
-       
+
        /**
         * Get an option as an integer value from the source string.
         * @param string $oname The option to check
@@ -1624,9 +1730,16 @@ class User {
                }
                // Filter out any newlines that may have passed through input validation.
                // Newlines are used to separate items in the options blob.
-               $val = str_replace( "\r\n", "\n", $val );
-               $val = str_replace( "\r", "\n", $val );
-               $val = str_replace( "\n", " ", $val );
+               if( $val ) {
+                       $val = str_replace( "\r\n", "\n", $val );
+                       $val = str_replace( "\r", "\n", $val );
+                       $val = str_replace( "\n", " ", $val );
+               }
+               // Explicitly NULL values should refer to defaults
+               global $wgDefaultUserOptions;
+               if( is_null($val) && isset($wgDefaultUserOptions[$oname]) ) {
+                       $val = $wgDefaultUserOptions[$oname];
+               }
                $this->mOptions[$oname] = $val;
        }
 
@@ -1634,6 +1747,8 @@ class User {
                if ( is_null( $this->mRights ) ) {
                        $this->mRights = self::getGroupPermissions( $this->getEffectiveGroups() );
                        wfRunHooks( 'UserGetRights', array( $this, &$this->mRights ) );
+                       // Force reindexation of rights when a hook has unset one of them
+                       $this->mRights = array_values( $this->mRights );
                }
                return $this->mRights;
        }
@@ -1657,10 +1772,9 @@ class User {
         */
        function getEffectiveGroups( $recache = false ) {
                if ( $recache || is_null( $this->mEffectiveGroups ) ) {
-                       $this->load();
-                       $this->mEffectiveGroups = $this->mGroups;
+                       $this->mEffectiveGroups = $this->getGroups();
                        $this->mEffectiveGroups[] = '*';
-                       if( $this->mId ) {
+                       if( $this->getId() ) {
                                $this->mEffectiveGroups[] = 'user';
 
                                $this->mEffectiveGroups = array_unique( array_merge(
@@ -1674,28 +1788,27 @@ class User {
                }
                return $this->mEffectiveGroups;
        }
-       
+
        /* Return the edit count for the user. This is where User::edits should have been */
        function getEditCount() {
                if ($this->mId) {
                        if ( !isset( $this->mEditCount ) ) {
                                /* Populate the count, if it has not been populated yet */
                                $this->mEditCount = User::edits($this->mId);
-                       } 
+                       }
                        return $this->mEditCount;
                } else {
                        /* nil */
                        return null;
                }
        }
-       
+
        /**
         * Add the user to the given group.
         * This takes immediate effect.
         * @param string $group
         */
        function addGroup( $group ) {
-               $this->load();
                $dbw = wfGetDB( DB_MASTER );
                if( $this->getId() ) {
                        $dbw->insert( 'user_groups',
@@ -1707,6 +1820,7 @@ class User {
                                array( 'IGNORE' ) );
                }
 
+               $this->loadGroups();
                $this->mGroups[] = $group;
                $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
 
@@ -1728,6 +1842,7 @@ class User {
                        ),
                        'User::removeGroup' );
 
+               $this->loadGroups();
                $this->mGroups = array_diff( $this->mGroups, array( $group ) );
                $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
 
@@ -1760,7 +1875,7 @@ class User {
         * @deprecated
         */
        function isBot() {
-               trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+               wfDeprecated( __METHOD__ );
                return $this->isAllowed( 'bot' );
        }
 
@@ -1777,6 +1892,24 @@ class User {
                return in_array( $action, $this->getRights() );
        }
 
+       /**
+       * Check whether to enable recent changes patrol features for this user
+       * @return bool
+       */
+       public function useRCPatrol() {
+               global $wgUseRCPatrol;
+               return( $wgUseRCPatrol && ($this->isAllowed('patrol') || $this->isAllowed('patrolmarks')) );
+       }
+
+       /**
+       * Check whether to enable recent changes patrol features for this user
+       * @return bool
+       */
+       public function useNPPatrol() {
+               global $wgUseRCPatrol, $wgUseNPPatrol;
+               return( ($wgUseRCPatrol || $wgUseNPPatrol) && ($this->isAllowed('patrol') || $this->isAllowed('patrolmarks')) );
+       }
+
        /**
         * Load a skin if it doesn't exist or return it
         * @todo FIXME : need to check the old failback system [AV]
@@ -1833,7 +1966,7 @@ class User {
         * the next change of the page if it's watched etc.
         */
        function clearNotification( &$title ) {
-               global $wgUser, $wgUseEnotif;
+               global $wgUser, $wgUseEnotif, $wgShowUpdatedMarker;
 
                # Do nothing if the database is locked to writes
                if( wfReadOnly() ) {
@@ -1847,7 +1980,7 @@ class User {
                        $this->setNewtalk( false );
                }
 
-               if( !$wgUseEnotif ) {
+               if( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
                        return;
                }
 
@@ -1864,7 +1997,7 @@ class User {
                        $title->getText() == $wgUser->getName())
                {
                        $watched = true;
-               } elseif ( $this->getID() == $wgUser->getID() ) {
+               } elseif ( $this->getId() == $wgUser->getId() ) {
                        $watched = $title->userIsWatching();
                } else {
                        $watched = true;
@@ -1897,13 +2030,12 @@ class User {
         * @public
         */
        function clearAllNotifications( $currentUser ) {
-               global $wgUseEnotif;
-               if ( !$wgUseEnotif ) {
+               global $wgUseEnotif, $wgShowUpdatedMarker;
+               if ( !$wgUseEnotif && !$wgShowUpdatedMarker ) {
                        $this->setNewtalk( false );
                        return;
                }
                if( $currentUser != 0 )  {
-
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                array( /* SET */
@@ -1912,8 +2044,7 @@ class User {
                                        'wl_user' => $currentUser
                                ), __METHOD__
                        );
-
-               #       we also need to clear here the "you have new message" notification for the own user_talk page
+               #       We also need to clear here the "you have new message" notification for the own user_talk page
                #       This is cleared one page view later in Article::viewUpdates();
                }
        }
@@ -1948,24 +2079,63 @@ class User {
                        }
                }
        }
+       
+       protected function setCookie( $name, $value, $exp=0 ) {
+               global $wgCookiePrefix,$wgCookieDomain,$wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
+               if( $exp == 0 ) {
+                       $exp = time() + $wgCookieExpiration;
+               }
+               $httpOnlySafe = wfHttpOnlySafe();
+               wfDebugLog( 'cookie',
+                       'setcookie: "' . implode( '", "',
+                               array(
+                                       $wgCookiePrefix . $name,
+                                       $value,
+                                       $exp,
+                                       '/',
+                                       $wgCookieDomain,
+                                       $wgCookieSecure,
+                                       $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
+               if( $httpOnlySafe && isset( $wgCookieHttpOnly ) ) {
+                       setcookie( $wgCookiePrefix . $name,
+                               $value,
+                               $exp,
+                               '/',
+                               $wgCookieDomain,
+                               $wgCookieSecure,
+                               $wgCookieHttpOnly );
+               } else {
+                       // setcookie() fails on PHP 5.1 if you give it future-compat paramters.
+                       // stab stab!
+                       setcookie( $wgCookiePrefix . $name,
+                               $value,
+                               $exp,
+                               '/',
+                               $wgCookieDomain,
+                               $wgCookieSecure );
+               }
+       }
+       
+       protected function clearCookie( $name ) {
+               $this->setCookie( $name, '', time() - 86400 );
+       }
 
        function setCookies() {
-               global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
                $this->load();
                if ( 0 == $this->mId ) return;
-               $exp = time() + $wgCookieExpiration;
-
+               
                $_SESSION['wsUserID'] = $this->mId;
-               setcookie( $wgCookiePrefix.'UserID', $this->mId, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               
+               $this->setCookie( 'UserID', $this->mId );
+               $this->setCookie( 'UserName', $this->getName() );
 
                $_SESSION['wsUserName'] = $this->getName();
-               setcookie( $wgCookiePrefix.'UserName', $this->getName(), $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
 
                $_SESSION['wsToken'] = $this->mToken;
                if ( 1 == $this->getOption( 'rememberpassword' ) ) {
-                       setcookie( $wgCookiePrefix.'Token', $this->mToken, $exp, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+                       $this->setCookie( 'Token', $this->mToken );
                } else {
-                       setcookie( $wgCookiePrefix.'Token', '', time() - 3600 );
+                       $this->clearCookie( 'Token' );
                }
        }
 
@@ -1976,7 +2146,6 @@ class User {
                global $wgUser;
                if( wfRunHooks( 'UserLogout', array(&$this) ) ) {
                        $this->doLogout();
-                       wfRunHooks( 'UserLogoutComplete', array(&$wgUser) );
                }
        }
 
@@ -1985,16 +2154,15 @@ class User {
         * Clears the cookies and session, resets the instance cache
         */
        function doLogout() {
-               global $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookiePrefix;
                $this->clearInstanceCache( 'defaults' );
 
                $_SESSION['wsUserID'] = 0;
 
-               setcookie( $wgCookiePrefix.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
-               setcookie( $wgCookiePrefix.'Token', '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               $this->clearCookie( 'UserID' );
+               $this->clearCookie( 'Token' );
 
                # Remember when user logged out, to prevent seeing cached pages
-               setcookie( $wgCookiePrefix.'LoggedOut', wfTimestampNow(), time() + 86400, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+               $this->setCookie( 'LoggedOut', wfTimestampNow(), time() + 86400 );
        }
 
        /**
@@ -2005,7 +2173,7 @@ class User {
                $this->load();
                if ( wfReadOnly() ) { return; }
                if ( 0 == $this->mId ) { return; }
-               
+
                $this->mTouched = self::newTouchedTimestamp();
 
                $dbw = wfGetDB( DB_MASTER );
@@ -2020,15 +2188,17 @@ class User {
                                'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ),
                                'user_options' => $this->encodeOptions(),
                                'user_touched' => $dbw->timestamp($this->mTouched),
-                               'user_token' => $this->mToken
+                               'user_token' => $this->mToken,
+                               'user_email_token' => $this->mEmailToken,
+                               'user_email_token_expires' => $dbw->timestampOrNull( $this->mEmailTokenExpires ),
                        ), array( /* WHERE */
                                'user_id' => $this->mId
                        ), __METHOD__
                );
+               wfRunHooks( 'UserSaveSettings', array( $this ) );
                $this->clearSharedCache();
        }
 
-
        /**
         * Checks if a user with the given name exists, returns the ID.
         */
@@ -2094,7 +2264,7 @@ class User {
                }
                return $newUser;
        }
-       
+
        /**
         * Add an existing user object to the database
         */
@@ -2212,7 +2382,7 @@ class User {
         * @deprecated
         */
        function setLoaded( $loaded ) {
-               trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+               wfDeprecated( __METHOD__ );
        }
 
        /**
@@ -2299,7 +2469,7 @@ class User {
                }
                return false;
        }
-       
+
        /**
         * Check if the given clear-text password matches the temporary password
         * sent by e-mail for password reset operations.
@@ -2376,21 +2546,29 @@ class User {
        }
 
        /**
-        * Generate a new e-mail confirmation token and send a confirmation
+        * Generate a new e-mail confirmation token and send a confirmation/invalidation
         * mail to the user's given address.
         *
+        * Calls saveSettings() internally; as it has side effects, not committing changes
+        * would be pretty silly.
+        *
         * @return mixed True on success, a WikiError object on failure.
         */
        function sendConfirmationMail() {
-               global $wgContLang;
+               global $wgLang;
                $expiration = null; // gets passed-by-ref and defined in next line.
-               $url = $this->confirmationTokenUrl( $expiration );
+               $token = $this->confirmationToken( $expiration );
+               $url = $this->confirmationTokenUrl( $token );
+               $invalidateURL = $this->invalidationTokenUrl( $token );
+               $this->saveSettings();
+               
                return $this->sendMail( wfMsg( 'confirmemail_subject' ),
                        wfMsg( 'confirmemail_body',
                                wfGetIP(),
                                $this->getName(),
                                $url,
-                               $wgContLang->timeanddate( $expiration, false ) ) );
+                               $wgLang->timeanddate( $expiration, false ),
+                               $invalidateURL ) );
        }
 
        /**
@@ -2416,6 +2594,10 @@ class User {
        /**
         * Generate, store, and return a new e-mail confirmation code.
         * A hash (unsalted since it's used as a key) is stored.
+        *
+        * Call saveSettings() after calling this function to commit
+        * this change to the database.
+        *
         * @param &$expiration mixed output: accepts the expiration time
         * @return string
         * @private
@@ -2424,43 +2606,65 @@ class User {
                $now = time();
                $expires = $now + 7 * 24 * 60 * 60;
                $expiration = wfTimestamp( TS_MW, $expires );
-
                $token = $this->generateToken( $this->mId . $this->mEmail . $expires );
                $hash = md5( $token );
-
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->update( 'user',
-                       array( 'user_email_token'         => $hash,
-                              'user_email_token_expires' => $dbw->timestamp( $expires ) ),
-                       array( 'user_id'                  => $this->mId ),
-                       __METHOD__ );
-
+               $this->load();
+               $this->mEmailToken = $hash;
+               $this->mEmailTokenExpires = $expiration;
                return $token;
        }
 
        /**
-        * Generate and store a new e-mail confirmation token, and return
-        * the URL the user can use to confirm.
-        * @param &$expiration mixed output: accepts the expiration time
+       * Return a URL the user can use to confirm their email address.
+        * @param $token: accepts the email confirmation token
         * @return string
         * @private
         */
-       function confirmationTokenUrl( &$expiration ) {
-               $token = $this->confirmationToken( $expiration );
+       function confirmationTokenUrl( $token ) {
                $title = SpecialPage::getTitleFor( 'Confirmemail', $token );
                return $title->getFullUrl();
        }
+       /**
+        * Return a URL the user can use to invalidate their email address.
+        * @param $token: accepts the email confirmation token
+        * @return string
+        * @private
+        */
+        function invalidationTokenUrl( $token ) {
+               $title = SpecialPage::getTitleFor( 'Invalidateemail', $token );
+               return $title->getFullUrl();
+       }
 
        /**
-        * Mark the e-mail address confirmed and save.
+        * Mark the e-mail address confirmed.
+        *
+        * Call saveSettings() after calling this function to commit the change.
         */
        function confirmEmail() {
+               $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
+               return true;
+       }
+
+       /**
+        * Invalidate the user's email confirmation, unauthenticate the email
+        * if it was already confirmed.
+        *
+        * Call saveSettings() after calling this function to commit the change.
+        */
+       function invalidateEmail() {
                $this->load();
-               $this->mEmailAuthenticated = wfTimestampNow();
-               $this->saveSettings();
+               $this->mEmailToken = null;
+               $this->mEmailTokenExpires = null;
+               $this->setEmailAuthenticationTimestamp( null );
                return true;
        }
 
+       function setEmailAuthenticationTimestamp( $timestamp ) {
+               $this->load();
+               $this->mEmailAuthenticated = $timestamp;
+               wfRunHooks( 'UserSetEmailAuthenticationTimestamp', array( $this, &$this->mEmailAuthenticated ) );
+       }
+
        /**
         * Is this user allowed to send e-mails within limits of current
         * site configuration?
@@ -2507,7 +2711,7 @@ class User {
                        return $confirmed;
                }
        }
-       
+
        /**
         * Return true if there is an outstanding request for e-mail confirmation.
         * @return bool
@@ -2519,7 +2723,7 @@ class User {
                        $this->mEmailToken &&
                        $this->mEmailTokenExpires > wfTimestamp();
        }
-       
+
        /**
         * Get the timestamp of account creation, or false for
         * non-existent/anonymous user accounts
@@ -2535,7 +2739,6 @@ class User {
        /**
         * @param array $groups list of groups
         * @return array list of permission key names for given groups combined
-        * @static
         */
        static function getGroupPermissions( $groups ) {
                global $wgGroupPermissions;
@@ -2552,7 +2755,6 @@ class User {
        /**
         * @param string $group key name
         * @return string localized descriptive name for group, if provided
-        * @static
         */
        static function getGroupName( $group ) {
                global $wgMessageCache;
@@ -2567,7 +2769,6 @@ class User {
        /**
         * @param string $group key name
         * @return string localized descriptive name for member of a group, if provided
-        * @static
         */
        static function getGroupMember( $group ) {
                global $wgMessageCache;
@@ -2581,11 +2782,10 @@ class User {
 
        /**
         * Return the set of defined explicit groups.
-        * The *, 'user', 'autoconfirmed' and 'emailconfirmed'
-        * groups are not included, as they are defined
-        * automatically, not in the database.
+        * The implicit groups (by default *, 'user' and 'autoconfirmed')
+        * are not included, as they are defined automatically,
+        * not in the database.
         * @return array
-        * @static
         */
        static function getAllGroups() {
                global $wgGroupPermissions;
@@ -2595,6 +2795,22 @@ class User {
                );
        }
 
+       /**
+        * Get a list of all available permissions
+        */
+       static function getAllRights() {
+               if ( self::$mAllRights === false ) {
+                       global $wgAvailableRights;
+                       if ( count( $wgAvailableRights ) ) {
+                               self::$mAllRights = array_unique( array_merge( self::$mCoreRights, $wgAvailableRights ) );
+                       } else {
+                               self::$mAllRights = self::$mCoreRights;
+                       }
+                       wfRunHooks( 'UserGetAllRights', array( &self::$mAllRights ) );
+               }
+               return self::$mAllRights;
+       }
+
        /**
         * Get a list of implicit groups
         *
@@ -2665,7 +2881,7 @@ class User {
                        return $text;
                }
        }
-       
+
        /**
         * Increment the user's edit-count field.
         * Will have no effect for anonymous users.
@@ -2677,7 +2893,7 @@ class User {
                                array( 'user_editcount=user_editcount+1' ),
                                array( 'user_id' => $this->getId() ),
                                __METHOD__ );
-                       
+
                        // Lazy initialization check...
                        if( $dbw->affectedRows() == 0 ) {
                                // Pull from a slave to be less cruel to servers
@@ -2687,7 +2903,7 @@ class User {
                                        'COUNT(rev_user)',
                                        array( 'rev_user' => $this->getId() ),
                                        __METHOD__ );
-                               
+
                                // Now here's a goddamn hack...
                                if( $dbr !== $dbw ) {
                                        // If we actually have a slave server, the count is
@@ -2699,7 +2915,7 @@ class User {
                                        // count we just read includes the revision that was
                                        // just added in the working transaction.
                                }
-                               
+
                                $dbw->update( 'user',
                                        array( 'user_editcount' => $count ),
                                        array( 'user_id' => $this->getId() ),
@@ -2709,7 +2925,14 @@ class User {
                // edit count in user cache too
                $this->invalidateCache();
        }
+       
+       static function getRightDescription( $right ) {
+               global $wgMessageCache;
+               $wgMessageCache->loadAllMessages();
+               $key = "right-$right";
+               $name = wfMsg( $key );
+               return $name == '' || wfEmptyMsg( $key, $name )
+                       ? $right
+                       : $name;
+       }
 }
-
-
-