Merge "Allow to send the memory usage with UDP profiler."
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to get information about a list of users
29 *
30 * @ingroup API
31 */
32 class ApiQueryUsers extends ApiQueryBase {
33
34 private $tokenFunctions, $prop;
35
36 /**
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.
39 * @var array
40 */
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
44 'groups',
45 'implicitgroups',
46 'rights',
47 'editcount',
48 'registration',
49 'emailable',
50 'gender',
51 );
52
53 public function __construct( ApiQuery $query, $moduleName ) {
54 parent::__construct( $query, $moduleName, 'us' );
55 }
56
57 /**
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 * @return array Array of tokenname => function
62 */
63 protected function getTokenFunctions() {
64 // Don't call the hooks twice
65 if ( isset( $this->tokenFunctions ) ) {
66 return $this->tokenFunctions;
67 }
68
69 // If we're in JSON callback mode, no tokens can be obtained
70 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
71 return array();
72 }
73
74 $this->tokenFunctions = array(
75 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
76 );
77 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
78
79 return $this->tokenFunctions;
80 }
81
82 /**
83 * @param User $user
84 * @return string
85 */
86 public static function getUserrightsToken( $user ) {
87 global $wgUser;
88
89 // Since the permissions check for userrights is non-trivial,
90 // don't bother with it here
91 return $wgUser->getEditToken( $user->getName() );
92 }
93
94 public function execute() {
95 $params = $this->extractRequestParams();
96
97 if ( !is_null( $params['prop'] ) ) {
98 $this->prop = array_flip( $params['prop'] );
99 } else {
100 $this->prop = array();
101 }
102
103 $users = (array)$params['users'];
104 $goodNames = $done = array();
105 $result = $this->getResult();
106 // Canonicalize user names
107 foreach ( $users as $u ) {
108 $n = User::getCanonicalName( $u );
109 if ( $n === false || $n === '' ) {
110 $vals = array( 'name' => $u, 'invalid' => '' );
111 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
112 null, $vals );
113 if ( !$fit ) {
114 $this->setContinueEnumParameter( 'users',
115 implode( '|', array_diff( $users, $done ) ) );
116 $goodNames = array();
117 break;
118 }
119 $done[] = $u;
120 } else {
121 $goodNames[] = $n;
122 }
123 }
124
125 $result = $this->getResult();
126
127 if ( count( $goodNames ) ) {
128 $this->addTables( 'user' );
129 $this->addFields( User::selectFields() );
130 $this->addWhereFld( 'user_name', $goodNames );
131
132 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
133
134 $data = array();
135 $res = $this->select( __METHOD__ );
136 $this->resetQueryParams();
137
138 // get user groups if needed
139 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
140 $userGroups = array();
141
142 $this->addTables( 'user' );
143 $this->addWhereFld( 'user_name', $goodNames );
144 $this->addTables( 'user_groups' );
145 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
146 $this->addFields( array( 'user_name', 'ug_group' ) );
147 $userGroupsRes = $this->select( __METHOD__ );
148
149 foreach ( $userGroupsRes as $row ) {
150 $userGroups[$row->user_name][] = $row->ug_group;
151 }
152 }
153
154 foreach ( $res as $row ) {
155 // create user object and pass along $userGroups if set
156 // that reduces the number of database queries needed in User dramatically
157 if ( !isset( $userGroups ) ) {
158 $user = User::newFromRow( $row );
159 } else {
160 if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
161 $userGroups[$row->user_name] = array();
162 }
163 $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) );
164 }
165 $name = $user->getName();
166
167 $data[$name]['userid'] = $user->getId();
168 $data[$name]['name'] = $name;
169
170 if ( isset( $this->prop['editcount'] ) ) {
171 $data[$name]['editcount'] = $user->getEditCount();
172 }
173
174 if ( isset( $this->prop['registration'] ) ) {
175 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
176 }
177
178 if ( isset( $this->prop['groups'] ) ) {
179 $data[$name]['groups'] = $user->getEffectiveGroups();
180 }
181
182 if ( isset( $this->prop['implicitgroups'] ) ) {
183 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
184 }
185
186 if ( isset( $this->prop['rights'] ) ) {
187 $data[$name]['rights'] = $user->getRights();
188 }
189 if ( $row->ipb_deleted ) {
190 $data[$name]['hidden'] = '';
191 }
192 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
193 $data[$name]['blockid'] = $row->ipb_id;
194 $data[$name]['blockedby'] = $row->ipb_by_text;
195 $data[$name]['blockedbyid'] = $row->ipb_by;
196 $data[$name]['blockreason'] = $row->ipb_reason;
197 $data[$name]['blockexpiry'] = $row->ipb_expiry;
198 }
199
200 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
201 $data[$name]['emailable'] = '';
202 }
203
204 if ( isset( $this->prop['gender'] ) ) {
205 $gender = $user->getOption( 'gender' );
206 if ( strval( $gender ) === '' ) {
207 $gender = 'unknown';
208 }
209 $data[$name]['gender'] = $gender;
210 }
211
212 if ( !is_null( $params['token'] ) ) {
213 $tokenFunctions = $this->getTokenFunctions();
214 foreach ( $params['token'] as $t ) {
215 $val = call_user_func( $tokenFunctions[$t], $user );
216 if ( $val === false ) {
217 $this->setWarning( "Action '$t' is not allowed for the current user" );
218 } else {
219 $data[$name][$t . 'token'] = $val;
220 }
221 }
222 }
223 }
224 }
225
226 $context = $this->getContext();
227 // Second pass: add result data to $retval
228 foreach ( $goodNames as $u ) {
229 if ( !isset( $data[$u] ) ) {
230 $data[$u] = array( 'name' => $u );
231 $urPage = new UserrightsPage;
232 $urPage->setContext( $context );
233 $iwUser = $urPage->fetchUser( $u );
234
235 if ( $iwUser instanceof UserRightsProxy ) {
236 $data[$u]['interwiki'] = '';
237
238 if ( !is_null( $params['token'] ) ) {
239 $tokenFunctions = $this->getTokenFunctions();
240
241 foreach ( $params['token'] as $t ) {
242 $val = call_user_func( $tokenFunctions[$t], $iwUser );
243 if ( $val === false ) {
244 $this->setWarning( "Action '$t' is not allowed for the current user" );
245 } else {
246 $data[$u][$t . 'token'] = $val;
247 }
248 }
249 }
250 } else {
251 $data[$u]['missing'] = '';
252 }
253 } else {
254 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
255 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
256 }
257 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
258 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
259 }
260 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
261 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
262 }
263 }
264
265 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
266 null, $data[$u] );
267 if ( !$fit ) {
268 $this->setContinueEnumParameter( 'users',
269 implode( '|', array_diff( $users, $done ) ) );
270 break;
271 }
272 $done[] = $u;
273 }
274 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
275 }
276
277 /**
278 * Gets all the groups that a user is automatically a member of (implicit groups)
279 *
280 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
281 * @param User $user
282 * @return array
283 */
284 public static function getAutoGroups( $user ) {
285 wfDeprecated( __METHOD__, '1.20' );
286
287 return $user->getAutomaticGroups();
288 }
289
290 public function getCacheMode( $params ) {
291 if ( isset( $params['token'] ) ) {
292 return 'private';
293 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
294 return 'anon-public-user-private';
295 } else {
296 return 'public';
297 }
298 }
299
300 public function getAllowedParams() {
301 return array(
302 'prop' => array(
303 ApiBase::PARAM_DFLT => null,
304 ApiBase::PARAM_ISMULTI => true,
305 ApiBase::PARAM_TYPE => array(
306 'blockinfo',
307 'groups',
308 'implicitgroups',
309 'rights',
310 'editcount',
311 'registration',
312 'emailable',
313 'gender',
314 )
315 ),
316 'users' => array(
317 ApiBase::PARAM_ISMULTI => true
318 ),
319 'token' => array(
320 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
321 ApiBase::PARAM_ISMULTI => true
322 ),
323 );
324 }
325
326 public function getParamDescription() {
327 return array(
328 'prop' => array(
329 'What pieces of information to include',
330 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
331 ' groups - Lists all the groups the user(s) belongs to',
332 ' implicitgroups - Lists all the groups a user is automatically a member of',
333 ' rights - Lists all the rights the user(s) has',
334 ' editcount - Adds the user\'s edit count',
335 ' registration - Adds the user\'s registration timestamp',
336 ' emailable - Tags if the user can and wants to receive ' .
337 'email through [[Special:Emailuser]]',
338 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
339 ),
340 'users' => 'A list of users to obtain the same information for',
341 'token' => 'Which tokens to obtain for each user',
342 );
343 }
344
345 public function getDescription() {
346 return 'Get information about a list of users.';
347 }
348
349 public function getExamples() {
350 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
351 }
352
353 public function getHelpUrls() {
354 return 'https://www.mediawiki.org/wiki/API:Users';
355 }
356 }