Don't try to verify XML well-formedness for partial SVG uploads
[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 * @deprecated since 1.24
62 * @return array Array of tokenname => function
63 */
64 protected function getTokenFunctions() {
65 // Don't call the hooks twice
66 if ( isset( $this->tokenFunctions ) ) {
67 return $this->tokenFunctions;
68 }
69
70 // If we're in JSON callback mode, no tokens can be obtained
71 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
72 return array();
73 }
74
75 $this->tokenFunctions = array(
76 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
77 );
78 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
79
80 return $this->tokenFunctions;
81 }
82
83 /**
84 * @deprecated since 1.24
85 * @param User $user
86 * @return string
87 */
88 public static function getUserrightsToken( $user ) {
89 global $wgUser;
90
91 // Since the permissions check for userrights is non-trivial,
92 // don't bother with it here
93 return $wgUser->getEditToken( $user->getName() );
94 }
95
96 public function execute() {
97 $params = $this->extractRequestParams();
98
99 if ( !is_null( $params['prop'] ) ) {
100 $this->prop = array_flip( $params['prop'] );
101 } else {
102 $this->prop = array();
103 }
104
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() ),
114 null, $vals );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'users',
117 implode( '|', array_diff( $users, $done ) ) );
118 $goodNames = array();
119 break;
120 }
121 $done[] = $u;
122 } else {
123 $goodNames[] = $n;
124 }
125 }
126
127 $result = $this->getResult();
128
129 if ( count( $goodNames ) ) {
130 $this->addTables( 'user' );
131 $this->addFields( User::selectFields() );
132 $this->addWhereFld( 'user_name', $goodNames );
133
134 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
135
136 $data = array();
137 $res = $this->select( __METHOD__ );
138 $this->resetQueryParams();
139
140 // get user groups if needed
141 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
142 $userGroups = array();
143
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__ );
150
151 foreach ( $userGroupsRes as $row ) {
152 $userGroups[$row->user_name][] = $row->ug_group;
153 }
154 }
155
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 );
161 } else {
162 if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
163 $userGroups[$row->user_name] = array();
164 }
165 $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) );
166 }
167 $name = $user->getName();
168
169 $data[$name]['userid'] = $user->getId();
170 $data[$name]['name'] = $name;
171
172 if ( isset( $this->prop['editcount'] ) ) {
173 $data[$name]['editcount'] = $user->getEditCount();
174 }
175
176 if ( isset( $this->prop['registration'] ) ) {
177 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
178 }
179
180 if ( isset( $this->prop['groups'] ) ) {
181 $data[$name]['groups'] = $user->getEffectiveGroups();
182 }
183
184 if ( isset( $this->prop['implicitgroups'] ) ) {
185 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
186 }
187
188 if ( isset( $this->prop['rights'] ) ) {
189 $data[$name]['rights'] = $user->getRights();
190 }
191 if ( $row->ipb_deleted ) {
192 $data[$name]['hidden'] = '';
193 }
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;
201 }
202
203 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
204 $data[$name]['emailable'] = '';
205 }
206
207 if ( isset( $this->prop['gender'] ) ) {
208 $gender = $user->getOption( 'gender' );
209 if ( strval( $gender ) === '' ) {
210 $gender = 'unknown';
211 }
212 $data[$name]['gender'] = $gender;
213 }
214
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" );
221 } else {
222 $data[$name][$t . 'token'] = $val;
223 }
224 }
225 }
226 }
227 }
228
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 );
237
238 if ( $iwUser instanceof UserRightsProxy ) {
239 $data[$u]['interwiki'] = '';
240
241 if ( !is_null( $params['token'] ) ) {
242 $tokenFunctions = $this->getTokenFunctions();
243
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" );
248 } else {
249 $data[$u][$t . 'token'] = $val;
250 }
251 }
252 }
253 } else {
254 $data[$u]['missing'] = '';
255 }
256 } else {
257 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
258 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
259 }
260 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
261 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
262 }
263 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
264 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
265 }
266 }
267
268 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
269 null, $data[$u] );
270 if ( !$fit ) {
271 $this->setContinueEnumParameter( 'users',
272 implode( '|', array_diff( $users, $done ) ) );
273 break;
274 }
275 $done[] = $u;
276 }
277 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
278 }
279
280 public function getCacheMode( $params ) {
281 if ( isset( $params['token'] ) ) {
282 return 'private';
283 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
284 return 'anon-public-user-private';
285 } else {
286 return 'public';
287 }
288 }
289
290 public function getAllowedParams() {
291 return array(
292 'prop' => array(
293 ApiBase::PARAM_DFLT => null,
294 ApiBase::PARAM_ISMULTI => true,
295 ApiBase::PARAM_TYPE => array(
296 'blockinfo',
297 'groups',
298 'implicitgroups',
299 'rights',
300 'editcount',
301 'registration',
302 'emailable',
303 'gender',
304 )
305 ),
306 'users' => array(
307 ApiBase::PARAM_ISMULTI => true
308 ),
309 'token' => array(
310 ApiBase::PARAM_DEPRECATED => true,
311 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
312 ApiBase::PARAM_ISMULTI => true
313 ),
314 );
315 }
316
317 public function getParamDescription() {
318 return array(
319 'prop' => array(
320 'What pieces of information to include',
321 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
322 ' groups - Lists all the groups the user(s) belongs to',
323 ' implicitgroups - Lists all the groups a user is automatically a member of',
324 ' rights - Lists all the rights the user(s) has',
325 ' editcount - Adds the user\'s edit count',
326 ' registration - Adds the user\'s registration timestamp',
327 ' emailable - Tags if the user can and wants to receive ' .
328 'email through [[Special:Emailuser]]',
329 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
330 ),
331 'users' => 'A list of users to obtain the same information for',
332 'token' => 'Which tokens to obtain for each user',
333 );
334 }
335
336 public function getDescription() {
337 return 'Get information about a list of users.';
338 }
339
340 public function getExamples() {
341 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
342 }
343
344 public function getHelpUrls() {
345 return 'https://www.mediawiki.org/wiki/API:Users';
346 }
347 }