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