Documentation
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to get information about a list of users
34 *
35 * @ingroup API
36 */
37 class ApiQueryUsers extends ApiQueryBase {
38
39 private $tokenFunctions, $prop;
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'us' );
43 }
44
45 /**
46 * Get an array mapping token names to their handler functions.
47 * The prototype for a token function is func($user)
48 * it should return a token or false (permission denied)
49 * @return Array tokenname => function
50 */
51 protected function getTokenFunctions() {
52 // Don't call the hooks twice
53 if ( isset( $this->tokenFunctions ) ) {
54 return $this->tokenFunctions;
55 }
56
57 // If we're in JSON callback mode, no tokens can be obtained
58 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
59 return array();
60 }
61
62 $this->tokenFunctions = array(
63 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
64 );
65 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
66 return $this->tokenFunctions;
67 }
68
69 /**
70 * @param $user User
71 * @return String
72 */
73 public static function getUserrightsToken( $user ) {
74 global $wgUser;
75 // Since the permissions check for userrights is non-trivial,
76 // don't bother with it here
77 return $wgUser->getEditToken( $user->getName() );
78 }
79
80 public function execute() {
81 $params = $this->extractRequestParams();
82
83 if ( !is_null( $params['prop'] ) ) {
84 $this->prop = array_flip( $params['prop'] );
85 } else {
86 $this->prop = array();
87 }
88
89 $users = (array)$params['users'];
90 $goodNames = $done = array();
91 $result = $this->getResult();
92 // Canonicalize user names
93 foreach ( $users as $u ) {
94 $n = User::getCanonicalName( $u );
95 if ( $n === false || $n === '' ) {
96 $vals = array( 'name' => $u, 'invalid' => '' );
97 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
98 null, $vals );
99 if ( !$fit ) {
100 $this->setContinueEnumParameter( 'users',
101 implode( '|', array_diff( $users, $done ) ) );
102 $goodNames = array();
103 break;
104 }
105 $done[] = $u;
106 } else {
107 $goodNames[] = $n;
108 }
109 }
110
111 $result = $this->getResult();
112
113 if ( count( $goodNames ) ) {
114 $this->addTables( 'user' );
115 $this->addFields( '*' );
116 $this->addWhereFld( 'user_name', $goodNames );
117
118 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
119 $this->addTables( 'user_groups' );
120 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
121 $this->addFields( 'ug_group' );
122 }
123
124 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
125
126 $data = array();
127 $res = $this->select( __METHOD__ );
128
129 foreach ( $res as $row ) {
130 $user = User::newFromRow( $row );
131 $name = $user->getName();
132
133 $data[$name]['userid'] = $user->getId();
134 $data[$name]['name'] = $name;
135
136 if ( isset( $this->prop['editcount'] ) ) {
137 $data[$name]['editcount'] = intval( $user->getEditCount() );
138 }
139
140 if ( isset( $this->prop['registration'] ) ) {
141 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
142 }
143
144 if ( isset( $this->prop['groups'] ) ) {
145 if ( !isset( $data[$name]['groups'] ) ) {
146 $data[$name]['groups'] = self::getAutoGroups( $user );
147 }
148
149 if ( !is_null( $row->ug_group ) ) {
150 // This row contains only one group, others will be added from other rows
151 $data[$name]['groups'][] = $row->ug_group;
152 }
153 }
154
155 if ( isset( $this->prop['implicitgroups'] ) && !isset( $data[$name]['implicitgroups'] ) ) {
156 $data[$name]['implicitgroups'] = self::getAutoGroups( $user );
157 }
158
159 if ( isset( $this->prop['rights'] ) ) {
160 if ( !isset( $data[$name]['rights'] ) ) {
161 $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
162 }
163
164 if ( !is_null( $row->ug_group ) ) {
165 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
166 User::getGroupPermissions( array( $row->ug_group ) ) ) );
167 }
168 }
169 if ( $row->ipb_deleted ) {
170 $data[$name]['hidden'] = '';
171 }
172 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
173 $data[$name]['blockedby'] = $row->ipb_by_text;
174 $data[$name]['blockreason'] = $row->ipb_reason;
175 $data[$name]['blockexpiry'] = $row->ipb_expiry;
176 }
177
178 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
179 $data[$name]['emailable'] = '';
180 }
181
182 if ( isset( $this->prop['gender'] ) ) {
183 $gender = $user->getOption( 'gender' );
184 if ( strval( $gender ) === '' ) {
185 $gender = 'unknown';
186 }
187 $data[$name]['gender'] = $gender;
188 }
189
190 if ( !is_null( $params['token'] ) ) {
191 $tokenFunctions = $this->getTokenFunctions();
192 foreach ( $params['token'] as $t ) {
193 $val = call_user_func( $tokenFunctions[$t], $user );
194 if ( $val === false ) {
195 $this->setWarning( "Action '$t' is not allowed for the current user" );
196 } else {
197 $data[$name][$t . 'token'] = $val;
198 }
199 }
200 }
201 }
202 }
203
204 // Second pass: add result data to $retval
205 foreach ( $goodNames as $u ) {
206 if ( !isset( $data[$u] ) ) {
207 $data[$u] = array( 'name' => $u );
208 $urPage = new UserrightsPage;
209 $iwUser = $urPage->fetchUser( $u );
210
211 if ( $iwUser instanceof UserRightsProxy ) {
212 $data[$u]['interwiki'] = '';
213
214 if ( !is_null( $params['token'] ) ) {
215 $tokenFunctions = $this->getTokenFunctions();
216
217 foreach ( $params['token'] as $t ) {
218 $val = call_user_func( $tokenFunctions[$t], $iwUser );
219 if ( $val === false ) {
220 $this->setWarning( "Action '$t' is not allowed for the current user" );
221 } else {
222 $data[$u][$t . 'token'] = $val;
223 }
224 }
225 }
226 } else {
227 $data[$u]['missing'] = '';
228 }
229 } else {
230 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
231 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
232 }
233 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
234 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
235 }
236 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
237 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
238 }
239 }
240
241 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
242 null, $data[$u] );
243 if ( !$fit ) {
244 $this->setContinueEnumParameter( 'users',
245 implode( '|', array_diff( $users, $done ) ) );
246 break;
247 }
248 $done[] = $u;
249 }
250 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
251 }
252
253 /**
254 * Gets all the groups that a user is automatically a member of (implicit groups)
255 * @param $user User
256 * @return array
257 */
258 public static function getAutoGroups( $user ) {
259 $groups = array();
260 $groups[] = '*';
261
262 if ( !$user->isAnon() ) {
263 $groups[] = 'user';
264 }
265
266 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
267 }
268
269 public function getCacheMode( $params ) {
270 if ( isset( $params['token'] ) ) {
271 return 'private';
272 } else {
273 return 'anon-public-user-private';
274 }
275 }
276
277 public function getAllowedParams() {
278 return array(
279 'prop' => array(
280 ApiBase::PARAM_DFLT => null,
281 ApiBase::PARAM_ISMULTI => true,
282 ApiBase::PARAM_TYPE => array(
283 'blockinfo',
284 'groups',
285 'implicitgroups',
286 'rights',
287 'editcount',
288 'registration',
289 'emailable',
290 'gender',
291 )
292 ),
293 'users' => array(
294 ApiBase::PARAM_ISMULTI => true
295 ),
296 'token' => array(
297 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
298 ApiBase::PARAM_ISMULTI => true
299 ),
300 );
301 }
302
303 public function getParamDescription() {
304 return array(
305 'prop' => array(
306 'What pieces of information to include',
307 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
308 ' groups - Lists all the groups the user(s) belongs to',
309 ' implicitgroups - Lists all the groups a user is automatically a member of',
310 ' rights - Lists all the rights the user(s) has',
311 ' editcount - Adds the user\'s edit count',
312 ' registration - Adds the user\'s registration timestamp',
313 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
314 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
315 ),
316 'users' => 'A list of users to obtain the same information for',
317 'token' => 'Which tokens to obtain for each user',
318 );
319 }
320
321 public function getDescription() {
322 return 'Get information about a list of users';
323 }
324
325 public function getExamples() {
326 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
327 }
328
329 public function getHelpUrls() {
330 return 'http://www.mediawiki.org/wiki/API:Users';
331 }
332
333 public function getVersion() {
334 return __CLASS__ . ': $Id$';
335 }
336 }