Standardised file description headers, added @file
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'us' );
41 }
42
43 /**
44 * Get an array mapping token names to their handler functions.
45 * The prototype for a token function is func($user)
46 * it should return a token or false (permission denied)
47 * @return array(tokenname => function)
48 */
49 protected function getTokenFunctions() {
50 // Don't call the hooks twice
51 if ( isset( $this->tokenFunctions ) ) {
52 return $this->tokenFunctions;
53 }
54
55 // If we're in JSON callback mode, no tokens can be obtained
56 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
57 return array();
58 }
59
60 $this->tokenFunctions = array(
61 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
62 );
63 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
64 return $this->tokenFunctions;
65 }
66
67 public static function getUserrightsToken( $user ) {
68 global $wgUser;
69 // Since the permissions check for userrights is non-trivial,
70 // don't bother with it here
71 return $wgUser->editToken( $user->getName() );
72 }
73
74 public function execute() {
75 $params = $this->extractRequestParams();
76 $result = $this->getResult();
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 if ( count( $goodNames ) ) {
107 $this->addTables( 'user', 'u1' );
108 $this->addFields( 'u1.*' );
109 $this->addWhereFld( 'u1.user_name', $goodNames );
110
111 if ( isset( $this->prop['groups'] ) ) {
112 $this->addTables( 'user_groups' );
113 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=u1.user_id' ) ) );
114 $this->addFields( 'ug_group' );
115 }
116 if ( isset( $this->prop['blockinfo'] ) ) {
117 $this->addTables( 'ipblocks' );
118 $this->addTables( 'user', 'u2' );
119 $u2 = $this->getAliasedName( 'user', 'u2' );
120 $this->addJoinConds( array(
121 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
122 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
123 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
124 }
125
126 $data = array();
127 $res = $this->select( __METHOD__ );
128 foreach ( $res as $row ) {
129 $user = User::newFromRow( $row );
130 $name = $user->getName();
131 $data[$name]['name'] = $name;
132
133 if ( isset( $this->prop['editcount'] ) ) {
134 $data[$name]['editcount'] = intval( $user->getEditCount() );
135 }
136
137 if ( isset( $this->prop['registration'] ) ) {
138 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
139 }
140
141 if ( isset( $this->prop['groups'] ) && !is_null( $row->ug_group ) ) {
142 // This row contains only one group, others will be added from other rows
143 $data[$name]['groups'][] = $row->ug_group;
144 }
145
146 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->blocker_name ) ) {
147 $data[$name]['blockedby'] = $row->blocker_name;
148 $data[$name]['blockreason'] = $row->ipb_reason;
149 }
150
151 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
152 $data[$name]['emailable'] = '';
153 }
154
155 if ( isset( $this->prop['gender'] ) ) {
156 $gender = $user->getOption( 'gender' );
157 if ( strval( $gender ) === '' ) {
158 $gender = 'unknown';
159 }
160 $data[$name]['gender'] = $gender;
161 }
162
163 if ( !is_null( $params['token'] ) ) {
164 $tokenFunctions = $this->getTokenFunctions();
165 foreach ( $params['token'] as $t ) {
166 $val = call_user_func( $tokenFunctions[$t], $user );
167 if ( $val === false ) {
168 $this->setWarning( "Action '$t' is not allowed for the current user" );
169 } else {
170 $data[$name][$t . 'token'] = $val;
171 }
172 }
173 }
174 }
175 }
176 // Second pass: add result data to $retval
177 foreach ( $goodNames as $u ) {
178 if ( !isset( $data[$u] ) ) {
179 $data[$u] = array( 'name' => $u );
180 $urPage = new UserrightsPage;
181 $iwUser = $urPage->fetchUser( $u );
182
183 if ( $iwUser instanceof UserRightsProxy ) {
184 $data[$u]['interwiki'] = '';
185
186 if ( !is_null( $params['token'] ) ) {
187 $tokenFunctions = $this->getTokenFunctions();
188
189 foreach ( $params['token'] as $t ) {
190 $val = call_user_func( $tokenFunctions[$t], $iwUser );
191 if ( $val === false ) {
192 $this->setWarning( "Action '$t' is not allowed for the current user" );
193 } else {
194 $data[$u][$t . 'token'] = $val;
195 }
196 }
197 }
198 } else {
199 $data[$u]['missing'] = '';
200 }
201 } else {
202 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
203 $autolist = ApiQueryUsers::getAutoGroups( User::newFromName( $u ) );
204
205 $data[$u]['groups'] = array_merge( $autolist, $data[$u]['groups'] );
206
207 $this->getResult()->setIndexedTagName( $data[$u]['groups'], 'g' );
208 }
209 }
210 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
211 null, $data[$u] );
212 if ( !$fit ) {
213 $this->setContinueEnumParameter( 'users',
214 implode( '|', array_diff( $users, $done ) ) );
215 break;
216 }
217 $done[] = $u;
218 }
219 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
220 }
221
222 /**
223 * Gets all the groups that a user is automatically a member of
224 * @return array
225 */
226 public static function getAutoGroups( $user ) {
227 $groups = array( '*' );
228
229 if ( !$user->isAnon() ) {
230 $groups[] = 'user';
231 }
232
233 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
234 }
235
236 public function getCacheMode( $params ) {
237 if ( isset( $params['token'] ) ) {
238 return 'private';
239 } else {
240 return 'public';
241 }
242 }
243
244 public function getAllowedParams() {
245 return array(
246 'prop' => array(
247 ApiBase::PARAM_DFLT => null,
248 ApiBase::PARAM_ISMULTI => true,
249 ApiBase::PARAM_TYPE => array(
250 'blockinfo',
251 'groups',
252 'editcount',
253 'registration',
254 'emailable',
255 'gender',
256 )
257 ),
258 'users' => array(
259 ApiBase::PARAM_ISMULTI => true
260 ),
261 'token' => array(
262 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
263 ApiBase::PARAM_ISMULTI => true
264 ),
265 );
266 }
267
268 public function getParamDescription() {
269 return array(
270 'prop' => array(
271 'What pieces of information to include',
272 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
273 ' groups - lists all the groups the user belongs to',
274 ' editcount - adds the user\'s edit count',
275 ' registration - adds the user\'s registration timestamp',
276 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
277 ' gender - tags the gender of the user. Returns "male", "female", or "unknown"',
278 ),
279 'users' => 'A list of users to obtain the same information for',
280 'token' => 'Which tokens to obtain for each user',
281 );
282 }
283
284 public function getDescription() {
285 return 'Get information about a list of users';
286 }
287
288 protected function getExamples() {
289 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
290 }
291
292 public function getVersion() {
293 return __CLASS__ . ': $Id$';
294 }
295 }