MediaWiki  REL1_22
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 $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                 $block = $user->getBlock();
00067                 $vals['blockid'] = $block->getId();
00068                 $vals['blockedby'] = $block->getByName();
00069                 $vals['blockedbyid'] = $block->getBy();
00070                 $vals['blockreason'] = $user->blockedFor();
00071             }
00072         }
00073 
00074         if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
00075             $vals['messages'] = '';
00076         }
00077 
00078         if ( isset( $this->prop['groups'] ) ) {
00079             $vals['groups'] = $user->getEffectiveGroups();
00080             $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
00081         }
00082 
00083         if ( isset( $this->prop['implicitgroups'] ) ) {
00084             $vals['implicitgroups'] = $user->getAutomaticGroups();
00085             $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
00086         }
00087 
00088         if ( isset( $this->prop['rights'] ) ) {
00089             // User::getRights() may return duplicate values, strip them
00090             $vals['rights'] = array_values( array_unique( $user->getRights() ) );
00091             $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
00092         }
00093 
00094         if ( isset( $this->prop['changeablegroups'] ) ) {
00095             $vals['changeablegroups'] = $user->changeableGroups();
00096             $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
00097             $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
00098             $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
00099             $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
00100         }
00101 
00102         if ( isset( $this->prop['options'] ) ) {
00103             $vals['options'] = $user->getOptions();
00104         }
00105 
00106         if ( isset( $this->prop['preferencestoken'] ) &&
00107             is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
00108             $user->isAllowed( 'editmyoptions' )
00109         ) {
00110             $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
00111         }
00112 
00113         if ( isset( $this->prop['editcount'] ) ) {
00114             // use intval to prevent null if a non-logged-in user calls
00115             // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
00116             $vals['editcount'] = intval( $user->getEditCount() );
00117         }
00118 
00119         if ( isset( $this->prop['ratelimits'] ) ) {
00120             $vals['ratelimits'] = $this->getRateLimits();
00121         }
00122 
00123         if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
00124             $vals['realname'] = $user->getRealName();
00125         }
00126 
00127         if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
00128             if ( isset( $this->prop['email'] ) ) {
00129                 $vals['email'] = $user->getEmail();
00130                 $auth = $user->getEmailAuthenticationTimestamp();
00131                 if ( !is_null( $auth ) ) {
00132                     $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
00133                 }
00134             }
00135         }
00136 
00137         if ( isset( $this->prop['registrationdate'] ) ) {
00138             $regDate = $user->getRegistration();
00139             if ( $regDate !== false ) {
00140                 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
00141             }
00142         }
00143 
00144         if ( isset( $this->prop['acceptlang'] ) ) {
00145             $langs = $this->getRequest()->getAcceptLang();
00146             $acceptLang = array();
00147             foreach ( $langs as $lang => $val ) {
00148                 $r = array( 'q' => $val );
00149                 ApiResult::setContent( $r, $lang );
00150                 $acceptLang[] = $r;
00151             }
00152             $result->setIndexedTagName( $acceptLang, 'lang' );
00153             $vals['acceptlang'] = $acceptLang;
00154         }
00155         return $vals;
00156     }
00157 
00158     protected function getRateLimits() {
00159         global $wgRateLimits;
00160         $user = $this->getUser();
00161         if ( !$user->isPingLimitable() ) {
00162             return array(); // No limits
00163         }
00164 
00165         // Find out which categories we belong to
00166         $categories = array();
00167         if ( $user->isAnon() ) {
00168             $categories[] = 'anon';
00169         } else {
00170             $categories[] = 'user';
00171         }
00172         if ( $user->isNewbie() ) {
00173             $categories[] = 'ip';
00174             $categories[] = 'subnet';
00175             if ( !$user->isAnon() ) {
00176                 $categories[] = 'newbie';
00177             }
00178         }
00179         $categories = array_merge( $categories, $user->getGroups() );
00180 
00181         // Now get the actual limits
00182         $retval = array();
00183         foreach ( $wgRateLimits as $action => $limits ) {
00184             foreach ( $categories as $cat ) {
00185                 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
00186                     $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
00187                     $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
00188                 }
00189             }
00190         }
00191         return $retval;
00192     }
00193 
00194     public function getAllowedParams() {
00195         return array(
00196             'prop' => array(
00197                 ApiBase::PARAM_DFLT => null,
00198                 ApiBase::PARAM_ISMULTI => true,
00199                 ApiBase::PARAM_TYPE => array(
00200                     'blockinfo',
00201                     'hasmsg',
00202                     'groups',
00203                     'implicitgroups',
00204                     'rights',
00205                     'changeablegroups',
00206                     'options',
00207                     'preferencestoken',
00208                     'editcount',
00209                     'ratelimits',
00210                     'email',
00211                     'realname',
00212                     'acceptlang',
00213                     'registrationdate'
00214                 )
00215             )
00216         );
00217     }
00218 
00219     public function getParamDescription() {
00220         return array(
00221             'prop' => array(
00222                 'What pieces of information to include',
00223                 '  blockinfo        - Tags if the current user is blocked, by whom, and for what reason',
00224                 '  hasmsg           - Adds a tag "message" if the current user has pending messages',
00225                 '  groups           - Lists all the groups the current user belongs to',
00226                 '  implicitgroups   - Lists all the groups the current user is automatically a member of',
00227                 '  rights           - Lists all the rights the current user has',
00228                 '  changeablegroups - Lists the groups the current user can add to and remove from',
00229                 '  options          - Lists all preferences the current user has set',
00230                 '  preferencestoken - Get a token to change current user\'s preferences',
00231                 '  editcount        - Adds the current user\'s edit count',
00232                 '  ratelimits       - Lists all rate limits applying to the current user',
00233                 '  realname         - Adds the user\'s real name',
00234                 '  email            - Adds the user\'s email address and email authentication date',
00235                 '  acceptlang       - Echoes the Accept-Language header sent by the client in a structured format',
00236                 '  registrationdate - Adds the user\'s registration date',
00237             )
00238         );
00239     }
00240 
00241     public function getResultProperties() {
00242         return array(
00243             ApiBase::PROP_LIST => false,
00244             '' => array(
00245                 'id' => 'integer',
00246                 'name' => 'string',
00247                 'anon' => 'boolean'
00248             ),
00249             'blockinfo' => array(
00250                 'blockid' => array(
00251                     ApiBase::PROP_TYPE => 'integer',
00252                     ApiBase::PROP_NULLABLE => true
00253                 ),
00254                 'blockedby' => array(
00255                     ApiBase::PROP_TYPE => 'string',
00256                     ApiBase::PROP_NULLABLE => true
00257                 ),
00258                 'blockedbyid' => array(
00259                     ApiBase::PROP_TYPE => 'integer',
00260                     ApiBase::PROP_NULLABLE => true
00261                 ),
00262                 'blockedreason' => array(
00263                     ApiBase::PROP_TYPE => 'string',
00264                     ApiBase::PROP_NULLABLE => true
00265                 )
00266             ),
00267             'hasmsg' => array(
00268                 'messages' => 'boolean'
00269             ),
00270             'preferencestoken' => array(
00271                 'preferencestoken' => 'string'
00272             ),
00273             'editcount' => array(
00274                 'editcount' => 'integer'
00275             ),
00276             'realname' => array(
00277                 'realname' => array(
00278                     ApiBase::PROP_TYPE => 'string',
00279                     ApiBase::PROP_NULLABLE => true
00280                 )
00281             ),
00282             'email' => array(
00283                 'email' => 'string',
00284                 'emailauthenticated' => array(
00285                     ApiBase::PROP_TYPE => 'timestamp',
00286                     ApiBase::PROP_NULLABLE => true
00287                 )
00288             ),
00289             'registrationdate' => array(
00290                 'registrationdate' => array(
00291                     ApiBase::PROP_TYPE => 'timestamp',
00292                     ApiBase::PROP_NULLABLE => true
00293                 )
00294             )
00295         );
00296     }
00297 
00298     public function getDescription() {
00299         return 'Get information about the current user';
00300     }
00301 
00302     public function getExamples() {
00303         return array(
00304             'api.php?action=query&meta=userinfo',
00305             'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
00306         );
00307     }
00308 
00309     public function getHelpUrls() {
00310         return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
00311     }
00312 }