Rewrite ajaxwatch.js to use the API watch action, and JQuery. Allows it to be used...
[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 © 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'] = intval( $wgUser->getId() );
61 $vals['name'] = $wgUser->getName();
62
63 if ( $wgUser->isAnon() ) {
64 $vals['anon'] = '';
65 }
66
67 if ( isset( $this->prop['blockinfo'] ) ) {
68 if ( $wgUser->isBlocked() ) {
69 $vals['blockedby'] = User::whoIs( $wgUser->blockedBy() );
70 $vals['blockreason'] = $wgUser->blockedFor();
71 }
72 }
73
74 if ( isset( $this->prop['hasmsg'] ) && $wgUser->getNewtalk() ) {
75 $vals['messages'] = '';
76 }
77
78 if ( isset( $this->prop['groups'] ) ) {
79 $vals['groups'] = $wgUser->getGroups();
80 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
81 }
82
83 if ( isset( $this->prop['rights'] ) ) {
84 // User::getRights() may return duplicate values, strip them
85 $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
86 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
87 }
88
89 if ( isset( $this->prop['changeablegroups'] ) ) {
90 $vals['changeablegroups'] = $wgUser->changeableGroups();
91 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
92 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
93 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
94 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
95 }
96
97 if ( isset( $this->prop['options'] ) ) {
98 $vals['options'] = $wgUser->getOptions();
99 }
100
101 if (
102 isset( $this->prop['preferencestoken'] ) &&
103 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
104 )
105 {
106 $vals['preferencestoken'] = $wgUser->editToken();
107 }
108
109 if ( isset( $this->prop['editcount'] ) ) {
110 $vals['editcount'] = intval( $wgUser->getEditCount() );
111 }
112
113 if ( isset( $this->prop['ratelimits'] ) ) {
114 $vals['ratelimits'] = $this->getRateLimits();
115 }
116
117 if ( isset( $this->prop['email'] ) ) {
118 $vals['email'] = $wgUser->getEmail();
119 $auth = $wgUser->getEmailAuthenticationTimestamp();
120 if ( !is_null( $auth ) ) {
121 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
122 }
123 }
124 return $vals;
125 }
126
127 protected function getRateLimits() {
128 global $wgUser, $wgRateLimits;
129 if ( !$wgUser->isPingLimitable() ) {
130 return array(); // No limits
131 }
132
133 // Find out which categories we belong to
134 $categories = array();
135 if ( $wgUser->isAnon() ) {
136 $categories[] = 'anon';
137 } else {
138 $categories[] = 'user';
139 }
140 if ( $wgUser->isNewbie() ) {
141 $categories[] = 'ip';
142 $categories[] = 'subnet';
143 if ( !$wgUser->isAnon() )
144 $categories[] = 'newbie';
145 }
146 $categories = array_merge( $categories, $wgUser->getGroups() );
147
148 // Now get the actual limits
149 $retval = array();
150 foreach ( $wgRateLimits as $action => $limits ) {
151 foreach ( $categories as $cat ) {
152 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
153 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
154 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
155 }
156 }
157 }
158 return $retval;
159 }
160
161 public function getAllowedParams() {
162 return array(
163 'prop' => array(
164 ApiBase::PARAM_DFLT => null,
165 ApiBase::PARAM_ISMULTI => true,
166 ApiBase::PARAM_TYPE => array(
167 'blockinfo',
168 'hasmsg',
169 'groups',
170 'rights',
171 'changeablegroups',
172 'options',
173 'preferencestoken',
174 'editcount',
175 'ratelimits',
176 'email',
177 )
178 )
179 );
180 }
181
182 public function getParamDescription() {
183 return array(
184 'prop' => array(
185 'What pieces of information to include',
186 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
187 ' hasmsg - adds a tag "message" if the current user has pending messages',
188 ' groups - lists all the groups the current user belongs to',
189 ' rights - lists all the rights the current user has',
190 ' changeablegroups - lists the groups the current user can add to and remove from',
191 ' options - lists all preferences the current user has set',
192 ' editcount - adds the current user\'s edit count',
193 ' ratelimits - lists all rate limits applying to the current user'
194 )
195 );
196 }
197
198 public function getDescription() {
199 return 'Get information about the current user';
200 }
201
202 protected function getExamples() {
203 return array(
204 'api.php?action=query&meta=userinfo',
205 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
206 );
207 }
208
209 public function getVersion() {
210 return __CLASS__ . ': $Id$';
211 }
212 }