diff options
Diffstat (limited to 'includes/api/ApiQueryUsers.php')
-rw-r--r-- | includes/api/ApiQueryUsers.php | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/includes/api/ApiQueryUsers.php b/includes/api/ApiQueryUsers.php index bf438d1d..dccfee67 100644 --- a/includes/api/ApiQueryUsers.php +++ b/includes/api/ApiQueryUsers.php @@ -110,26 +110,46 @@ class ApiQueryUsers extends ApiQueryBase { $this->addFields( User::selectFields() ); $this->addWhereFld( 'user_name', $goodNames ); - if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) { - $this->addTables( 'user_groups' ); - $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) ); - $this->addFields( 'ug_group' ); - } - $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) ); $data = array(); $res = $this->select( __METHOD__ ); + $this->resetQueryParams(); + + // get user groups if needed + if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) { + $userGroups = array(); + + $this->addTables( 'user' ); + $this->addWhereFld( 'user_name', $goodNames ); + $this->addTables( 'user_groups' ); + $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) ); + $this->addFields( array( 'user_name', 'ug_group' ) ); + $userGroupsRes = $this->select( __METHOD__ ); + + foreach ( $userGroupsRes as $row ) { + $userGroups[$row->user_name][] = $row->ug_group; + } + } foreach ( $res as $row ) { - $user = User::newFromRow( $row ); + // create user object and pass along $userGroups if set + // that reduces the number of database queries needed in User dramatically + if ( !isset( $userGroups ) ) { + $user = User::newFromRow( $row ); + } else { + if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) { + $userGroups[$row->user_name] = array(); + } + $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) ); + } $name = $user->getName(); $data[$name]['userid'] = $user->getId(); $data[$name]['name'] = $name; if ( isset( $this->prop['editcount'] ) ) { - $data[$name]['editcount'] = intval( $user->getEditCount() ); + $data[$name]['editcount'] = $user->getEditCount(); } if ( isset( $this->prop['registration'] ) ) { @@ -137,29 +157,15 @@ class ApiQueryUsers extends ApiQueryBase { } if ( isset( $this->prop['groups'] ) ) { - if ( !isset( $data[$name]['groups'] ) ) { - $data[$name]['groups'] = $user->getAutomaticGroups(); - } - - if ( !is_null( $row->ug_group ) ) { - // This row contains only one group, others will be added from other rows - $data[$name]['groups'][] = $row->ug_group; - } + $data[$name]['groups'] = $user->getEffectiveGroups(); } - if ( isset( $this->prop['implicitgroups'] ) && !isset( $data[$name]['implicitgroups'] ) ) { - $data[$name]['implicitgroups'] = $user->getAutomaticGroups(); + if ( isset( $this->prop['implicitgroups'] ) ) { + $data[$name]['implicitgroups'] = $user->getAutomaticGroups(); } if ( isset( $this->prop['rights'] ) ) { - if ( !isset( $data[$name]['rights'] ) ) { - $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() ); - } - - if ( !is_null( $row->ug_group ) ) { - $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'], - User::getGroupPermissions( array( $row->ug_group ) ) ) ); - } + $data[$name]['rights'] = $user->getRights(); } if ( $row->ipb_deleted ) { $data[$name]['hidden'] = ''; @@ -198,11 +204,13 @@ class ApiQueryUsers extends ApiQueryBase { } } + $context = $this->getContext(); // Second pass: add result data to $retval foreach ( $goodNames as $u ) { if ( !isset( $data[$u] ) ) { $data[$u] = array( 'name' => $u ); $urPage = new UserrightsPage; + $urPage->setContext( $context ); $iwUser = $urPage->fetchUser( $u ); if ( $iwUser instanceof UserRightsProxy ) { @@ -244,16 +252,16 @@ class ApiQueryUsers extends ApiQueryBase { } $done[] = $u; } - return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' ); + $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' ); } /** - * Gets all the groups that a user is automatically a member of (implicit groups) - * - * @deprecated since 1.20; call User::getAutomaticGroups() directly. - * @param $user User - * @return array - */ + * Gets all the groups that a user is automatically a member of (implicit groups) + * + * @deprecated since 1.20; call User::getAutomaticGroups() directly. + * @param $user User + * @return array + */ public static function getAutoGroups( $user ) { wfDeprecated( __METHOD__, '1.20' ); @@ -304,7 +312,7 @@ class ApiQueryUsers extends ApiQueryBase { ' rights - Lists all the rights the user(s) has', ' editcount - Adds the user\'s edit count', ' registration - Adds the user\'s registration timestamp', - ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]', + ' emailable - Tags if the user can and wants to receive email through [[Special:Emailuser]]', ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"', ), 'users' => 'A list of users to obtain the same information for', @@ -390,8 +398,4 @@ class ApiQueryUsers extends ApiQueryBase { public function getHelpUrls() { return 'https://www.mediawiki.org/wiki/API:Users'; } - - public function getVersion() { - return __CLASS__ . ': $Id$'; - } } |