- $data = array();
- $res = $this->select(__METHOD__);
- while(($r = $db->fetchObject($res))) {
- $user = User::newFromRow($r);
- $name = $user->getName();
- $data[$name]['name'] = $name;
- if(isset($this->prop['editcount']))
- $data[$name]['editcount'] = intval($user->getEditCount());
- if(isset($this->prop['registration']))
- $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $user->getRegistration());
- if(isset($this->prop['groups']) && !is_null($r->ug_group))
- // This row contains only one group, others will be added from other rows
- $data[$name]['groups'][] = $r->ug_group;
- if(isset($this->prop['blockinfo']) && !is_null($r->blocker_name)) {
- $data[$name]['blockedby'] = $r->blocker_name;
- $data[$name]['blockreason'] = $r->ipb_reason;
- }
- if(isset($this->prop['emailable']) && $user->canReceiveEmail())
- $data[$name]['emailable'] = '';
+ 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 ) ) {
+ $user = User::newFromRow( $row );
+ } else {
+ if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
+ $userGroups[$row->user_name] = [];
+ }
+ $user = User::newFromRow( $row, [ 'user_groups' => $userGroups[$row->user_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[$key]['editcount'] = $user->getEditCount();
+ }
+
+ if ( isset( $this->prop['registration'] ) ) {
+ $data[$key]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
+ }
+
+ if ( isset( $this->prop['groups'] ) ) {
+ $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[$key]['implicitgroups'] = $user->getAutomaticGroups();
+ }
+
+ if ( isset( $this->prop['rights'] ) ) {
+ $data[$key]['rights'] = $user->getRights();
+ }
+ if ( $row->ipb_deleted ) {
+ $data[$key]['hidden'] = true;
+ }
+ if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
+ $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'] = $commentStore->getComment( $row )->text;
+ $data[$key]['blockexpiry'] = $row->ipb_expiry;
+ }
+
+ if ( isset( $this->prop['emailable'] ) ) {
+ $data[$key]['emailable'] = $user->canReceiveEmail();
+ }
+
+ if ( isset( $this->prop['gender'] ) ) {
+ $gender = $user->getOption( 'gender' );
+ if ( strval( $gender ) === '' ) {
+ $gender = 'unknown';
+ }
+ $data[$key]['gender'] = $gender;
+ }
+
+ if ( isset( $this->prop['centralids'] ) ) {
+ $data[$key] += ApiQueryUserInfo::getCentralUserInfo(
+ $this->getConfig(), $user, $params['attachedwiki']
+ );
+ }
+
+ if ( !is_null( $params['token'] ) ) {
+ $tokenFunctions = $this->getTokenFunctions();
+ foreach ( $params['token'] as $t ) {
+ $val = call_user_func( $tokenFunctions[$t], $user );
+ if ( $val === false ) {
+ $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
+ } else {
+ $data[$key][$t . 'token'] = $val;
+ }
+ }
+ }