r30128 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r30127‎ | r30128 | r30129 >
Date:17:59, 24 January 2008
Author:catrope
Status:old
Tags:
Comment:
API:
* Added uiuser parameter to meta=userinfo that gets information about users other than the current user.
* Pretty much completely rewrote userinfo to facilitate this
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryUserInfo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryUserInfo.php
@@ -40,45 +40,117 @@
4141 }
4242
4343 public function execute() {
 44+ $params = $this->extractRequestParams();
 45+ $result = $this->getResult();
 46+ $r = array();
4447
 48+ if (!is_null($params['prop'])) {
 49+ $this->prop = array_flip($params['prop']);
 50+ } else {
 51+ $this->prop = array();
 52+ }
 53+ $r['currentuser'] = $this->getCurrentUserInfo();
 54+
 55+ if(is_array($params['users'])) {
 56+ $r['users'] = $this->getOtherUsersInfo($params['users']);
 57+ $result->setIndexedTagName($r['users'], 'user');
 58+ }
 59+ $result->addValue("query", $this->getModuleName(), $r);
 60+ }
 61+
 62+ protected function getOtherUsersInfo($users) {
 63+ $goodNames = $retval = array();
 64+ // Canonicalize user names
 65+ foreach($users as $u) {
 66+ $n = User::getCanonicalName($u);
 67+ if($n === false)
 68+ $retval[] = array('name' => $u, 'invalid' => '');
 69+ else
 70+ $goodNames[] = $n;
 71+ }
 72+
 73+ $db = $this->getDb();
 74+ $userTable = $db->tableName('user');
 75+ $tables = "$userTable AS u1";
 76+ $this->addFields('u1.user_name');
 77+ $this->addWhereFld('u1.user_name', $goodNames);
 78+ $this->addFieldsIf('u1.user_editcount', isset($this->prop['editcount']));
 79+
 80+ if(isset($this->prop['groups'])) {
 81+ $ug = $db->tableName('user_groups');
 82+ $tables = "$tables LEFT JOIN $ug ON ug_user=u1.user_id";
 83+ $this->addFields('ug_group');
 84+ }
 85+ if(isset($this->prop['blockinfo'])) {
 86+ $ipb = $db->tableName('ipblocks');
 87+ $tables = "$tables LEFT JOIN $ipb ON ipb_user=u1.user_id";
 88+ $tables = "$tables LEFT JOIN $userTable AS u2 ON ipb_by=u2.user_id";
 89+ $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
 90+ }
 91+ $this->addTables($tables);
 92+
 93+ $data = array();
 94+ $res = $this->select(__METHOD__);
 95+ while(($r = $db->fetchObject($res))) {
 96+ $data[$r->user_name]['name'] = $r->user_name;
 97+ if(isset($this->prop['editcount']))
 98+ $data[$r->user_name]['editcount'] = $r->user_editcount;
 99+ if(isset($this->prop['groups']))
 100+ // This row contains only one group, others will be added from other rows
 101+ if(!is_null($r->ug_group))
 102+ $data[$r->user_name]['groups'][] = $r->ug_group;
 103+ if(isset($this->prop['blockinfo']))
 104+ if(!is_null($r->blocker_name)) {
 105+ $data[$r->user_name]['blockedby'] = $r->blocker_name;
 106+ $data[$r->user_name]['blockreason'] = $r->ipb_reason;
 107+ }
 108+ }
 109+
 110+ // Second pass: add result data to $retval
 111+ foreach($goodNames as $u) {
 112+ if(!isset($data[$u]))
 113+ $retval[] = array('name' => $u, 'missing' => '');
 114+ else {
 115+ if(isset($this->prop['groups']) && isset($data[$u]['groups']))
 116+ $this->getResult()->setIndexedTagName($data[$u]['groups'], 'g');
 117+ $retval[] = $data[$u];
 118+ }
 119+ }
 120+ return $retval;
 121+ }
 122+
 123+ protected function getCurrentUserInfo() {
45124 global $wgUser;
46 -
47 - $params = $this->extractRequestParams();
48125 $result = $this->getResult();
49 -
50126 $vals = array();
 127+ $vals['id'] = $wgUser->getId();
51128 $vals['name'] = $wgUser->getName();
52129
53130 if( $wgUser->isAnon() ) $vals['anon'] = '';
54 -
55 - if (!is_null($params['prop'])) {
56 - $prop = array_flip($params['prop']);
57 - if (isset($prop['blockinfo'])) {
58 - if ($wgUser->isBlocked()) {
59 - $vals['blockedby'] = User::whoIs($wgUser->blockedBy());
60 - $vals['blockreason'] = $wgUser->blockedFor();
61 - }
62 - }
63 - if (isset($prop['hasmsg']) && $wgUser->getNewtalk()) {
64 - $vals['messages'] = '';
 131+ if (isset($this->prop['blockinfo'])) {
 132+ if ($wgUser->isBlocked()) {
 133+ $vals['blockedby'] = User::whoIs($wgUser->blockedBy());
 134+ $vals['blockreason'] = $wgUser->blockedFor();
65135 }
66 - if (isset($prop['groups'])) {
67 - $vals['groups'] = $wgUser->getGroups();
68 - $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
69 - }
70 - if (isset($prop['rights'])) {
71 - $vals['rights'] = $wgUser->getRights();
72 - $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
73 - }
74 - if (isset($prop['options'])) {
75 - $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
76 - }
77 - if (isset($prop['editcount'])) {
78 - $vals['editcount'] = $wgUser->getEditCount();
79 - }
 136+ }
 137+ if (isset($this->prop['hasmsg']) && $wgUser->getNewtalk()) {
 138+ $vals['messages'] = '';
80139 }
81 -
82 - $result->addValue("query", $this->getModuleName(), $vals);
 140+ if (isset($this->prop['groups'])) {
 141+ $vals['groups'] = $wgUser->getGroups();
 142+ $result->setIndexedTagName($vals['groups'], 'g'); // even if empty
 143+ }
 144+ if (isset($this->prop['rights'])) {
 145+ $vals['rights'] = $wgUser->getRights();
 146+ $result->setIndexedTagName($vals['rights'], 'r'); // even if empty
 147+ }
 148+ if (isset($this->prop['options'])) {
 149+ $vals['options'] = (is_null($wgUser->mOptions) ? User::getDefaultOptions() : $wgUser->mOptions);
 150+ }
 151+ if (isset($this->prop['editcount'])) {
 152+ $vals['editcount'] = $wgUser->getEditCount();
 153+ }
 154+ return $vals;
83155 }
84156
85157 protected function getAllowedParams() {
@@ -93,7 +165,11 @@
94166 'rights',
95167 'options',
96168 'editcount'
97 - ))
 169+ )
 170+ ),
 171+ 'users' => array(
 172+ ApiBase :: PARAM_ISMULTI => true
 173+ )
98174 );
99175 }
100176
@@ -102,17 +178,18 @@
103179 'prop' => array(
104180 'What pieces of information to include',
105181 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
106 - ' hasmsg - adds a tag "message" if user has pending messages',
107 - ' groups - lists all the groups the current user belongs to',
108 - ' rights - lists of all rights the current user has',
109 - ' options - lists all preferences the current user has set',
 182+ ' hasmsg - adds a tag "message" if user has pending messages (current user only)',
 183+ ' groups - lists all the groups the user belongs to',
 184+ ' rights - lists of all rights the user has (current user only)',
 185+ ' options - lists all preferences the user has set (current user only)',
110186 ' editcount - adds the user\'s edit count'
111 - )
 187+ ),
 188+ 'users' => 'A list of other users to obtain the same information for'
112189 );
113190 }
114191
115192 protected function getDescription() {
116 - return 'Get information about the current user';
 193+ return 'Get information about the current user and other users';
117194 }
118195
119196 protected function getExamples() {
@@ -127,4 +204,3 @@
128205 return __CLASS__ . ': $Id$';
129206 }
130207 }
131 -
Index: trunk/phase3/RELEASE-NOTES
@@ -468,6 +468,7 @@
469469 * Added iiurlwidth and iiurlheight parameters to prop=imageinfo
470470 * Added format=txt and format=dbg, imported from query.php
471471 * Added uiprop=editcount to meta=userinfo
 472+* meta=userinfo can now fetch information on other users as well, through the uiusers parameter
472473
473474 === Languages updated in 1.12 ===
474475

Status & tagging log