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