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