]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryUsers.php
MediaWiki 1.14.0
[autoinstalls/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
37  class ApiQueryUsers extends ApiQueryBase {
38
39         public function __construct($query, $moduleName) {
40                 parent :: __construct($query, $moduleName, 'us');
41         }
42
43         public function execute() {
44                 $params = $this->extractRequestParams();
45                 $result = $this->getResult();
46                 $r = array();
47
48                 if (!is_null($params['prop'])) {
49                         $this->prop = array_flip($params['prop']);
50                 } else {
51                         $this->prop = array();
52                 }
53
54                 if(is_array($params['users'])) {
55                         $r = $this->getOtherUsersInfo($params['users']);
56                         $result->setIndexedTagName($r, 'user');
57                 }
58                 $result->addValue("query", $this->getModuleName(), $r);
59         }
60
61         protected function getOtherUsersInfo($users) {
62                 $goodNames = $retval = array();
63                 // Canonicalize user names
64                 foreach($users as $u) {
65                         $n = User::getCanonicalName($u);
66                         if($n === false || $n === '')
67                                 $retval[] = array('name' => $u, 'invalid' => '');
68                          else
69                                 $goodNames[] = $n;
70                 }
71                 if(!count($goodNames))
72                         return $retval;
73
74                 $db = $this->getDB();
75                 $this->addTables('user', 'u1');
76                 $this->addFields('u1.*');
77                 $this->addWhereFld('u1.user_name', $goodNames);
78
79                 if(isset($this->prop['groups'])) {
80                         $this->addTables('user_groups');
81                         $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
82                         $this->addFields('ug_group');
83                 }
84                 if(isset($this->prop['blockinfo'])) {
85                         $this->addTables('ipblocks');
86                         $this->addTables('user', 'u2');
87                         $u2 = $this->getAliasedName('user', 'u2');
88                         $this->addJoinConds(array(
89                                 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
90                                 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
91                         $this->addFields(array('ipb_reason', 'u2.user_name blocker_name'));
92                 }
93
94                 $data = array();
95                 $res = $this->select(__METHOD__);
96                 while(($r = $db->fetchObject($res))) {
97                         $user = User::newFromRow($r);
98                         $name = $user->getName();
99                         $data[$name]['name'] = $name;
100                         if(isset($this->prop['editcount']))
101                                 // No proper member function in User class for this
102                                 $data[$name]['editcount'] = $r->user_editcount;
103                         if(isset($this->prop['registration']))
104                                 // Nor for this one
105                                 $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $r->user_registration);
106                         if(isset($this->prop['groups']))
107                                 // This row contains only one group, others will be added from other rows
108                                 if(!is_null($r->ug_group))
109                                         $data[$name]['groups'][] = $r->ug_group;
110                         if(isset($this->prop['blockinfo']))
111                                 if(!is_null($r->blocker_name)) {
112                                         $data[$name]['blockedby'] = $r->blocker_name;
113                                         $data[$name]['blockreason'] = $r->ipb_reason;
114                                 }
115                         if(isset($this->prop['emailable']) && $user->canReceiveEmail())
116                                 $data[$name]['emailable'] = '';
117                 }
118
119                 // Second pass: add result data to $retval
120                 foreach($goodNames as $u) {
121                         if(!isset($data[$u]))
122                                 $retval[] = array('name' => $u, 'missing' => '');
123                         else {
124                                 if(isset($this->prop['groups']) && isset($data[$u]['groups']))
125                                         $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
126                                 $retval[] = $data[$u];
127                         }
128                 }
129                 return $retval;
130         }
131
132         public function getAllowedParams() {
133                 return array (
134                         'prop' => array (
135                                 ApiBase :: PARAM_DFLT => NULL,
136                                 ApiBase :: PARAM_ISMULTI => true,
137                                 ApiBase :: PARAM_TYPE => array (
138                                         'blockinfo',
139                                         'groups',
140                                         'editcount',
141                                         'registration',
142                                         'emailable',
143                                 )
144                         ),
145                         'users' => array(
146                                 ApiBase :: PARAM_ISMULTI => true
147                         )
148                 );
149         }
150
151         public function getParamDescription() {
152                 return array (
153                         'prop' => array(
154                                 'What pieces of information to include',
155                                 '  blockinfo    - tags if the user is blocked, by whom, and for what reason',
156                                 '  groups       - lists all the groups the user belongs to',
157                                 '  editcount    - adds the user\'s edit count',
158                                 '  registration - adds the user\'s registration timestamp',
159                                 '  emailable    - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
160                         ),
161                         'users' => 'A list of users to obtain the same information for'
162                 );
163         }
164
165         public function getDescription() {
166                 return 'Get information about a list of users';
167         }
168
169         protected function getExamples() {
170                 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
171         }
172
173         public function getVersion() {
174                 return __CLASS__ . ': $Id: ApiQueryUsers.php 44231 2008-12-04 14:42:30Z catrope $';
175         }
176 }