Fix some method documentation
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 get information about a list of users
34 *
35 * @ingroup API
36 */
37 class ApiQueryUsers extends ApiQueryBase {
38
39 private $tokenFunctions;
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'us' );
43 }
44
45 /**
46 * Get an array mapping token names to their handler functions.
47 * The prototype for a token function is func($user)
48 * it should return a token or false (permission denied)
49 * @return array(tokenname => function)
50 */
51 protected function getTokenFunctions() {
52 // Don't call the hooks twice
53 if ( isset( $this->tokenFunctions ) ) {
54 return $this->tokenFunctions;
55 }
56
57 // If we're in JSON callback mode, no tokens can be obtained
58 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
59 return array();
60 }
61
62 $this->tokenFunctions = array(
63 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
64 );
65 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
66 return $this->tokenFunctions;
67 }
68
69 public static function getUserrightsToken( $user ) {
70 global $wgUser;
71 // Since the permissions check for userrights is non-trivial,
72 // don't bother with it here
73 return $wgUser->editToken( $user->getName() );
74 }
75
76 public function execute() {
77 $params = $this->extractRequestParams();
78
79 if ( !is_null( $params['prop'] ) ) {
80 $this->prop = array_flip( $params['prop'] );
81 } else {
82 $this->prop = array();
83 }
84
85 $users = (array)$params['users'];
86 $goodNames = $done = array();
87 $result = $this->getResult();
88 // Canonicalize user names
89 foreach ( $users as $u ) {
90 $n = User::getCanonicalName( $u );
91 if ( $n === false || $n === '' ) {
92 $vals = array( 'name' => $u, 'invalid' => '' );
93 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
94 null, $vals );
95 if ( !$fit ) {
96 $this->setContinueEnumParameter( 'users',
97 implode( '|', array_diff( $users, $done ) ) );
98 $goodNames = array();
99 break;
100 }
101 $done[] = $u;
102 } else {
103 $goodNames[] = $n;
104 }
105 }
106
107 if ( count( $goodNames ) ) {
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 foreach ( $res as $row ) {
130 $user = User::newFromRow( $row );
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( $row->ug_group ) ) {
143 // This row contains only one group, others will be added from other rows
144 $data[$name]['groups'][] = $row->ug_group;
145 }
146
147 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->blocker_name ) ) {
148 $data[$name]['blockedby'] = $row->blocker_name;
149 $data[$name]['blockreason'] = $row->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 /**
224 * Gets all the groups that a user is automatically a member of
225 * @return array
226 */
227 public static function getAutoGroups( $user ) {
228 $groups = array( '*' );
229
230 if ( !$user->isAnon() ) {
231 $groups[] = 'user';
232 }
233
234 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
235 }
236
237 public function getCacheMode( $params ) {
238 if ( isset( $params['token'] ) ) {
239 return 'private';
240 } else {
241 return 'public';
242 }
243 }
244
245 public function getAllowedParams() {
246 return array(
247 'prop' => array(
248 ApiBase::PARAM_DFLT => null,
249 ApiBase::PARAM_ISMULTI => true,
250 ApiBase::PARAM_TYPE => array(
251 'blockinfo',
252 'groups',
253 'editcount',
254 'registration',
255 'emailable',
256 'gender',
257 )
258 ),
259 'users' => array(
260 ApiBase::PARAM_ISMULTI => true
261 ),
262 'token' => array(
263 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
264 ApiBase::PARAM_ISMULTI => true
265 ),
266 );
267 }
268
269 public function getParamDescription() {
270 return array(
271 'prop' => array(
272 'What pieces of information to include',
273 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
274 ' groups - lists all the groups the user belongs to',
275 ' editcount - adds the user\'s edit count',
276 ' registration - adds the user\'s registration timestamp',
277 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
278 ' gender - tags the gender of the user. Returns "male", "female", or "unknown"',
279 ),
280 'users' => 'A list of users to obtain the same information for',
281 'token' => 'Which tokens to obtain for each user',
282 );
283 }
284
285 public function getDescription() {
286 return 'Get information about a list of users';
287 }
288
289 protected function getExamples() {
290 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
291 }
292
293 public function getVersion() {
294 return __CLASS__ . ': $Id$';
295 }
296 }