PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/api/ApiQueryUserInfo.php

https://github.com/tav/confluence
PHP | 186 lines | 139 code | 16 blank | 31 comment | 23 complexity | 785fd74b3633088ef4796c9c55cbbfcd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /*
  3. * Created on July 30, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  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.
  13. *
  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.
  18. *
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to get information about the currently logged-in user
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryUserInfo extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'ui');
  36. }
  37. public function execute() {
  38. $params = $this->extractRequestParams();
  39. $result = $this->getResult();
  40. $r = array();
  41. if (!is_null($params['prop'])) {
  42. $this->prop = array_flip($params['prop']);
  43. } else {
  44. $this->prop = array();
  45. }
  46. $r = $this->getCurrentUserInfo();
  47. $result->addValue("query", $this->getModuleName(), $r);
  48. }
  49. protected function getCurrentUserInfo() {
  50. global $wgUser;
  51. $result = $this->getResult();
  52. $vals = array();
  53. $vals['id'] = intval($wgUser->getId());
  54. $vals['name'] = $wgUser->getName();
  55. if($wgUser->isAnon())
  56. $vals['anon'] = '';
  57. if (isset($this->prop['blockinfo'])) {
  58. if ($wgUser->isBlocked()) {
  59. $vals['blockedby'] = User::whoIs($wgUser->blockedBy());
  60. $vals['blockreason'] = $wgUser->blockedFor();
  61. }
  62. }
  63. if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) {
  64. $vals['messages'] = '';
  65. }
  66. if (isset($this->prop['groups'])) {
  67. $vals['groups'] = $wgUser->getGroups();
  68. $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
  69. }
  70. if (isset($this->prop['rights'])) {
  71. // User::getRights() may return duplicate values, strip them
  72. $vals['rights'] = array_values(array_unique($wgUser->getRights()));
  73. $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
  74. }
  75. if (isset($this->prop['options'])) {
  76. $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
  77. }
  78. if (isset($this->prop['preferencestoken']) && is_null($this->getMain()->getRequest()->getVal('callback'))) {
  79. $vals['preferencestoken'] = $wgUser->editToken();
  80. }
  81. if (isset($this->prop['editcount'])) {
  82. $vals['editcount'] = intval($wgUser->getEditCount());
  83. }
  84. if (isset($this->prop['ratelimits'])) {
  85. $vals['ratelimits'] = $this->getRateLimits();
  86. }
  87. if (isset($this->prop['email'])) {
  88. $vals['email'] = $wgUser->getEmail();
  89. $auth = $wgUser->getEmailAuthenticationTimestamp();
  90. if(!is_null($auth))
  91. $vals['emailauthenticated'] = wfTimestamp(TS_ISO_8601, $auth);
  92. }
  93. return $vals;
  94. }
  95. protected function getRateLimits()
  96. {
  97. global $wgUser, $wgRateLimits;
  98. if(!$wgUser->isPingLimitable())
  99. return array(); // No limits
  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. $categories = array_merge($categories, $wgUser->getGroups());
  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'] = intval($limits[$cat][0]);
  121. $retval[$action][$cat]['seconds'] = intval($limits[$cat][1]);
  122. }
  123. return $retval;
  124. }
  125. public function getAllowedParams() {
  126. return array (
  127. 'prop' => array (
  128. ApiBase :: PARAM_DFLT => NULL,
  129. ApiBase :: PARAM_ISMULTI => true,
  130. ApiBase :: PARAM_TYPE => array (
  131. 'blockinfo',
  132. 'hasmsg',
  133. 'groups',
  134. 'rights',
  135. 'options',
  136. 'preferencestoken',
  137. 'editcount',
  138. 'ratelimits',
  139. 'email',
  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. public function getDescription() {
  159. return 'Get information about the current user';
  160. }
  161. protected function getExamples() {
  162. return array (
  163. 'api.php?action=query&meta=userinfo',
  164. 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
  165. );
  166. }
  167. public function getVersion() {
  168. return __CLASS__ . ': $Id: ApiQueryUserInfo.php 47865 2009-02-27 16:03:01Z catrope $';
  169. }
  170. }