4 * Created on July 30, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to get information about the currently logged-in user
36 class ApiQueryUserInfo
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'ui');
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
47 if (!is_null($params['prop'])) {
48 $this->prop
= array_flip($params['prop']);
50 $this->prop
= array();
52 $r = $this->getCurrentUserInfo();
53 $result->addValue("query", $this->getModuleName(), $r);
56 protected function getCurrentUserInfo() {
58 $result = $this->getResult();
60 $vals['id'] = intval($wgUser->getId());
61 $vals['name'] = $wgUser->getName();
65 if (isset($this->prop
['blockinfo'])) {
66 if ($wgUser->isBlocked()) {
67 $vals['blockedby'] = User
::whoIs($wgUser->blockedBy());
68 $vals['blockreason'] = $wgUser->blockedFor();
71 if (isset($this->prop
['hasmsg']) && $wgUser->getNewtalk()) {
72 $vals['messages'] = '';
74 if (isset($this->prop
['groups'])) {
75 $vals['groups'] = $wgUser->getGroups();
76 $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
78 if (isset($this->prop
['rights'])) {
79 // User::getRights() may return duplicate values, strip them
80 $vals['rights'] = array_values(array_unique($wgUser->getRights()));
81 $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
83 if (isset($this->prop
['changeablegroups'])) {
84 $vals['changeablegroups'] = $wgUser->changeableGroups();
85 $result->setIndexedTagName($vals['changeablegroups']['add'], 'g');
86 $result->setIndexedTagName($vals['changeablegroups']['remove'], 'g');
87 $result->setIndexedTagName($vals['changeablegroups']['add-self'], 'g');
88 $result->setIndexedTagName($vals['changeablegroups']['remove-self'], 'g');
90 if (isset($this->prop
['options'])) {
91 $vals['options'] = (is_null($wgUser->mOptions
) ? User
::getDefaultOptions() : $wgUser->mOptions
);
93 if (isset($this->prop
['preferencestoken']) && is_null($this->getMain()->getRequest()->getVal('callback'))) {
94 $vals['preferencestoken'] = $wgUser->editToken();
96 if (isset($this->prop
['editcount'])) {
97 $vals['editcount'] = intval($wgUser->getEditCount());
99 if (isset($this->prop
['ratelimits'])) {
100 $vals['ratelimits'] = $this->getRateLimits();
102 if (isset($this->prop
['email'])) {
103 $vals['email'] = $wgUser->getEmail();
104 $auth = $wgUser->getEmailAuthenticationTimestamp();
106 $vals['emailauthenticated'] = wfTimestamp(TS_ISO_8601
, $auth);
111 protected function getRateLimits()
113 global $wgUser, $wgRateLimits;
114 if(!$wgUser->isPingLimitable())
115 return array(); // No limits
117 // Find out which categories we belong to
118 $categories = array();
119 if($wgUser->isAnon())
120 $categories[] = 'anon';
122 $categories[] = 'user';
123 if($wgUser->isNewBie())
125 $categories[] = 'ip';
126 $categories[] = 'subnet';
127 if(!$wgUser->isAnon())
128 $categories[] = 'newbie';
130 $categories = array_merge($categories, $wgUser->getGroups());
132 // Now get the actual limits
134 foreach($wgRateLimits as $action => $limits)
135 foreach($categories as $cat)
136 if(isset($limits[$cat]) && !is_null($limits[$cat]))
138 $retval[$action][$cat]['hits'] = intval($limits[$cat][0]);
139 $retval[$action][$cat]['seconds'] = intval($limits[$cat][1]);
144 public function getAllowedParams() {
147 ApiBase
:: PARAM_DFLT
=> NULL,
148 ApiBase
:: PARAM_ISMULTI
=> true,
149 ApiBase
:: PARAM_TYPE
=> array (
165 public function getParamDescription() {
168 'What pieces of information to include',
169 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
170 ' hasmsg - adds a tag "message" if the current user has pending messages',
171 ' groups - lists all the groups the current user belongs to',
172 ' rights - lists all the rights the current user has',
173 ' changeablegroups - lists the groups the current user can add to and remove from',
174 ' options - lists all preferences the current user has set',
175 ' editcount - adds the current user\'s edit count',
176 ' ratelimits - lists all rate limits applying to the current user'
181 public function getDescription() {
182 return 'Get information about the current user';
185 protected function getExamples() {
187 'api.php?action=query&meta=userinfo',
188 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
192 public function getVersion() {
193 return __CLASS__
. ': $Id$';