]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryAllUsers.php
MediaWiki 1.15.5
[autoinstalls/mediawiki.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 (C) 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  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
60                 if (!is_null($params['from']))
61                         $this->addWhere('u1.user_name >= ' . $db->addQuotes($this->keyToTitle($params['from'])));
62
63                 if (!is_null($params['prefix']))
64                         $this->addWhere('u1.user_name LIKE "' . $db->escapeLike($this->keyToTitle( $params['prefix'])) . '%"');
65
66                 if (!is_null($params['group'])) {
67                         // Filter only users that belong to a given group
68                         $this->addTables('user_groups', 'ug1');
69                         $this->addWhere('ug1.ug_user=u1.user_id');
70                         $this->addWhereFld('ug1.ug_group', $params['group']);
71                 }
72
73                 if ($params['witheditsonly'])
74                         $this->addWhere('u1.user_editcount > 0');
75
76                 if ($fld_groups) {
77                         // Show the groups the given users belong to
78                         // request more than needed to avoid not getting all rows that belong to one user
79                         $groupCount = count(User::getAllGroups());
80                         $sqlLimit = $limit+$groupCount+1;
81
82                         $this->addTables('user_groups', 'ug2');
83                         $tname = $this->getAliasedName('user_groups', 'ug2');
84                         $this->addJoinConds(array($tname => array('LEFT JOIN', 'ug2.ug_user=u1.user_id')));
85                         $this->addFields('ug2.ug_group ug_group2');
86                 } else {
87                         $sqlLimit = $limit+1;
88                 }
89                 if ($fld_blockinfo) {
90                         $this->addTables('ipblocks');
91                         $this->addTables('user', 'u2');
92                         $u2 = $this->getAliasedName('user', 'u2');
93                         $this->addJoinConds(array(
94                                 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
95                                 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
96                         $this->addFields(array('ipb_reason', 'u2.user_name blocker_name'));
97                 }
98
99                 $this->addOption('LIMIT', $sqlLimit);
100
101                 $this->addFields('u1.user_name');
102                 $this->addFieldsIf('u1.user_editcount', $fld_editcount);
103                 $this->addFieldsIf('u1.user_registration', $fld_registration);
104
105                 $this->addOption('ORDER BY', 'u1.user_name');
106
107                 $res = $this->select(__METHOD__);
108
109                 $data = array ();
110                 $count = 0;
111                 $lastUserData = false;
112                 $lastUser = false;
113                 $result = $this->getResult();
114
115                 //
116                 // This loop keeps track of the last entry.
117                 // For each new row, if the new row is for different user then the last, the last entry is added to results.
118                 // Otherwise, the group of the new row is appended to the last entry.
119                 // The setContinue... is more complex because of this, and takes into account the higher sql limit
120                 // to make sure all rows that belong to the same user are received.
121                 //
122                 while (true) {
123
124                         $row = $db->fetchObject($res);
125                         $count++;
126
127                         if (!$row || $lastUser !== $row->user_name) {
128                                 // Save the last pass's user data
129                                 if (is_array($lastUserData))
130                                 {
131                                         $fit = $result->addValue(array('query', $this->getModuleName()),
132                                                         null, $lastUserData);
133                                         if(!$fit)
134                                         {
135                                                 $this->setContinueEnumParameter('from',
136                                                                 $this->keyToTitle($lastUserData['name']));
137                                                 break;
138                                         }
139                                 }
140
141                                 // No more rows left
142                                 if (!$row)
143                                         break;
144
145                                 if ($count > $limit) {
146                                         // We've reached the one extra which shows that there are additional pages to be had. Stop here...
147                                         $this->setContinueEnumParameter('from', $this->keyToTitle($row->user_name));
148                                         break;
149                                 }
150
151                                 // Record new user's data
152                                 $lastUser = $row->user_name;
153                                 $lastUserData = array( 'name' => $lastUser );
154                                 if ($fld_blockinfo) {
155                                         $lastUserData['blockedby'] = $row->blocker_name;
156                                         $lastUserData['blockreason'] = $row->ipb_reason;
157                                 }
158                                 if ($fld_editcount)
159                                         $lastUserData['editcount'] = intval($row->user_editcount);
160                                 if ($fld_registration)
161                                         $lastUserData['registration'] = wfTimestamp(TS_ISO_8601, $row->user_registration);
162
163                         }
164
165                         if ($sqlLimit == $count) {
166                                 // BUG!  database contains group name that User::getAllGroups() does not return
167                                 // TODO: should handle this more gracefully
168                                 ApiBase :: dieDebug(__METHOD__,
169                                         'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
170                         }
171
172                         // Add user's group info
173                         if ($fld_groups && !is_null($row->ug_group2)) {
174                                 $lastUserData['groups'][] = $row->ug_group2;
175                                 $result->setIndexedTagName($lastUserData['groups'], 'g');
176                         }
177                 }
178
179                 $db->freeResult($res);
180
181                 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'u');
182         }
183
184         public function getCacheMode( $params ) {
185                 return 'public';
186         }
187
188         public function getAllowedParams() {
189                 return array (
190                         'from' => null,
191                         'prefix' => null,
192                         'group' => array(
193                                 ApiBase :: PARAM_TYPE => User::getAllGroups()
194                         ),
195                         'prop' => array (
196                                 ApiBase :: PARAM_ISMULTI => true,
197                                 ApiBase :: PARAM_TYPE => array (
198                                         'blockinfo',
199                                         'groups',
200                                         'editcount',
201                                         'registration'
202                                 )
203                         ),
204                         'limit' => array (
205                                 ApiBase :: PARAM_DFLT => 10,
206                                 ApiBase :: PARAM_TYPE => 'limit',
207                                 ApiBase :: PARAM_MIN => 1,
208                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
209                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
210                         ),
211                         'witheditsonly' => false,
212                 );
213         }
214
215         public function getParamDescription() {
216                 return array (
217                         'from' => 'The user name to start enumerating from.',
218                         'prefix' => 'Search for all page titles that begin with this value.',
219                         'group' => 'Limit users to a given group name',
220                         'prop' => array(
221                                 'What pieces of information to include.',
222                                 '`groups` property uses more server resources and may return fewer results than the limit.'),
223                         'limit' => 'How many total user names to return.',
224                         'witheditsonly' => 'Only list users who have made edits',
225                 );
226         }
227
228         public function getDescription() {
229                 return 'Enumerate all registered users';
230         }
231
232         protected function getExamples() {
233                 return array (
234                         'api.php?action=query&list=allusers&aufrom=Y',
235                 );
236         }
237
238         public function getVersion() {
239                 return __CLASS__ . ': $Id: ApiQueryAllUsers.php 69986 2010-07-27 03:57:39Z tstarling $';
240         }
241 }