Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / api / ApiQueryUsers.php
blob5afb66f225c37dc3288985dfa5cbd3b0600984ce
1 <?php
2 /**
5 * Created on July 30, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to get information about a list of users
30 * @ingroup API
32 class ApiQueryUsers extends ApiQueryBase {
34 private $tokenFunctions, $prop;
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
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',
55 public function __construct( ApiQuery $query, $moduleName ) {
56 parent::__construct( $query, $moduleName, 'us' );
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
66 protected function getTokenFunctions() {
67 // Don't call the hooks twice
68 if ( isset( $this->tokenFunctions ) ) {
69 return $this->tokenFunctions;
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 [];
78 $this->tokenFunctions = [
79 'userrights' => [ 'ApiQueryUsers', 'getUserrightsToken' ],
81 Hooks::run( 'APIQueryUsersTokens', [ &$this->tokenFunctions ] );
83 return $this->tokenFunctions;
86 /**
87 * @deprecated since 1.24
88 * @param User $user
89 * @return string
91 public static function getUserrightsToken( $user ) {
92 global $wgUser;
94 // Since the permissions check for userrights is non-trivial,
95 // don't bother with it here
96 return $wgUser->getEditToken( $user->getName() );
99 public function execute() {
100 $params = $this->extractRequestParams();
102 if ( !is_null( $params['prop'] ) ) {
103 $this->prop = array_flip( $params['prop'] );
104 } else {
105 $this->prop = [];
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;
124 $done[] = $u;
125 } else {
126 $goodNames[] = $n;
130 $result = $this->getResult();
132 if ( count( $goodNames ) ) {
133 $this->addTables( 'user' );
134 $this->addFields( User::selectFields() );
135 $this->addWhereFld( 'user_name', $goodNames );
137 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
139 $data = [];
140 $res = $this->select( __METHOD__ );
141 $this->resetQueryParams();
143 // get user groups if needed
144 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
145 $userGroups = [];
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__ );
154 foreach ( $userGroupsRes as $row ) {
155 $userGroups[$row->user_name][] = $row->ug_group;
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] = [];
168 $user = User::newFromRow( $row, [ 'user_groups' => $userGroups[$row->user_name] ] );
170 $name = $user->getName();
172 $data[$name]['userid'] = $user->getId();
173 $data[$name]['name'] = $name;
175 if ( isset( $this->prop['editcount'] ) ) {
176 $data[$name]['editcount'] = $user->getEditCount();
179 if ( isset( $this->prop['registration'] ) ) {
180 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
183 if ( isset( $this->prop['groups'] ) ) {
184 $data[$name]['groups'] = $user->getEffectiveGroups();
187 if ( isset( $this->prop['implicitgroups'] ) ) {
188 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
191 if ( isset( $this->prop['rights'] ) ) {
192 $data[$name]['rights'] = $user->getRights();
194 if ( $row->ipb_deleted ) {
195 $data[$name]['hidden'] = true;
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;
206 if ( isset( $this->prop['emailable'] ) ) {
207 $data[$name]['emailable'] = $user->canReceiveEmail();
210 if ( isset( $this->prop['gender'] ) ) {
211 $gender = $user->getOption( 'gender' );
212 if ( strval( $gender ) === '' ) {
213 $gender = 'unknown';
215 $data[$name]['gender'] = $gender;
218 if ( isset( $this->prop['centralids'] ) ) {
219 $data[$name] += ApiQueryUserInfo::getCentralUserInfo(
220 $this->getConfig(), $user, $params['attachedwiki']
224 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;
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 );
247 if ( $iwUser instanceof UserRightsProxy ) {
248 $data[$u]['interwiki'] = true;
250 if ( !is_null( $params['token'] ) ) {
251 $tokenFunctions = $this->getTokenFunctions();
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;
262 } else {
263 $data[$u]['missing'] = true;
264 if ( isset( $this->prop['cancreate'] ) && !$this->getConfig()->get( 'DisableAuthManager' ) ) {
265 $status = MediaWiki\Auth\AuthManager::singleton()->canCreateAccount( $u );
266 $data[$u]['cancreate'] = $status->isGood();
267 if ( !$status->isGood() ) {
268 $data[$u]['cancreateerror'] = $this->getErrorFormatter()->arrayFromStatus( $status );
272 } else {
273 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
274 ApiResult::setArrayType( $data[$u]['groups'], 'array' );
275 ApiResult::setIndexedTagName( $data[$u]['groups'], 'g' );
277 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
278 ApiResult::setArrayType( $data[$u]['implicitgroups'], 'array' );
279 ApiResult::setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
281 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
282 ApiResult::setArrayType( $data[$u]['rights'], 'array' );
283 ApiResult::setIndexedTagName( $data[$u]['rights'], 'r' );
287 $fit = $result->addValue( [ 'query', $this->getModuleName() ],
288 null, $data[$u] );
289 if ( !$fit ) {
290 $this->setContinueEnumParameter( 'users',
291 implode( '|', array_diff( $users, $done ) ) );
292 break;
294 $done[] = $u;
296 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'user' );
299 public function getCacheMode( $params ) {
300 if ( isset( $params['token'] ) ) {
301 return 'private';
302 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
303 return 'anon-public-user-private';
304 } else {
305 return 'public';
309 public function getAllowedParams() {
310 $ret = [
311 'prop' => [
312 ApiBase::PARAM_ISMULTI => true,
313 ApiBase::PARAM_TYPE => [
314 'blockinfo',
315 'groups',
316 'implicitgroups',
317 'rights',
318 'editcount',
319 'registration',
320 'emailable',
321 'gender',
322 'centralids',
323 // When adding a prop, consider whether it should be added
324 // to self::$publicProps
326 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
328 'attachedwiki' => null,
329 'users' => [
330 ApiBase::PARAM_TYPE => 'user',
331 ApiBase::PARAM_ISMULTI => true
333 'token' => [
334 ApiBase::PARAM_DEPRECATED => true,
335 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
336 ApiBase::PARAM_ISMULTI => true
339 if ( !$this->getConfig()->get( 'DisableAuthManager' ) ) {
340 $ret['prop'][ApiBase::PARAM_TYPE][] = 'cancreate';
342 return $ret;
345 protected function getExamplesMessages() {
346 return [
347 'action=query&list=users&ususers=Example&usprop=groups|editcount|gender'
348 => 'apihelp-query+users-example-simple',
352 public function getHelpUrls() {
353 return 'https://www.mediawiki.org/wiki/API:Users';