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
53 public function __construct( ApiQuery
$query, $moduleName ) {
54 parent
::__construct( $query, $moduleName, 'us' );
58 * Get an array mapping token names to their handler functions.
59 * The prototype for a token function is func($user)
60 * it should return a token or false (permission denied)
61 * @deprecated since 1.24
62 * @return array Array of tokenname => function
64 protected function getTokenFunctions() {
65 // Don't call the hooks twice
66 if ( isset( $this->tokenFunctions
) ) {
67 return $this->tokenFunctions
;
70 // If we're in JSON callback mode, no tokens can be obtained
71 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
75 $this->tokenFunctions
= array(
76 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
78 Hooks
::run( 'APIQueryUsersTokens', array( &$this->tokenFunctions
) );
80 return $this->tokenFunctions
;
84 * @deprecated since 1.24
88 public static function getUserrightsToken( $user ) {
91 // Since the permissions check for userrights is non-trivial,
92 // don't bother with it here
93 return $wgUser->getEditToken( $user->getName() );
96 public function execute() {
97 $params = $this->extractRequestParams();
99 if ( !is_null( $params['prop'] ) ) {
100 $this->prop
= array_flip( $params['prop'] );
102 $this->prop
= array();
105 $users = (array)$params['users'];
106 $goodNames = $done = array();
107 $result = $this->getResult();
108 // Canonicalize user names
109 foreach ( $users as $u ) {
110 $n = User
::getCanonicalName( $u );
111 if ( $n === false ||
$n === '' ) {
112 $vals = array( 'name' => $u, 'invalid' => '' );
113 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
116 $this->setContinueEnumParameter( 'users',
117 implode( '|', array_diff( $users, $done ) ) );
118 $goodNames = array();
127 $result = $this->getResult();
129 if ( count( $goodNames ) ) {
130 $this->addTables( 'user' );
131 $this->addFields( User
::selectFields() );
132 $this->addWhereFld( 'user_name', $goodNames );
134 $this->showHiddenUsersAddBlockInfo( isset( $this->prop
['blockinfo'] ) );
137 $res = $this->select( __METHOD__
);
138 $this->resetQueryParams();
140 // get user groups if needed
141 if ( isset( $this->prop
['groups'] ) ||
isset( $this->prop
['rights'] ) ) {
142 $userGroups = array();
144 $this->addTables( 'user' );
145 $this->addWhereFld( 'user_name', $goodNames );
146 $this->addTables( 'user_groups' );
147 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
148 $this->addFields( array( 'user_name', 'ug_group' ) );
149 $userGroupsRes = $this->select( __METHOD__
);
151 foreach ( $userGroupsRes as $row ) {
152 $userGroups[$row->user_name
][] = $row->ug_group
;
156 foreach ( $res as $row ) {
157 // create user object and pass along $userGroups if set
158 // that reduces the number of database queries needed in User dramatically
159 if ( !isset( $userGroups ) ) {
160 $user = User
::newFromRow( $row );
162 if ( !isset( $userGroups[$row->user_name
] ) ||
!is_array( $userGroups[$row->user_name
] ) ) {
163 $userGroups[$row->user_name
] = array();
165 $user = User
::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name
] ) );
167 $name = $user->getName();
169 $data[$name]['userid'] = $user->getId();
170 $data[$name]['name'] = $name;
172 if ( isset( $this->prop
['editcount'] ) ) {
173 $data[$name]['editcount'] = $user->getEditCount();
176 if ( isset( $this->prop
['registration'] ) ) {
177 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
180 if ( isset( $this->prop
['groups'] ) ) {
181 $data[$name]['groups'] = $user->getEffectiveGroups();
184 if ( isset( $this->prop
['implicitgroups'] ) ) {
185 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
188 if ( isset( $this->prop
['rights'] ) ) {
189 $data[$name]['rights'] = $user->getRights();
191 if ( $row->ipb_deleted
) {
192 $data[$name]['hidden'] = '';
194 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->ipb_by_text
) ) {
195 $data[$name]['blockid'] = $row->ipb_id
;
196 $data[$name]['blockedby'] = $row->ipb_by_text
;
197 $data[$name]['blockedbyid'] = $row->ipb_by
;
198 $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $row->ipb_timestamp
);
199 $data[$name]['blockreason'] = $row->ipb_reason
;
200 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
203 if ( isset( $this->prop
['emailable'] ) && $user->canReceiveEmail() ) {
204 $data[$name]['emailable'] = '';
207 if ( isset( $this->prop
['gender'] ) ) {
208 $gender = $user->getOption( 'gender' );
209 if ( strval( $gender ) === '' ) {
212 $data[$name]['gender'] = $gender;
215 if ( !is_null( $params['token'] ) ) {
216 $tokenFunctions = $this->getTokenFunctions();
217 foreach ( $params['token'] as $t ) {
218 $val = call_user_func( $tokenFunctions[$t], $user );
219 if ( $val === false ) {
220 $this->setWarning( "Action '$t' is not allowed for the current user" );
222 $data[$name][$t . 'token'] = $val;
229 $context = $this->getContext();
230 // Second pass: add result data to $retval
231 foreach ( $goodNames as $u ) {
232 if ( !isset( $data[$u] ) ) {
233 $data[$u] = array( 'name' => $u );
234 $urPage = new UserrightsPage
;
235 $urPage->setContext( $context );
236 $iwUser = $urPage->fetchUser( $u );
238 if ( $iwUser instanceof UserRightsProxy
) {
239 $data[$u]['interwiki'] = '';
241 if ( !is_null( $params['token'] ) ) {
242 $tokenFunctions = $this->getTokenFunctions();
244 foreach ( $params['token'] as $t ) {
245 $val = call_user_func( $tokenFunctions[$t], $iwUser );
246 if ( $val === false ) {
247 $this->setWarning( "Action '$t' is not allowed for the current user" );
249 $data[$u][$t . 'token'] = $val;
254 $data[$u]['missing'] = '';
257 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
258 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
260 if ( isset( $this->prop
['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
261 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
263 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
264 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
268 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
271 $this->setContinueEnumParameter( 'users',
272 implode( '|', array_diff( $users, $done ) ) );
277 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
280 public function getCacheMode( $params ) {
281 if ( isset( $params['token'] ) ) {
283 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
284 return 'anon-public-user-private';
290 public function getAllowedParams() {
293 ApiBase
::PARAM_DFLT
=> null,
294 ApiBase
::PARAM_ISMULTI
=> true,
295 ApiBase
::PARAM_TYPE
=> array(
307 ApiBase
::PARAM_ISMULTI
=> true
310 ApiBase
::PARAM_DEPRECATED
=> true,
311 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
312 ApiBase
::PARAM_ISMULTI
=> true
317 protected function getExamplesMessages() {
319 'action=query&list=users&ususers=Example&usprop=groups|editcount|gender'
320 => 'apihelp-query+users-example-simple',
324 public function getHelpUrls() {
325 return 'https://www.mediawiki.org/wiki/API:Users';