Move a few braces noticed while doing CR
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2
3 /**
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to get information about a list of users
33 *
34 * @ingroup API
35 */
36 class ApiQueryUsers extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'us' );
40 }
41
42 /**
43 * Get an array mapping token names to their handler functions.
44 * The prototype for a token function is func($user)
45 * it should return a token or false (permission denied)
46 * @return array(tokenname => function)
47 */
48 protected function getTokenFunctions() {
49 // Don't call the hooks twice
50 if ( isset( $this->tokenFunctions ) ) {
51 return $this->tokenFunctions;
52 }
53
54 // If we're in JSON callback mode, no tokens can be obtained
55 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
56 return array();
57 }
58
59 $this->tokenFunctions = array(
60 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
61 );
62 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
63 return $this->tokenFunctions;
64 }
65
66 public static function getUserrightsToken( $user ) {
67 global $wgUser;
68 // Since the permissions check for userrights is non-trivial,
69 // don't bother with it here
70 return $wgUser->editToken( $user->getName() );
71 }
72
73 public function execute() {
74 $params = $this->extractRequestParams();
75 $result = $this->getResult();
76
77 if ( !is_null( $params['prop'] ) ) {
78 $this->prop = array_flip( $params['prop'] );
79 } else {
80 $this->prop = array();
81 }
82
83 $users = (array)$params['users'];
84 $goodNames = $done = array();
85 $result = $this->getResult();
86 // Canonicalize user names
87 foreach ( $users as $u ) {
88 $n = User::getCanonicalName( $u );
89 if ( $n === false || $n === '' ) {
90 $vals = array( 'name' => $u, 'invalid' => '' );
91 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
92 null, $vals );
93 if ( !$fit ) {
94 $this->setContinueEnumParameter( 'users',
95 implode( '|', array_diff( $users, $done ) ) );
96 $goodNames = array();
97 break;
98 }
99 $done[] = $u;
100 } else {
101 $goodNames[] = $n;
102 }
103 }
104
105 if ( count( $goodNames ) ) {
106 $this->addTables( 'user', 'u1' );
107 $this->addFields( 'u1.*' );
108 $this->addWhereFld( 'u1.user_name', $goodNames );
109
110 if ( isset( $this->prop['groups'] ) ) {
111 $this->addTables( 'user_groups' );
112 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=u1.user_id' ) ) );
113 $this->addFields( 'ug_group' );
114 }
115 if ( isset( $this->prop['blockinfo'] ) ) {
116 $this->addTables( 'ipblocks' );
117 $this->addTables( 'user', 'u2' );
118 $u2 = $this->getAliasedName( 'user', 'u2' );
119 $this->addJoinConds( array(
120 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
121 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
122 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
123 }
124
125 $data = array();
126 $res = $this->select( __METHOD__ );
127 foreach ( $res as $row ) {
128 $user = User::newFromRow( $row );
129 $name = $user->getName();
130 $data[$name]['name'] = $name;
131
132 if ( isset( $this->prop['editcount'] ) ) {
133 $data[$name]['editcount'] = intval( $user->getEditCount() );
134 }
135
136 if ( isset( $this->prop['registration'] ) ) {
137 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
138 }
139
140 if ( isset( $this->prop['groups'] ) && !is_null( $row->ug_group ) ) {
141 // This row contains only one group, others will be added from other rows
142 $data[$name]['groups'][] = $row->ug_group;
143 }
144
145 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->blocker_name ) ) {
146 $data[$name]['blockedby'] = $row->blocker_name;
147 $data[$name]['blockreason'] = $row->ipb_reason;
148 }
149
150 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
151 $data[$name]['emailable'] = '';
152 }
153
154 if ( isset( $this->prop['gender'] ) ) {
155 $gender = $user->getOption( 'gender' );
156 if ( strval( $gender ) === '' ) {
157 $gender = 'unknown';
158 }
159 $data[$name]['gender'] = $gender;
160 }
161
162 if ( !is_null( $params['token'] ) ) {
163 $tokenFunctions = $this->getTokenFunctions();
164 foreach ( $params['token'] as $t ) {
165 $val = call_user_func( $tokenFunctions[$t], $user );
166 if ( $val === false ) {
167 $this->setWarning( "Action '$t' is not allowed for the current user" );
168 } else {
169 $data[$name][$t . 'token'] = $val;
170 }
171 }
172 }
173 }
174 }
175 // Second pass: add result data to $retval
176 foreach ( $goodNames as $u ) {
177 if ( !isset( $data[$u] ) ) {
178 $data[$u] = array( 'name' => $u );
179 $urPage = new UserrightsPage;
180 $iwUser = $urPage->fetchUser( $u );
181
182 if ( $iwUser instanceof UserRightsProxy ) {
183 $data[$u]['interwiki'] = '';
184
185 if ( !is_null( $params['token'] ) ) {
186 $tokenFunctions = $this->getTokenFunctions();
187
188 foreach ( $params['token'] as $t ) {
189 $val = call_user_func( $tokenFunctions[$t], $iwUser );
190 if ( $val === false ) {
191 $this->setWarning( "Action '$t' is not allowed for the current user" );
192 } else {
193 $data[$u][$t . 'token'] = $val;
194 }
195 }
196 }
197 } else {
198 $data[$u]['missing'] = '';
199 }
200 } else {
201 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
202 $autolist = ApiQueryUsers::getAutoGroups( User::newFromName( $u ) );
203
204 $data[$u]['groups'] = array_merge( $autolist, $data[$u]['groups'] );
205
206 $this->getResult()->setIndexedTagName( $data[$u]['groups'], 'g' );
207 }
208 }
209 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
210 null, $data[$u] );
211 if ( !$fit ) {
212 $this->setContinueEnumParameter( 'users',
213 implode( '|', array_diff( $users, $done ) ) );
214 break;
215 }
216 $done[] = $u;
217 }
218 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
219 }
220
221 /**
222 * Gets all the groups that a user is automatically a member of
223 * @return array
224 */
225 public static function getAutoGroups( $user ) {
226 $groups = array( '*' );
227
228 if ( !$user->isAnon() ) {
229 $groups[] = 'user';
230 }
231
232 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
233 }
234
235 public function getCacheMode( $params ) {
236 if ( isset( $params['token'] ) ) {
237 return 'private';
238 } else {
239 return 'public';
240 }
241 }
242
243 public function getAllowedParams() {
244 return array(
245 'prop' => array(
246 ApiBase::PARAM_DFLT => null,
247 ApiBase::PARAM_ISMULTI => true,
248 ApiBase::PARAM_TYPE => array(
249 'blockinfo',
250 'groups',
251 'editcount',
252 'registration',
253 'emailable',
254 'gender',
255 )
256 ),
257 'users' => array(
258 ApiBase::PARAM_ISMULTI => true
259 ),
260 'token' => array(
261 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
262 ApiBase::PARAM_ISMULTI => true
263 ),
264 );
265 }
266
267 public function getParamDescription() {
268 return array(
269 'prop' => array(
270 'What pieces of information to include',
271 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
272 ' groups - lists all the groups the user belongs to',
273 ' editcount - adds the user\'s edit count',
274 ' registration - adds the user\'s registration timestamp',
275 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
276 ' gender - tags the gender of the user. Returns "male", "female", or "unknown"',
277 ),
278 'users' => 'A list of users to obtain the same information for',
279 'token' => 'Which tokens to obtain for each user',
280 );
281 }
282
283 public function getDescription() {
284 return 'Get information about a list of users';
285 }
286
287 protected function getExamples() {
288 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
289 }
290
291 public function getVersion() {
292 return __CLASS__ . ': $Id$';
293 }
294 }