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