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
00037 class ApiQueryUsers extends ApiQueryBase {
00038
00039 public function __construct($query, $moduleName) {
00040 parent :: __construct($query, $moduleName, 'us');
00041 }
00042
00043 public function execute() {
00044 $params = $this->extractRequestParams();
00045 $result = $this->getResult();
00046 $r = array();
00047
00048 if (!is_null($params['prop'])) {
00049 $this->prop = array_flip($params['prop']);
00050 } else {
00051 $this->prop = array();
00052 }
00053
00054 $users = (array)$params['users'];
00055 $goodNames = $done = array();
00056 $result = $this->getResult();
00057
00058 foreach($users as $u) {
00059 $n = User::getCanonicalName($u);
00060 if($n === false || $n === '')
00061 {
00062 $vals = array('name' => $u, 'invalid' => '');
00063 $fit = $result->addValue(array('query', $this->getModuleName()),
00064 null, $vals);
00065 if(!$fit)
00066 {
00067 $this->setContinueEnumParameter('users',
00068 implode('|', array_diff($users, $done)));
00069 $goodNames = array();
00070 break;
00071 }
00072 $done[] = $u;
00073 }
00074 else
00075 $goodNames[] = $n;
00076 }
00077 if(count($goodNames))
00078 {
00079 $db = $this->getDb();
00080 $this->addTables('user', 'u1');
00081 $this->addFields('u1.*');
00082 $this->addWhereFld('u1.user_name', $goodNames);
00083
00084 if(isset($this->prop['groups'])) {
00085 $this->addTables('user_groups');
00086 $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
00087 $this->addFields('ug_group');
00088 }
00089 if(isset($this->prop['blockinfo'])) {
00090 $this->addTables('ipblocks');
00091 $this->addTables('user', 'u2');
00092 $u2 = $this->getAliasedName('user', 'u2');
00093 $this->addJoinConds(array(
00094 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
00095 $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
00096 $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
00097 }
00098
00099 $data = array();
00100 $res = $this->select(__METHOD__);
00101 while(($r = $db->fetchObject($res))) {
00102 $user = User::newFromRow($r);
00103 $name = $user->getName();
00104 $data[$name]['name'] = $name;
00105 if(isset($this->prop['editcount']))
00106 $data[$name]['editcount'] = intval($user->getEditCount());
00107 if(isset($this->prop['registration']))
00108 $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $user->getRegistration());
00109 if(isset($this->prop['groups']) && !is_null($r->ug_group))
00110
00111 $data[$name]['groups'][] = $r->ug_group;
00112 if(isset($this->prop['blockinfo']) && !is_null($r->blocker_name)) {
00113 $data[$name]['blockedby'] = $r->blocker_name;
00114 $data[$name]['blockreason'] = $r->ipb_reason;
00115 }
00116 if(isset($this->prop['emailable']) && $user->canReceiveEmail())
00117 $data[$name]['emailable'] = '';
00118 }
00119 }
00120
00121 foreach($goodNames as $u) {
00122 if(!isset($data[$u]))
00123 $data[$u] = array('name' => $u, 'missing' => '');
00124 else {
00125 if(isset($this->prop['groups']) && isset($data[$u]['groups']))
00126 $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
00127 }
00128 $fit = $result->addValue(array('query', $this->getModuleName()),
00129 null, $data[$u]);
00130 if(!$fit)
00131 {
00132 $this->setContinueEnumParameter('users',
00133 implode('|', array_diff($users, $done)));
00134 break;
00135 }
00136 $done[] = $u;
00137 }
00138 return $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'user');
00139 }
00140
00141 public function getAllowedParams() {
00142 return array (
00143 'prop' => array (
00144 ApiBase :: PARAM_DFLT => NULL,
00145 ApiBase :: PARAM_ISMULTI => true,
00146 ApiBase :: PARAM_TYPE => array (
00147 'blockinfo',
00148 'groups',
00149 'editcount',
00150 'registration',
00151 'emailable',
00152 )
00153 ),
00154 'users' => array(
00155 ApiBase :: PARAM_ISMULTI => true
00156 )
00157 );
00158 }
00159
00160 public function getParamDescription() {
00161 return array (
00162 'prop' => array(
00163 'What pieces of information to include',
00164 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
00165 ' groups - lists all the groups the user belongs to',
00166 ' editcount - adds the user\'s edit count',
00167 ' registration - adds the user\'s registration timestamp',
00168 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
00169 ),
00170 'users' => 'A list of users to obtain the same information for'
00171 );
00172 }
00173
00174 public function getDescription() {
00175 return 'Get information about a list of users';
00176 }
00177
00178 protected function getExamples() {
00179 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
00180 }
00181
00182 public function getVersion() {
00183 return __CLASS__ . ': $Id: ApiQueryUsers.php 50094 2009-05-01 06:24:09Z tstarling $';
00184 }
00185 }