5 * Created on July 30, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * Query module to get information about a list of users
32 class ApiQueryUsers
extends ApiQueryBase
{
34 private $tokenFunctions, $prop;
37 * Properties whose contents does not depend on who is looking at them. If the usprops field
38 * contains anything not listed here, the cache mode will never be public for logged-in users.
41 protected static $publicProps = array(
42 // everything except 'blockinfo' which might show hidden records if the user
43 // making the request has the appropriate permissions
54 public function __construct( ApiQuery
$query, $moduleName ) {
55 parent
::__construct( $query, $moduleName, 'us' );
59 * Get an array mapping token names to their handler functions.
60 * The prototype for a token function is func($user)
61 * it should return a token or false (permission denied)
62 * @deprecated since 1.24
63 * @return array Array of tokenname => function
65 protected function getTokenFunctions() {
66 // Don't call the hooks twice
67 if ( isset( $this->tokenFunctions
) ) {
68 return $this->tokenFunctions
;
71 // If we're in a mode that breaks the same-origin policy, no tokens can
73 if ( $this->lacksSameOriginSecurity() ) {
77 $this->tokenFunctions
= array(
78 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
80 Hooks
::run( 'APIQueryUsersTokens', array( &$this->tokenFunctions
) );
82 return $this->tokenFunctions
;
86 * @deprecated since 1.24
90 public static function getUserrightsToken( $user ) {
93 // Since the permissions check for userrights is non-trivial,
94 // don't bother with it here
95 return $wgUser->getEditToken( $user->getName() );
98 public function execute() {
99 $params = $this->extractRequestParams();
101 if ( !is_null( $params['prop'] ) ) {
102 $this->prop
= array_flip( $params['prop'] );
104 $this->prop
= array();
107 $users = (array)$params['users'];
108 $goodNames = $done = array();
109 $result = $this->getResult();
110 // Canonicalize user names
111 foreach ( $users as $u ) {
112 $n = User
::getCanonicalName( $u );
113 if ( $n === false ||
$n === '' ) {
114 $vals = array( 'name' => $u, 'invalid' => true );
115 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
118 $this->setContinueEnumParameter( 'users',
119 implode( '|', array_diff( $users, $done ) ) );
120 $goodNames = array();
129 $result = $this->getResult();
131 if ( count( $goodNames ) ) {
132 $this->addTables( 'user' );
133 $this->addFields( User
::selectFields() );
134 $this->addWhereFld( 'user_name', $goodNames );
136 $this->showHiddenUsersAddBlockInfo( isset( $this->prop
['blockinfo'] ) );
139 $res = $this->select( __METHOD__
);
140 $this->resetQueryParams();
142 // get user groups if needed
143 if ( isset( $this->prop
['groups'] ) ||
isset( $this->prop
['rights'] ) ) {
144 $userGroups = array();
146 $this->addTables( 'user' );
147 $this->addWhereFld( 'user_name', $goodNames );
148 $this->addTables( 'user_groups' );
149 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
150 $this->addFields( array( 'user_name', 'ug_group' ) );
151 $userGroupsRes = $this->select( __METHOD__
);
153 foreach ( $userGroupsRes as $row ) {
154 $userGroups[$row->user_name
][] = $row->ug_group
;
158 foreach ( $res as $row ) {
159 // create user object and pass along $userGroups if set
160 // that reduces the number of database queries needed in User dramatically
161 if ( !isset( $userGroups ) ) {
162 $user = User
::newFromRow( $row );
164 if ( !isset( $userGroups[$row->user_name
] ) ||
!is_array( $userGroups[$row->user_name
] ) ) {
165 $userGroups[$row->user_name
] = array();
167 $user = User
::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name
] ) );
169 $name = $user->getName();
171 $data[$name]['userid'] = $user->getId();
172 $data[$name]['name'] = $name;
174 if ( isset( $this->prop
['editcount'] ) ) {
175 $data[$name]['editcount'] = $user->getEditCount();
178 if ( isset( $this->prop
['registration'] ) ) {
179 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
182 if ( isset( $this->prop
['groups'] ) ) {
183 $data[$name]['groups'] = $user->getEffectiveGroups();
186 if ( isset( $this->prop
['implicitgroups'] ) ) {
187 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
190 if ( isset( $this->prop
['rights'] ) ) {
191 $data[$name]['rights'] = $user->getRights();
193 if ( $row->ipb_deleted
) {
194 $data[$name]['hidden'] = true;
196 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->ipb_by_text
) ) {
197 $data[$name]['blockid'] = (int)$row->ipb_id
;
198 $data[$name]['blockedby'] = $row->ipb_by_text
;
199 $data[$name]['blockedbyid'] = (int)$row->ipb_by
;
200 $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
201 $data[$name]['blockreason'] = $row->ipb_reason
;
202 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
205 if ( isset( $this->prop
['emailable'] ) ) {
206 $data[$name]['emailable'] = $user->canReceiveEmail();
209 if ( isset( $this->prop
['gender'] ) ) {
210 $gender = $user->getOption( 'gender' );
211 if ( strval( $gender ) === '' ) {
214 $data[$name]['gender'] = $gender;
217 if ( isset( $this->prop
['centralids'] ) ) {
218 $data[$name] +
= ApiQueryUserInfo
::getCentralUserInfo(
219 $this->getConfig(), $user, $params['attachedwiki']
223 if ( !is_null( $params['token'] ) ) {
224 $tokenFunctions = $this->getTokenFunctions();
225 foreach ( $params['token'] as $t ) {
226 $val = call_user_func( $tokenFunctions[$t], $user );
227 if ( $val === false ) {
228 $this->setWarning( "Action '$t' is not allowed for the current user" );
230 $data[$name][$t . 'token'] = $val;
237 $context = $this->getContext();
238 // Second pass: add result data to $retval
239 foreach ( $goodNames as $u ) {
240 if ( !isset( $data[$u] ) ) {
241 $data[$u] = array( 'name' => $u );
242 $urPage = new UserrightsPage
;
243 $urPage->setContext( $context );
244 $iwUser = $urPage->fetchUser( $u );
246 if ( $iwUser instanceof UserRightsProxy
) {
247 $data[$u]['interwiki'] = true;
249 if ( !is_null( $params['token'] ) ) {
250 $tokenFunctions = $this->getTokenFunctions();
252 foreach ( $params['token'] as $t ) {
253 $val = call_user_func( $tokenFunctions[$t], $iwUser );
254 if ( $val === false ) {
255 $this->setWarning( "Action '$t' is not allowed for the current user" );
257 $data[$u][$t . 'token'] = $val;
262 $data[$u]['missing'] = true;
265 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
266 ApiResult
::setArrayType( $data[$u]['groups'], 'array' );
267 ApiResult
::setIndexedTagName( $data[$u]['groups'], 'g' );
269 if ( isset( $this->prop
['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
270 ApiResult
::setArrayType( $data[$u]['implicitgroups'], 'array' );
271 ApiResult
::setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
273 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
274 ApiResult
::setArrayType( $data[$u]['rights'], 'array' );
275 ApiResult
::setIndexedTagName( $data[$u]['rights'], 'r' );
279 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
282 $this->setContinueEnumParameter( 'users',
283 implode( '|', array_diff( $users, $done ) ) );
288 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'user' );
291 public function getCacheMode( $params ) {
292 if ( isset( $params['token'] ) ) {
294 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
295 return 'anon-public-user-private';
301 public function getAllowedParams() {
304 ApiBase
::PARAM_ISMULTI
=> true,
305 ApiBase
::PARAM_TYPE
=> array(
316 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(),
318 'attachedwiki' => null,
320 ApiBase
::PARAM_ISMULTI
=> true
323 ApiBase
::PARAM_DEPRECATED
=> true,
324 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
325 ApiBase
::PARAM_ISMULTI
=> true
330 protected function getExamplesMessages() {
332 'action=query&list=users&ususers=Example&usprop=groups|editcount|gender'
333 => 'apihelp-query+users-example-simple',
337 public function getHelpUrls() {
338 return 'https://www.mediawiki.org/wiki/API:Users';