]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryAllUsers.php
MediaWiki 1.11.0
[autoinstallsdev/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  * @addtogroup 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_editcount = isset($prop['editcount']);
50                         $fld_groups = isset($prop['groups']);
51                 } else {
52                         $fld_editcount = $fld_groups = false;
53                 }
54
55                 $limit = $params['limit'];
56                 $tables = $db->tableName('user');
57                 
58                 if( !is_null( $params['from'] ) )
59                         $this->addWhere( 'user_name >= ' . $db->addQuotes( self::keyToTitle( $params['from'] ) ) );
60                 
61                 if( isset( $params['prefix'] ) )
62                         $this->addWhere( 'user_name LIKE "' . $db->escapeLike( self::keyToTitle( $params['prefix'] ) ) . '%"' );
63
64                 if (!is_null($params['group'])) {
65                         // Filter only users that belong to a given group
66                         $tblName = $db->tableName('user_groups');
67                         $tables = "$tables INNER JOIN $tblName ug1 ON ug1.ug_user=user_id";
68                         $this->addWhereFld('ug1.ug_group', $params['group']);
69                 }
70
71                 if ($fld_groups) {
72                         // Show the groups the given users belong to
73                         // request more than needed to avoid not getting all rows that belong to one user
74                         $groupCount = count(User::getAllGroups());
75                         $sqlLimit = $limit+$groupCount+1;
76
77                         $tblName = $db->tableName('user_groups');
78                         $tables = "$tables LEFT JOIN $tblName ug2 ON ug2.ug_user=user_id";
79                         $this->addFields('ug2.ug_group ug_group2');
80                 } else {
81                         $sqlLimit = $limit+1;
82                 }
83
84                 $this->addOption('LIMIT', $sqlLimit);
85                 $this->addTables($tables);
86
87                 $this->addFields('user_name');
88                 $this->addFieldsIf('user_editcount', $fld_editcount);
89
90                 $this->addOption('ORDER BY', 'user_name');
91
92                 $res = $this->select(__METHOD__);
93
94                 $data = array ();
95                 $count = 0;
96                 $lastUserData = false;
97                 $lastUser = false;
98                 $result = $this->getResult();
99                                 
100                 //
101                 // This loop keeps track of the last entry.
102                 // For each new row, if the new row is for different user then the last, the last entry is added to results.
103                 // Otherwise, the group of the new row is appended to the last entry.
104                 // The setContinue... is more complex because of this, and takes into account the higher sql limit
105                 // to make sure all rows that belong to the same user are received.
106                 //
107                 while (true) {
108                         
109                         $row = $db->fetchObject($res);
110                         $count++;
111                         
112                         if (!$row || $lastUser != $row->user_name) {
113                                 // Save the last pass's user data
114                                 if (is_array($lastUserData))
115                                         $data[] = $lastUserData;
116                                 
117                                 // No more rows left
118                                 if (!$row)
119                                         break;
120
121                                 if ($count > $limit) {
122                                         // We've reached the one extra which shows that there are additional pages to be had. Stop here...
123                                         $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->user_name));
124                                         break;
125                                 }
126
127                                 // Record new user's data
128                                 $lastUser = $row->user_name;
129                                 $lastUserData = array( 'name' => $lastUser );
130                                 if ($fld_editcount)
131                                         $lastUserData['editcount'] = intval($row->user_editcount);
132                                         
133                         }
134                         
135                         if ($sqlLimit == $count) {
136                                 // BUG!  database contains group name that User::getAllGroups() does not return
137                                 // TODO: should handle this more gracefully
138                                 ApiBase :: dieDebug(__METHOD__, 
139                                         'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function');
140                         }
141                                                                 
142                         // Add user's group info
143                         if ($fld_groups && !is_null($row->ug_group2)) {
144                                 $lastUserData['groups'][] = $row->ug_group2;
145                                 $result->setIndexedTagName($lastUserData['groups'], 'g');
146                         }
147                 }
148                 
149                 $db->freeResult($res);
150
151                 $result->setIndexedTagName($data, 'u');
152                 $result->addValue('query', $this->getModuleName(), $data);
153         }
154
155         protected function getAllowedParams() {
156                 return array (
157                         'from' => null,
158                         'prefix' => null,
159                         'group' => array(
160                                 ApiBase :: PARAM_TYPE => User::getAllGroups()
161                         ),
162                         'prop' => array (
163                                 ApiBase :: PARAM_ISMULTI => true, 
164                                 ApiBase :: PARAM_TYPE => array (
165                                         'editcount',
166                                         'groups',
167                                 )
168                         ),
169                         'limit' => array (
170                                 ApiBase :: PARAM_DFLT => 10,
171                                 ApiBase :: PARAM_TYPE => 'limit',
172                                 ApiBase :: PARAM_MIN => 1,
173                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
174                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
175                         )
176                 );
177         }
178
179         protected function getParamDescription() {
180                 return array (
181                         'from' => 'The user name to start enumerating from.',
182                         'prefix' => 'Search for all page titles that begin with this value.',
183                         'group' => 'Limit users to a given group name',
184                         'prop' => array(
185                                 'What pieces of information to include.',
186                                 '`groups` property uses more server resources and may return fewer results than the limit.'),
187                         'limit' => 'How many total user names to return.',
188                 );
189         }
190
191         protected function getDescription() {
192                 return 'Enumerate all registered users';
193         }
194
195         protected function getExamples() {
196                 return array (
197                         'api.php?action=query&list=allusers&aufrom=Y',
198                 );
199         }
200
201         public function getVersion() {
202                 return __CLASS__ . ': $Id: ApiQueryAllUsers.php 24870 2007-08-17 13:01:35Z robchurch $';
203         }
204 }