Merge "Removed references to DB ignoreErrors() function."
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
index 6ecfeda..26ac3dc 100644 (file)
@@ -1,4 +1,24 @@
 <?php
+/**
+ * Representation of an user on a other locally-hosted wiki.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * Cut-down copy of User interface for local-interwiki-database
@@ -21,6 +41,7 @@ class UserRightsProxy {
                $this->database = $database;
                $this->name = $name;
                $this->id = intval( $id );
+               $this->newOptions = array();
        }
 
        /**
@@ -48,10 +69,11 @@ class UserRightsProxy {
         *
         * @param $database String: database name
         * @param $id Integer: user ID
+        * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
         * @return String: user name or false if the user doesn't exist
         */
-       public static function whoIs( $database, $id ) {
-               $user = self::newFromId( $database, $id );
+       public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
+               $user = self::newFromId( $database, $id, $ignoreInvalidDB );
                if( $user ) {
                        return $user->name;
                } else {
@@ -64,10 +86,11 @@ class UserRightsProxy {
         *
         * @param $database String: database name
         * @param $id Integer: user ID
+        * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
         * @return UserRightsProxy or null if doesn't exist
         */
-       public static function newFromId( $database, $id ) {
-               return self::newFromLookup( $database, 'user_id', intval( $id ) );
+       public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
+               return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
        }
 
        /**
@@ -75,14 +98,22 @@ class UserRightsProxy {
         *
         * @param $database String: database name
         * @param $name String: user name
+        * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
         * @return UserRightsProxy or null if doesn't exist
         */
-       public static function newFromName( $database, $name ) {
-               return self::newFromLookup( $database, 'user_name', $name );
+       public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
+               return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
        }
 
-       private static function newFromLookup( $database, $field, $value ) {
-               $db = self::getDB( $database );
+       /**
+        * @param $database
+        * @param $field
+        * @param $value
+        * @param $ignoreInvalidDB bool
+        * @return null|UserRightsProxy
+        */
+       private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
+               $db = self::getDB( $database, $ignoreInvalidDB );
                if( $db ) {
                        $row = $db->selectRow( 'user',
                                array( 'user_id', 'user_name' ),
@@ -102,9 +133,10 @@ class UserRightsProxy {
         * This may be a new connection to another database for remote users.
         *
         * @param $database String
+        * @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
         * @return DatabaseBase or null if invalid selection
         */
-       public static function getDB( $database ) {
+       public static function getDB( $database, $ignoreInvalidDB = false ) {
                global $wgDBname;
                if( self::validDatabase( $database ) ) {
                        if( $database == $wgDBname ) {
@@ -117,10 +149,16 @@ class UserRightsProxy {
                return null;
        }
 
+       /**
+        * @return int
+        */
        public function getId() {
                return $this->id;
        }
 
+       /**
+        * @return bool
+        */
        public function isAnon() {
                return $this->getId() == 0;
        }
@@ -145,6 +183,7 @@ class UserRightsProxy {
 
        /**
         * Replaces User::getUserGroups()
+        * @return array
         */
        function getGroups() {
                $res = $this->db->select( 'user_groups',
@@ -152,7 +191,7 @@ class UserRightsProxy {
                        array( 'ug_user' => $this->id ),
                        __METHOD__ );
                $groups = array();
-               while( $row = $this->db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $groups[] = $row->ug_group;
                }
                return $groups;
@@ -183,6 +222,29 @@ class UserRightsProxy {
                        __METHOD__ );
        }
 
+       /**
+        * Replaces User::setOption()
+        */
+       public function setOption( $option, $value ) {
+               $this->newOptions[$option] = $value;
+       }
+
+       public function saveSettings() {
+               $rows = array();
+               foreach ( $this->newOptions as $option => $value ) {
+                       $rows[] = array(
+                               'up_user' => $this->id,
+                               'up_property' => $option,
+                               'up_value' => $value,
+                       );
+               }
+               $this->db->replace( 'user_properties',
+                       array( array( 'up_user', 'up_property' ) ),
+                       $rows, __METHOD__
+               );
+               $this->invalidateCache();
+       }
+
        /**
         * Replaces User::touchUser()
         */
@@ -193,11 +255,7 @@ class UserRightsProxy {
                        __METHOD__ );
 
                global $wgMemc;
-               if ( function_exists( 'wfForeignMemcKey' ) ) {
-                       $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
-               } else {
-                       $key = "$this->database:user:id:" . $this->id;
-               }
+               $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
                $wgMemc->delete( $key );
        }
 }