MediaWiki  REL1_19
ApiQueryUserInfo.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryUserInfo extends ApiQueryBase {
00033 
00034         private $prop = array();
00035 
00036         public function __construct( $query, $moduleName ) {
00037                 parent::__construct( $query, $moduleName, 'ui' );
00038         }
00039 
00040         public function execute() {
00041                 $params = $this->extractRequestParams();
00042                 $result = $this->getResult();
00043 
00044                 if ( !is_null( $params['prop'] ) ) {
00045                         $this->prop = array_flip( $params['prop'] );
00046                 }
00047 
00048                 $r = $this->getCurrentUserInfo();
00049                 $result->addValue( 'query', $this->getModuleName(), $r );
00050         }
00051 
00052         protected function getCurrentUserInfo() {
00053                 global $wgRequest, $wgHiddenPrefs;
00054                 $user = $this->getUser();
00055                 $result = $this->getResult();
00056                 $vals = array();
00057                 $vals['id'] = intval( $user->getId() );
00058                 $vals['name'] = $user->getName();
00059 
00060                 if ( $user->isAnon() ) {
00061                         $vals['anon'] = '';
00062                 }
00063 
00064                 if ( isset( $this->prop['blockinfo'] ) ) {
00065                         if ( $user->isBlocked() ) {
00066                                 $vals['blockedby'] = User::whoIs( $user->blockedBy() );
00067                                 $vals['blockreason'] = $user->blockedFor();
00068                         }
00069                 }
00070 
00071                 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
00072                         $vals['messages'] = '';
00073                 }
00074 
00075                 if ( isset( $this->prop['groups'] ) ) {
00076                         $autolist = ApiQueryUsers::getAutoGroups( $user );
00077 
00078                         $vals['groups'] = array_merge( $autolist, $user->getGroups() );
00079                         $result->setIndexedTagName( $vals['groups'], 'g' );     // even if empty
00080                 }
00081 
00082                 if ( isset( $this->prop['implicitgroups'] ) ) {
00083                         $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
00084                         $result->setIndexedTagName( $vals['implicitgroups'], 'g' );     // even if empty
00085                 }
00086 
00087                 if ( isset( $this->prop['rights'] ) ) {
00088                         // User::getRights() may return duplicate values, strip them
00089                         $vals['rights'] = array_values( array_unique( $user->getRights() ) );
00090                         $result->setIndexedTagName( $vals['rights'], 'r' );     // even if empty
00091                 }
00092 
00093                 if ( isset( $this->prop['changeablegroups'] ) ) {
00094                         $vals['changeablegroups'] = $user->changeableGroups();
00095                         $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
00096                         $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
00097                         $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
00098                         $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
00099                 }
00100 
00101                 if ( isset( $this->prop['options'] ) ) {
00102                         $vals['options'] = $user->getOptions();
00103                 }
00104 
00105                 if ( isset( $this->prop['preferencestoken'] ) &&
00106                         is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
00107                 ) {
00108                         $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
00109                 }
00110 
00111                 if ( isset( $this->prop['editcount'] ) ) {
00112                         $vals['editcount'] = intval( $user->getEditCount() );
00113                 }
00114 
00115                 if ( isset( $this->prop['ratelimits'] ) ) {
00116                         $vals['ratelimits'] = $this->getRateLimits();
00117                 }
00118 
00119                 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
00120                         $vals['realname'] = $user->getRealName();
00121                 }
00122 
00123                 if ( isset( $this->prop['email'] ) ) {
00124                         $vals['email'] = $user->getEmail();
00125                         $auth = $user->getEmailAuthenticationTimestamp();
00126                         if ( !is_null( $auth ) ) {
00127                                 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
00128                         }
00129                 }
00130 
00131                 if ( isset( $this->prop['registrationdate'] ) ) {
00132                         $regDate = $user->getRegistration();
00133                         if ( $regDate !== false ) {
00134                                 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
00135                         }
00136                 }
00137 
00138                 if ( isset( $this->prop['acceptlang'] ) ) {
00139                         $langs = $wgRequest->getAcceptLang();
00140                         $acceptLang = array();
00141                         foreach ( $langs as $lang => $val ) {
00142                                 $r = array( 'q' => $val );
00143                                 ApiResult::setContent( $r, $lang );
00144                                 $acceptLang[] = $r;
00145                         }
00146                         $result->setIndexedTagName( $acceptLang, 'lang' );
00147                         $vals['acceptlang'] = $acceptLang;
00148                 }
00149                 return $vals;
00150         }
00151 
00152         protected function getRateLimits() {
00153                 global $wgRateLimits;
00154                 $user = $this->getUser();
00155                 if ( !$user->isPingLimitable() ) {
00156                         return array(); // No limits
00157                 }
00158 
00159                 // Find out which categories we belong to
00160                 $categories = array();
00161                 if ( $user->isAnon() ) {
00162                         $categories[] = 'anon';
00163                 } else {
00164                         $categories[] = 'user';
00165                 }
00166                 if ( $user->isNewbie() ) {
00167                         $categories[] = 'ip';
00168                         $categories[] = 'subnet';
00169                         if ( !$user->isAnon() )
00170                                 $categories[] = 'newbie';
00171                 }
00172                 $categories = array_merge( $categories, $user->getGroups() );
00173 
00174                 // Now get the actual limits
00175                 $retval = array();
00176                 foreach ( $wgRateLimits as $action => $limits ) {
00177                         foreach ( $categories as $cat ) {
00178                                 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
00179                                         $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
00180                                         $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
00181                                 }
00182                         }
00183                 }
00184                 return $retval;
00185         }
00186 
00187         public function getAllowedParams() {
00188                 return array(
00189                         'prop' => array(
00190                                 ApiBase::PARAM_DFLT => null,
00191                                 ApiBase::PARAM_ISMULTI => true,
00192                                 ApiBase::PARAM_TYPE => array(
00193                                         'blockinfo',
00194                                         'hasmsg',
00195                                         'groups',
00196                                         'implicitgroups',
00197                                         'rights',
00198                                         'changeablegroups',
00199                                         'options',
00200                                         'preferencestoken',
00201                                         'editcount',
00202                                         'ratelimits',
00203                                         'email',
00204                                         'realname',
00205                                         'acceptlang',
00206                                         'registrationdate'
00207                                 )
00208                         )
00209                 );
00210         }
00211 
00212         public function getParamDescription() {
00213                 return array(
00214                         'prop' => array(
00215                                 'What pieces of information to include',
00216                                 '  blockinfo        - Tags if the current user is blocked, by whom, and for what reason',
00217                                 '  hasmsg           - Adds a tag "message" if the current user has pending messages',
00218                                 '  groups           - Lists all the groups the current user belongs to',
00219                                 '  implicitgroups   - Lists all the groups the current user is automatically a member of',
00220                                 '  rights           - Lists all the rights the current user has',
00221                                 '  changeablegroups - Lists the groups the current user can add to and remove from',
00222                                 '  options          - Lists all preferences the current user has set',
00223                                 '  preferencestoken - Get a token to change current user\'s preferences',
00224                                 '  editcount        - Adds the current user\'s edit count',
00225                                 '  ratelimits       - Lists all rate limits applying to the current user',
00226                                 '  realname         - Adds the user\'s real name',
00227                                 '  email            - Adds the user\'s email address and email authentication date',
00228                                 '  acceptlang       - Echoes the Accept-Language header sent by the client in a structured format',
00229                                 '  registrationdate - Adds the user\'s registration date',
00230                         )
00231                 );
00232         }
00233 
00234         public function getDescription() {
00235                 return 'Get information about the current user';
00236         }
00237 
00238         public function getExamples() {
00239                 return array(
00240                         'api.php?action=query&meta=userinfo',
00241                         'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
00242                 );
00243         }
00244 
00245         public function getHelpUrls() {
00246                 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
00247         }
00248 
00249         public function getVersion() {
00250                 return __CLASS__ . ': $Id$';
00251         }
00252 }