MediaWiki  REL1_24
ApiQueryUserInfo.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryUserInfo extends ApiQueryBase {
00033 
00034     const WL_UNREAD_LIMIT = 1000;
00035 
00036     private $prop = array();
00037 
00038     public function __construct( ApiQuery $query, $moduleName ) {
00039         parent::__construct( $query, $moduleName, 'ui' );
00040     }
00041 
00042     public function execute() {
00043         $params = $this->extractRequestParams();
00044         $result = $this->getResult();
00045 
00046         if ( !is_null( $params['prop'] ) ) {
00047             $this->prop = array_flip( $params['prop'] );
00048         }
00049 
00050         $r = $this->getCurrentUserInfo();
00051         $result->addValue( 'query', $this->getModuleName(), $r );
00052     }
00053 
00054     protected function getCurrentUserInfo() {
00055         $user = $this->getUser();
00056         $result = $this->getResult();
00057         $vals = array();
00058         $vals['id'] = intval( $user->getId() );
00059         $vals['name'] = $user->getName();
00060 
00061         if ( $user->isAnon() ) {
00062             $vals['anon'] = '';
00063         }
00064 
00065         if ( isset( $this->prop['blockinfo'] ) ) {
00066             if ( $user->isBlocked() ) {
00067                 $block = $user->getBlock();
00068                 $vals['blockid'] = $block->getId();
00069                 $vals['blockedby'] = $block->getByName();
00070                 $vals['blockedbyid'] = $block->getBy();
00071                 $vals['blockreason'] = $user->blockedFor();
00072                 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
00073                 $vals['blockexpiry'] = $block->getExpiry() === 'infinity'
00074                     ? 'infinite'
00075                     : wfTimestamp( TS_ISO_8601, $block->getExpiry() );
00076             }
00077         }
00078 
00079         if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
00080             $vals['messages'] = '';
00081         }
00082 
00083         if ( isset( $this->prop['groups'] ) ) {
00084             $vals['groups'] = $user->getEffectiveGroups();
00085             $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
00086         }
00087 
00088         if ( isset( $this->prop['implicitgroups'] ) ) {
00089             $vals['implicitgroups'] = $user->getAutomaticGroups();
00090             $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
00091         }
00092 
00093         if ( isset( $this->prop['rights'] ) ) {
00094             // User::getRights() may return duplicate values, strip them
00095             $vals['rights'] = array_values( array_unique( $user->getRights() ) );
00096             $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
00097         }
00098 
00099         if ( isset( $this->prop['changeablegroups'] ) ) {
00100             $vals['changeablegroups'] = $user->changeableGroups();
00101             $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
00102             $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
00103             $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
00104             $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
00105         }
00106 
00107         if ( isset( $this->prop['options'] ) ) {
00108             $vals['options'] = $user->getOptions();
00109         }
00110 
00111         if ( isset( $this->prop['preferencestoken'] ) ) {
00112             $p = $this->getModulePrefix();
00113             $this->setWarning(
00114                 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
00115             );
00116         }
00117         if ( isset( $this->prop['preferencestoken'] ) &&
00118             is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
00119             $user->isAllowed( 'editmyoptions' )
00120         ) {
00121             $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
00122         }
00123 
00124         if ( isset( $this->prop['editcount'] ) ) {
00125             // use intval to prevent null if a non-logged-in user calls
00126             // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
00127             $vals['editcount'] = intval( $user->getEditCount() );
00128         }
00129 
00130         if ( isset( $this->prop['ratelimits'] ) ) {
00131             $vals['ratelimits'] = $this->getRateLimits();
00132         }
00133 
00134         if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
00135             $vals['realname'] = $user->getRealName();
00136         }
00137 
00138         if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
00139             if ( isset( $this->prop['email'] ) ) {
00140                 $vals['email'] = $user->getEmail();
00141                 $auth = $user->getEmailAuthenticationTimestamp();
00142                 if ( !is_null( $auth ) ) {
00143                     $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
00144                 }
00145             }
00146         }
00147 
00148         if ( isset( $this->prop['registrationdate'] ) ) {
00149             $regDate = $user->getRegistration();
00150             if ( $regDate !== false ) {
00151                 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
00152             }
00153         }
00154 
00155         if ( isset( $this->prop['acceptlang'] ) ) {
00156             $langs = $this->getRequest()->getAcceptLang();
00157             $acceptLang = array();
00158             foreach ( $langs as $lang => $val ) {
00159                 $r = array( 'q' => $val );
00160                 ApiResult::setContent( $r, $lang );
00161                 $acceptLang[] = $r;
00162             }
00163             $result->setIndexedTagName( $acceptLang, 'lang' );
00164             $vals['acceptlang'] = $acceptLang;
00165         }
00166 
00167         if ( isset( $this->prop['unreadcount'] ) ) {
00168             $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
00169 
00170             $sql = $dbr->selectSQLText(
00171                 'watchlist',
00172                 array( 'dummy' => 1 ),
00173                 array(
00174                     'wl_user' => $user->getId(),
00175                     'wl_notificationtimestamp IS NOT NULL',
00176                 ),
00177                 __METHOD__,
00178                 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
00179             );
00180             $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
00181 
00182             if ( $count >= self::WL_UNREAD_LIMIT ) {
00183                 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
00184             } else {
00185                 $vals['unreadcount'] = (int)$count;
00186             }
00187         }
00188 
00189         return $vals;
00190     }
00191 
00192     protected function getRateLimits() {
00193         $user = $this->getUser();
00194         if ( !$user->isPingLimitable() ) {
00195             return array(); // No limits
00196         }
00197 
00198         // Find out which categories we belong to
00199         $categories = array();
00200         if ( $user->isAnon() ) {
00201             $categories[] = 'anon';
00202         } else {
00203             $categories[] = 'user';
00204         }
00205         if ( $user->isNewbie() ) {
00206             $categories[] = 'ip';
00207             $categories[] = 'subnet';
00208             if ( !$user->isAnon() ) {
00209                 $categories[] = 'newbie';
00210             }
00211         }
00212         $categories = array_merge( $categories, $user->getGroups() );
00213 
00214         // Now get the actual limits
00215         $retval = array();
00216         foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
00217             foreach ( $categories as $cat ) {
00218                 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
00219                     $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
00220                     $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
00221                 }
00222             }
00223         }
00224 
00225         return $retval;
00226     }
00227 
00228     public function getAllowedParams() {
00229         return array(
00230             'prop' => array(
00231                 ApiBase::PARAM_DFLT => null,
00232                 ApiBase::PARAM_ISMULTI => true,
00233                 ApiBase::PARAM_TYPE => array(
00234                     'blockinfo',
00235                     'hasmsg',
00236                     'groups',
00237                     'implicitgroups',
00238                     'rights',
00239                     'changeablegroups',
00240                     'options',
00241                     'preferencestoken',
00242                     'editcount',
00243                     'ratelimits',
00244                     'email',
00245                     'realname',
00246                     'acceptlang',
00247                     'registrationdate',
00248                     'unreadcount',
00249                 )
00250             )
00251         );
00252     }
00253 
00254     public function getParamDescription() {
00255         return array(
00256             'prop' => array(
00257                 'What pieces of information to include',
00258                 '  blockinfo        - Tags if the current user is blocked, by whom, and for what reason',
00259                 '  hasmsg           - Adds a tag "message" if the current user has pending messages',
00260                 '  groups           - Lists all the groups the current user belongs to',
00261                 '  implicitgroups   - Lists all the groups the current user is automatically a member of',
00262                 '  rights           - Lists all the rights the current user has',
00263                 '  changeablegroups - Lists the groups the current user can add to and remove from',
00264                 '  options          - Lists all preferences the current user has set',
00265                 '  preferencestoken - DEPRECATED! Get a token to change current user\'s preferences',
00266                 '  editcount        - Adds the current user\'s edit count',
00267                 '  ratelimits       - Lists all rate limits applying to the current user',
00268                 '  realname         - Adds the user\'s real name',
00269                 '  email            - Adds the user\'s email address and email authentication date',
00270                 '  acceptlang       - Echoes the Accept-Language header sent by ' .
00271                     'the client in a structured format',
00272                 '  registrationdate - Adds the user\'s registration date',
00273                 '  unreadcount      - Adds the count of unread pages on the user\'s watchlist ' .
00274                     '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
00275                     self::WL_UNREAD_LIMIT . '+" if more)',
00276             )
00277         );
00278     }
00279 
00280     public function getDescription() {
00281         return 'Get information about the current user.';
00282     }
00283 
00284     public function getExamples() {
00285         return array(
00286             'api.php?action=query&meta=userinfo',
00287             'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
00288         );
00289     }
00290 
00291     public function getHelpUrls() {
00292         return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
00293     }
00294 }