5f6b016c35c59019a1a794670d8178cdcc4f0df8
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2
3 /**
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 get information about a list of users
33 *
34 * @ingroup API
35 */
36 class ApiQueryUsers extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'us' );
40 }
41
42 /**
43 * Get an array mapping token names to their handler functions.
44 * The prototype for a token function is func($user)
45 * it should return a token or false (permission denied)
46 * @return array(tokenname => function)
47 */
48 protected function getTokenFunctions() {
49 // Don't call the hooks twice
50 if ( isset( $this->tokenFunctions ) ) {
51 return $this->tokenFunctions;
52 }
53
54 // If we're in JSON callback mode, no tokens can be obtained
55 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
56 return array();
57 }
58
59 $this->tokenFunctions = array(
60 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
61 );
62 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
63 return $this->tokenFunctions;
64 }
65
66 public static function getUserrightsToken( $user ) {
67 global $wgUser;
68 // Since the permissions check for userrights is non-trivial,
69 // don't bother with it here
70 return $wgUser->editToken( $user->getName() );
71 }
72
73 public function execute() {
74 $params = $this->extractRequestParams();
75 $result = $this->getResult();
76 $r = array();
77
78 if ( !is_null( $params['prop'] ) ) {
79 $this->prop = array_flip( $params['prop'] );
80 } else {
81 $this->prop = array();
82 }
83
84 $users = (array)$params['users'];
85 $goodNames = $done = array();
86 $result = $this->getResult();
87 // Canonicalize user names
88 foreach ( $users as $u ) {
89 $n = User::getCanonicalName( $u );
90 if ( $n === false || $n === '' ) {
91 $vals = array( 'name' => $u, 'invalid' => '' );
92 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
93 null, $vals );
94 if ( !$fit ) {
95 $this->setContinueEnumParameter( 'users',
96 implode( '|', array_diff( $users, $done ) ) );
97 $goodNames = array();
98 break;
99 }
100 $done[] = $u;
101 } else {
102 $goodNames[] = $n;
103 }
104 }
105
106 if ( count( $goodNames ) ) {
107 $db = $this->getDb();
108 $this->addTables( 'user', 'u1' );
109 $this->addFields( 'u1.*' );
110 $this->addWhereFld( 'u1.user_name', $goodNames );
111
112 if ( isset( $this->prop['groups'] ) ) {
113 $this->addTables( 'user_groups' );
114 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=u1.user_id' ) ) );
115 $this->addFields( 'ug_group' );
116 }
117 if ( isset( $this->prop['blockinfo'] ) ) {
118 $this->addTables( 'ipblocks' );
119 $this->addTables( 'user', 'u2' );
120 $u2 = $this->getAliasedName( 'user', 'u2' );
121 $this->addJoinConds( array(
122 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
123 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
124 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
125 }
126
127 $data = array();
128 $res = $this->select( __METHOD__ );
129 while ( ( $r = $db->fetchObject( $res ) ) ) {
130 $user = User::newFromRow( $r );
131 $name = $user->getName();
132 $data[$name]['name'] = $name;
133
134 if ( isset( $this->prop['editcount'] ) ) {
135 $data[$name]['editcount'] = intval( $user->getEditCount() );
136 }
137
138 if ( isset( $this->prop['registration'] ) ) {
139 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
140 }
141
142 if ( isset( $this->prop['groups'] ) && !is_null( $r->ug_group ) ) {
143 // This row contains only one group, others will be added from other rows
144 $data[$name]['groups'][] = $r->ug_group;
145 }
146
147 if ( isset( $this->prop['blockinfo'] ) && !is_null( $r->blocker_name ) ) {
148 $data[$name]['blockedby'] = $r->blocker_name;
149 $data[$name]['blockreason'] = $r->ipb_reason;
150 }
151
152 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
153 $data[$name]['emailable'] = '';
154 }
155
156 if ( isset( $this->prop['gender'] ) ) {
157 $gender = $user->getOption( 'gender' );
158 if ( strval( $gender ) === '' ) {
159 $gender = 'unknown';
160 }
161 $data[$name]['gender'] = $gender;
162 }
163
164 if ( !is_null( $params['token'] ) ) {
165 $tokenFunctions = $this->getTokenFunctions();
166 foreach ( $params['token'] as $t ) {
167 $val = call_user_func( $tokenFunctions[$t], $user );
168 if ( $val === false ) {
169 $this->setWarning( "Action '$t' is not allowed for the current user" );
170 } else {
171 $data[$name][$t . 'token'] = $val;
172 }
173 }
174 }
175 }
176 }
177 // Second pass: add result data to $retval
178 foreach ( $goodNames as $u ) {
179 if ( !isset( $data[$u] ) ) {
180 $data[$u] = array( 'name' => $u );
181 $urPage = new UserrightsPage;
182 $iwUser = $urPage->fetchUser( $u );
183
184 if ( $iwUser instanceof UserRightsProxy ) {
185 $data[$u]['interwiki'] = '';
186
187 if ( !is_null( $params['token'] ) ) {
188 $tokenFunctions = $this->getTokenFunctions();
189
190 foreach ( $params['token'] as $t ) {
191 $val = call_user_func( $tokenFunctions[$t], $iwUser );
192 if ( $val === false ) {
193 $this->setWarning( "Action '$t' is not allowed for the current user" );
194 } else {
195 $data[$u][$t . 'token'] = $val;
196 }
197 }
198 }
199 } else {
200 $data[$u]['missing'] = '';
201 }
202 } else {
203 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
204 $autolist = ApiQueryUsers::getAutoGroups( User::newFromName( $u ) );
205
206 $data[$u]['groups'] = array_merge( $autolist, $data[$u]['groups'] );
207
208 $this->getResult()->setIndexedTagName( $data[$u]['groups'], 'g' );
209 }
210 }
211 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
212 null, $data[$u] );
213 if ( !$fit ) {
214 $this->setContinueEnumParameter( 'users',
215 implode( '|', array_diff( $users, $done ) ) );
216 break;
217 }
218 $done[] = $u;
219 }
220 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
221 }
222
223 public static function getAutoGroups( $user ) {
224 $autolist = array();
225 $autolist[] = "*";
226 $autolist[] = "user";
227
228 foreach( Autopromote::getAutopromoteGroups( $user ) as $group ) {
229 $autolist[] = $group;
230 }
231
232 return $autolist;
233 }
234
235 public function getAllowedParams() {
236 return array(
237 'prop' => array(
238 ApiBase::PARAM_DFLT => null,
239 ApiBase::PARAM_ISMULTI => true,
240 ApiBase::PARAM_TYPE => array(
241 'blockinfo',
242 'groups',
243 'editcount',
244 'registration',
245 'emailable',
246 'gender',
247 )
248 ),
249 'users' => array(
250 ApiBase::PARAM_ISMULTI => true
251 ),
252 'token' => array(
253 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
254 ApiBase::PARAM_ISMULTI => true
255 ),
256 );
257 }
258
259 public function getParamDescription() {
260 return array(
261 'prop' => array(
262 'What pieces of information to include',
263 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
264 ' groups - lists all the groups the user belongs to',
265 ' editcount - adds the user\'s edit count',
266 ' registration - adds the user\'s registration timestamp',
267 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
268 ' gender - tags the gender of the user. Returns "male", "female", or "unknown"',
269 ),
270 'users' => 'A list of users to obtain the same information for',
271 'token' => 'Which tokens to obtain for each user',
272 );
273 }
274
275 public function getDescription() {
276 return 'Get information about a list of users';
277 }
278
279 protected function getExamples() {
280 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
281 }
282
283 public function getVersion() {
284 return __CLASS__ . ': $Id$';
285 }
286 }