Removed all instances of empty() where error suppression was not intended. Replaced...
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blobc938604a03531b753eb02e6fe9608258e6b84dc6
1 <?php
3 /*
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');
31 /**
32 * Query module to get information about the currently logged-in user
34 * @ingroup API
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();
45 $r = array();
47 if (!is_null($params['prop'])) {
48 $this->prop = array_flip($params['prop']);
49 } else {
50 $this->prop = array();
52 $r = $this->getCurrentUserInfo();
53 $result->addValue("query", $this->getModuleName(), $r);
56 protected function getCurrentUserInfo() {
57 global $wgUser;
58 $result = $this->getResult();
59 $vals = array();
60 $vals['id'] = $wgUser->getId();
61 $vals['name'] = $wgUser->getName();
63 if($wgUser->isAnon())
64 $vals['anon'] = '';
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 $vals['rights'] = $wgUser->getRights();
80 $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
82 if (isset($this->prop['options'])) {
83 $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
85 if (isset($this->prop['preferencestoken']) && is_null($this->getMain()->getRequest()->getVal('callback'))) {
86 $vals['preferencestoken'] = $wgUser->editToken();
88 if (isset($this->prop['editcount'])) {
89 $vals['editcount'] = $wgUser->getEditCount();
91 if (isset($this->prop['ratelimits'])) {
92 $vals['ratelimits'] = $this->getRateLimits();
94 return $vals;
97 protected function getRateLimits()
99 global $wgUser, $wgRateLimits;
100 if(!$wgUser->isPingLimitable())
101 return array(); // No limits
103 // Find out which categories we belong to
104 $categories = array();
105 if($wgUser->isAnon())
106 $categories[] = 'anon';
107 else
108 $categories[] = 'user';
109 if($wgUser->isNewBie())
111 $categories[] = 'ip';
112 $categories[] = 'subnet';
113 if(!$wgUser->isAnon())
114 $categories[] = 'newbie';
117 // Now get the actual limits
118 $retval = array();
119 foreach($wgRateLimits as $action => $limits)
120 foreach($categories as $cat)
121 if(isset($limits[$cat]) && !is_null($limits[$cat]))
123 $retval[$action][$cat]['hits'] = $limits[$cat][0];
124 $retval[$action][$cat]['seconds'] = $limits[$cat][1];
126 return $retval;
129 public function getAllowedParams() {
130 return array (
131 'prop' => array (
132 ApiBase :: PARAM_DFLT => NULL,
133 ApiBase :: PARAM_ISMULTI => true,
134 ApiBase :: PARAM_TYPE => array (
135 'blockinfo',
136 'hasmsg',
137 'groups',
138 'rights',
139 'options',
140 'preferencestoken',
141 'editcount',
142 'ratelimits'
148 public function getParamDescription() {
149 return array (
150 'prop' => array(
151 'What pieces of information to include',
152 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
153 ' hasmsg - adds a tag "message" if the current user has pending messages',
154 ' groups - lists all the groups the current user belongs to',
155 ' rights - lists of all rights the current user has',
156 ' options - lists all preferences the current user has set',
157 ' editcount - adds the current user\'s edit count',
158 ' ratelimits - lists all rate limits applying to the current user'
163 public function getDescription() {
164 return 'Get information about the current user';
167 protected function getExamples() {
168 return array (
169 'api.php?action=query&meta=userinfo',
170 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
174 public function getVersion() {
175 return __CLASS__ . ': $Id$';