]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllUsers.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryAllUsers.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
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_registration = isset( $prop['registration'] );
53                 } else {
54                         $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = 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                 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
99
100                 $this->addOption( 'LIMIT', $sqlLimit );
101
102                 $this->addFields( array(
103                         'u1.user_name',
104                         'u1.user_id'
105                 ) );
106                 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
107                 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
108
109                 $this->addOption( 'ORDER BY', 'u1.user_name' );
110                 if ( $useIndex ) {
111                         $u1 = $this->getAliasedName( 'user', 'u1' );
112                         $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
113                 }
114
115                 $res = $this->select( __METHOD__ );
116
117                 $count = 0;
118                 $lastUserData = false;
119                 $lastUser = false;
120                 $result = $this->getResult();
121
122                 //
123                 // This loop keeps track of the last entry.
124                 // For each new row, if the new row is for different user then the last, the last entry is added to results.
125                 // Otherwise, the group of the new row is appended to the last entry.
126                 // The setContinue... is more complex because of this, and takes into account the higher sql limit
127                 // to make sure all rows that belong to the same user are received.
128
129                 foreach ( $res as $row ) {
130                         $count++;
131
132                         if ( $lastUser !== $row->user_name ) {
133                                 // Save the last pass's user data
134                                 if ( is_array( $lastUserData ) ) {
135                                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
136                                                         null, $lastUserData );
137
138                                         $lastUserData = null;
139
140                                         if ( !$fit ) {
141                                                 $this->setContinueEnumParameter( 'from',
142                                                                 $this->keyToTitle( $lastUserData['name'] ) );
143                                                 break;
144                                         }
145                                 }
146
147                                 if ( $count > $limit ) {
148                                         // We've reached the one extra which shows that there are additional pages to be had. Stop here...
149                                         $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
150                                         break;
151                                 }
152
153                                 // Record new user's data
154                                 $lastUser = $row->user_name;
155                                 $lastUserData = array(
156                                         'name' => $lastUser,
157                                         'userid' => $row->user_id,
158                                 );
159                                 if ( $fld_blockinfo && !is_null( $row->blocker_name ) ) {
160                                         $lastUserData['blockedby'] = $row->blocker_name;
161                                         $lastUserData['blockreason'] = $row->ipb_reason;
162                                 }
163                                 if ( $row->ipb_deleted ) {
164                                         $lastUserData['hidden'] = '';
165                                 }
166                                 if ( $fld_editcount ) {
167                                         $lastUserData['editcount'] = intval( $row->user_editcount );
168                                 }
169                                 if ( $fld_registration ) {
170                                         $lastUserData['registration'] = $row->user_registration ?
171                                                 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
172                                 }
173
174                         }
175
176                         if ( $sqlLimit == $count ) {
177                                 // BUG!  database contains group name that User::getAllGroups() does not return
178                                 // TODO: should handle this more gracefully
179                                 ApiBase::dieDebug( __METHOD__,
180                                         'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
181                         }
182
183                         // Add user's group info
184                         if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
185                                 $lastUserData['groups'][] = $row->ug_group2;
186                                 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
187                         }
188                 }
189
190                 if ( is_array( $lastUserData ) ) {
191                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
192                                 null, $lastUserData );
193                         if ( !$fit ) {
194                                 $this->setContinueEnumParameter( 'from',
195                                         $this->keyToTitle( $lastUserData['name'] ) );
196                         }
197                 }
198
199                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
200         }
201
202         public function getCacheMode( $params ) {
203                 return 'anon-public-user-private';
204         }
205
206         public function getAllowedParams() {
207                 return array(
208                         'from' => null,
209                         'to' => null,
210                         'prefix' => null,
211                         'group' => array(
212                                 ApiBase::PARAM_TYPE => User::getAllGroups()
213                         ),
214                         'prop' => array(
215                                 ApiBase::PARAM_ISMULTI => true,
216                                 ApiBase::PARAM_TYPE => array(
217                                         'blockinfo',
218                                         'groups',
219                                         'editcount',
220                                         'registration'
221                                 )
222                         ),
223                         'limit' => array(
224                                 ApiBase::PARAM_DFLT => 10,
225                                 ApiBase::PARAM_TYPE => 'limit',
226                                 ApiBase::PARAM_MIN => 1,
227                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
228                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
229                         ),
230                         'witheditsonly' => false,
231                 );
232         }
233
234         public function getParamDescription() {
235                 return array(
236                         'from' => 'The user name to start enumerating from',
237                         'to' => 'The user name to stop enumerating at',
238                         'prefix' => 'Search for all users that begin with this value',
239                         'group' => 'Limit users to a given group name',
240                         'prop' => array(
241                                 'What pieces of information to include.',
242                                 ' blockinfo     - Adds the information about a current block on the user',
243                                 ' groups        - Lists groups that the user is in',
244                                 ' editcount     - Adds the edit count of the user',
245                                 ' registration  - Adds the timestamp of when the user registered',
246                                 '`groups` property uses more server resources and may return fewer results than the limit' ),
247                         'limit' => 'How many total user names to return',
248                         'witheditsonly' => 'Only list users who have made edits',
249                 );
250         }
251
252         public function getDescription() {
253                 return 'Enumerate all registered users';
254         }
255
256         protected function getExamples() {
257                 return array(
258                         'api.php?action=query&list=allusers&aufrom=Y',
259                 );
260         }
261
262         public function getVersion() {
263                 return __CLASS__ . ': $Id$';
264         }
265 }