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