From: rlot Date: Mon, 5 Dec 2016 07:35:42 +0000 (+0100) Subject: ApiQueryUsers: Add ability to search by user ID X-Git-Tag: 1.31.0-rc.0~4629^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/?a=commitdiff_plain;h=ac609b45be97650dcde7a98d1a9287beaf804009;p=lhc%2Fweb%2Fwiklou.git ApiQueryUsers: Add ability to search by user ID Added new 'userids' parameter to ApiQueryUsers.php. It accepts user ID numbers. Bug: T34494 Change-Id: I8021bc8660f19e90f0f7d43ea0e82d5258c586ab --- diff --git a/includes/api/ApiQueryUsers.php b/includes/api/ApiQueryUsers.php index 65d3797a74..2d620a4e9e 100644 --- a/includes/api/ApiQueryUsers.php +++ b/includes/api/ApiQueryUsers.php @@ -98,14 +98,18 @@ class ApiQueryUsers extends ApiQueryBase { public function execute() { $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 +131,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,7 +159,12 @@ 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' ] ); @@ -157,6 +176,7 @@ class ApiQueryUsers extends ApiQueryBase { } 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 +187,47 @@ 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['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 +235,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'] ); } @@ -228,7 +251,7 @@ class ApiQueryUsers extends ApiQueryBase { if ( $val === false ) { $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] ); } else { - $data[$name][$t . 'token'] = $val; + $data[$key][$t . 'token'] = $val; } } } @@ -237,38 +260,44 @@ 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->addWarning( [ 'apiwarn-tokennotallowed', $t ] ); - } 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' ); @@ -287,8 +316,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; @@ -330,6 +364,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() ), diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 02aa6dbc29..a37b7cf461 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -1250,6 +1250,7 @@ "apihelp-query+users-paramvalue-prop-cancreate": "Indicates whether an account for valid but unregistered usernames can be created.", "apihelp-query+users-param-attachedwiki": "With $1prop=centralids, indicate whether the user is attached with the wiki identified by this ID.", "apihelp-query+users-param-users": "A list of users to obtain information for.", + "apihelp-query+users-param-userids": "A list of user IDs to obtain information for.", "apihelp-query+users-param-token": "Use [[Special:ApiHelp/query+tokens|action=query&meta=tokens]] instead.", "apihelp-query+users-example-simple": "Return information for user Example.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index c5d9bc041e..6516ea9773 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -1167,6 +1167,7 @@ "apihelp-query+users-paramvalue-prop-cancreate": "{{doc-apihelp-paramvalue|query+users|prop|cancreate}}", "apihelp-query+users-param-attachedwiki": "{{doc-apihelp-param|query+users|attachedwiki}}", "apihelp-query+users-param-users": "{{doc-apihelp-param|query+users|users}}", + "apihelp-query+users-param-userids": "{{doc-apihelp-param|query+users|userids}}", "apihelp-query+users-param-token": "{{doc-apihelp-param|query+users|token}}", "apihelp-query+users-example-simple": "{{doc-apihelp-example|query+users}}", "apihelp-query+watchlist-description": "{{doc-apihelp-description|query+watchlist}}",