* Throw a showstopper exception when a hook function fails to return a value.
[lhc/web/wiklou.git] / includes / User.php
index 53e651b..9b62e2d 100644 (file)
@@ -2,14 +2,13 @@
 /**
  * See user.txt
  *
- * @package MediaWiki
  */
 
 # Number of characters in user_token field
 define( 'USER_TOKEN_LENGTH', 32 );
 
 # Serialized record version
-define( 'MW_USER_VERSION', 4 );
+define( 'MW_USER_VERSION', 5 );
 
 # Some punctuation to prevent editing from broken text-mangling proxies.
 # FIXME: this is embedded unescaped into HTML attributes in various
@@ -18,14 +17,21 @@ define( 'EDIT_TOKEN_SUFFIX', '\\' );
 
 /**
  * Thrown by User::setPassword() on error
+ * @addtogroup Exception
  */
 class PasswordError extends MWException {
        // NOP
 }
 
 /**
- *
- * @package MediaWiki
+ * The User object encapsulates all of the user-specific settings (user_id,
+ * name, rights, password, email address, options, last login time). Client
+ * classes use the getXXX() functions to access these fields. These functions
+ * do all the work of determining whether the user is logged in,
+ * whether the requested option can be satisfied from cookies or
+ * whether a database query is needed. Most of the settings needed
+ * for rendering normal pages are set in the cookie to minimize use
+ * of the database.
  */
 class User {
 
@@ -50,6 +56,8 @@ class User {
                'editwidth',
                'watchcreations',
                'watchdefault',
+               'watchmoves',
+               'watchdeletion',
                'minordefault',
                'previewontop',
                'previewonfirst',
@@ -64,11 +72,12 @@ class User {
                'externaldiff',
                'showjumplinks',
                'uselivepreview',
-               'autopatrol',
                'forceeditsummary',
                'watchlisthideown',
                'watchlisthidebots',
+               'watchlisthideminor',
                'ccmeonemails',
+               'diffonly',
        );
 
        /**
@@ -92,7 +101,7 @@ class User {
                'mEmailToken',
                'mEmailTokenExpires',
                'mRegistration',
-               
+               'mEditCount',
                # user_group table
                'mGroups',
        );
@@ -186,7 +195,6 @@ class User {
                # Try cache
                $key = wfMemcKey( 'user', 'id', $this->mId );
                $data = $wgMemc->get( $key );
-               
                if ( !is_array( $data ) || $data['mVersion'] < MW_USER_VERSION ) {
                        # Object is expired, load from DB
                        $data = false;
@@ -268,7 +276,7 @@ class User {
         * @static
         */
        static function newFromConfirmationCode( $code ) {
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $id = $dbr->selectField( 'user', 'user_id', array(
                        'user_email_token' => md5( $code ),
                        'user_email_token_expires > ' . $dbr->addQuotes( $dbr->timestamp() ),
@@ -300,7 +308,7 @@ class User {
         * @static
         */
        static function whoIs( $id ) {
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), 'User::whoIs' );
        }
 
@@ -311,7 +319,7 @@ class User {
         * @static
         */
        static function whoIsReal( $id ) {
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                return $dbr->selectField( 'user', 'user_real_name', array( 'user_id' => $id ), 'User::whoIsReal' );
        }
 
@@ -327,7 +335,7 @@ class User {
                        # Illegal name
                        return null;
                }
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), __METHOD__ );
 
                if ( $s === false ) {
@@ -350,14 +358,12 @@ class User {
         * addresses like this, if we allowed accounts like this to be created
         * new users could get the old edits of these anonymous users.
         *
-        * @bug 3631
-        *
         * @static
         * @param string $name Nickname of a user
         * @return bool
         */
        static function isIP( $name ) {
-               return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name);
+               return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name) || User::isIPv6($name);
                /*return preg_match("/^
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
@@ -366,6 +372,27 @@ class User {
                $/x", $name);*/
        }
 
+       /**
+        * 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  
+                * address.
+                */
+               if (preg_match("/[^:a-fA-F0-9]/", $name))
+                       return false;
+
+               $parts = explode(":", $name);
+               if (count($parts) < 3)
+                       return false;
+               foreach ($parts as $part) {
+                       if (!preg_match("/^[0-9a-fA-F]{0,4}$/", $part))
+                               return false;
+               }
+               return true;
+       }
+
        /**
         * Is the input a valid username?
         *
@@ -460,11 +487,15 @@ class User {
         *
         * @param string $password
         * @return bool
-        * @static
         */
-       static function isValidPassword( $password ) {
-               global $wgMinimalPasswordLength;
-               return strlen( $password ) >= $wgMinimalPasswordLength;
+       function isValidPassword( $password ) {
+               global $wgMinimalPasswordLength, $wgContLang;
+
+               $result = null;
+               if( !wfRunHooks( 'isValidPassword', array( $password, &$result ) ) ) return $result;
+               if ($result === false) return false;
+               return (strlen( $password ) >= $wgMinimalPasswordLength) &&
+                       ($wgContLang->lc( $password ) !== $wgContLang->lc( $this->mName ));
        }
 
        /**
@@ -474,8 +505,7 @@ class User {
         * rejected valid addresses. Actually just check if there is '@' somewhere
         * in the given address.
         *
-        * @todo Check for RFC 2822 compilance
-        * @bug 959
+        * @todo Check for RFC 2822 compilance (bug 959)
         *
         * @param string $addr email address
         * @static
@@ -501,6 +531,12 @@ class User {
                global $wgContLang;
                $name = $wgContLang->ucfirst( $name );
 
+               # Reject names containing '#'; these will be cleaned up
+               # with title normalisation, but then it's too late to
+               # check elsewhere
+               if( strpos( $name, '#' ) !== false )
+                       return false;
+
                # Clean up name according to title rules
                $t = Title::newFromText( $name );
                if( is_null( $t ) ) {
@@ -539,22 +575,45 @@ 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
         */
        static function edits( $uid ) {
-               $dbr =& wfGetDB( DB_SLAVE );
-               return $dbr->selectField(
-                       'revision', 'count(*)',
-                       array( 'rev_user' => $uid ),
+               wfProfileIn( __METHOD__ );
+               $dbr = wfGetDB( DB_SLAVE );
+               // check if the user_editcount field has been initialized
+               $field = $dbr->selectField(
+                       'user', 'user_editcount',
+                       array( 'user_id' => $uid ),
                        __METHOD__
                );
+
+               if( $field === null ) { // it has not been initialized. do so.
+                       $dbw = wfGetDb( DB_MASTER );
+                       $count = $dbr->selectField(
+                               'revision', 'count(*)',
+                               array( 'rev_user' => $uid ),
+                               __METHOD__
+                       );
+                       $dbw->update(
+                               'user',
+                               array( 'user_editcount' => $count ),
+                               array( 'user_id' => $uid ),
+                               __METHOD__
+                       );
+               } else {
+                       $count = $field;
+               }
+               wfProfileOut( __METHOD__ );
+               return $count;
        }
 
        /**
         * Return a random password. Sourced from mt_rand, so it's not particularly secure.
-        * @todo: hash random numbers to improve security, like generateToken()
+        * @todo hash random numbers to improve security, like generateToken()
         *
         * @return string
         * @static
@@ -620,10 +679,8 @@ class User {
         * Load user data from the session or login cookie. If there are no valid
         * credentials, initialises the user as an anon.
         * @return true if the user is logged in, false otherwise
-        * 
-        * @private
         */
-       function loadFromSession() {
+       private function loadFromSession() {
                global $wgMemc, $wgCookiePrefix;
 
                if ( isset( $_SESSION['wsUserID'] ) ) {
@@ -697,7 +754,7 @@ class User {
                        return false;
                }
 
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_MASTER );
                $s = $dbr->selectRow( 'user', '*', array( 'user_id' => $this->mId ), __METHOD__ );
 
                if ( $s !== false ) {
@@ -715,6 +772,8 @@ class User {
                        $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->getEditCount(); // revalidation for nulls
 
                        # Load group data
                        $res = $dbr->select( 'user_groups',
@@ -758,12 +817,12 @@ class User {
        /**
         * Combine the language default options with any site-specific options
         * and add the default language variants.
-        *
+        * Not really private cause it's called by Language class
         * @return array
         * @static
         * @private
         */
-       function getDefaultOptions() {
+       static function getDefaultOptions() {
                global $wgNamespacesToBeSearchedDefault;
                /**
                 * Site defaults will override the global/language defaults
@@ -831,9 +890,15 @@ class User {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": checking...\n" );
 
-               $this->mBlockedby = 0;
+               $this->mBlockedby = 0; 
+               $this->mHideName = 0;
                $ip = wfGetIP();
 
+               if ($this->isAllowed( 'ipblock-exempt' ) ) {
+                       # Exempt from all types of IP-block
+                       $ip = '';
+               }
+
                # User/IP blocking
                $this->mBlock = new Block();
                $this->mBlock->fromMaster( !$bFromSlave );
@@ -841,6 +906,7 @@ class User {
                        wfDebug( __METHOD__.": Found block.\n" );
                        $this->mBlockedby = $this->mBlock->mBy;
                        $this->mBlockreason = $this->mBlock->mReason;
+                       $this->mHideName = $this->mBlock->mHideName;
                        if ( $this->isLoggedIn() ) {
                                $this->spreadBlock();
                        }
@@ -909,6 +975,16 @@ class User {
                return $found;
        }
 
+       /**
+        * Is this user subject to rate limiting?
+        *
+        * @return bool
+        */
+       public function isPingLimitable() {
+               global $wgRateLimitsExcludedGroups;
+               return array_intersect($this->getEffectiveGroups(), $wgRateLimitsExcludedGroups) == array();
+       }
+
        /**
         * Primitive rate limits: enforce maximum actions per time period
         * to put a brake on flooding.
@@ -920,17 +996,22 @@ class User {
         * @public
         */
        function pingLimiter( $action='edit' ) {
+
+               # Call the 'PingLimiter' hook
+               $result = false;
+               if( !wfRunHooks( 'PingLimiter', array( &$this, $action, $result ) ) ) {
+                       return $result;
+               }
+
                global $wgRateLimits, $wgRateLimitsExcludedGroups;
                if( !isset( $wgRateLimits[$action] ) ) {
                        return false;
                }
-               
+
                # Some groups shouldn't trigger the ping limiter, ever
-               foreach( $this->getGroups() as $group ) {
-                       if( array_search( $group, $wgRateLimitsExcludedGroups ) !== false )
-                               return false;
-               }
-               
+               if( !$this->isPingLimitable() )
+                       return false;
+
                global $wgMemc, $wgRateLimitLog;
                wfProfileIn( __METHOD__ );
 
@@ -1004,14 +1085,13 @@ class User {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": enter\n" );
 
-               if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
-                 $title->getNamespace() == NS_USER_TALK )
-               {
+               wfDebug( __METHOD__.": asking isBlocked()\n" );
+               $blocked = $this->isBlocked( $bFromSlave );
+               # If a user's name is suppressed, they cannot make edits anywhere
+               if ( !$this->mHideName && $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
+                 $title->getNamespace() == NS_USER_TALK ) {
                        $blocked = false;
                        wfDebug( __METHOD__.": self-talk page, ignoring any blocks\n" );
-               } else {
-                       wfDebug( __METHOD__.": asking isBlocked()\n" );
-                       $blocked = $this->isBlocked( $bFromSlave );
                }
                wfProfileOut( __METHOD__ );
                return $blocked;
@@ -1062,7 +1142,8 @@ class User {
                } else {
                        $this->load();
                        if ( $this->mName === false ) {
-                               $this->mName = wfGetIP();
+                               # Clean up IPs
+                               $this->mName = IP::sanitizeIP( wfGetIP() );
                        }
                        return $this->mName;
                }
@@ -1107,11 +1188,11 @@ class User {
                                global $wgMemc;
                                $key = wfMemcKey( 'newtalk', 'ip', $this->getName() );
                                $newtalk = $wgMemc->get( $key );
-                               if( is_integer( $newtalk ) ) {
+                               if( $newtalk != "" ) {
                                        $this->mNewtalk = (bool)$newtalk;
                                } else {
                                        $this->mNewtalk = $this->checkNewtalk( 'user_ip', $this->getName() );
-                                       $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
+                                       $wgMemc->set( $key, (int)$this->mNewtalk, time() + 1800 );
                                }
                        } else {
                                $this->mNewtalk = $this->checkNewtalk( 'user_id', $this->mId );
@@ -1148,7 +1229,7 @@ class User {
         * @private
         */
        function checkNewtalk( $field, $id ) {
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $ok = $dbr->selectField( 'user_newtalk', $field,
                        array( $field => $id ), __METHOD__ );
                return $ok !== false;
@@ -1165,7 +1246,7 @@ class User {
                        wfDebug( __METHOD__." already set ($field, $id), ignoring\n" );
                        return false;
                }
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $dbw->insert( 'user_newtalk',
                        array( $field => $id ),
                        __METHOD__,
@@ -1185,7 +1266,7 @@ class User {
                        wfDebug( __METHOD__.": already gone ($field, $id), ignoring\n" );
                        return false;
                }
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'user_newtalk',
                        array( $field => $id ),
                        __METHOD__ );
@@ -1270,7 +1351,7 @@ class User {
                if( $this->mId ) {
                        $this->mTouched = self::newTouchedTimestamp();
                        
-                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'user',
                                array( 'user_touched' => $dbw->timestamp( $this->mTouched ) ),
                                array( 'user_id' => $this->mId ),
@@ -1303,35 +1384,56 @@ class User {
         * pass the change through or if the legal password
         * checks fail.
         *
+        * As a special case, setting the password to null
+        * wipes it, so the account cannot be logged in until
+        * a new password is set, for instance via e-mail.
+        *
         * @param string $str
         * @throws PasswordError on failure
         */
        function setPassword( $str ) {
-               global $wgAuth, $wgMinimalPasswordLength;
+               global $wgAuth;
                
-               if( !$wgAuth->allowPasswordChange() ) {
-                       throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
-               }
+               if( $str !== null ) {
+                       if( !$wgAuth->allowPasswordChange() ) {
+                               throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
+                       }
                
-               if( $wgMinimalPasswordLength &&
-                       strlen( $str ) < $wgMinimalPasswordLength ) {
-                       throw new PasswordError( wfMsg( 'passwordtooshort',
-                               $wgMinimalPasswordLength ) );
+                       if( !$this->isValidPassword( $str ) ) {
+                               global $wgMinimalPasswordLength;
+                               throw new PasswordError( wfMsg( 'passwordtooshort',
+                                       $wgMinimalPasswordLength ) );
+                       }
                }
-               
+
                if( !$wgAuth->setPassword( $this, $str ) ) {
                        throw new PasswordError( wfMsg( 'externaldberror' ) );
                }
                
+               $this->setInternalPassword( $str );
+
+               return true;
+       }
+
+       /**
+        * Set the password and reset the random token no matter
+        * what.
+        *
+        * @param string $str
+        */
+       function setInternalPassword( $str ) {
                $this->load();
                $this->setToken();
-               $this->mPassword = $this->encryptPassword( $str );
-               $this->mNewpassword = '';
-               $this->mNewpassTime = NULL;
                
-               return true;
+               if( $str === null ) {
+                       // Save an invalid hash...
+                       $this->mPassword = '';
+               } else {
+                       $this->mPassword = $this->encryptPassword( $str );
+               }
+               $this->mNewpassword = '';
+               $this->mNewpassTime = null;
        }
-
        /**
         * Set the random token (used for persistent authentication)
         * Called from loadDefaults() among other places.
@@ -1412,17 +1514,23 @@ class User {
 
        /**
         * @param string $oname The option to check
+        * @param string $defaultOverride A default value returned if the option does not exist
         * @return string
         */
-       function getOption( $oname ) {
+       function getOption( $oname, $defaultOverride = '' ) {
                $this->load();
+
                if ( is_null( $this->mOptions ) ) {
+                       if($defaultOverride != '') {
+                               return $defaultOverride;
+                       }
                        $this->mOptions = User::getDefaultOptions();
                }
+
                if ( array_key_exists( $oname, $this->mOptions ) ) {
                        return trim( $this->mOptions[$oname] );
                } else {
-                       return '';
+                       return $defaultOverride;
                }
        }
 
@@ -1514,12 +1622,12 @@ class User {
                        if( $this->mId ) {
                                $this->mEffectiveGroups[] = 'user';
                                
-                               global $wgAutoConfirmAge;
+                               global $wgAutoConfirmAge, $wgAutoConfirmCount;
+
                                $accountAge = time() - wfTimestampOrNull( TS_UNIX, $this->mRegistration );
-                               if( $accountAge >= $wgAutoConfirmAge ) {
+                               if( $accountAge >= $wgAutoConfirmAge && $this->getEditCount() >= $wgAutoConfirmCount ) {
                                        $this->mEffectiveGroups[] = 'autoconfirmed';
                                }
-                               
                                # Implicit group for users whose email addresses are confirmed
                                global $wgEmailAuthentication;
                                if( self::isValidEmailAddr( $this->mEmail ) ) {
@@ -1534,22 +1642,38 @@ 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.
-        * @string $group
+        * @param string $group
         */
        function addGroup( $group ) {
                $this->load();
-               $dbw =& wfGetDB( DB_MASTER );
-               $dbw->insert( 'user_groups',
-                       array(
-                               'ug_user'  => $this->getID(),
-                               'ug_group' => $group,
-                       ),
-                       'User::addGroup',
-                       array( 'IGNORE' ) );
+               $dbw = wfGetDB( DB_MASTER );
+               if( $this->getId() ) {
+                       $dbw->insert( 'user_groups',
+                               array(
+                                       'ug_user'  => $this->getID(),
+                                       'ug_group' => $group,
+                               ),
+                               'User::addGroup',
+                               array( 'IGNORE' ) );
+               }
 
                $this->mGroups[] = $group;
                $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
@@ -1560,11 +1684,11 @@ class User {
        /**
         * Remove the user from the given group.
         * This takes immediate effect.
-        * @string $group
+        * @param string $group
         */
        function removeGroup( $group ) {
                $this->load();
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'user_groups',
                        array(
                                'ug_user'  => $this->getID(),
@@ -1678,6 +1802,11 @@ class User {
        function clearNotification( &$title ) {
                global $wgUser, $wgUseEnotif;
 
+               # Do nothing if the database is locked to writes
+               if( wfReadOnly() ) {
+                       return;
+               }
+
                if ($title->getNamespace() == NS_USER_TALK &&
                        $title->getText() == $this->getName() ) {
                        if (!wfRunHooks('UserClearNewTalkNotification', array(&$this)))
@@ -1711,7 +1840,7 @@ class User {
                // If the page is watched by the user (or may be watched), update the timestamp on any
                // any matching rows
                if ( $watched ) {
-                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                        array( /* SET */
                                                'wl_notificationtimestamp' => NULL
@@ -1742,7 +1871,7 @@ class User {
                }
                if( $currentUser != 0 )  {
 
-                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                array( /* SET */
                                        'wl_notificationtimestamp' => NULL
@@ -1826,7 +1955,7 @@ class User {
 
        /**
         * Save object settings into database
-        * @fixme Only rarely do all these fields need to be set!
+        * @todo Only rarely do all these fields need to be set!
         */
        function saveSettings() {
                $this->load();
@@ -1835,7 +1964,7 @@ class User {
                
                $this->mTouched = self::newTouchedTimestamp();
 
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $dbw->update( 'user',
                        array( /* SET */
                                'user_name' => $this->mName,
@@ -1863,7 +1992,7 @@ class User {
                $s = trim( $this->getName() );
                if ( 0 == strcmp( '', $s ) ) return 0;
 
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), __METHOD__ );
                if ( $id === false ) {
                        $id = 0;
@@ -1894,7 +2023,7 @@ class User {
                        $user->mOptions = $params['options'] + $user->mOptions;
                        unset( $params['options'] );
                }
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
                $fields = array(
                        'user_id' => $seqVal,
@@ -1908,6 +2037,7 @@ class User {
                        'user_options' => $user->encodeOptions(),
                        'user_token' => $user->mToken,
                        'user_registration' => $dbw->timestamp( $user->mRegistration ),
+                       'user_editcount' => 0,
                );
                foreach ( $params as $name => $value ) {
                        $fields["user_$name"] = $value;
@@ -1926,7 +2056,7 @@ class User {
         */
        function addToDatabase() {
                $this->load();
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
                $dbw->insert( 'user',
                        array(
@@ -1941,6 +2071,7 @@ class User {
                                'user_options' => $this->encodeOptions(),
                                'user_token' => $this->mToken,
                                'user_registration' => $dbw->timestamp( $this->mRegistration ),
+                               'user_editcount' => 0,
                        ), __METHOD__
                );
                $this->mId = $dbw->insertId();
@@ -1983,7 +2114,7 @@ class User {
         * @return string
         */
        function getPageRenderingHash() {
-               global $wgContLang, $wgUseDynamicDates;
+               global $wgContLang, $wgUseDynamicDates, $wgLang;
                if( $this->mHash ){
                        return $this->mHash;
                }
@@ -1997,7 +2128,7 @@ class User {
                        $confstr .= '!' . $this->getDatePreference();
                }
                $confstr .= '!' . ($this->getOption( 'numberheadings' ) ? '1' : '');
-               $confstr .= '!' . $this->getOption( 'language' );
+               $confstr .= '!' . $wgLang->getCode();
                $confstr .= '!' . $this->getOption( 'thumbsize' );
                // add in language specific options, if any
                $extra = $wgContLang->getExtraHashOptions();
@@ -2007,6 +2138,8 @@ class User {
                // extra options or other effects on the parser cache.
                wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
 
+               // Make it a valid memcached key fragment
+               $confstr = str_replace( ' ', '_', $confstr );
                $this->mHash = $confstr;
                return $confstr;
        }
@@ -2016,6 +2149,17 @@ class User {
                return $this->mBlock && $this->mBlock->mCreateAccount;
        }
 
+       /**
+        * Determine if the user is blocked from using Special:Emailuser.
+        *
+        * @public
+        * @return boolean
+        */
+       function isBlockedFromEmailuser() {
+               $this->getBlockedStatus();
+               return $this->mBlock && $this->mBlock->mBlockEmail;
+       }
+
        function isAllowedToCreateAccount() {
                return $this->isAllowed( 'createaccount' ) && !$this->isBlockedFromCreateAccount();
        }
@@ -2055,7 +2199,7 @@ class User {
                if ( isset( $res ) )
                        return $res;
                else {
-                       $dbr =& wfGetDB( DB_SLAVE );
+                       $dbr = wfGetDB( DB_SLAVE );
                        return $res = $dbr->selectField( 'user', 'max(user_id)', false, 'User::getMaxID' );
                }
        }
@@ -2075,7 +2219,7 @@ class User {
         * @return bool True if the given password is correct otherwise False.
         */
        function checkPassword( $password ) {
-               global $wgAuth, $wgMinimalPasswordLength;
+               global $wgAuth;
                $this->load();
 
                // Even though we stop people from creating passwords that
@@ -2083,7 +2227,7 @@ class User {
                // to. Certain authentication plugins do NOT want to save
                // domain passwords in a mysql database, so we should
                // check this (incase $wgAuth->strict() is false).
-               if( strlen( $password ) < $wgMinimalPasswordLength ) {
+               if( !$this->isValidPassword( $password ) ) {
                        return false;
                }
 
@@ -2231,7 +2375,7 @@ class User {
                $token = $this->generateToken( $this->mId . $this->mEmail . $expires );
                $hash = md5( $token );
 
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                $dbw->update( 'user',
                        array( 'user_email_token'         => $hash,
                               'user_email_token_expires' => $dbw->timestamp( $expires ) ),
@@ -2308,6 +2452,18 @@ class User {
                        return $confirmed;
                }
        }
+       
+       /**
+        * Return true if there is an outstanding request for e-mail confirmation.
+        * @return bool
+        */
+       function isEmailConfirmationPending() {
+               global $wgEmailAuthentication;
+               return $wgEmailAuthentication &&
+                       !$this->isEmailConfirmed() &&
+                       $this->mEmailToken &&
+                       $this->mEmailTokenExpires > wfTimestamp();
+       }
 
        /**
         * @param array $groups list of groups
@@ -2332,13 +2488,12 @@ class User {
         * @static
         */
        static function getGroupName( $group ) {
+               MessageCache::loadAllMessages();
                $key = "group-$group";
                $name = wfMsg( $key );
-               if( $name == '' || wfEmptyMsg( $key, $name ) ) {
-                       return $group;
-               } else {
-                       return $name;
-               }
+               return $name == '' || wfEmptyMsg( $key, $name )
+                       ? $group
+                       : $name;
        }
 
        /**
@@ -2347,13 +2502,12 @@ class User {
         * @static
         */
        static function getGroupMember( $group ) {
+               MessageCache::loadAllMessages();
                $key = "group-$group-member";
                $name = wfMsg( $key );
-               if( $name == '' || wfEmptyMsg( $key, $name ) ) {
-                       return $group;
-               } else {
-                       return $name;
-               }
+               return $name == '' || wfEmptyMsg( $key, $name )
+                       ? $group
+                       : $name;
        }
 
        /**
@@ -2378,6 +2532,7 @@ class User {
         * @return mixed
         */
        static function getGroupPage( $group ) {
+               MessageCache::loadAllMessages();
                $page = wfMsgForContent( 'grouppage-' . $group );
                if( !wfEmptyMsg( 'grouppage-' . $group, $page ) ) {
                        $title = Title::newFromText( $page );
@@ -2402,7 +2557,7 @@ class User {
                if( $title ) {
                        global $wgUser;
                        $sk = $wgUser->getSkin();
-                       return $sk->makeLinkObj( $title, $text );
+                       return $sk->makeLinkObj( $title, htmlspecialchars( $text ) );
                } else {
                        return $text;
                }
@@ -2427,6 +2582,50 @@ class User {
                        return $text;
                }
        }
+       
+       /**
+        * Increment the user's edit-count field.
+        * Will have no effect for anonymous users.
+        */
+       function incEditCount() {
+               if( !$this->isAnon() ) {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw->update( '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
+                               // Accuracy isn't the point anyway here
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $count = $dbr->selectField( 'revision',
+                                       '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
+                                       // at least one behind because the current transaction
+                                       // has not been committed and replicated.
+                                       $count++;
+                               } else {
+                                       // But if DB_SLAVE is selecting the master, then the
+                                       // 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() ),
+                                       __METHOD__ );
+                       }
+               }
+               // edit count in user cache too
+               $this->invalidateCache();
+       }
 }
 
 ?>