Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob7fc0ba88b71e14bc0c763926c56e7f4655082caa
1 <?php
2 /**
5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * Query module to get information about the currently logged-in user
30 * @ingroup API
32 class ApiQueryUserInfo extends ApiQueryBase {
34 const WL_UNREAD_LIMIT = 1000;
36 private $prop = array();
38 public function __construct( ApiQuery $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ui' );
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
46 if ( !is_null( $params['prop'] ) ) {
47 $this->prop = array_flip( $params['prop'] );
50 $r = $this->getCurrentUserInfo();
51 $result->addValue( 'query', $this->getModuleName(), $r );
54 protected function getCurrentUserInfo() {
55 $user = $this->getUser();
56 $result = $this->getResult();
57 $vals = array();
58 $vals['id'] = intval( $user->getId() );
59 $vals['name'] = $user->getName();
61 if ( $user->isAnon() ) {
62 $vals['anon'] = '';
65 if ( isset( $this->prop['blockinfo'] ) ) {
66 if ( $user->isBlocked() ) {
67 $block = $user->getBlock();
68 $vals['blockid'] = $block->getId();
69 $vals['blockedby'] = $block->getByName();
70 $vals['blockedbyid'] = $block->getBy();
71 $vals['blockreason'] = $user->blockedFor();
72 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
73 $vals['blockexpiry'] = $block->getExpiry() === 'infinity'
74 ? 'infinite'
75 : wfTimestamp( TS_ISO_8601, $block->getExpiry() );
79 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
80 $vals['messages'] = '';
83 if ( isset( $this->prop['groups'] ) ) {
84 $vals['groups'] = $user->getEffectiveGroups();
85 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
88 if ( isset( $this->prop['implicitgroups'] ) ) {
89 $vals['implicitgroups'] = $user->getAutomaticGroups();
90 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
93 if ( isset( $this->prop['rights'] ) ) {
94 // User::getRights() may return duplicate values, strip them
95 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
96 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
99 if ( isset( $this->prop['changeablegroups'] ) ) {
100 $vals['changeablegroups'] = $user->changeableGroups();
101 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
102 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
103 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
104 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
107 if ( isset( $this->prop['options'] ) ) {
108 $vals['options'] = $user->getOptions();
111 if ( isset( $this->prop['preferencestoken'] ) ) {
112 $p = $this->getModulePrefix();
113 $this->setWarning(
114 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
117 if ( isset( $this->prop['preferencestoken'] ) &&
118 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
119 $user->isAllowed( 'editmyoptions' )
121 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
124 if ( isset( $this->prop['editcount'] ) ) {
125 // use intval to prevent null if a non-logged-in user calls
126 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
127 $vals['editcount'] = intval( $user->getEditCount() );
130 if ( isset( $this->prop['ratelimits'] ) ) {
131 $vals['ratelimits'] = $this->getRateLimits();
134 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
135 $vals['realname'] = $user->getRealName();
138 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
139 if ( isset( $this->prop['email'] ) ) {
140 $vals['email'] = $user->getEmail();
141 $auth = $user->getEmailAuthenticationTimestamp();
142 if ( !is_null( $auth ) ) {
143 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
148 if ( isset( $this->prop['registrationdate'] ) ) {
149 $regDate = $user->getRegistration();
150 if ( $regDate !== false ) {
151 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
155 if ( isset( $this->prop['acceptlang'] ) ) {
156 $langs = $this->getRequest()->getAcceptLang();
157 $acceptLang = array();
158 foreach ( $langs as $lang => $val ) {
159 $r = array( 'q' => $val );
160 ApiResult::setContent( $r, $lang );
161 $acceptLang[] = $r;
163 $result->setIndexedTagName( $acceptLang, 'lang' );
164 $vals['acceptlang'] = $acceptLang;
167 if ( isset( $this->prop['unreadcount'] ) ) {
168 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
170 $count = $dbr->selectRowCount(
171 'watchlist',
172 '1',
173 array(
174 'wl_user' => $user->getId(),
175 'wl_notificationtimestamp IS NOT NULL',
177 __METHOD__,
178 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
181 if ( $count >= self::WL_UNREAD_LIMIT ) {
182 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
183 } else {
184 $vals['unreadcount'] = $count;
188 return $vals;
191 protected function getRateLimits() {
192 $user = $this->getUser();
193 if ( !$user->isPingLimitable() ) {
194 return array(); // No limits
197 // Find out which categories we belong to
198 $categories = array();
199 if ( $user->isAnon() ) {
200 $categories[] = 'anon';
201 } else {
202 $categories[] = 'user';
204 if ( $user->isNewbie() ) {
205 $categories[] = 'ip';
206 $categories[] = 'subnet';
207 if ( !$user->isAnon() ) {
208 $categories[] = 'newbie';
211 $categories = array_merge( $categories, $user->getGroups() );
213 // Now get the actual limits
214 $retval = array();
215 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
216 foreach ( $categories as $cat ) {
217 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
218 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
219 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
224 return $retval;
227 public function getAllowedParams() {
228 return array(
229 'prop' => array(
230 ApiBase::PARAM_DFLT => null,
231 ApiBase::PARAM_ISMULTI => true,
232 ApiBase::PARAM_TYPE => array(
233 'blockinfo',
234 'hasmsg',
235 'groups',
236 'implicitgroups',
237 'rights',
238 'changeablegroups',
239 'options',
240 'preferencestoken',
241 'editcount',
242 'ratelimits',
243 'email',
244 'realname',
245 'acceptlang',
246 'registrationdate',
247 'unreadcount',
249 ApiBase::PARAM_HELP_MSG => array(
250 'apihelp-query+userinfo-param-prop',
251 self::WL_UNREAD_LIMIT - 1,
252 self::WL_UNREAD_LIMIT . '+',
258 protected function getExamplesMessages() {
259 return array(
260 'action=query&meta=userinfo'
261 => 'apihelp-query+userinfo-example-simple',
262 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
263 => 'apihelp-query+userinfo-example-data',
267 public function getHelpUrls() {
268 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';