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