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
00066 if ( isset( $this->prop['blockinfo'] ) ) {
00067 if ( $wgUser->isBlocked() ) {
00068 $vals['blockedby'] = User::whoIs( $wgUser->blockedBy() );
00069 $vals['blockreason'] = $wgUser->blockedFor();
00070 }
00071 }
00072
00073 if ( isset( $this->prop['hasmsg'] ) && $wgUser->getNewtalk() ) {
00074 $vals['messages'] = '';
00075 }
00076
00077 if ( isset( $this->prop['groups'] ) ) {
00078 $vals['groups'] = $wgUser->getGroups();
00079 $result->setIndexedTagName( $vals['groups'], 'g' );
00080 }
00081
00082 if ( isset( $this->prop['rights'] ) ) {
00083
00084 $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
00085 $result->setIndexedTagName( $vals['rights'], 'r' );
00086 }
00087
00088 if ( isset( $this->prop['changeablegroups'] ) ) {
00089 $vals['changeablegroups'] = $wgUser->changeableGroups();
00090 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
00091 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
00092 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
00093 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
00094 }
00095
00096 if ( isset( $this->prop['options'] ) ) {
00097 $vals['options'] = $wgUser->getOptions();
00098 }
00099
00100 if ( isset( $this->prop['preferencestoken'] ) && is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00101 $vals['preferencestoken'] = $wgUser->editToken();
00102 }
00103
00104 if ( isset( $this->prop['editcount'] ) ) {
00105 $vals['editcount'] = intval( $wgUser->getEditCount() );
00106 }
00107
00108 if ( isset( $this->prop['ratelimits'] ) ) {
00109 $vals['ratelimits'] = $this->getRateLimits();
00110 }
00111
00112 if ( isset( $this->prop['email'] ) ) {
00113 $vals['email'] = $wgUser->getEmail();
00114 $auth = $wgUser->getEmailAuthenticationTimestamp();
00115 if ( !is_null( $auth ) )
00116 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
00117 }
00118 return $vals;
00119 }
00120
00121 protected function getRateLimits()
00122 {
00123 global $wgUser, $wgRateLimits;
00124 if ( !$wgUser->isPingLimitable() )
00125 return array();
00126
00127
00128 $categories = array();
00129 if ( $wgUser->isAnon() )
00130 $categories[] = 'anon';
00131 else
00132 $categories[] = 'user';
00133 if ( $wgUser->isNewBie() )
00134 {
00135 $categories[] = 'ip';
00136 $categories[] = 'subnet';
00137 if ( !$wgUser->isAnon() )
00138 $categories[] = 'newbie';
00139 }
00140 $categories = array_merge( $categories, $wgUser->getGroups() );
00141
00142
00143 $retval = array();
00144 foreach ( $wgRateLimits as $action => $limits )
00145 foreach ( $categories as $cat )
00146 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) )
00147 {
00148 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
00149 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
00150 }
00151 return $retval;
00152 }
00153
00154 public function getAllowedParams() {
00155 return array (
00156 'prop' => array (
00157 ApiBase :: PARAM_DFLT => null,
00158 ApiBase :: PARAM_ISMULTI => true,
00159 ApiBase :: PARAM_TYPE => array (
00160 'blockinfo',
00161 'hasmsg',
00162 'groups',
00163 'rights',
00164 'changeablegroups',
00165 'options',
00166 'preferencestoken',
00167 'editcount',
00168 'ratelimits',
00169 'email',
00170 )
00171 )
00172 );
00173 }
00174
00175 public function getParamDescription() {
00176 return array (
00177 'prop' => array(
00178 'What pieces of information to include',
00179 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
00180 ' hasmsg - adds a tag "message" if the current user has pending messages',
00181 ' groups - lists all the groups the current user belongs to',
00182 ' rights - lists all the rights the current user has',
00183 ' changeablegroups - lists the groups the current user can add to and remove from',
00184 ' options - lists all preferences the current user has set',
00185 ' editcount - adds the current user\'s edit count',
00186 ' ratelimits - lists all rate limits applying to the current user'
00187 )
00188 );
00189 }
00190
00191 public function getDescription() {
00192 return 'Get information about the current user';
00193 }
00194
00195 protected function getExamples() {
00196 return array (
00197 'api.php?action=query&meta=userinfo',
00198 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
00199 );
00200 }
00201
00202 public function getVersion() {
00203 return __CLASS__ . ': $Id: ApiQueryUserInfo.php 69578 2010-07-20 02:46:20Z tstarling $';
00204 }
00205 }