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