PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/phase3/includes/api/ApiQueryUserInfo.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 248 lines | 183 code | 32 blank | 33 comment | 28 complexity | 7bb52a966311dc18f728f5e882336617 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on July 30, 2007
  6. *
  7. * Copyright © 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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * Query module to get information about the currently logged-in user
  32. *
  33. * @ingroup API
  34. */
  35. class ApiQueryUserInfo extends ApiQueryBase {
  36. private $prop = array();
  37. public function __construct( $query, $moduleName ) {
  38. parent::__construct( $query, $moduleName, 'ui' );
  39. }
  40. public function execute() {
  41. $params = $this->extractRequestParams();
  42. $result = $this->getResult();
  43. if ( !is_null( $params['prop'] ) ) {
  44. $this->prop = array_flip( $params['prop'] );
  45. }
  46. $r = $this->getCurrentUserInfo();
  47. $result->addValue( 'query', $this->getModuleName(), $r );
  48. }
  49. protected function getCurrentUserInfo() {
  50. global $wgUser, $wgRequest, $wgHiddenPrefs;
  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. }
  58. if ( isset( $this->prop['blockinfo'] ) ) {
  59. if ( $wgUser->isBlocked() ) {
  60. $vals['blockedby'] = User::whoIs( $wgUser->blockedBy() );
  61. $vals['blockreason'] = $wgUser->blockedFor();
  62. }
  63. }
  64. if ( isset( $this->prop['hasmsg'] ) && $wgUser->getNewtalk() ) {
  65. $vals['messages'] = '';
  66. }
  67. if ( isset( $this->prop['groups'] ) ) {
  68. $autolist = ApiQueryUsers::getAutoGroups( $wgUser );
  69. $vals['groups'] = array_merge( $autolist, $wgUser->getGroups() );
  70. $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
  71. }
  72. if ( isset( $this->prop['rights'] ) ) {
  73. // User::getRights() may return duplicate values, strip them
  74. $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
  75. $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
  76. }
  77. if ( isset( $this->prop['changeablegroups'] ) ) {
  78. $vals['changeablegroups'] = $wgUser->changeableGroups();
  79. $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
  80. $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
  81. $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
  82. $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
  83. }
  84. if ( isset( $this->prop['options'] ) ) {
  85. $vals['options'] = $wgUser->getOptions();
  86. }
  87. if ( isset( $this->prop['preferencestoken'] ) &&
  88. is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
  89. ) {
  90. $vals['preferencestoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
  91. }
  92. if ( isset( $this->prop['editcount'] ) ) {
  93. $vals['editcount'] = intval( $wgUser->getEditCount() );
  94. }
  95. if ( isset( $this->prop['ratelimits'] ) ) {
  96. $vals['ratelimits'] = $this->getRateLimits();
  97. }
  98. if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
  99. $vals['realname'] = $wgUser->getRealName();
  100. }
  101. if ( isset( $this->prop['email'] ) ) {
  102. $vals['email'] = $wgUser->getEmail();
  103. $auth = $wgUser->getEmailAuthenticationTimestamp();
  104. if ( !is_null( $auth ) ) {
  105. $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
  106. }
  107. }
  108. if ( isset( $this->prop['registrationdate'] ) ) {
  109. $regDate = $wgUser->getRegistration();
  110. if ( $regDate !== false ) {
  111. $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
  112. }
  113. }
  114. if ( isset( $this->prop['acceptlang'] ) ) {
  115. $langs = $wgRequest->getAcceptLang();
  116. $acceptLang = array();
  117. foreach ( $langs as $lang => $val ) {
  118. $r = array( 'q' => $val );
  119. ApiResult::setContent( $r, $lang );
  120. $acceptLang[] = $r;
  121. }
  122. $result->setIndexedTagName( $acceptLang, 'lang' );
  123. $vals['acceptlang'] = $acceptLang;
  124. }
  125. return $vals;
  126. }
  127. protected function getRateLimits() {
  128. global $wgUser, $wgRateLimits;
  129. if ( !$wgUser->isPingLimitable() ) {
  130. return array(); // No limits
  131. }
  132. // Find out which categories we belong to
  133. $categories = array();
  134. if ( $wgUser->isAnon() ) {
  135. $categories[] = 'anon';
  136. } else {
  137. $categories[] = 'user';
  138. }
  139. if ( $wgUser->isNewbie() ) {
  140. $categories[] = 'ip';
  141. $categories[] = 'subnet';
  142. if ( !$wgUser->isAnon() )
  143. $categories[] = 'newbie';
  144. }
  145. $categories = array_merge( $categories, $wgUser->getGroups() );
  146. // Now get the actual limits
  147. $retval = array();
  148. foreach ( $wgRateLimits as $action => $limits ) {
  149. foreach ( $categories as $cat ) {
  150. if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
  151. $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
  152. $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
  153. }
  154. }
  155. }
  156. return $retval;
  157. }
  158. public function getAllowedParams() {
  159. return array(
  160. 'prop' => array(
  161. ApiBase::PARAM_DFLT => null,
  162. ApiBase::PARAM_ISMULTI => true,
  163. ApiBase::PARAM_TYPE => array(
  164. 'blockinfo',
  165. 'hasmsg',
  166. 'groups',
  167. 'rights',
  168. 'changeablegroups',
  169. 'options',
  170. 'preferencestoken',
  171. 'editcount',
  172. 'ratelimits',
  173. 'email',
  174. 'realname',
  175. 'acceptlang',
  176. 'registrationdate'
  177. )
  178. )
  179. );
  180. }
  181. public function getParamDescription() {
  182. return array(
  183. 'prop' => array(
  184. 'What pieces of information to include',
  185. ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
  186. ' hasmsg - Adds a tag "message" if the current user has pending messages',
  187. ' groups - Lists all the groups the current user belongs to',
  188. ' rights - Lists all the rights the current user has',
  189. ' changeablegroups - Lists the groups the current user can add to and remove from',
  190. ' options - Lists all preferences the current user has set',
  191. ' preferencestoken - Get a token to change current user\'s preferences',
  192. ' editcount - Adds the current user\'s edit count',
  193. ' ratelimits - Lists all rate limits applying to the current user',
  194. ' realname - Adds the user\'s real name',
  195. ' email - Adds the user\'s email address and email authentication date',
  196. ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
  197. ' registrationdate - Adds the user\'s registration date',
  198. )
  199. );
  200. }
  201. public function getDescription() {
  202. return 'Get information about the current user';
  203. }
  204. public function getExamples() {
  205. return array(
  206. 'api.php?action=query&meta=userinfo',
  207. 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
  208. );
  209. }
  210. public function getHelpUrls() {
  211. return 'http://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
  212. }
  213. public function getVersion() {
  214. return __CLASS__ . ': $Id$';
  215. }
  216. }