Followup comment to r81507. The api is using a legacy langlinks method no modern...
[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
130 $result = $this->getResult();
131
132 foreach ( $res as $row ) {
133 $user = User::newFromRow( $row );
134 $name = $user->getName();
135 $data[$name]['name'] = $name;
136
137 if ( isset( $this->prop['editcount'] ) ) {
138 $data[$name]['editcount'] = intval( $user->getEditCount() );
139 }
140
141 if ( isset( $this->prop['registration'] ) ) {
142 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
143 }
144
145 if ( isset( $this->prop['groups'] ) && !is_null( $row->ug_group ) ) {
146 if ( !isset( $data[$u]['groups'] ) ) {
147 $data[$u]['groups'] = ApiQueryUsers::getAutoGroups( User::newFromName( $u ) );
148 }
149
150 // This row contains only one group, others will be added from other rows
151 $data[$name]['groups'][] = $row->ug_group;
152 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
153 }
154
155 if ( isset( $this->prop['rights'] ) && !is_null( $row->ug_group ) ) {
156 if ( !isset( $data[$name]['rights'] ) ) {
157 $data[$name]['rights'] = User::getGroupPermissions( User::getImplicitGroups() );
158 }
159
160 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
161 User::getGroupPermissions( array( $row->ug_group ) ) ) );
162 $result->setIndexedTagName( $data[$name]['rights'], 'r' );
163 }
164
165 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->blocker_name ) ) {
166 $data[$name]['blockedby'] = $row->blocker_name;
167 $data[$name]['blockreason'] = $row->ipb_reason;
168 $data[$name]['blockexpiry'] = $row->ipb_expiry;
169 }
170
171 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
172 $data[$name]['emailable'] = '';
173 }
174
175 if ( isset( $this->prop['gender'] ) ) {
176 $gender = $user->getOption( 'gender' );
177 if ( strval( $gender ) === '' ) {
178 $gender = 'unknown';
179 }
180 $data[$name]['gender'] = $gender;
181 }
182
183 if ( !is_null( $params['token'] ) ) {
184 $tokenFunctions = $this->getTokenFunctions();
185 foreach ( $params['token'] as $t ) {
186 $val = call_user_func( $tokenFunctions[$t], $user );
187 if ( $val === false ) {
188 $this->setWarning( "Action '$t' is not allowed for the current user" );
189 } else {
190 $data[$name][$t . 'token'] = $val;
191 }
192 }
193 }
194 }
195 }
196 // Second pass: add result data to $retval
197 foreach ( $goodNames as $u ) {
198 if ( !isset( $data[$u] ) ) {
199 $data[$u] = array( 'name' => $u );
200 $urPage = new UserrightsPage;
201 $iwUser = $urPage->fetchUser( $u );
202
203 if ( $iwUser instanceof UserRightsProxy ) {
204 $data[$u]['interwiki'] = '';
205
206 if ( !is_null( $params['token'] ) ) {
207 $tokenFunctions = $this->getTokenFunctions();
208
209 foreach ( $params['token'] as $t ) {
210 $val = call_user_func( $tokenFunctions[$t], $iwUser );
211 if ( $val === false ) {
212 $this->setWarning( "Action '$t' is not allowed for the current user" );
213 } else {
214 $data[$u][$t . 'token'] = $val;
215 }
216 }
217 }
218 } else {
219 $data[$u]['missing'] = '';
220 }
221 }
222
223 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
224 null, $data[$u] );
225 if ( !$fit ) {
226 $this->setContinueEnumParameter( 'users',
227 implode( '|', array_diff( $users, $done ) ) );
228 break;
229 }
230 $done[] = $u;
231 }
232 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
233 }
234
235 /**
236 * Gets all the groups that a user is automatically a member of
237 * @param $user User
238 * @return array
239 */
240 public static function getAutoGroups( $user ) {
241 $groups = array( '*' );
242
243 if ( !$user->isAnon() ) {
244 $groups[] = 'user';
245 }
246
247 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
248 }
249
250 public function getCacheMode( $params ) {
251 if ( isset( $params['token'] ) ) {
252 return 'private';
253 } else {
254 return 'public';
255 }
256 }
257
258 public function getAllowedParams() {
259 return array(
260 'prop' => array(
261 ApiBase::PARAM_DFLT => null,
262 ApiBase::PARAM_ISMULTI => true,
263 ApiBase::PARAM_TYPE => array(
264 'blockinfo',
265 'groups',
266 'rights',
267 'editcount',
268 'registration',
269 'emailable',
270 'gender',
271 )
272 ),
273 'users' => array(
274 ApiBase::PARAM_ISMULTI => true
275 ),
276 'token' => array(
277 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
278 ApiBase::PARAM_ISMULTI => true
279 ),
280 );
281 }
282
283 public function getParamDescription() {
284 return array(
285 'prop' => array(
286 'What pieces of information to include',
287 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
288 ' groups - Lists all the groups the user(s) belongs to',
289 ' rights - Lists all the rights the user(s) has',
290 ' editcount - Adds the user\'s edit count',
291 ' registration - Adds the user\'s registration timestamp',
292 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
293 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
294 ),
295 'users' => 'A list of users to obtain the same information for',
296 'token' => 'Which tokens to obtain for each user',
297 );
298 }
299
300 public function getDescription() {
301 return 'Get information about a list of users';
302 }
303
304 protected function getExamples() {
305 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
306 }
307
308 public function getVersion() {
309 return __CLASS__ . ': $Id$';
310 }
311 }