Clarifying unorthodox MIME type
[lhc/web/wiklou.git] / includes / api / ApiQueryUserInfo.php
1 <?php
2
3 /*
4 * Created on July 30, 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 get information about the currently logged-in user
33 *
34 * @addtogroup API
35 */
36 class ApiQueryUserInfo extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'ui');
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45 $r = array();
46
47 if (!is_null($params['prop'])) {
48 $this->prop = array_flip($params['prop']);
49 } else {
50 $this->prop = array();
51 }
52 $r['currentuser'] = $this->getCurrentUserInfo();
53
54 if(is_array($params['users'])) {
55 $r['users'] = $this->getOtherUsersInfo($params['users']);
56 $result->setIndexedTagName($r['users'], '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)
67 $retval[] = array('name' => $u, 'invalid' => '');
68 else
69 $goodNames[] = $n;
70 }
71
72 $db = $this->getDb();
73 $userTable = $db->tableName('user');
74 $tables = "$userTable AS u1";
75 $this->addFields('u1.user_name');
76 $this->addWhereFld('u1.user_name', $goodNames);
77 $this->addFieldsIf('u1.user_editcount', isset($this->prop['editcount']));
78
79 if(isset($this->prop['groups'])) {
80 $ug = $db->tableName('user_groups');
81 $tables = "$tables LEFT JOIN $ug ON ug_user=u1.user_id";
82 $this->addFields('ug_group');
83 }
84 if(isset($this->prop['blockinfo'])) {
85 $ipb = $db->tableName('ipblocks');
86 $tables = "$tables LEFT JOIN $ipb ON ipb_user=u1.user_id";
87 $tables = "$tables LEFT JOIN $userTable AS u2 ON ipb_by=u2.user_id";
88 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
89 }
90 $this->addTables($tables);
91
92 $data = array();
93 $res = $this->select(__METHOD__);
94 while(($r = $db->fetchObject($res))) {
95 $data[$r->user_name]['name'] = $r->user_name;
96 if(isset($this->prop['editcount']))
97 $data[$r->user_name]['editcount'] = $r->user_editcount;
98 if(isset($this->prop['groups']))
99 // This row contains only one group, others will be added from other rows
100 if(!is_null($r->ug_group))
101 $data[$r->user_name]['groups'][] = $r->ug_group;
102 if(isset($this->prop['blockinfo']))
103 if(!is_null($r->blocker_name)) {
104 $data[$r->user_name]['blockedby'] = $r->blocker_name;
105 $data[$r->user_name]['blockreason'] = $r->ipb_reason;
106 }
107 }
108
109 // Second pass: add result data to $retval
110 foreach($goodNames as $u) {
111 if(!isset($data[$u]))
112 $retval[] = array('name' => $u, 'missing' => '');
113 else {
114 if(isset($this->prop['groups']) && isset($data[$u]['groups']))
115 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
116 $retval[] = $data[$u];
117 }
118 }
119 return $retval;
120 }
121
122 protected function getCurrentUserInfo() {
123 global $wgUser;
124 $result = $this->getResult();
125 $vals = array();
126 $vals['id'] = $wgUser->getId();
127 $vals['name'] = $wgUser->getName();
128
129 if( $wgUser->isAnon() ) $vals['anon'] = '';
130 if (isset($this->prop['blockinfo'])) {
131 if ($wgUser->isBlocked()) {
132 $vals['blockedby'] = User::whoIs($wgUser->blockedBy());
133 $vals['blockreason'] = $wgUser->blockedFor();
134 }
135 }
136 if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) {
137 $vals['messages'] = '';
138 }
139 if (isset($this->prop['groups'])) {
140 $vals['groups'] = $wgUser->getGroups();
141 $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
142 }
143 if (isset($this->prop['rights'])) {
144 $vals['rights'] = $wgUser->getRights();
145 $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
146 }
147 if (isset($this->prop['options'])) {
148 $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
149 }
150 if (isset($this->prop['editcount'])) {
151 $vals['editcount'] = $wgUser->getEditCount();
152 }
153 return $vals;
154 }
155
156 protected function getAllowedParams() {
157 return array (
158 'prop' => array (
159 ApiBase :: PARAM_DFLT => NULL,
160 ApiBase :: PARAM_ISMULTI => true,
161 ApiBase :: PARAM_TYPE => array (
162 'blockinfo',
163 'hasmsg',
164 'groups',
165 'rights',
166 'options',
167 'editcount'
168 )
169 ),
170 'users' => array(
171 ApiBase :: PARAM_ISMULTI => true
172 )
173 );
174 }
175
176 protected function getParamDescription() {
177 return array (
178 'prop' => array(
179 'What pieces of information to include',
180 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
181 ' hasmsg - adds a tag "message" if user has pending messages (current user only)',
182 ' groups - lists all the groups the user belongs to',
183 ' rights - lists of all rights the user has (current user only)',
184 ' options - lists all preferences the user has set (current user only)',
185 ' editcount - adds the user\'s edit count'
186 ),
187 'users' => 'A list of other users to obtain the same information for'
188 );
189 }
190
191 protected function getDescription() {
192 return 'Get information about the current user and other users';
193 }
194
195 protected function getExamples() {
196 return array (
197 'api.php?action=query&meta=userinfo',
198 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
199 'api.php?action=query&meta=userinfo&uioption=rememberpassword',
200 );
201 }
202
203 public function getVersion() {
204 return __CLASS__ . ': $Id$';
205 }
206 }