From 72152a7ffc102440cfd30c326dc0c0d5f2def2d6 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 19 Nov 2015 15:57:12 -0500 Subject: [PATCH] API: Allow querying central user info meta=siteinfo gets a list of all configured central ID lookup providers and which one is being used as the default, while meta=userinfo, list=users, and list=allusers get the ability to return the IDs and attachment status. Change-Id: Iea15b6c22baac79b3f8ca6df0e20a6a4299507d2 --- includes/api/ApiQueryAllUsers.php | 13 +++++-- includes/api/ApiQuerySiteinfo.php | 4 +++ includes/api/ApiQueryUserInfo.php | 56 ++++++++++++++++++++++++++++--- includes/api/ApiQueryUsers.php | 9 +++++ includes/api/i18n/en.json | 6 ++++ includes/api/i18n/qqq.json | 6 ++++ 6 files changed, 88 insertions(+), 6 deletions(-) diff --git a/includes/api/ApiQueryAllUsers.php b/includes/api/ApiQueryAllUsers.php index ffcb2f532c..9ea1b1e5c1 100644 --- a/includes/api/ApiQueryAllUsers.php +++ b/includes/api/ApiQueryAllUsers.php @@ -59,9 +59,10 @@ class ApiQueryAllUsers extends ApiQueryBase { $fld_rights = isset( $prop['rights'] ); $fld_registration = isset( $prop['registration'] ); $fld_implicitgroups = isset( $prop['implicitgroups'] ); + $fld_centralids = isset( $prop['centralids'] ); } else { $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = - $fld_rights = $fld_implicitgroups = false; + $fld_rights = $fld_implicitgroups = $fld_centralids = false; } $limit = $params['limit']; @@ -239,6 +240,12 @@ class ApiQueryAllUsers extends ApiQueryBase { 'name' => $row->user_name, ); + if ( $fld_centralids ) { + $data += ApiQueryUserInfo::getCentralUserInfo( + $this->getConfig(), User::newFromId( $row->user_id ), $params['attachedwiki'] + ); + } + if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) { $data['blockid'] = (int)$row->ipb_id; $data['blockedby'] = $row->ipb_by_text; @@ -338,7 +345,8 @@ class ApiQueryAllUsers extends ApiQueryBase { 'implicitgroups', 'rights', 'editcount', - 'registration' + 'registration', + 'centralids', ), ApiBase::PARAM_HELP_MSG_PER_VALUE => array(), ), @@ -357,6 +365,7 @@ class ApiQueryAllUsers extends ApiQueryBase { $this->getConfig()->get( 'ActiveUserDays' ) ), ), + 'attachedwiki' => null, ); } diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php index 704733984f..1c031ff4aa 100644 --- a/includes/api/ApiQuerySiteinfo.php +++ b/includes/api/ApiQuerySiteinfo.php @@ -265,6 +265,10 @@ class ApiQuerySiteinfo extends ApiQueryBase { $data['favicon'] = wfExpandUrl( $favicon, PROTO_RELATIVE ); } + $data['centralidlookupprovider'] = $this->getConfig()->get( 'CentralIdLookupProvider' ); + $providerIds = array_keys( $this->getConfig()->get( 'CentralIdLookupProviders' ) ); + $data['allcentralidlookupproviders'] = $providerIds; + Hooks::run( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) ); return $this->getResult()->addValue( 'query', $property, $data ); diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index 93c0dd053c..27663a1a4f 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -33,6 +33,7 @@ class ApiQueryUserInfo extends ApiQueryBase { const WL_UNREAD_LIMIT = 1000; + private $params = array(); private $prop = array(); public function __construct( ApiQuery $query, $moduleName ) { @@ -40,11 +41,11 @@ class ApiQueryUserInfo extends ApiQueryBase { } public function execute() { - $params = $this->extractRequestParams(); + $this->params = $this->extractRequestParams(); $result = $this->getResult(); - if ( !is_null( $params['prop'] ) ) { - $this->prop = array_flip( $params['prop'] ); + if ( !is_null( $this->params['prop'] ) ) { + $this->prop = array_flip( $this->params['prop'] ); } $r = $this->getCurrentUserInfo(); @@ -76,6 +77,45 @@ class ApiQueryUserInfo extends ApiQueryBase { return $vals; } + /** + * Get central user info + * @param Config $config + * @param User $user + * @param string|null $attachedWiki + * @return array Central user info + * - centralids: Array mapping non-local Central ID provider names to IDs + * - attachedlocal: Array mapping Central ID provider names to booleans + * indicating whether the local user is attached. + * - attachedwiki: Array mapping Central ID provider names to booleans + * indicating whether the user is attached to $attachedWiki. + */ + public static function getCentralUserInfo( Config $config, User $user, $attachedWiki = null ) { + $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) ); + + $ret = array( + 'centralids' => array(), + 'attachedlocal' => array(), + ); + ApiResult::setArrayType( $ret['centralids'], 'assoc' ); + ApiResult::setArrayType( $ret['attachedlocal'], 'assoc' ); + if ( $attachedWiki ) { + $ret['attachedwiki'] = array(); + ApiResult::setArrayType( $ret['attachedwiki'], 'assoc' ); + } + + $name = $user->getName(); + foreach ( $providerIds as $providerId ) { + $provider = CentralIdLookup::factory( $providerId ); + $ret['centralids'][$providerId] = $provider->centralIdFromName( $name ); + $ret['attachedlocal'][$providerId] = $provider->isAttached( $user ); + if ( $attachedWiki ) { + $ret['attachedwiki'][$providerId] = $provider->isAttached( $user, $attachedWiki ); + } + } + + return $ret; + } + protected function getCurrentUserInfo() { $user = $this->getUser(); $vals = array(); @@ -205,6 +245,12 @@ class ApiQueryUserInfo extends ApiQueryBase { } } + if ( isset( $this->prop['centralids'] ) ) { + $vals += self::getCentralUserInfo( + $this->getConfig(), $this->getUser(), $this->params['attachedwiki'] + ); + } + return $vals; } @@ -267,6 +313,7 @@ class ApiQueryUserInfo extends ApiQueryBase { 'acceptlang', 'registrationdate', 'unreadcount', + 'centralids', ), ApiBase::PARAM_HELP_MSG_PER_VALUE => array( 'unreadcount' => array( @@ -275,7 +322,8 @@ class ApiQueryUserInfo extends ApiQueryBase { self::WL_UNREAD_LIMIT . '+', ), ), - ) + ), + 'attachedwiki' => null, ); } diff --git a/includes/api/ApiQueryUsers.php b/includes/api/ApiQueryUsers.php index a826c1b0a2..db5fb65621 100644 --- a/includes/api/ApiQueryUsers.php +++ b/includes/api/ApiQueryUsers.php @@ -48,6 +48,7 @@ class ApiQueryUsers extends ApiQueryBase { 'registration', 'emailable', 'gender', + 'centralids', ); public function __construct( ApiQuery $query, $moduleName ) { @@ -213,6 +214,12 @@ class ApiQueryUsers extends ApiQueryBase { $data[$name]['gender'] = $gender; } + if ( isset( $this->prop['centralids'] ) ) { + $data[$name] += ApiQueryUserInfo::getCentralUserInfo( + $this->getConfig(), $user, $params['attachedwiki'] + ); + } + if ( !is_null( $params['token'] ) ) { $tokenFunctions = $this->getTokenFunctions(); foreach ( $params['token'] as $t ) { @@ -304,9 +311,11 @@ class ApiQueryUsers extends ApiQueryBase { 'registration', 'emailable', 'gender', + 'centralids', ), ApiBase::PARAM_HELP_MSG_PER_VALUE => array(), ), + 'attachedwiki' => null, 'users' => array( ApiBase::PARAM_ISMULTI => true ), diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 4c2d0be0f0..897c05ed4b 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -545,9 +545,11 @@ "apihelp-query+allusers-paramvalue-prop-rights": "Lists rights that the user has.", "apihelp-query+allusers-paramvalue-prop-editcount": "Adds the edit count of the user.", "apihelp-query+allusers-paramvalue-prop-registration": "Adds the timestamp of when the user registered if available (may be blank).", + "apihelp-query+allusers-paramvalue-prop-centralids": "Adds the central IDs and attachment status for the user.", "apihelp-query+allusers-param-limit": "How many total user names to return.", "apihelp-query+allusers-param-witheditsonly": "Only list users who have made edits.", "apihelp-query+allusers-param-activeusers": "Only list users active in the last $1 {{PLURAL:$1|day|days}}.", + "apihelp-query+allusers-param-attachedwiki": "With $1prop=centralids, also indicate whether the user is attached with the wiki identified by this ID.", "apihelp-query+allusers-example-Y": "List users starting at Y.", "apihelp-query+backlinks-description": "Find all pages that link to the given page.", @@ -1177,6 +1179,8 @@ "apihelp-query+userinfo-paramvalue-prop-acceptlang": "Echoes the Accept-Language header sent by the client in a structured format.", "apihelp-query+userinfo-paramvalue-prop-registrationdate": "Adds the user's registration date.", "apihelp-query+userinfo-paramvalue-prop-unreadcount": "Adds the count of unread pages on the user's watchlist (maximum $1; returns $2 if more).", + "apihelp-query+userinfo-paramvalue-prop-centralids": "Adds the central IDs and attachment status for the user.", + "apihelp-query+userinfo-param-attachedwiki": "With $1prop=centralids, indicate whether the user is attached with the wiki identified by this ID.", "apihelp-query+userinfo-example-simple": "Get information about the current user.", "apihelp-query+userinfo-example-data": "Get additional information about the current user.", @@ -1190,6 +1194,8 @@ "apihelp-query+users-paramvalue-prop-registration": "Adds the user's registration timestamp.", "apihelp-query+users-paramvalue-prop-emailable": "Tags if the user can and wants to receive email through [[Special:Emailuser]].", "apihelp-query+users-paramvalue-prop-gender": "Tags the gender of the user. Returns \"male\", \"female\", or \"unknown\".", + "apihelp-query+users-paramvalue-prop-centralids": "Adds the central IDs and attachment status for the user.", + "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-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 047a806e16..f4ec17796a 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -513,6 +513,8 @@ "apihelp-query+allusers-paramvalue-prop-rights": "{{doc-apihelp-paramvalue|query+allusers|prop|rights}}", "apihelp-query+allusers-paramvalue-prop-editcount": "{{doc-apihelp-paramvalue|query+allusers|prop|editcount}}", "apihelp-query+allusers-paramvalue-prop-registration": "{{doc-apihelp-paramvalue|query+allusers|prop|registration}}", + "apihelp-query+allusers-paramvalue-prop-centralids": "{{doc-apihelp-paramvalue|query+allusers|prop|centralids}}", + "apihelp-query+allusers-param-attachedwiki": "{{doc-apihelp-param|query+allusers|attachedwiki}}", "apihelp-query+allusers-param-limit": "{{doc-apihelp-param|query+allusers|limit}}", "apihelp-query+allusers-param-witheditsonly": "{{doc-apihelp-param|query+allusers|witheditsonly}}", "apihelp-query+allusers-param-activeusers": "{{doc-apihelp-param|query+allusers|activeusers|params=* $1 - Value of [[mw:Manual:$wgActiveUserDays]]|paramstart=2}}", @@ -1098,6 +1100,8 @@ "apihelp-query+userinfo-paramvalue-prop-acceptlang": "{{doc-apihelp-paramvalue|query+userinfo|prop|acceptlang}}", "apihelp-query+userinfo-paramvalue-prop-registrationdate": "{{doc-apihelp-paramvalue|query+userinfo|prop|registrationdate}}", "apihelp-query+userinfo-paramvalue-prop-unreadcount": "{{doc-apihelp-paramvalue|query+userinfo|prop|unreadcount|params=* $1 - Maximum value for the \"unreadcount\" property.\n* $2 - Return value when there are more unread pages.|paramstart=3}}", + "apihelp-query+userinfo-paramvalue-prop-centralids": "{{doc-apihelp-paramvalue|query+userinfo|prop|centralids}}", + "apihelp-query+userinfo-param-attachedwiki": "{{doc-apihelp-param|query+userinfo|attachedwiki}}", "apihelp-query+userinfo-example-simple": "{{doc-apihelp-example|query+userinfo}}", "apihelp-query+userinfo-example-data": "{{doc-apihelp-example|query+userinfo}}", "apihelp-query+users-description": "{{doc-apihelp-description|query+users}}", @@ -1110,6 +1114,8 @@ "apihelp-query+users-paramvalue-prop-registration": "{{doc-apihelp-paramvalue|query+users|prop|registration}}", "apihelp-query+users-paramvalue-prop-emailable": "{{doc-apihelp-paramvalue|query+users|prop|emailable}}", "apihelp-query+users-paramvalue-prop-gender": "{{doc-apihelp-paramvalue|query+users|prop|gender}}", + "apihelp-query+users-paramvalue-prop-centralids": "{{doc-apihelp-paramvalue|query+users|prop|centralids}}", + "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-token": "{{doc-apihelp-param|query+users|token}}", "apihelp-query+users-example-simple": "{{doc-apihelp-example|query+users}}", -- 2.20.1