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