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