Merge "Handle missing namespace prefix in XML dumps more gracefully"
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
index 9b45b91..3a814c4 100644 (file)
@@ -42,6 +42,7 @@ class ApiQueryUsers extends ApiQueryBase {
                // everything except 'blockinfo' which might show hidden records if the user
                // making the request has the appropriate permissions
                'groups',
+               'groupmemberships',
                'implicitgroups',
                'rights',
                'editcount',
@@ -97,15 +98,21 @@ class ApiQueryUsers extends ApiQueryBase {
        }
 
        public function execute() {
+               $db = $this->getDB();
+
                $params = $this->extractRequestParams();
+               $this->requireMaxOneParameter( $params, 'userids', 'users' );
 
                if ( !is_null( $params['prop'] ) ) {
                        $this->prop = array_flip( $params['prop'] );
                } else {
                        $this->prop = [];
                }
+               $useNames = !is_null( $params['users'] );
 
                $users = (array)$params['users'];
+               $userids = (array)$params['userids'];
+
                $goodNames = $done = [];
                $result = $this->getResult();
                // Canonicalize user names
@@ -127,12 +134,22 @@ class ApiQueryUsers extends ApiQueryBase {
                        }
                }
 
+               if ( $useNames ) {
+                       $parameters = &$goodNames;
+               } else {
+                       $parameters = &$userids;
+               }
+
                $result = $this->getResult();
 
-               if ( count( $goodNames ) ) {
+               if ( count( $parameters ) ) {
                        $this->addTables( 'user' );
                        $this->addFields( User::selectFields() );
-                       $this->addWhereFld( 'user_name', $goodNames );
+                       if ( $useNames ) {
+                               $this->addWhereFld( 'user_name', $goodNames );
+                       } else {
+                               $this->addWhereFld( 'user_id', $userids );
+                       }
 
                        $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
 
@@ -145,18 +162,29 @@ class ApiQueryUsers extends ApiQueryBase {
                                $userGroups = [];
 
                                $this->addTables( 'user' );
-                               $this->addWhereFld( 'user_name', $goodNames );
+                               if ( $useNames ) {
+                                       $this->addWhereFld( 'user_name', $goodNames );
+                               } else {
+                                       $this->addWhereFld( 'user_id', $userids );
+                               }
+
                                $this->addTables( 'user_groups' );
                                $this->addJoinConds( [ 'user_groups' => [ 'INNER JOIN', 'ug_user=user_id' ] ] );
-                               $this->addFields( [ 'user_name', 'ug_group' ] );
+                               $this->addFields( [ 'user_name' ] );
+                               $this->addFields( UserGroupMembership::selectFields() );
+                               if ( !$this->getConfig()->get( 'DisableUserGroupExpiry' ) ) {
+                                       $this->addWhere( 'ug_expiry IS NULL OR ug_expiry >= ' .
+                                               $db->addQuotes( $db->timestamp() ) );
+                               }
                                $userGroupsRes = $this->select( __METHOD__ );
 
                                foreach ( $userGroupsRes as $row ) {
-                                       $userGroups[$row->user_name][] = $row->ug_group;
+                                       $userGroups[$row->user_name][] = $row;
                                }
                        }
 
                        foreach ( $res as $row ) {
+
                                // create user object and pass along $userGroups if set
                                // that reduces the number of database queries needed in User dramatically
                                if ( !isset( $userGroups ) ) {
@@ -167,44 +195,56 @@ class ApiQueryUsers extends ApiQueryBase {
                                        }
                                        $user = User::newFromRow( $row, [ 'user_groups' => $userGroups[$row->user_name] ] );
                                }
-                               $name = $user->getName();
-
-                               $data[$name]['userid'] = $user->getId();
-                               $data[$name]['name'] = $name;
+                               if ( $useNames ) {
+                                       $key = $user->getName();
+                               } else {
+                                       $key = $user->getId();
+                               }
+                               $data[$key]['userid'] = $user->getId();
+                               $data[$key]['name'] = $user->getName();
 
                                if ( isset( $this->prop['editcount'] ) ) {
-                                       $data[$name]['editcount'] = $user->getEditCount();
+                                       $data[$key]['editcount'] = $user->getEditCount();
                                }
 
                                if ( isset( $this->prop['registration'] ) ) {
-                                       $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
+                                       $data[$key]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
                                }
 
                                if ( isset( $this->prop['groups'] ) ) {
-                                       $data[$name]['groups'] = $user->getEffectiveGroups();
+                                       $data[$key]['groups'] = $user->getEffectiveGroups();
+                               }
+
+                               if ( isset( $this->prop['groupmemberships'] ) ) {
+                                       $data[$key]['groupmemberships'] = array_map( function( $ugm ) {
+                                               return [
+                                                       'group' => $ugm->getGroup(),
+                                                       'expiry' => ApiResult::formatExpiry( $ugm->getExpiry() ),
+                                               ];
+                                       }, $user->getGroupMemberships() );
                                }
 
                                if ( isset( $this->prop['implicitgroups'] ) ) {
-                                       $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
+                                       $data[$key]['implicitgroups'] = $user->getAutomaticGroups();
                                }
 
                                if ( isset( $this->prop['rights'] ) ) {
-                                       $data[$name]['rights'] = $user->getRights();
+                                       $data[$key]['rights'] = $user->getRights();
                                }
                                if ( $row->ipb_deleted ) {
-                                       $data[$name]['hidden'] = true;
+                                       $data[$key]['hidden'] = true;
                                }
                                if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
-                                       $data[$name]['blockid'] = (int)$row->ipb_id;
-                                       $data[$name]['blockedby'] = $row->ipb_by_text;
-                                       $data[$name]['blockedbyid'] = (int)$row->ipb_by;
-                                       $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
-                                       $data[$name]['blockreason'] = $row->ipb_reason;
-                                       $data[$name]['blockexpiry'] = $row->ipb_expiry;
+                                       $data[$key]['blockid'] = (int)$row->ipb_id;
+                                       $data[$key]['blockedby'] = $row->ipb_by_text;
+                                       $data[$key]['blockedbyid'] = (int)$row->ipb_by;
+                                       $data[$key]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
+                                       $data[$key]['blockreason'] = $row->ipb_reason;
+                                       $data[$key]['blockexpiry'] = $row->ipb_expiry;
                                }
 
                                if ( isset( $this->prop['emailable'] ) ) {
-                                       $data[$name]['emailable'] = $user->canReceiveEmail();
+                                       $data[$key]['emailable'] = $user->canReceiveEmail();
                                }
 
                                if ( isset( $this->prop['gender'] ) ) {
@@ -212,11 +252,11 @@ class ApiQueryUsers extends ApiQueryBase {
                                        if ( strval( $gender ) === '' ) {
                                                $gender = 'unknown';
                                        }
-                                       $data[$name]['gender'] = $gender;
+                                       $data[$key]['gender'] = $gender;
                                }
 
                                if ( isset( $this->prop['centralids'] ) ) {
-                                       $data[$name] += ApiQueryUserInfo::getCentralUserInfo(
+                                       $data[$key] += ApiQueryUserInfo::getCentralUserInfo(
                                                $this->getConfig(), $user, $params['attachedwiki']
                                        );
                                }
@@ -226,9 +266,9 @@ class ApiQueryUsers extends ApiQueryBase {
                                        foreach ( $params['token'] as $t ) {
                                                $val = call_user_func( $tokenFunctions[$t], $user );
                                                if ( $val === false ) {
-                                                       $this->setWarning( "Action '$t' is not allowed for the current user" );
+                                                       $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
                                                } else {
-                                                       $data[$name][$t . 'token'] = $val;
+                                                       $data[$key][$t . 'token'] = $val;
                                                }
                                        }
                                }
@@ -237,43 +277,53 @@ class ApiQueryUsers extends ApiQueryBase {
 
                $context = $this->getContext();
                // Second pass: add result data to $retval
-               foreach ( $goodNames as $u ) {
+               foreach ( $parameters as $u ) {
                        if ( !isset( $data[$u] ) ) {
-                               $data[$u] = [ 'name' => $u ];
-                               $urPage = new UserrightsPage;
-                               $urPage->setContext( $context );
-                               $iwUser = $urPage->fetchUser( $u );
-
-                               if ( $iwUser instanceof UserRightsProxy ) {
-                                       $data[$u]['interwiki'] = true;
-
-                                       if ( !is_null( $params['token'] ) ) {
-                                               $tokenFunctions = $this->getTokenFunctions();
-
-                                               foreach ( $params['token'] as $t ) {
-                                                       $val = call_user_func( $tokenFunctions[$t], $iwUser );
-                                                       if ( $val === false ) {
-                                                               $this->setWarning( "Action '$t' is not allowed for the current user" );
-                                                       } else {
-                                                               $data[$u][$t . 'token'] = $val;
+                               if ( $useNames ) {
+                                       $data[$u] = [ 'name' => $u ];
+                                       $urPage = new UserrightsPage;
+                                       $urPage->setContext( $context );
+
+                                       $iwUser = $urPage->fetchUser( $u );
+
+                                       if ( $iwUser instanceof UserRightsProxy ) {
+                                               $data[$u]['interwiki'] = true;
+
+                                               if ( !is_null( $params['token'] ) ) {
+                                                       $tokenFunctions = $this->getTokenFunctions();
+
+                                                       foreach ( $params['token'] as $t ) {
+                                                               $val = call_user_func( $tokenFunctions[$t], $iwUser );
+                                                               if ( $val === false ) {
+                                                                       $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
+                                                               } else {
+                                                                       $data[$u][$t . 'token'] = $val;
+                                                               }
                                                        }
                                                }
-                                       }
-                               } else {
-                                       $data[$u]['missing'] = true;
-                                       if ( isset( $this->prop['cancreate'] ) ) {
-                                               $status = MediaWiki\Auth\AuthManager::singleton()->canCreateAccount( $u );
-                                               $data[$u]['cancreate'] = $status->isGood();
-                                               if ( !$status->isGood() ) {
-                                                       $data[$u]['cancreateerror'] = $this->getErrorFormatter()->arrayFromStatus( $status );
+                                       } else {
+                                               $data[$u]['missing'] = true;
+                                               if ( isset( $this->prop['cancreate'] ) ) {
+                                                       $status = MediaWiki\Auth\AuthManager::singleton()->canCreateAccount( $u );
+                                                       $data[$u]['cancreate'] = $status->isGood();
+                                                       if ( !$status->isGood() ) {
+                                                               $data[$u]['cancreateerror'] = $this->getErrorFormatter()->arrayFromStatus( $status );
+                                                       }
                                                }
                                        }
+                               } else {
+                                       $data[$u] = [ 'userid' => $u, 'missing' => true ];
                                }
+
                        } else {
                                if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
                                        ApiResult::setArrayType( $data[$u]['groups'], 'array' );
                                        ApiResult::setIndexedTagName( $data[$u]['groups'], 'g' );
                                }
+                               if ( isset( $this->prop['groupmemberships'] ) && isset( $data[$u]['groupmemberships'] ) ) {
+                                       ApiResult::setArrayType( $data[$u]['groupmemberships'], 'array' );
+                                       ApiResult::setIndexedTagName( $data[$u]['groupmemberships'], 'groupmembership' );
+                               }
                                if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
                                        ApiResult::setArrayType( $data[$u]['implicitgroups'], 'array' );
                                        ApiResult::setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
@@ -287,8 +337,13 @@ class ApiQueryUsers extends ApiQueryBase {
                        $fit = $result->addValue( [ 'query', $this->getModuleName() ],
                                null, $data[$u] );
                        if ( !$fit ) {
-                               $this->setContinueEnumParameter( 'users',
-                                       implode( '|', array_diff( $users, $done ) ) );
+                               if ( $useNames ) {
+                                       $this->setContinueEnumParameter( 'users',
+                                               implode( '|', array_diff( $users, $done ) ) );
+                               } else {
+                                       $this->setContinueEnumParameter( 'userids',
+                                               implode( '|', array_diff( $userids, $done ) ) );
+                               }
                                break;
                        }
                        $done[] = $u;
@@ -313,6 +368,7 @@ class ApiQueryUsers extends ApiQueryBase {
                                ApiBase::PARAM_TYPE => [
                                        'blockinfo',
                                        'groups',
+                                       'groupmemberships',
                                        'implicitgroups',
                                        'rights',
                                        'editcount',
@@ -330,6 +386,10 @@ class ApiQueryUsers extends ApiQueryBase {
                        'users' => [
                                ApiBase::PARAM_ISMULTI => true
                        ],
+                       'userids' => [
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_TYPE => 'integer'
+                       ],
                        'token' => [
                                ApiBase::PARAM_DEPRECATED => true,
                                ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),