00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryUserInfo extends ApiQueryBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'ui');
00040 }
00041
00042 public function execute() {
00043 $params = $this->extractRequestParams();
00044 $result = $this->getResult();
00045 $r = array();
00046
00047 if (!is_null($params['prop'])) {
00048 $this->prop = array_flip($params['prop']);
00049 } else {
00050 $this->prop = array();
00051 }
00052 $r = $this->getCurrentUserInfo();
00053 $result->addValue("query", $this->getModuleName(), $r);
00054 }
00055
00056 protected function getCurrentUserInfo() {
00057 global $wgUser;
00058 $result = $this->getResult();
00059 $vals = array();
00060 $vals['id'] = intval($wgUser->getId());
00061 $vals['name'] = $wgUser->getName();
00062
00063 if($wgUser->isAnon())
00064 $vals['anon'] = '';
00065 if (isset($this->prop['blockinfo'])) {
00066 if ($wgUser->isBlocked()) {
00067 $vals['blockedby'] = User::whoIs($wgUser->blockedBy());
00068 $vals['blockreason'] = $wgUser->blockedFor();
00069 }
00070 }
00071 if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) {
00072 $vals['messages'] = '';
00073 }
00074 if (isset($this->prop['groups'])) {
00075 $vals['groups'] = $wgUser->getGroups();
00076 $result->setIndexedTagName($vals['groups'], 'g');
00077 }
00078 if (isset($this->prop['rights'])) {
00079
00080 $vals['rights'] = array_values(array_unique($wgUser->getRights()));
00081 $result->setIndexedTagName($vals['rights'], 'r');
00082 }
00083 if (isset($this->prop['options'])) {
00084 $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
00085 }
00086 if (isset($this->prop['preferencestoken']) && is_null($this->getMain()->getRequest()->getVal('callback'))) {
00087 $vals['preferencestoken'] = $wgUser->editToken();
00088 }
00089 if (isset($this->prop['editcount'])) {
00090 $vals['editcount'] = intval($wgUser->getEditCount());
00091 }
00092 if (isset($this->prop['ratelimits'])) {
00093 $vals['ratelimits'] = $this->getRateLimits();
00094 }
00095 if (isset($this->prop['email'])) {
00096 $vals['email'] = $wgUser->getEmail();
00097 $auth = $wgUser->getEmailAuthenticationTimestamp();
00098 if(!is_null($auth))
00099 $vals['emailauthenticated'] = wfTimestamp(TS_ISO_8601, $auth);
00100 }
00101 return $vals;
00102 }
00103
00104 protected function getRateLimits()
00105 {
00106 global $wgUser, $wgRateLimits;
00107 if(!$wgUser->isPingLimitable())
00108 return array();
00109
00110
00111 $categories = array();
00112 if($wgUser->isAnon())
00113 $categories[] = 'anon';
00114 else
00115 $categories[] = 'user';
00116 if($wgUser->isNewBie())
00117 {
00118 $categories[] = 'ip';
00119 $categories[] = 'subnet';
00120 if(!$wgUser->isAnon())
00121 $categories[] = 'newbie';
00122 }
00123 $categories = array_merge($categories, $wgUser->getGroups());
00124
00125
00126 $retval = array();
00127 foreach($wgRateLimits as $action => $limits)
00128 foreach($categories as $cat)
00129 if(isset($limits[$cat]) && !is_null($limits[$cat]))
00130 {
00131 $retval[$action][$cat]['hits'] = intval($limits[$cat][0]);
00132 $retval[$action][$cat]['seconds'] = intval($limits[$cat][1]);
00133 }
00134 return $retval;
00135 }
00136
00137 public function getAllowedParams() {
00138 return array (
00139 'prop' => array (
00140 ApiBase :: PARAM_DFLT => NULL,
00141 ApiBase :: PARAM_ISMULTI => true,
00142 ApiBase :: PARAM_TYPE => array (
00143 'blockinfo',
00144 'hasmsg',
00145 'groups',
00146 'rights',
00147 'options',
00148 'preferencestoken',
00149 'editcount',
00150 'ratelimits',
00151 'email',
00152 )
00153 )
00154 );
00155 }
00156
00157 public function getParamDescription() {
00158 return array (
00159 'prop' => array(
00160 'What pieces of information to include',
00161 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
00162 ' hasmsg - adds a tag "message" if the current user has pending messages',
00163 ' groups - lists all the groups the current user belongs to',
00164 ' rights - lists of all rights the current user has',
00165 ' options - lists all preferences the current user has set',
00166 ' editcount - adds the current user\'s edit count',
00167 ' ratelimits - lists all rate limits applying to the current user'
00168 )
00169 );
00170 }
00171
00172 public function getDescription() {
00173 return 'Get information about the current user';
00174 }
00175
00176 protected function getExamples() {
00177 return array (
00178 'api.php?action=query&meta=userinfo',
00179 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
00180 );
00181 }
00182
00183 public function getVersion() {
00184 return __CLASS__ . ': $Id: ApiQueryUserInfo.php 47865 2009-02-27 16:03:01Z catrope $';
00185 }
00186 }