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