Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob8b7831cb8e7c85ababfff3c6ce390c550884a97e
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();
75 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
76 $vals['messages'] = '';
79 if ( isset( $this->prop['groups'] ) ) {
80 $vals['groups'] = $user->getEffectiveGroups();
81 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
84 if ( isset( $this->prop['implicitgroups'] ) ) {
85 $vals['implicitgroups'] = $user->getAutomaticGroups();
86 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
89 if ( isset( $this->prop['rights'] ) ) {
90 // User::getRights() may return duplicate values, strip them
91 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
92 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
95 if ( isset( $this->prop['changeablegroups'] ) ) {
96 $vals['changeablegroups'] = $user->changeableGroups();
97 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
103 if ( isset( $this->prop['options'] ) ) {
104 $vals['options'] = $user->getOptions();
107 if ( isset( $this->prop['preferencestoken'] ) ) {
108 $p = $this->getModulePrefix();
109 $this->setWarning(
110 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
113 if ( isset( $this->prop['preferencestoken'] ) &&
114 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
115 $user->isAllowed( 'editmyoptions' )
117 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
120 if ( isset( $this->prop['editcount'] ) ) {
121 // use intval to prevent null if a non-logged-in user calls
122 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
123 $vals['editcount'] = intval( $user->getEditCount() );
126 if ( isset( $this->prop['ratelimits'] ) ) {
127 $vals['ratelimits'] = $this->getRateLimits();
130 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
131 $vals['realname'] = $user->getRealName();
134 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
135 if ( isset( $this->prop['email'] ) ) {
136 $vals['email'] = $user->getEmail();
137 $auth = $user->getEmailAuthenticationTimestamp();
138 if ( !is_null( $auth ) ) {
139 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
144 if ( isset( $this->prop['registrationdate'] ) ) {
145 $regDate = $user->getRegistration();
146 if ( $regDate !== false ) {
147 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
151 if ( isset( $this->prop['acceptlang'] ) ) {
152 $langs = $this->getRequest()->getAcceptLang();
153 $acceptLang = array();
154 foreach ( $langs as $lang => $val ) {
155 $r = array( 'q' => $val );
156 ApiResult::setContent( $r, $lang );
157 $acceptLang[] = $r;
159 $result->setIndexedTagName( $acceptLang, 'lang' );
160 $vals['acceptlang'] = $acceptLang;
163 if ( isset( $this->prop['unreadcount'] ) ) {
164 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
166 $sql = $dbr->selectSQLText(
167 'watchlist',
168 array( 'dummy' => 1 ),
169 array(
170 'wl_user' => $user->getId(),
171 'wl_notificationtimestamp IS NOT NULL',
173 __METHOD__,
174 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
176 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
178 if ( $count >= self::WL_UNREAD_LIMIT ) {
179 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
180 } else {
181 $vals['unreadcount'] = (int)$count;
185 return $vals;
188 protected function getRateLimits() {
189 $user = $this->getUser();
190 if ( !$user->isPingLimitable() ) {
191 return array(); // No limits
194 // Find out which categories we belong to
195 $categories = array();
196 if ( $user->isAnon() ) {
197 $categories[] = 'anon';
198 } else {
199 $categories[] = 'user';
201 if ( $user->isNewbie() ) {
202 $categories[] = 'ip';
203 $categories[] = 'subnet';
204 if ( !$user->isAnon() ) {
205 $categories[] = 'newbie';
208 $categories = array_merge( $categories, $user->getGroups() );
210 // Now get the actual limits
211 $retval = array();
212 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
213 foreach ( $categories as $cat ) {
214 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
215 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
216 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
221 return $retval;
224 public function getAllowedParams() {
225 return array(
226 'prop' => array(
227 ApiBase::PARAM_DFLT => null,
228 ApiBase::PARAM_ISMULTI => true,
229 ApiBase::PARAM_TYPE => array(
230 'blockinfo',
231 'hasmsg',
232 'groups',
233 'implicitgroups',
234 'rights',
235 'changeablegroups',
236 'options',
237 'preferencestoken',
238 'editcount',
239 'ratelimits',
240 'email',
241 'realname',
242 'acceptlang',
243 'registrationdate',
244 'unreadcount',
250 public function getParamDescription() {
251 return array(
252 'prop' => array(
253 'What pieces of information to include',
254 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
255 ' hasmsg - Adds a tag "message" if the current user has pending messages',
256 ' groups - Lists all the groups the current user belongs to',
257 ' implicitgroups - Lists all the groups the current user is automatically a member of',
258 ' rights - Lists all the rights the current user has',
259 ' changeablegroups - Lists the groups the current user can add to and remove from',
260 ' options - Lists all preferences the current user has set',
261 ' preferencestoken - DEPRECATED! Get a token to change current user\'s preferences',
262 ' editcount - Adds the current user\'s edit count',
263 ' ratelimits - Lists all rate limits applying to the current user',
264 ' realname - Adds the user\'s real name',
265 ' email - Adds the user\'s email address and email authentication date',
266 ' acceptlang - Echoes the Accept-Language header sent by ' .
267 'the client in a structured format',
268 ' registrationdate - Adds the user\'s registration date',
269 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
270 '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
271 self::WL_UNREAD_LIMIT . '+" if more)',
276 public function getDescription() {
277 return 'Get information about the current user.';
280 public function getExamples() {
281 return array(
282 'api.php?action=query&meta=userinfo',
283 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
287 public function getHelpUrls() {
288 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';