* (bug 24677) axto= parameters added to allcategories, allimages, alllinks, allmessag...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2
3 /**
4 * Created on July 7, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate all registered users.
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllUsers extends ApiQueryBase {
37
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_registration = isset( $prop['registration'] );
53 } else {
54 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
55 }
56
57 $limit = $params['limit'];
58 $this->addTables( 'user', 'u1' );
59 $useIndex = true;
60
61 if ( !is_null( $params['from'] ) ) {
62 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
63 }
64 if ( !is_null( $params['to'] ) ) {
65 $this->addWhere( 'u1.user_name <= ' . $db->addQuotes( $this->keyToTitle( $params['to'] ) ) );
66 }
67
68 if ( !is_null( $params['prefix'] ) ) {
69 $this->addWhere( 'u1.user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
70 }
71
72 if ( !is_null( $params['group'] ) ) {
73 $useIndex = false;
74 // Filter only users that belong to a given group
75 $this->addTables( 'user_groups', 'ug1' );
76 $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
77 $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=u1.user_id',
78 'ug1.ug_group' => $params['group'] ) ) ) );
79 }
80
81 if ( $params['witheditsonly'] ) {
82 $this->addWhere( 'u1.user_editcount > 0' );
83 }
84
85 if ( $fld_groups ) {
86 // Show the groups the given users belong to
87 // request more than needed to avoid not getting all rows that belong to one user
88 $groupCount = count( User::getAllGroups() );
89 $sqlLimit = $limit + $groupCount + 1;
90
91 $this->addTables( 'user_groups', 'ug2' );
92 $tname = $this->getAliasedName( 'user_groups', 'ug2' );
93 $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=u1.user_id' ) ) );
94 $this->addFields( 'ug2.ug_group ug_group2' );
95 } else {
96 $sqlLimit = $limit + 1;
97 }
98 if ( $fld_blockinfo ) {
99 $this->addTables( 'ipblocks' );
100 $this->addTables( 'user', 'u2' );
101 $u2 = $this->getAliasedName( 'user', 'u2' );
102 $this->addJoinConds( array(
103 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
104 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
105 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
106 }
107
108 $this->addOption( 'LIMIT', $sqlLimit );
109
110 $this->addFields( 'u1.user_name' );
111 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
112 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
113
114 $this->addOption( 'ORDER BY', 'u1.user_name' );
115 if ( $useIndex ) {
116 $u1 = $this->getAliasedName( 'user', 'u1' );
117 $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
118 }
119
120 $res = $this->select( __METHOD__ );
121
122 $count = 0;
123 $lastUserData = false;
124 $lastUser = false;
125 $result = $this->getResult();
126
127 //
128 // This loop keeps track of the last entry.
129 // For each new row, if the new row is for different user then the last, the last entry is added to results.
130 // Otherwise, the group of the new row is appended to the last entry.
131 // The setContinue... is more complex because of this, and takes into account the higher sql limit
132 // to make sure all rows that belong to the same user are received.
133
134 $row = $db->fetchObject( $res );
135 foreach ( $res as $row ) {
136 $count++;
137
138 if ( $lastUser !== $row->user_name ) {
139 // Save the last pass's user data
140 if ( is_array( $lastUserData ) ) {
141 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
142 null, $lastUserData );
143 if ( !$fit ) {
144 $this->setContinueEnumParameter( 'from',
145 $this->keyToTitle( $lastUserData['name'] ) );
146 break;
147 }
148 }
149
150 if ( $count > $limit ) {
151 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
152 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
153 break;
154 }
155
156 // Record new user's data
157 $lastUser = $row->user_name;
158 $lastUserData = array( 'name' => $lastUser );
159 if ( $fld_blockinfo ) {
160 $lastUserData['blockedby'] = $row->blocker_name;
161 $lastUserData['blockreason'] = $row->ipb_reason;
162 }
163 if ( $fld_editcount ) {
164 $lastUserData['editcount'] = intval( $row->user_editcount );
165 }
166 if ( $fld_registration ) {
167 $lastUserData['registration'] = $row->user_registration ?
168 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
169 }
170
171 }
172
173 if ( $sqlLimit == $count ) {
174 // BUG! database contains group name that User::getAllGroups() does not return
175 // TODO: should handle this more gracefully
176 ApiBase::dieDebug( __METHOD__,
177 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
178 }
179
180 // Add user's group info
181 if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
182 $lastUserData['groups'][] = $row->ug_group2;
183 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
184 }
185 }
186
187 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
188 }
189
190 public function getCacheMode( $params ) {
191 return 'public';
192 }
193
194 public function getAllowedParams() {
195 return array(
196 'from' => null,
197 'to' => null,
198 'prefix' => null,
199 'group' => array(
200 ApiBase::PARAM_TYPE => User::getAllGroups()
201 ),
202 'prop' => array(
203 ApiBase::PARAM_ISMULTI => true,
204 ApiBase::PARAM_TYPE => array(
205 'blockinfo',
206 'groups',
207 'editcount',
208 'registration'
209 )
210 ),
211 'limit' => array(
212 ApiBase::PARAM_DFLT => 10,
213 ApiBase::PARAM_TYPE => 'limit',
214 ApiBase::PARAM_MIN => 1,
215 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
216 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
217 ),
218 'witheditsonly' => false,
219 );
220 }
221
222 public function getParamDescription() {
223 return array(
224 'from' => 'The user name to start enumerating from',
225 'to' => 'The user name to stop enumerating at',
226 'prefix' => 'Search for all page titles that begin with this value',
227 'group' => 'Limit users to a given group name',
228 'prop' => array(
229 'What pieces of information to include.',
230 ' blockinfo - Adds the information about a current block on the user',
231 ' groups - Lists groups that the user is in',
232 ' editcount - Adds the edit count of the user',
233 ' registration - Adds the timestamp of when the user registered',
234 '`groups` property uses more server resources and may return fewer results than the limit' ),
235 'limit' => 'How many total user names to return',
236 'witheditsonly' => 'Only list users who have made edits',
237 );
238 }
239
240 public function getDescription() {
241 return 'Enumerate all registered users';
242 }
243
244 protected function getExamples() {
245 return array(
246 'api.php?action=query&list=allusers&aufrom=Y',
247 );
248 }
249
250 public function getVersion() {
251 return __CLASS__ . ': $Id$';
252 }
253 }