Merge "(bug 1) doc for SiteConfiguration"
[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( User::selectFields() );
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]['blockid'] = $row->ipb_id;
169 $data[$name]['blockedby'] = $row->ipb_by_text;
170 $data[$name]['blockedbyid'] = $row->ipb_by;
171 $data[$name]['blockreason'] = $row->ipb_reason;
172 $data[$name]['blockexpiry'] = $row->ipb_expiry;
173 }
174
175 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
176 $data[$name]['emailable'] = '';
177 }
178
179 if ( isset( $this->prop['gender'] ) ) {
180 $gender = $user->getOption( 'gender' );
181 if ( strval( $gender ) === '' ) {
182 $gender = 'unknown';
183 }
184 $data[$name]['gender'] = $gender;
185 }
186
187 if ( !is_null( $params['token'] ) ) {
188 $tokenFunctions = $this->getTokenFunctions();
189 foreach ( $params['token'] as $t ) {
190 $val = call_user_func( $tokenFunctions[$t], $user );
191 if ( $val === false ) {
192 $this->setWarning( "Action '$t' is not allowed for the current user" );
193 } else {
194 $data[$name][$t . 'token'] = $val;
195 }
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 } else {
227 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
228 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
229 }
230 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
231 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
232 }
233 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
234 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
235 }
236 }
237
238 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
239 null, $data[$u] );
240 if ( !$fit ) {
241 $this->setContinueEnumParameter( 'users',
242 implode( '|', array_diff( $users, $done ) ) );
243 break;
244 }
245 $done[] = $u;
246 }
247 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
248 }
249
250 /**
251 * Gets all the groups that a user is automatically a member of (implicit groups)
252 * @param $user User
253 * @return array
254 */
255 public static function getAutoGroups( $user ) {
256 $groups = array();
257 $groups[] = '*';
258
259 if ( !$user->isAnon() ) {
260 $groups[] = 'user';
261 }
262
263 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
264 }
265
266 public function getCacheMode( $params ) {
267 if ( isset( $params['token'] ) ) {
268 return 'private';
269 } else {
270 return 'anon-public-user-private';
271 }
272 }
273
274 public function getAllowedParams() {
275 return array(
276 'prop' => array(
277 ApiBase::PARAM_DFLT => null,
278 ApiBase::PARAM_ISMULTI => true,
279 ApiBase::PARAM_TYPE => array(
280 'blockinfo',
281 'groups',
282 'implicitgroups',
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 ' implicitgroups - Lists all the groups a user is automatically a member of',
307 ' rights - Lists all the rights the user(s) has',
308 ' editcount - Adds the user\'s edit count',
309 ' registration - Adds the user\'s registration timestamp',
310 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
311 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
312 ),
313 'users' => 'A list of users to obtain the same information for',
314 'token' => 'Which tokens to obtain for each user',
315 );
316 }
317
318 public function getDescription() {
319 return 'Get information about a list of users';
320 }
321
322 public function getExamples() {
323 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
324 }
325
326 public function getHelpUrls() {
327 return 'https://www.mediawiki.org/wiki/API:Users';
328 }
329
330 public function getVersion() {
331 return __CLASS__ . ': $Id$';
332 }
333 }