Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryAllUsers.php
blob1c3f9fbb4c5753b4cc5837cc5942025b34c9f68c
1 <?php
2 /**
5 * Created on July 7, 2007
7 * Copyright © 2007 Yuri Astrakhan "<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 enumerate all registered users.
30 * @ingroup API
32 class ApiQueryAllUsers extends ApiQueryBase {
33 public function __construct( ApiQuery $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'au' );
37 /**
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
40 * @param string $name
41 * @return string
43 private function getCanonicalUserName( $name ) {
44 return str_replace( '_', ' ', $name );
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
51 if ( $params['activeusers'] ) {
52 // Update active user cache
53 SpecialActiveUsers::mergeActiveUsers( 300, $activeUserDays );
56 $db = $this->getDB();
58 $prop = $params['prop'];
59 if ( !is_null( $prop ) ) {
60 $prop = array_flip( $prop );
61 $fld_blockinfo = isset( $prop['blockinfo'] );
62 $fld_editcount = isset( $prop['editcount'] );
63 $fld_groups = isset( $prop['groups'] );
64 $fld_rights = isset( $prop['rights'] );
65 $fld_registration = isset( $prop['registration'] );
66 $fld_implicitgroups = isset( $prop['implicitgroups'] );
67 } else {
68 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
69 $fld_rights = $fld_implicitgroups = false;
72 $limit = $params['limit'];
74 $this->addTables( 'user' );
76 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
77 $from = is_null( $params['from'] ) ? null : $this->getCanonicalUserName( $params['from'] );
78 $to = is_null( $params['to'] ) ? null : $this->getCanonicalUserName( $params['to'] );
80 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
81 # despite the JOIN condition, so manually sort on the correct one.
82 $userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name';
84 # Some of these subtable joins are going to give us duplicate rows, so
85 # calculate the maximum number of duplicates we might see.
86 $maxDuplicateRows = 1;
88 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
90 if ( !is_null( $params['prefix'] ) ) {
91 $this->addWhere( $userFieldToSort .
92 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
95 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
96 $groups = array();
97 foreach ( $params['rights'] as $r ) {
98 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
101 // no group with the given right(s) exists, no need for a query
102 if ( !count( $groups ) ) {
103 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), '' );
105 return;
108 $groups = array_unique( $groups );
110 if ( is_null( $params['group'] ) ) {
111 $params['group'] = $groups;
112 } else {
113 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
117 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
118 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
121 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
122 // Filter only users that belong to a given group. This might
123 // produce as many rows-per-user as there are groups being checked.
124 $this->addTables( 'user_groups', 'ug1' );
125 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
126 'ug1.ug_group' => $params['group'] ) ) ) );
127 $maxDuplicateRows *= count( $params['group'] );
130 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
131 // Filter only users don't belong to a given group. This can only
132 // produce one row-per-user, because we only keep on "no match".
133 $this->addTables( 'user_groups', 'ug1' );
135 if ( count( $params['excludegroup'] ) == 1 ) {
136 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
137 } else {
138 $exclude = array( $db->makeList(
139 array( 'ug1.ug_group' => $params['excludegroup'] ),
140 LIST_OR
141 ) );
143 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
144 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
145 ) ) );
146 $this->addWhere( 'ug1.ug_user IS NULL' );
149 if ( $params['witheditsonly'] ) {
150 $this->addWhere( 'user_editcount > 0' );
153 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
155 if ( $fld_groups || $fld_rights ) {
156 $this->addFields( array( 'groups' =>
157 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', 'ug_user=user_id' )
158 ) );
161 if ( $params['activeusers'] ) {
162 $activeUserSeconds = $activeUserDays * 86400;
164 // Filter query to only include users in the active users cache.
165 // There shouldn't be any duplicate rows in querycachetwo here.
166 $this->addTables( 'querycachetwo' );
167 $this->addJoinConds( array( 'querycachetwo' => array(
168 'INNER JOIN', array(
169 'qcc_type' => 'activeusers',
170 'qcc_namespace' => NS_USER,
171 'qcc_title=user_name',
173 ) ) );
175 // Actually count the actions using a subquery (bug 64505 and bug 64507)
176 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
177 $this->addFields( array(
178 'recentactions' => '(' . $db->selectSQLText(
179 'recentchanges',
180 'COUNT(*)',
181 array(
182 'rc_user_text = user_name',
183 'rc_type != ' . $db->addQuotes( RC_EXTERNAL ), // no wikidata
184 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
185 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
187 ) . ')'
188 ) );
191 $sqlLimit = $limit + $maxDuplicateRows;
192 $this->addOption( 'LIMIT', $sqlLimit );
194 $this->addFields( array(
195 'user_name',
196 'user_id'
197 ) );
198 $this->addFieldsIf( 'user_editcount', $fld_editcount );
199 $this->addFieldsIf( 'user_registration', $fld_registration );
201 $res = $this->select( __METHOD__ );
202 $count = 0;
203 $countDuplicates = 0;
204 $lastUser = false;
205 $result = $this->getResult();
206 foreach ( $res as $row ) {
207 $count++;
209 if ( $lastUser === $row->user_name ) {
210 // Duplicate row due to one of the needed subtable joins.
211 // Ignore it, but count the number of them to sanely handle
212 // miscalculation of $maxDuplicateRows.
213 $countDuplicates++;
214 if ( $countDuplicates == $maxDuplicateRows ) {
215 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
217 continue;
220 $countDuplicates = 0;
221 $lastUser = $row->user_name;
223 if ( $count > $limit ) {
224 // We've reached the one extra which shows that there are
225 // additional pages to be had. Stop here...
226 $this->setContinueEnumParameter( 'from', $row->user_name );
227 break;
230 if ( $count == $sqlLimit ) {
231 // Should never hit this (either the $countDuplicates check or
232 // the $count > $limit check should hit first), but check it
233 // anyway just in case.
234 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
237 if ( $params['activeusers'] && $row->recentactions === 0 ) {
238 // activeusers cache was out of date
239 continue;
242 $data = array(
243 'userid' => $row->user_id,
244 'name' => $row->user_name,
247 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
248 $data['blockid'] = $row->ipb_id;
249 $data['blockedby'] = $row->ipb_by_text;
250 $data['blockedbyid'] = $row->ipb_by;
251 $data['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
252 $data['blockreason'] = $row->ipb_reason;
253 $data['blockexpiry'] = $row->ipb_expiry;
255 if ( $row->ipb_deleted ) {
256 $data['hidden'] = '';
258 if ( $fld_editcount ) {
259 $data['editcount'] = intval( $row->user_editcount );
261 if ( $params['activeusers'] ) {
262 $data['recentactions'] = intval( $row->recentactions );
263 // @todo 'recenteditcount' is set for BC, remove in 1.25
264 $data['recenteditcount'] = $data['recentactions'];
266 if ( $fld_registration ) {
267 $data['registration'] = $row->user_registration ?
268 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
271 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
272 $user = User::newFromId( $row->user_id );
273 $implicitGroups = User::newFromId( $row->user_id )->getAutomaticGroups();
274 if ( isset( $row->groups ) && $row->groups !== '' ) {
275 $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
276 } else {
277 $groups = $implicitGroups;
280 if ( $fld_groups ) {
281 $data['groups'] = $groups;
282 $result->setIndexedTagName( $data['groups'], 'g' );
285 if ( $fld_implicitgroups ) {
286 $data['implicitgroups'] = $implicitGroups;
287 $result->setIndexedTagName( $data['implicitgroups'], 'g' );
290 if ( $fld_rights ) {
291 $data['rights'] = User::getGroupPermissions( $groups );
292 $result->setIndexedTagName( $data['rights'], 'r' );
296 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
297 if ( !$fit ) {
298 $this->setContinueEnumParameter( 'from', $data['name'] );
299 break;
303 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
306 public function getCacheMode( $params ) {
307 return 'anon-public-user-private';
310 public function getAllowedParams() {
311 $userGroups = User::getAllGroups();
313 return array(
314 'from' => null,
315 'to' => null,
316 'prefix' => null,
317 'dir' => array(
318 ApiBase::PARAM_DFLT => 'ascending',
319 ApiBase::PARAM_TYPE => array(
320 'ascending',
321 'descending'
324 'group' => array(
325 ApiBase::PARAM_TYPE => $userGroups,
326 ApiBase::PARAM_ISMULTI => true,
328 'excludegroup' => array(
329 ApiBase::PARAM_TYPE => $userGroups,
330 ApiBase::PARAM_ISMULTI => true,
332 'rights' => array(
333 ApiBase::PARAM_TYPE => User::getAllRights(),
334 ApiBase::PARAM_ISMULTI => true,
336 'prop' => array(
337 ApiBase::PARAM_ISMULTI => true,
338 ApiBase::PARAM_TYPE => array(
339 'blockinfo',
340 'groups',
341 'implicitgroups',
342 'rights',
343 'editcount',
344 'registration'
347 'limit' => array(
348 ApiBase::PARAM_DFLT => 10,
349 ApiBase::PARAM_TYPE => 'limit',
350 ApiBase::PARAM_MIN => 1,
351 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
352 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
354 'witheditsonly' => false,
355 'activeusers' => array(
356 ApiBase::PARAM_DFLT => false,
357 ApiBase::PARAM_HELP_MSG => array(
358 'apihelp-query+allusers-param-activeusers',
359 $this->getConfig()->get( 'ActiveUserDays' )
365 protected function getExamplesMessages() {
366 return array(
367 'action=query&list=allusers&aufrom=Y'
368 => 'apihelp-query+allusers-example-Y',
372 public function getHelpUrls() {
373 return 'https://www.mediawiki.org/wiki/API:Allusers';