Fix rc_logid in RecentChange::loadFromCurRow
[lhc/web/wiklou.git] / includes / User.php
index f80319d..e210eba 100644 (file)
@@ -455,11 +455,12 @@ class User {
         * will be loaded once more from the database when accessing them.
         *
         * @param $row Array A row from the user table
+        * @param $data Array Further data to load into the object (see User::loadFromRow for valid keys)
         * @return User
         */
-       public static function newFromRow( $row ) {
+       public static function newFromRow( $row, $data = null ) {
                $user = new User;
-               $user->loadFromRow( $row );
+               $user->loadFromRow( $row, $data );
                return $user;
        }
 
@@ -817,39 +818,16 @@ class User {
 
        /**
         * Count the number of edits of a user
-        * @todo It should not be static and some day should be merged as proper member function / deprecated -- domas
         *
         * @param $uid Int User ID to check
         * @return Int the user's edit count
+        *
+        * @deprecated since 1.21 in favour of User::getEditCount
         */
        public static function edits( $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;
+               wfDeprecated( __METHOD__, '1.21' );
+               $user = self::newFromId( $uid );
+               return $user->getEditCount();
        }
 
        /**
@@ -1059,8 +1037,12 @@ class User {
         * Initialize this object from a row from the user table.
         *
         * @param $row Array Row from the user table to load.
+        * @param $data Array Further user data to load into the object
+        *
+        *      user_groups             Array with groups out of the user_groups table
+        *      user_properties         Array with properties out of the user_properties table
         */
-       public function loadFromRow( $row ) {
+       public function loadFromRow( $row, $data = null ) {
                $all = true;
 
                $this->mGroups = null; // deferred
@@ -1118,6 +1100,15 @@ class User {
                if ( $all ) {
                        $this->mLoadedItems = true;
                }
+
+               if ( is_array( $data ) ) {
+                       if ( is_array( $data['user_groups'] ) ) {
+                               $this->mGroups = $data['user_groups'];
+                       }
+                       if ( is_array( $data['user_properties'] ) ) {
+                               $this->loadOptions( $data['user_properties'] );
+                       }
+               }
        }
 
        /**
@@ -1191,7 +1182,9 @@ class User {
        }
 
        /**
-        * Clear various cached data stored in this object.
+        * Clear various cached data stored in this object. The cache of the user table
+        * data (i.e. self::$mCacheVars) is not cleared unless $reloadFrom is given.
+        *
         * @param $reloadFrom bool|String Reload user and user_groups table data from a
         *   given source. May be "name", "id", "defaults", "session", or false for
         *   no reload.
@@ -1205,6 +1198,7 @@ class User {
                $this->mEffectiveGroups = null;
                $this->mImplicitGroups = null;
                $this->mOptions = null;
+               $this->mOptionsLoaded = false;
                $this->mEditCount = null;
 
                if ( $reloadFrom ) {
@@ -2466,7 +2460,21 @@ class User {
                if( $this->getId() ) {
                        if ( !isset( $this->mEditCount ) ) {
                                /* Populate the count, if it has not been populated yet */
-                               $this->mEditCount = User::edits( $this->mId );
+                               wfProfileIn( __METHOD__ );
+                               $dbr = wfGetDB( DB_SLAVE );
+                               // check if the user_editcount field has been initialized
+                               $count = $dbr->selectField(
+                                       'user', 'user_editcount',
+                                       array( 'user_id' => $this->mId ),
+                                       __METHOD__
+                               );
+
+                               if( $count === null ) {
+                                       // it has not been initialized. do so.
+                                       $count = $this->initEditCount();
+                               }
+                               wfProfileOut( __METHOD__ );
+                               $this->mEditCount = $count;
                        }
                        return $this->mEditCount;
                } else {
@@ -3008,8 +3016,8 @@ class User {
        }
 
        /**
-        * Add this existing user object to the database. If the user already 
-        * exists, a fatal status object is returned, and the user object is 
+        * Add this existing user object to the database. If the user already
+        * exists, a fatal status object is returned, and the user object is
         * initialised with the data from the database.
         *
         * Previously, this function generated a DB error due to a key conflict
@@ -3022,12 +3030,12 @@ class User {
         *   }
         *   // do something with $user...
         *
-        * However, this was vulnerable to a race condition (bug 16020). By 
+        * However, this was vulnerable to a race condition (bug 16020). By
         * initialising the user object if the user exists, we aim to support this
         * calling sequence as far as possible.
         *
         * Note that if the user exists, this function will acquire a write lock,
-        * so it is still advisable to make the call conditional on isLoggedIn(), 
+        * so it is still advisable to make the call conditional on isLoggedIn(),
         * and to commit the transaction after calling.
         *
         * @return Status
@@ -3057,7 +3065,7 @@ class User {
                        array( 'IGNORE' )
                );
                if ( !$dbw->affectedRows() ) {
-                       $this->mId = $dbw->selectField( 'user', 'user_id', 
+                       $this->mId = $dbw->selectField( 'user', 'user_id',
                                array( 'user_name' => $this->mName ), __METHOD__ );
                        $loaded = false;
                        if ( $this->mId ) {
@@ -3938,43 +3946,63 @@ class User {
        public function incEditCount() {
                if( !$this->isAnon() ) {
                        $dbw = wfGetDB( DB_MASTER );
-                       $dbw->update( 'user',
+                       $dbw->update(
+                               'user',
                                array( 'user_editcount=user_editcount+1' ),
                                array( 'user_id' => $this->getId() ),
-                               __METHOD__ );
+                               __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...
+                               $dbr = wfGetDB( DB_SLAVE );
                                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++;
+                                       $this->initEditCount( 1 );
                                } 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.
+                                       $this->initEditCount();
                                }
-
-                               $dbw->update( 'user',
-                                       array( 'user_editcount' => $count ),
-                                       array( 'user_id' => $this->getId() ),
-                                       __METHOD__ );
                        }
                }
                // edit count in user cache too
                $this->invalidateCache();
        }
 
+       /**
+        * Initialize user_editcount from data out of the revision table
+        *
+        * @param $add Integer Edits to add to the count from the revision table
+        * @return Integer Number of edits
+        */
+       protected function initEditCount( $add = 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__
+               );
+               $count = $count + $add;
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->update(
+                       'user',
+                       array( 'user_editcount' => $count ),
+                       array( 'user_id' => $this->getId() ),
+                       __METHOD__
+               );
+
+               return $count;
+       }
+
        /**
         * Get the description of a given right
         *
@@ -4114,9 +4142,11 @@ class User {
        }
 
        /**
-        * @todo document
+        * Load the user options either from cache, the database or an array
+        *
+        * @param $data Rows for the current user out of the user_properties table
         */
-       protected function loadOptions() {
+       protected function loadOptions( $data = null ) {
                global $wgContLang;
 
                $this->load();
@@ -4146,21 +4176,27 @@ class User {
                                $this->mOptions[$key] = $value;
                        }
                } else {
-                       wfDebug( "User: loading options for user " . $this->getId() . " from database.\n" );
-                       // Load from database
-                       $dbr = wfGetDB( DB_SLAVE );
-
-                       $res = $dbr->select(
-                               'user_properties',
-                               array( 'up_property', 'up_value' ),
-                               array( 'up_user' => $this->getId() ),
-                               __METHOD__
-                       );
+                       if( !is_array( $data ) ) {
+                               wfDebug( "User: loading options for user " . $this->getId() . " from database.\n" );
+                               // Load from database
+                               $dbr = wfGetDB( DB_SLAVE );
 
-                       $this->mOptionOverrides = array();
-                       foreach ( $res as $row ) {
-                               $this->mOptionOverrides[$row->up_property] = $row->up_value;
-                               $this->mOptions[$row->up_property] = $row->up_value;
+                               $res = $dbr->select(
+                                       'user_properties',
+                                       array( 'up_property', 'up_value' ),
+                                       array( 'up_user' => $this->getId() ),
+                                       __METHOD__
+                               );
+
+                               $this->mOptionOverrides = array();
+                               $data = array();
+                               foreach ( $res as $row ) {
+                                       $data[$row->up_property] = $row->up_value;
+                               }
+                       }
+                       foreach ( $data as $property => $value ) {
+                               $this->mOptionOverrides[$property] = $value;
+                               $this->mOptions[$property] = $value;
                        }
                }