Merge "setSquidMaxage() globally if we can purge it, instead of in actions."
[lhc/web/wiklou.git] / includes / User.php
index cb146b5..01407b1 100644 (file)
@@ -1028,7 +1028,7 @@ class User {
                }
 
                $dbr = wfGetDB( DB_MASTER );
-               $s = $dbr->selectRow( 'user', '*', array( 'user_id' => $this->mId ), __METHOD__ );
+               $s = $dbr->selectRow( 'user', self::selectFields(), array( 'user_id' => $this->mId ), __METHOD__ );
 
                wfRunHooks( 'UserLoadFromDatabase', array( $this, &$s ) );
 
@@ -1287,7 +1287,7 @@ class User {
 
                # Proxy blocking
                if ( !$block instanceof Block && $ip !== null && !$this->isAllowed( 'proxyunbannable' )
-                       && !in_array( $ip, $wgProxyWhitelist ) ) 
+                       && !in_array( $ip, $wgProxyWhitelist ) )
                {
                        # Local list
                        if ( self::isLocallyBlockedProxy( $ip ) ) {
@@ -1926,10 +1926,19 @@ class User {
                        $this->mTouched = self::newTouchedTimestamp();
 
                        $dbw = wfGetDB( DB_MASTER );
-                       $dbw->update( 'user',
-                               array( 'user_touched' => $dbw->timestamp( $this->mTouched ) ),
-                               array( 'user_id' => $this->mId ),
-                               __METHOD__ );
+
+                       // Prevent contention slams by checking user_touched first
+                       $now = $dbw->timestamp( $this->mTouched );
+                       $needsPurge = $dbw->selectField( 'user', '1',
+                               array( 'user_id' => $this->mId, 'user_touched < ' . $dbw->addQuotes( $now ) )
+                       );
+                       if ( $needsPurge ) {
+                               $dbw->update( 'user',
+                                       array( 'user_touched' => $now ),
+                                       array( 'user_id' => $this->mId, 'user_touched < ' . $dbw->addQuotes( $now ) ),
+                                       __METHOD__
+                               );
+                       }
 
                        $this->clearSharedCache();
                }
@@ -3014,7 +3023,7 @@ class User {
         */
        public function getPageRenderingHash() {
                wfDeprecated( __METHOD__, '1.17' );
-               
+
                global $wgUseDynamicDates, $wgRenderHashAppend, $wgLang, $wgContLang;
                if( $this->mHash ){
                        return $this->mHash;
@@ -4009,7 +4018,7 @@ class User {
 
                        $res = $dbr->select(
                                'user_properties',
-                               '*',
+                               array( 'up_property', 'up_value' ),
                                array( 'up_user' => $this->getId() ),
                                __METHOD__
                        );
@@ -4032,13 +4041,9 @@ class User {
        protected function saveOptions() {
                global $wgAllowPrefChange;
 
-               $extuser = ExternalUser::newFromUser( $this );
-
                $this->loadOptions();
-               $dbw = wfGetDB( DB_MASTER );
-
-               $insert_rows = array();
 
+               // Not using getOptions(), to keep hidden preferences in database
                $saveOptions = $this->mOptions;
 
                // Allow hooks to abort, for instance to save to a global profile.
@@ -4047,13 +4052,17 @@ class User {
                        return;
                }
 
+               $extuser = ExternalUser::newFromUser( $this );
+               $userId = $this->getId();
+               $insert_rows = array();
                foreach( $saveOptions as $key => $value ) {
                        # Don't bother storing default values
-                       if ( ( is_null( self::getDefaultOption( $key ) ) &&
-                                       !( $value === false || is_null($value) ) ) ||
-                                       $value != self::getDefaultOption( $key ) ) {
+                       $defaultOption = self::getDefaultOption( $key );
+                       if ( ( is_null( $defaultOption ) &&
+                                       !( $value === false || is_null( $value ) ) ) ||
+                                       $value != $defaultOption ) {
                                $insert_rows[] = array(
-                                               'up_user' => $this->getId(),
+                                               'up_user' => $userId,
                                                'up_property' => $key,
                                                'up_value' => $value,
                                        );
@@ -4070,7 +4079,8 @@ class User {
                        }
                }
 
-               $dbw->delete( 'user_properties', array( 'up_user' => $this->getId() ), __METHOD__ );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'user_properties', array( 'up_user' => $userId ), __METHOD__ );
                $dbw->insert( 'user_properties', $insert_rows, __METHOD__ );
        }
 
@@ -4132,4 +4142,28 @@ class User {
 
                return $ret;
        }
+
+       /**
+        * Return the list of user fields that should be selected to create
+        * a new user object.
+        * @return array
+        */
+       public static function selectFields() {
+               return array(
+                       'user_id',
+                       'user_name',
+                       'user_real_name',
+                       'user_password',
+                       'user_newpassword',
+                       'user_newpass_time',
+                       'user_email',
+                       'user_touched',
+                       'user_token',
+                       'user_email_authenticated',
+                       'user_email_token',
+                       'user_email_token_expires',
+                       'user_registration',
+                       'user_editcount',
+               );
+       }
 }