Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[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->editToken( $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['rights'] ) ) {
156 if ( !isset( $data[$name]['rights'] ) ) {
157 $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
158 }
159
160 if ( !is_null( $row->ug_group ) ) {
161 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
162 User::getGroupPermissions( array( $row->ug_group ) ) ) );
163 }
164 }
165 if ( $row->ipb_deleted ) {
166 $data[$name]['hidden'] = '';
167 }
168 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
169 $data[$name]['blockedby'] = $row->ipb_by_text;
170 $data[$name]['blockreason'] = $row->ipb_reason;
171 $data[$name]['blockexpiry'] = $row->ipb_expiry;
172 }
173
174 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
175 $data[$name]['emailable'] = '';
176 }
177
178 if ( isset( $this->prop['gender'] ) ) {
179 $gender = $user->getOption( 'gender' );
180 if ( strval( $gender ) === '' ) {
181 $gender = 'unknown';
182 }
183 $data[$name]['gender'] = $gender;
184 }
185
186 if ( !is_null( $params['token'] ) ) {
187 $tokenFunctions = $this->getTokenFunctions();
188 foreach ( $params['token'] as $t ) {
189 $val = call_user_func( $tokenFunctions[$t], $user );
190 if ( $val === false ) {
191 $this->setWarning( "Action '$t' is not allowed for the current user" );
192 } else {
193 $data[$name][$t . 'token'] = $val;
194 }
195 }
196 }
197 }
198 }
199
200 // Second pass: add result data to $retval
201 foreach ( $goodNames as $u ) {
202 if ( !isset( $data[$u] ) ) {
203 $data[$u] = array( 'name' => $u );
204 $urPage = new UserrightsPage;
205 $iwUser = $urPage->fetchUser( $u );
206
207 if ( $iwUser instanceof UserRightsProxy ) {
208 $data[$u]['interwiki'] = '';
209
210 if ( !is_null( $params['token'] ) ) {
211 $tokenFunctions = $this->getTokenFunctions();
212
213 foreach ( $params['token'] as $t ) {
214 $val = call_user_func( $tokenFunctions[$t], $iwUser );
215 if ( $val === false ) {
216 $this->setWarning( "Action '$t' is not allowed for the current user" );
217 } else {
218 $data[$u][$t . 'token'] = $val;
219 }
220 }
221 }
222 } else {
223 $data[$u]['missing'] = '';
224 }
225 } else {
226 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
227 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
228 }
229 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
230 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
231 }
232 }
233
234 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
235 null, $data[$u] );
236 if ( !$fit ) {
237 $this->setContinueEnumParameter( 'users',
238 implode( '|', array_diff( $users, $done ) ) );
239 break;
240 }
241 $done[] = $u;
242 }
243 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
244 }
245
246 /**
247 * Gets all the groups that a user is automatically a member of (implicit groups)
248 * @param $user User
249 * @return array
250 */
251 public static function getAutoGroups( $user ) {
252 $groups = array();
253 $groups[] = '*';
254
255 if ( !$user->isAnon() ) {
256 $groups[] = 'user';
257 }
258
259 $builtGroups = array();
260 foreach( array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) ) as $i => $group ) {
261 $builtGroups[$i] = array( 'implicit' => '' );
262 ApiResult::setContent( $builtGroups[$i], $group );
263 }
264 return $builtGroups;
265 }
266
267 public function getCacheMode( $params ) {
268 if ( isset( $params['token'] ) ) {
269 return 'private';
270 } else {
271 return 'anon-public-user-private';
272 }
273 }
274
275 public function getAllowedParams() {
276 return array(
277 'prop' => array(
278 ApiBase::PARAM_DFLT => null,
279 ApiBase::PARAM_ISMULTI => true,
280 ApiBase::PARAM_TYPE => array(
281 'blockinfo',
282 'groups',
283 'rights',
284 'editcount',
285 'registration',
286 'emailable',
287 'gender',
288 )
289 ),
290 'users' => array(
291 ApiBase::PARAM_ISMULTI => true
292 ),
293 'token' => array(
294 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
295 ApiBase::PARAM_ISMULTI => true
296 ),
297 );
298 }
299
300 public function getParamDescription() {
301 return array(
302 'prop' => array(
303 'What pieces of information to include',
304 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
305 ' groups - Lists all the groups the user(s) belongs to',
306 ' rights - Lists all the rights the user(s) has',
307 ' editcount - Adds the user\'s edit count',
308 ' registration - Adds the user\'s registration timestamp',
309 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
310 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
311 ),
312 'users' => 'A list of users to obtain the same information for',
313 'token' => 'Which tokens to obtain for each user',
314 );
315 }
316
317 public function getDescription() {
318 return 'Get information about a list of users';
319 }
320
321 public function getExamples() {
322 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
323 }
324
325 public function getHelpUrls() {
326 return 'http://www.mediawiki.org/wiki/API:Users';
327 }
328
329 public function getVersion() {
330 return __CLASS__ . ': $Id$';
331 }
332 }