Merge "Http::getProxy() method to get proxy configuration"
[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 /**
28 * Query module to get information about a list of users
29 *
30 * @ingroup API
31 */
32 class ApiQueryUsers extends ApiQueryBase {
33
34 private $tokenFunctions, $prop;
35
36 /**
37 * Properties whose contents does not depend on who is looking at them. If the usprops field
38 * contains anything not listed here, the cache mode will never be public for logged-in users.
39 * @var array
40 */
41 protected static $publicProps = [
42 // everything except 'blockinfo' which might show hidden records if the user
43 // making the request has the appropriate permissions
44 'groups',
45 'implicitgroups',
46 'rights',
47 'editcount',
48 'registration',
49 'emailable',
50 'gender',
51 'centralids',
52 ];
53
54 public function __construct( ApiQuery $query, $moduleName ) {
55 parent::__construct( $query, $moduleName, 'us' );
56 }
57
58 /**
59 * Get an array mapping token names to their handler functions.
60 * The prototype for a token function is func($user)
61 * it should return a token or false (permission denied)
62 * @deprecated since 1.24
63 * @return array Array of tokenname => function
64 */
65 protected function getTokenFunctions() {
66 // Don't call the hooks twice
67 if ( isset( $this->tokenFunctions ) ) {
68 return $this->tokenFunctions;
69 }
70
71 // If we're in a mode that breaks the same-origin policy, no tokens can
72 // be obtained
73 if ( $this->lacksSameOriginSecurity() ) {
74 return [];
75 }
76
77 $this->tokenFunctions = [
78 'userrights' => [ 'ApiQueryUsers', 'getUserrightsToken' ],
79 ];
80 Hooks::run( 'APIQueryUsersTokens', [ &$this->tokenFunctions ] );
81
82 return $this->tokenFunctions;
83 }
84
85 /**
86 * @deprecated since 1.24
87 * @param User $user
88 * @return string
89 */
90 public static function getUserrightsToken( $user ) {
91 global $wgUser;
92
93 // Since the permissions check for userrights is non-trivial,
94 // don't bother with it here
95 return $wgUser->getEditToken( $user->getName() );
96 }
97
98 public function execute() {
99 $params = $this->extractRequestParams();
100
101 if ( !is_null( $params['prop'] ) ) {
102 $this->prop = array_flip( $params['prop'] );
103 } else {
104 $this->prop = [];
105 }
106
107 $users = (array)$params['users'];
108 $goodNames = $done = [];
109 $result = $this->getResult();
110 // Canonicalize user names
111 foreach ( $users as $u ) {
112 $n = User::getCanonicalName( $u );
113 if ( $n === false || $n === '' ) {
114 $vals = [ 'name' => $u, 'invalid' => true ];
115 $fit = $result->addValue( [ 'query', $this->getModuleName() ],
116 null, $vals );
117 if ( !$fit ) {
118 $this->setContinueEnumParameter( 'users',
119 implode( '|', array_diff( $users, $done ) ) );
120 $goodNames = [];
121 break;
122 }
123 $done[] = $u;
124 } else {
125 $goodNames[] = $n;
126 }
127 }
128
129 $result = $this->getResult();
130
131 if ( count( $goodNames ) ) {
132 $this->addTables( 'user' );
133 $this->addFields( User::selectFields() );
134 $this->addWhereFld( 'user_name', $goodNames );
135
136 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
137
138 $data = [];
139 $res = $this->select( __METHOD__ );
140 $this->resetQueryParams();
141
142 // get user groups if needed
143 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
144 $userGroups = [];
145
146 $this->addTables( 'user' );
147 $this->addWhereFld( 'user_name', $goodNames );
148 $this->addTables( 'user_groups' );
149 $this->addJoinConds( [ 'user_groups' => [ 'INNER JOIN', 'ug_user=user_id' ] ] );
150 $this->addFields( [ 'user_name', 'ug_group' ] );
151 $userGroupsRes = $this->select( __METHOD__ );
152
153 foreach ( $userGroupsRes as $row ) {
154 $userGroups[$row->user_name][] = $row->ug_group;
155 }
156 }
157
158 foreach ( $res as $row ) {
159 // create user object and pass along $userGroups if set
160 // that reduces the number of database queries needed in User dramatically
161 if ( !isset( $userGroups ) ) {
162 $user = User::newFromRow( $row );
163 } else {
164 if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
165 $userGroups[$row->user_name] = [];
166 }
167 $user = User::newFromRow( $row, [ 'user_groups' => $userGroups[$row->user_name] ] );
168 }
169 $name = $user->getName();
170
171 $data[$name]['userid'] = $user->getId();
172 $data[$name]['name'] = $name;
173
174 if ( isset( $this->prop['editcount'] ) ) {
175 $data[$name]['editcount'] = $user->getEditCount();
176 }
177
178 if ( isset( $this->prop['registration'] ) ) {
179 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
180 }
181
182 if ( isset( $this->prop['groups'] ) ) {
183 $data[$name]['groups'] = $user->getEffectiveGroups();
184 }
185
186 if ( isset( $this->prop['implicitgroups'] ) ) {
187 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
188 }
189
190 if ( isset( $this->prop['rights'] ) ) {
191 $data[$name]['rights'] = $user->getRights();
192 }
193 if ( $row->ipb_deleted ) {
194 $data[$name]['hidden'] = true;
195 }
196 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
197 $data[$name]['blockid'] = (int)$row->ipb_id;
198 $data[$name]['blockedby'] = $row->ipb_by_text;
199 $data[$name]['blockedbyid'] = (int)$row->ipb_by;
200 $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
201 $data[$name]['blockreason'] = $row->ipb_reason;
202 $data[$name]['blockexpiry'] = $row->ipb_expiry;
203 }
204
205 if ( isset( $this->prop['emailable'] ) ) {
206 $data[$name]['emailable'] = $user->canReceiveEmail();
207 }
208
209 if ( isset( $this->prop['gender'] ) ) {
210 $gender = $user->getOption( 'gender' );
211 if ( strval( $gender ) === '' ) {
212 $gender = 'unknown';
213 }
214 $data[$name]['gender'] = $gender;
215 }
216
217 if ( isset( $this->prop['centralids'] ) ) {
218 $data[$name] += ApiQueryUserInfo::getCentralUserInfo(
219 $this->getConfig(), $user, $params['attachedwiki']
220 );
221 }
222
223 if ( !is_null( $params['token'] ) ) {
224 $tokenFunctions = $this->getTokenFunctions();
225 foreach ( $params['token'] as $t ) {
226 $val = call_user_func( $tokenFunctions[$t], $user );
227 if ( $val === false ) {
228 $this->setWarning( "Action '$t' is not allowed for the current user" );
229 } else {
230 $data[$name][$t . 'token'] = $val;
231 }
232 }
233 }
234 }
235 }
236
237 $context = $this->getContext();
238 // Second pass: add result data to $retval
239 foreach ( $goodNames as $u ) {
240 if ( !isset( $data[$u] ) ) {
241 $data[$u] = [ 'name' => $u ];
242 $urPage = new UserrightsPage;
243 $urPage->setContext( $context );
244 $iwUser = $urPage->fetchUser( $u );
245
246 if ( $iwUser instanceof UserRightsProxy ) {
247 $data[$u]['interwiki'] = true;
248
249 if ( !is_null( $params['token'] ) ) {
250 $tokenFunctions = $this->getTokenFunctions();
251
252 foreach ( $params['token'] as $t ) {
253 $val = call_user_func( $tokenFunctions[$t], $iwUser );
254 if ( $val === false ) {
255 $this->setWarning( "Action '$t' is not allowed for the current user" );
256 } else {
257 $data[$u][$t . 'token'] = $val;
258 }
259 }
260 }
261 } else {
262 $data[$u]['missing'] = true;
263 }
264 } else {
265 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
266 ApiResult::setArrayType( $data[$u]['groups'], 'array' );
267 ApiResult::setIndexedTagName( $data[$u]['groups'], 'g' );
268 }
269 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
270 ApiResult::setArrayType( $data[$u]['implicitgroups'], 'array' );
271 ApiResult::setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
272 }
273 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
274 ApiResult::setArrayType( $data[$u]['rights'], 'array' );
275 ApiResult::setIndexedTagName( $data[$u]['rights'], 'r' );
276 }
277 }
278
279 $fit = $result->addValue( [ 'query', $this->getModuleName() ],
280 null, $data[$u] );
281 if ( !$fit ) {
282 $this->setContinueEnumParameter( 'users',
283 implode( '|', array_diff( $users, $done ) ) );
284 break;
285 }
286 $done[] = $u;
287 }
288 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'user' );
289 }
290
291 public function getCacheMode( $params ) {
292 if ( isset( $params['token'] ) ) {
293 return 'private';
294 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
295 return 'anon-public-user-private';
296 } else {
297 return 'public';
298 }
299 }
300
301 public function getAllowedParams() {
302 return [
303 'prop' => [
304 ApiBase::PARAM_ISMULTI => true,
305 ApiBase::PARAM_TYPE => [
306 'blockinfo',
307 'groups',
308 'implicitgroups',
309 'rights',
310 'editcount',
311 'registration',
312 'emailable',
313 'gender',
314 'centralids',
315 ],
316 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
317 ],
318 'attachedwiki' => null,
319 'users' => [
320 ApiBase::PARAM_TYPE => 'user',
321 ApiBase::PARAM_ISMULTI => true
322 ],
323 'token' => [
324 ApiBase::PARAM_DEPRECATED => true,
325 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
326 ApiBase::PARAM_ISMULTI => true
327 ],
328 ];
329 }
330
331 protected function getExamplesMessages() {
332 return [
333 'action=query&list=users&ususers=Example&usprop=groups|editcount|gender'
334 => 'apihelp-query+users-example-simple',
335 ];
336 }
337
338 public function getHelpUrls() {
339 return 'https://www.mediawiki.org/wiki/API:Users';
340 }
341 }