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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to enumerate all registered users.
37 class ApiQueryAllUsers
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'au' );
43 public function execute() {
45 $params = $this->extractRequestParams();
47 $prop = $params['prop'];
48 if ( !is_null( $prop ) ) {
49 $prop = array_flip( $prop );
50 $fld_blockinfo = isset( $prop['blockinfo'] );
51 $fld_editcount = isset( $prop['editcount'] );
52 $fld_groups = isset( $prop['groups'] );
53 $fld_rights = isset( $prop['rights'] );
54 $fld_registration = isset( $prop['registration'] );
56 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
59 $limit = $params['limit'];
60 $this->addTables( 'user', 'u1' );
63 if ( !is_null( $params['from'] ) ) {
64 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
66 if ( !is_null( $params['to'] ) ) {
67 $this->addWhere( 'u1.user_name <= ' . $db->addQuotes( $this->keyToTitle( $params['to'] ) ) );
70 if ( !is_null( $params['prefix'] ) ) {
71 $this->addWhere( 'u1.user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
74 if ( !is_null( $params['group'] ) ) {
76 // Filter only users that belong to a given group
77 $this->addTables( 'user_groups', 'ug1' );
78 $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
79 $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=u1.user_id',
80 'ug1.ug_group' => $params['group'] ) ) ) );
83 if ( $params['witheditsonly'] ) {
84 $this->addWhere( 'u1.user_editcount > 0' );
87 if ( $fld_groups ||
$fld_rights ) {
88 // Show the groups the given users belong to
89 // request more than needed to avoid not getting all rows that belong to one user
90 $groupCount = count( User
::getAllGroups() );
91 $sqlLimit = $limit +
$groupCount +
1;
93 $this->addTables( 'user_groups', 'ug2' );
94 $tname = $this->getAliasedName( 'user_groups', 'ug2' );
95 $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=u1.user_id' ) ) );
96 $this->addFields( 'ug2.ug_group ug_group2' );
98 $sqlLimit = $limit +
1;
100 if ( $fld_blockinfo ) {
101 $this->addTables( 'ipblocks' );
102 $this->addTables( 'user', 'u2' );
103 $u2 = $this->getAliasedName( 'user', 'u2' );
104 $this->addJoinConds( array(
105 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
106 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
107 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
110 $this->addOption( 'LIMIT', $sqlLimit );
112 $this->addFields( array(
116 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
117 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
119 $this->addOption( 'ORDER BY', 'u1.user_name' );
121 $u1 = $this->getAliasedName( 'user', 'u1' );
122 $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
125 $res = $this->select( __METHOD__
);
128 $lastUserData = false;
130 $result = $this->getResult();
133 // This loop keeps track of the last entry.
134 // For each new row, if the new row is for different user then the last, the last entry is added to results.
135 // Otherwise, the group of the new row is appended to the last entry.
136 // The setContinue... is more complex because of this, and takes into account the higher sql limit
137 // to make sure all rows that belong to the same user are received.
139 foreach ( $res as $row ) {
142 if ( $lastUser !== $row->user_name
) {
143 // Save the last pass's user data
144 if ( is_array( $lastUserData ) ) {
145 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
146 null, $lastUserData );
148 $this->setContinueEnumParameter( 'from',
149 $this->keyToTitle( $lastUserData['name'] ) );
154 if ( $count > $limit ) {
155 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
156 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name
) );
160 // Record new user's data
161 $lastUser = $row->user_name
;
162 $lastUserData = array(
164 'userid' => $row->user_id
,
166 if ( $fld_blockinfo && !is_null( $row->blocker_name
) ) {
167 $lastUserData['blockedby'] = $row->blocker_name
;
168 $lastUserData['blockreason'] = $row->ipb_reason
;
170 if ( $fld_editcount ) {
171 $lastUserData['editcount'] = intval( $row->user_editcount
);
173 if ( $fld_registration ) {
174 $lastUserData['registration'] = $row->user_registration ?
175 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
180 if ( $sqlLimit == $count ) {
181 // BUG! database contains group name that User::getAllGroups() does not return
182 // TODO: should handle this more gracefully
183 ApiBase
::dieDebug( __METHOD__
,
184 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
187 // Add user's group info
188 if ( $fld_groups && !is_null( $row->ug_group2
) ) {
189 $lastUserData['groups'][] = $row->ug_group2
;
190 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
193 if ( $fld_rights && !is_null( $row->ug_group2
) ) {
194 if ( !isset( $lastUserData['rights'] ) ) {
195 $lastUserData['rights'] = User
::getGroupPermissions( User
::getImplicitGroups() );
198 $lastUserData['rights'] = array_merge( $lastUserData['rights'], User
::getGroupPermissions( array( $row->ug_group2
) ) );
199 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
203 if ( is_array( $lastUserData ) ) {
204 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
205 null, $lastUserData );
207 $this->setContinueEnumParameter( 'from',
208 $this->keyToTitle( $lastUserData['name'] ) );
212 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
215 public function getCacheMode( $params ) {
219 public function getAllowedParams() {
225 ApiBase
::PARAM_TYPE
=> User
::getAllGroups()
228 ApiBase
::PARAM_ISMULTI
=> true,
229 ApiBase
::PARAM_TYPE
=> array(
237 ApiBase
::PARAM_DFLT
=> 10,
238 ApiBase
::PARAM_TYPE
=> 'limit',
239 ApiBase
::PARAM_MIN
=> 1,
240 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
241 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
243 'witheditsonly' => false,
247 public function getParamDescription() {
249 'from' => 'The user name to start enumerating from',
250 'to' => 'The user name to stop enumerating at',
251 'prefix' => 'Search for all users that begin with this value',
252 'group' => 'Limit users to a given group name',
254 'What pieces of information to include.',
255 ' blockinfo - Adds the information about a current block on the user',
256 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
257 ' rights - Lists groups that the user has',
258 ' editcount - Adds the edit count of the user',
259 ' registration - Adds the timestamp of when the user registered',
261 'limit' => 'How many total user names to return',
262 'witheditsonly' => 'Only list users who have made edits',
266 public function getDescription() {
267 return 'Enumerate all registered users';
270 protected function getExamples() {
272 'api.php?action=query&list=allusers&aufrom=Y',
276 public function getVersion() {
277 return __CLASS__
. ': $Id$';