Cleanup from r37839: just put ipb_auto in the relevant arrays. We have those arrays...
[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 * @ingroup 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 = $this->getCurrentUserInfo();
53 $result->addValue("query", $this->getModuleName(), $r);
54 }
55
56 protected function getCurrentUserInfo() {
57 global $wgUser;
58 $result = $this->getResult();
59 $vals = array();
60 $vals['id'] = $wgUser->getId();
61 $vals['name'] = $wgUser->getName();
62
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();
69 }
70 }
71 if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) {
72 $vals['messages'] = '';
73 }
74 if (isset($this->prop['groups'])) {
75 $vals['groups'] = $wgUser->getGroups();
76 $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
77 }
78 if (isset($this->prop['rights'])) {
79 $vals['rights'] = $wgUser->getRights();
80 $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
81 }
82 if (isset($this->prop['options'])) {
83 $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
84 }
85 if (isset($this->prop['editcount'])) {
86 $vals['editcount'] = $wgUser->getEditCount();
87 }
88 if (isset($this->prop['ratelimits'])) {
89 $vals['ratelimits'] = $this->getRateLimits();
90 }
91 return $vals;
92 }
93
94 protected function getRateLimits()
95 {
96 global $wgUser, $wgRateLimits;
97 if(!$wgUser->isPingLimitable())
98 return array(); // No limits
99
100 // Find out which categories we belong to
101 $categories = array();
102 if($wgUser->isAnon())
103 $categories[] = 'anon';
104 else
105 $categories[] = 'user';
106 if($wgUser->isNewBie())
107 {
108 $categories[] = 'ip';
109 $categories[] = 'subnet';
110 if(!$wgUser->isAnon())
111 $categories[] = 'newbie';
112 }
113
114 // Now get the actual limits
115 $retval = array();
116 foreach($wgRateLimits as $action => $limits)
117 foreach($categories as $cat)
118 if(isset($limits[$cat]) && !is_null($limits[$cat]))
119 {
120 $retval[$action][$cat]['hits'] = $limits[$cat][0];
121 $retval[$action][$cat]['seconds'] = $limits[$cat][1];
122 }
123 return $retval;
124 }
125
126 public function getAllowedParams() {
127 return array (
128 'prop' => array (
129 ApiBase :: PARAM_DFLT => NULL,
130 ApiBase :: PARAM_ISMULTI => true,
131 ApiBase :: PARAM_TYPE => array (
132 'blockinfo',
133 'hasmsg',
134 'groups',
135 'rights',
136 'options',
137 'editcount',
138 'ratelimits'
139 )
140 )
141 );
142 }
143
144 public function getParamDescription() {
145 return array (
146 'prop' => array(
147 'What pieces of information to include',
148 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
149 ' hasmsg - adds a tag "message" if the current user has pending messages',
150 ' groups - lists all the groups the current user belongs to',
151 ' rights - lists of all rights the current user has',
152 ' options - lists all preferences the current user has set',
153 ' editcount - adds the current user\'s edit count',
154 ' ratelimits - lists all rate limits applying to the current user'
155 )
156 );
157 }
158
159 public function getDescription() {
160 return 'Get information about the current user';
161 }
162
163 protected function getExamples() {
164 return array (
165 'api.php?action=query&meta=userinfo',
166 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }