Fix the rest of 'inlanguage ' in r93765
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 7, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan <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 enumerate all registered users.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllUsers extends ApiQueryBase {
33 public function __construct( $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'au' );
35 }
36
37 public function execute() {
38 $db = $this->getDB();
39 $params = $this->extractRequestParams();
40
41 $prop = $params['prop'];
42 if ( !is_null( $prop ) ) {
43 $prop = array_flip( $prop );
44 $fld_blockinfo = isset( $prop['blockinfo'] );
45 $fld_editcount = isset( $prop['editcount'] );
46 $fld_groups = isset( $prop['groups'] );
47 $fld_rights = isset( $prop['rights'] );
48 $fld_registration = isset( $prop['registration'] );
49 $fld_implicitgroups = isset( $prop['implicitgroups'] );
50 } else {
51 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
52 }
53
54 $limit = $params['limit'];
55
56 $this->addTables( 'user' );
57 $useIndex = true;
58
59 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
60 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
61 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
62
63 $this->addWhereRange( 'user_name', $dir, $from, $to );
64
65 if ( !is_null( $params['prefix'] ) ) {
66 $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
67 }
68
69 if ( !is_null( $params['rights'] ) ) {
70 $groups = array();
71 foreach( $params['rights'] as $r ) {
72 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
73 }
74
75 $groups = array_unique( $groups );
76
77 if ( is_null( $params['group'] ) ) {
78 $params['group'] = $groups;
79 } else {
80 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
81 }
82 }
83
84 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
85 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
86 }
87
88 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
89 $useIndex = false;
90 // Filter only users that belong to a given group
91 $this->addTables( 'user_groups', 'ug1' );
92 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
93 'ug1.ug_group' => $params['group'] ) ) ) );
94 }
95
96 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
97 $useIndex = false;
98 // Filter only users don't belong to a given group
99 $this->addTables( 'user_groups', 'ug1' );
100
101 if ( count( $params['excludegroup'] ) == 1 ) {
102 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
103 } else {
104 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
105 }
106 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
107 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
108 )
109 ) );
110 $this->addWhere( 'ug1.ug_user IS NULL' );
111 }
112
113 if ( $params['witheditsonly'] ) {
114 $this->addWhere( 'user_editcount > 0' );
115 }
116
117 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
118
119 if ( $fld_groups || $fld_rights ) {
120 // Show the groups the given users belong to
121 // request more than needed to avoid not getting all rows that belong to one user
122 $groupCount = count( User::getAllGroups() );
123 $sqlLimit = $limit + $groupCount + 1;
124
125 $this->addTables( 'user_groups', 'ug2' );
126 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
127 $this->addFields( 'ug2.ug_group ug_group2' );
128 } else {
129 $sqlLimit = $limit + 1;
130 }
131
132 if ( $params['activeusers'] ) {
133 global $wgActiveUserDays;
134 $this->addTables( 'recentchanges' );
135
136 $this->addJoinConds( array( 'recentchanges' => array(
137 'INNER JOIN', 'rc_user_text=user_name'
138 ) ) );
139
140 $this->addFields( 'COUNT(*) AS recentedits' );
141
142 $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
143 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
144 $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
145
146 $this->addOption( 'GROUP BY', 'user_name' );
147 }
148
149 $this->addOption( 'LIMIT', $sqlLimit );
150
151 $this->addFields( array(
152 'user_name',
153 'user_id'
154 ) );
155 $this->addFieldsIf( 'user_editcount', $fld_editcount );
156 $this->addFieldsIf( 'user_registration', $fld_registration );
157
158 if ( $useIndex ) {
159 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
160 }
161
162 $res = $this->select( __METHOD__ );
163
164 $count = 0;
165 $lastUserData = false;
166 $lastUser = false;
167 $result = $this->getResult();
168
169 //
170 // This loop keeps track of the last entry.
171 // For each new row, if the new row is for different user then the last, the last entry is added to results.
172 // Otherwise, the group of the new row is appended to the last entry.
173 // The setContinue... is more complex because of this, and takes into account the higher sql limit
174 // to make sure all rows that belong to the same user are received.
175
176 foreach ( $res as $row ) {
177 $count++;
178
179 if ( $lastUser !== $row->user_name ) {
180 // Save the last pass's user data
181 if ( is_array( $lastUserData ) ) {
182 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
183 null, $lastUserData );
184
185 $lastUserData = null;
186
187 if ( !$fit ) {
188 $this->setContinueEnumParameter( 'from',
189 $this->keyToTitle( $lastUserData['name'] ) );
190 break;
191 }
192 }
193
194 if ( $count > $limit ) {
195 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
196 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
197 break;
198 }
199
200 // Record new user's data
201 $lastUser = $row->user_name;
202 $lastUserData = array(
203 'userid' => $row->user_id,
204 'name' => $lastUser,
205 );
206 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
207 $lastUserData['blockedby'] = $row->ipb_by_text;
208 $lastUserData['blockreason'] = $row->ipb_reason;
209 $lastUserData['blockexpiry'] = $row->ipb_expiry;
210 }
211 if ( $row->ipb_deleted ) {
212 $lastUserData['hidden'] = '';
213 }
214 if ( $fld_editcount ) {
215 $lastUserData['editcount'] = intval( $row->user_editcount );
216 }
217 if ( $params['activeusers'] ) {
218 $lastUserData['recenteditcount'] = intval( $row->recentedits );
219 }
220 if ( $fld_registration ) {
221 $lastUserData['registration'] = $row->user_registration ?
222 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
223 }
224 }
225
226 if ( $sqlLimit == $count ) {
227 // BUG! database contains group name that User::getAllGroups() does not return
228 // TODO: should handle this more gracefully
229 ApiBase::dieDebug( __METHOD__,
230 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
231 }
232
233 $lastUserObj = User::newFromName( $lastUser );
234
235 // Add user's group info
236 if ( $fld_groups ) {
237 if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
238 $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
239 }
240
241 if ( !is_null( $row->ug_group2 ) ) {
242 $lastUserData['groups'][] = $row->ug_group2;
243 }
244 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
245 }
246
247 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
248 $lastUserData['implicitgroups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
249 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
250 }
251 if ( $fld_rights ) {
252 if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
253 $lastUserData['rights'] = User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
254 }
255 if ( !is_null( $row->ug_group2 ) ) {
256 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
257 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
258 }
259 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
260 }
261 }
262
263 if ( is_array( $lastUserData ) ) {
264 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
265 null, $lastUserData );
266 if ( !$fit ) {
267 $this->setContinueEnumParameter( 'from',
268 $this->keyToTitle( $lastUserData['name'] ) );
269 }
270 }
271
272 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
273 }
274
275 public function getCacheMode( $params ) {
276 return 'anon-public-user-private';
277 }
278
279 public function getAllowedParams() {
280 $userGroups = User::getAllGroups();
281 return array(
282 'from' => null,
283 'to' => null,
284 'prefix' => null,
285 'dir' => array(
286 ApiBase::PARAM_DFLT => 'ascending',
287 ApiBase::PARAM_TYPE => array(
288 'ascending',
289 'descending'
290 ),
291 ),
292 'group' => array(
293 ApiBase::PARAM_TYPE => $userGroups,
294 ApiBase::PARAM_ISMULTI => true,
295 ),
296 'excludegroup' => array(
297 ApiBase::PARAM_TYPE => $userGroups,
298 ApiBase::PARAM_ISMULTI => true,
299 ),
300 'rights' => array(
301 ApiBase::PARAM_TYPE => User::getAllRights(),
302 ApiBase::PARAM_ISMULTI => true,
303 ),
304 'prop' => array(
305 ApiBase::PARAM_ISMULTI => true,
306 ApiBase::PARAM_TYPE => array(
307 'blockinfo',
308 'groups',
309 'implicitgroups',
310 'rights',
311 'editcount',
312 'registration'
313 )
314 ),
315 'limit' => array(
316 ApiBase::PARAM_DFLT => 10,
317 ApiBase::PARAM_TYPE => 'limit',
318 ApiBase::PARAM_MIN => 1,
319 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
320 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
321 ),
322 'witheditsonly' => false,
323 'activeusers' => false,
324 );
325 }
326
327 public function getParamDescription() {
328 global $wgActiveUserDays;
329 return array(
330 'from' => 'The user name to start enumerating from',
331 'to' => 'The user name to stop enumerating at',
332 'prefix' => 'Search for all users that begin with this value',
333 'dir' => 'Direction to sort in',
334 'group' => 'Limit users to given group name(s)',
335 'excludegroup' => 'Exclude users in given group name(s)',
336 'rights' => 'Limit users to given right(s)',
337 'prop' => array(
338 'What pieces of information to include.',
339 ' blockinfo - Adds the information about a current block on the user',
340 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
341 ' implicitgroups - Lists all the groups the user is automatically in',
342 ' rights - Lists rights that the user has',
343 ' editcount - Adds the edit count of the user',
344 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
345 ),
346 'limit' => 'How many total user names to return',
347 'witheditsonly' => 'Only list users who have made edits',
348 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
349 );
350 }
351
352 public function getDescription() {
353 return 'Enumerate all registered users';
354 }
355
356 public function getPossibleErrors() {
357 return array_merge( parent::getPossibleErrors(), array(
358 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
359 ) );
360 }
361
362 public function getExamples() {
363 return array(
364 'api.php?action=query&list=allusers&aufrom=Y',
365 );
366 }
367
368 public function getHelpUrls() {
369 return 'http://www.mediawiki.org/wiki/API:Allusers';
370 }
371
372 public function getVersion() {
373 return __CLASS__ . ': $Id$';
374 }
375 }