mediawiki.searchSuggest: Enable for iPhone and iPod touch
[lhc/web/wiklou.git] / includes / User.php
index b5fb1ff..67ee7f9 100644 (file)
@@ -56,7 +56,7 @@ class PasswordError extends MWException {
  * for rendering normal pages are set in the cookie to minimize use
  * of the database.
  */
-class User {
+class User implements IDBAccessObject {
        /**
         * Global constants made accessible as class constants so that autoloader
         * magic can be used.
@@ -149,6 +149,7 @@ class User {
                'nominornewtalk',
                'noratelimit',
                'override-export-depth',
+               'pagelang',
                'passwordreset',
                'patrol',
                'patrolmarks',
@@ -1169,9 +1170,10 @@ class User {
         * Load user and user_group data from the database.
         * $this->mId must be set, this is how the user is identified.
         *
+        * @param integer $flags Supports User::READ_LOCKING
         * @return bool True if the user exists, false if the user is anonymous
         */
-       public function loadFromDatabase() {
+       public function loadFromDatabase( $flags = 0 ) {
                // Paranoia
                $this->mId = intval( $this->mId );
 
@@ -1186,7 +1188,10 @@ class User {
                        'user',
                        self::selectFields(),
                        array( 'user_id' => $this->mId ),
-                       __METHOD__
+                       __METHOD__,
+                       ( $flags & self::READ_LOCKING == self::READ_LOCKING )
+                               ? array( 'LOCK IN SHARE MODE' )
+                               : array()
                );
 
                wfRunHooks( 'UserLoadFromDatabase', array( $this, &$s ) );
@@ -3605,17 +3610,25 @@ class User {
                        array( 'IGNORE' )
                );
                if ( !$dbw->affectedRows() ) {
-                       if ( !$inWrite ) {
-                               // XXX: Get out of REPEATABLE-READ so the SELECT below works.
-                               // Often this case happens early in views before any writes.
-                               // This shows up at least with CentralAuth.
+                       // The queries below cannot happen in the same REPEATABLE-READ snapshot.
+                       // Handle this by COMMIT, if possible, or by LOCK IN SHARE MODE otherwise.
+                       if ( $inWrite ) {
+                               // Can't commit due to pending writes that may need atomicity.
+                               // This may cause some lock contention unlike the case below.
+                               $options = array( 'LOCK IN SHARE MODE' );
+                               $flags = self::READ_LOCKING;
+                       } else {
+                               // Often, this case happens early in views before any writes when
+                               // using CentralAuth. It's should be OK to commit and break the snapshot.
                                $dbw->commit( __METHOD__, 'flush' );
+                               $options = array();
+                               $flags = 0;
                        }
                        $this->mId = $dbw->selectField( 'user', 'user_id',
-                               array( 'user_name' => $this->mName ), __METHOD__ );
+                               array( 'user_name' => $this->mName ), __METHOD__, $options );
                        $loaded = false;
                        if ( $this->mId ) {
-                               if ( $this->loadFromDatabase() ) {
+                               if ( $this->loadFromDatabase( $flags ) ) {
                                        $loaded = true;
                                }
                        }
@@ -4795,13 +4808,13 @@ class User {
                }
 
                $userId = $this->getId();
-               $insert_rows = array();
+
+               $insert_rows = array(); // all the new preference rows
                foreach ( $saveOptions as $key => $value ) {
                        // Don't bother storing default values
                        $defaultOption = self::getDefaultOption( $key );
-                       if ( ( is_null( $defaultOption ) &&
-                               !( $value === false || is_null( $value ) ) ) ||
-                               $value != $defaultOption
+                       if ( ( $defaultOption === null && $value !== false && $value !== null )
+                               || $value != $defaultOption
                        ) {
                                $insert_rows[] = array(
                                        'up_user' => $userId,
@@ -4812,14 +4825,22 @@ class User {
                }
 
                $dbw = wfGetDB( DB_MASTER );
-               // Find and delete any prior preference rows...
+
                $res = $dbw->select( 'user_properties',
-                       array( 'up_property' ), array( 'up_user' => $userId ), __METHOD__ );
-               $priorKeys = array();
+                       array( 'up_property', 'up_value' ), array( 'up_user' => $userId ), __METHOD__ );
+
+               // Find prior rows that need to be removed or updated. These rows will
+               // all be deleted (the later so that INSERT IGNORE applies the new values).
+               $keysDelete = array();
                foreach ( $res as $row ) {
-                       $priorKeys[] = $row->up_property;
+                       if ( !isset( $saveOptions[$row->up_property] )
+                               || strcmp( $saveOptions[$row->up_property], $row->up_value ) != 0
+                       ) {
+                               $keysDelete[] = $row->up_property;
+                       }
                }
-               if ( count( $priorKeys ) ) {
+
+               if ( count( $keysDelete ) ) {
                        // Do the DELETE by PRIMARY KEY for prior rows.
                        // In the past a very large portion of calls to this function are for setting
                        // 'rememberpassword' for new accounts (a preference that has since been removed).
@@ -4828,7 +4849,7 @@ class User {
                        // updates would pile up on each other as they are for higher (newer) user IDs.
                        // It might not be necessary these days, but it shouldn't hurt either.
                        $dbw->delete( 'user_properties',
-                               array( 'up_user' => $userId, 'up_property' => $priorKeys ), __METHOD__ );
+                               array( 'up_user' => $userId, 'up_property' => $keysDelete ), __METHOD__ );
                }
                // Insert the new preference rows
                $dbw->insert( 'user_properties', $insert_rows, __METHOD__, array( 'IGNORE' ) );