Merge "Convert article delete to use OOUI"
[lhc/web/wiklou.git] / includes / user / UserRightsProxy.php
index e686ae3..98586e7 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  */
 
+use Wikimedia\Rdbms\IDatabase;
+
 /**
  * Cut-down copy of User interface for local-interwiki-database
  * user rights manipulation.
@@ -27,8 +29,6 @@
 class UserRightsProxy {
 
        /**
-        * Constructor.
-        *
         * @see newFromId()
         * @see newFromName()
         * @param IDatabase $db Db connection
@@ -41,7 +41,7 @@ class UserRightsProxy {
                $this->database = $database;
                $this->name = $name;
                $this->id = intval( $id );
-               $this->newOptions = array();
+               $this->newOptions = [];
        }
 
        /**
@@ -127,8 +127,8 @@ class UserRightsProxy {
 
                if ( $db && $userdb ) {
                        $row = $userdb->selectRow( 'user',
-                               array( 'user_id', 'user_name' ),
-                               array( $field => $value ),
+                               [ 'user_id', 'user_name' ],
+                               [ $field => $value ],
                                __METHOD__ );
 
                        if ( $row !== false ) {
@@ -155,7 +155,7 @@ class UserRightsProxy {
                                // Hmm... this shouldn't happen though. :)
                                return wfGetDB( DB_MASTER );
                        } else {
-                               return wfGetDB( DB_MASTER, array(), $database );
+                               return wfGetDB( DB_MASTER, [], $database );
                        }
                }
                return null;
@@ -198,50 +198,47 @@ class UserRightsProxy {
         * @return array
         */
        function getGroups() {
-               $res = $this->db->select( 'user_groups',
-                       array( 'ug_group' ),
-                       array( 'ug_user' => $this->id ),
-                       __METHOD__ );
-               $groups = array();
-               foreach ( $res as $row ) {
-                       $groups[] = $row->ug_group;
-               }
-               return $groups;
+               return array_keys( self::getGroupMemberships() );
        }
 
        /**
-        * Replaces User::addUserGroup()
-        * @param string $group
+        * Replaces User::getGroupMemberships()
+        *
+        * @return array
+        * @since 1.29
+        */
+       function getGroupMemberships() {
+               return UserGroupMembership::getMembershipsForUser( $this->id, $this->db );
+       }
+
+       /**
+        * Replaces User::addGroup()
         *
+        * @param string $group
+        * @param string|null $expiry
         * @return bool
         */
-       function addGroup( $group ) {
-               $this->db->insert( 'user_groups',
-                       array(
-                               'ug_user' => $this->id,
-                               'ug_group' => $group,
-                       ),
-                       __METHOD__,
-                       array( 'IGNORE' ) );
+       function addGroup( $group, $expiry = null ) {
+               if ( $expiry ) {
+                       $expiry = wfTimestamp( TS_MW, $expiry );
+               }
 
-               return true;
+               $ugm = new UserGroupMembership( $this->id, $group, $expiry );
+               return $ugm->insert( true, $this->db );
        }
 
        /**
-        * Replaces User::removeUserGroup()
-        * @param string $group
+        * Replaces User::removeGroup()
         *
+        * @param string $group
         * @return bool
         */
        function removeGroup( $group ) {
-               $this->db->delete( 'user_groups',
-                       array(
-                               'ug_user' => $this->id,
-                               'ug_group' => $group,
-                       ),
-                       __METHOD__ );
-
-               return true;
+               $ugm = UserGroupMembership::getMembership( $this->id, $group, $this->db );
+               if ( !$ugm ) {
+                       return false;
+               }
+               return $ugm->delete( $this->db );
        }
 
        /**
@@ -254,16 +251,16 @@ class UserRightsProxy {
        }
 
        public function saveSettings() {
-               $rows = array();
+               $rows = [];
                foreach ( $this->newOptions as $option => $value ) {
-                       $rows[] = array(
+                       $rows[] = [
                                'up_user' => $this->id,
                                'up_property' => $option,
                                'up_value' => $value,
-                       );
+                       ];
                }
                $this->db->replace( 'user_properties',
-                       array( array( 'up_user', 'up_property' ) ),
+                       [ [ 'up_user', 'up_property' ] ],
                        $rows, __METHOD__
                );
                $this->invalidateCache();
@@ -273,15 +270,20 @@ class UserRightsProxy {
         * Replaces User::touchUser()
         */
        function invalidateCache() {
-               $this->db->update( 'user',
-                       array( 'user_touched' => $this->db->timestamp() ),
-                       array( 'user_id' => $this->id ),
-                       __METHOD__ );
+               $this->db->update(
+                       'user',
+                       [ 'user_touched' => $this->db->timestamp() ],
+                       [ 'user_id' => $this->id ],
+                       __METHOD__
+               );
 
                $wikiId = $this->db->getWikiID();
                $userId = $this->id;
-               $this->db->onTransactionPreCommitOrIdle( function() use ( $wikiId, $userId ) {
-                       User::purge( $wikiId, $userId );
-               } );
+               $this->db->onTransactionPreCommitOrIdle(
+                       function () use ( $wikiId, $userId ) {
+                               User::purge( $wikiId, $userId );
+                       },
+                       __METHOD__
+               );
        }
 }