ApiQueryUsers.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * Created on July 30, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to get information about a list of users
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryUsers extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'us');
  36. }
  37. public function execute() {
  38. $params = $this->extractRequestParams();
  39. $result = $this->getResult();
  40. $r = array();
  41. if (!is_null($params['prop'])) {
  42. $this->prop = array_flip($params['prop']);
  43. } else {
  44. $this->prop = array();
  45. }
  46. $users = (array)$params['users'];
  47. $goodNames = $done = array();
  48. $result = $this->getResult();
  49. // Canonicalize user names
  50. foreach($users as $u) {
  51. $n = User::getCanonicalName($u);
  52. if($n === false || $n === '')
  53. {
  54. $vals = array('name' => $u, 'invalid' => '');
  55. $fit = $result->addValue(array('query', $this->getModuleName()),
  56. null, $vals);
  57. if(!$fit)
  58. {
  59. $this->setContinueEnumParameter('users',
  60. implode('|', array_diff($users, $done)));
  61. $goodNames = array();
  62. break;
  63. }
  64. $done[] = $u;
  65. }
  66. else
  67. $goodNames[] = $n;
  68. }
  69. if(count($goodNames))
  70. {
  71. $db = $this->getDb();
  72. $this->addTables('user', 'u1');
  73. $this->addFields('u1.*');
  74. $this->addWhereFld('u1.user_name', $goodNames);
  75. if(isset($this->prop['groups'])) {
  76. $this->addTables('user_groups');
  77. $this->addJoinConds(array('user_groups' => array('LEFT JOIN', 'ug_user=u1.user_id')));
  78. $this->addFields('ug_group');
  79. }
  80. if(isset($this->prop['blockinfo'])) {
  81. $this->addTables('ipblocks');
  82. $this->addTables('user', 'u2');
  83. $u2 = $this->getAliasedName('user', 'u2');
  84. $this->addJoinConds(array(
  85. 'ipblocks' => array('LEFT JOIN', 'ipb_user=u1.user_id'),
  86. $u2 => array('LEFT JOIN', 'ipb_by=u2.user_id')));
  87. $this->addFields(array('ipb_reason', 'u2.user_name AS blocker_name'));
  88. }
  89. $data = array();
  90. $res = $this->select(__METHOD__);
  91. while(($r = $db->fetchObject($res))) {
  92. $user = User::newFromRow($r);
  93. $name = $user->getName();
  94. $data[$name]['name'] = $name;
  95. if(isset($this->prop['editcount']))
  96. $data[$name]['editcount'] = intval($user->getEditCount());
  97. if(isset($this->prop['registration']))
  98. $data[$name]['registration'] = wfTimestampOrNull(TS_ISO_8601, $user->getRegistration());
  99. if(isset($this->prop['groups']) && !is_null($r->ug_group))
  100. // This row contains only one group, others will be added from other rows
  101. $data[$name]['groups'][] = $r->ug_group;
  102. if(isset($this->prop['blockinfo']) && !is_null($r->blocker_name)) {
  103. $data[$name]['blockedby'] = $r->blocker_name;
  104. $data[$name]['blockreason'] = $r->ipb_reason;
  105. }
  106. if(isset($this->prop['emailable']) && $user->canReceiveEmail())
  107. $data[$name]['emailable'] = '';
  108. }
  109. }
  110. // Second pass: add result data to $retval
  111. foreach($goodNames as $u) {
  112. if(!isset($data[$u]))
  113. $data[$u] = 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. }
  118. $fit = $result->addValue(array('query', $this->getModuleName()),
  119. null, $data[$u]);
  120. if(!$fit)
  121. {
  122. $this->setContinueEnumParameter('users',
  123. implode('|', array_diff($users, $done)));
  124. break;
  125. }
  126. $done[] = $u;
  127. }
  128. return $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'user');
  129. }
  130. public function getAllowedParams() {
  131. return array (
  132. 'prop' => array (
  133. ApiBase :: PARAM_DFLT => NULL,
  134. ApiBase :: PARAM_ISMULTI => true,
  135. ApiBase :: PARAM_TYPE => array (
  136. 'blockinfo',
  137. 'groups',
  138. 'editcount',
  139. 'registration',
  140. 'emailable',
  141. )
  142. ),
  143. 'users' => array(
  144. ApiBase :: PARAM_ISMULTI => true
  145. )
  146. );
  147. }
  148. public function getParamDescription() {
  149. return array (
  150. 'prop' => array(
  151. 'What pieces of information to include',
  152. ' blockinfo - tags if the user is blocked, by whom, and for what reason',
  153. ' groups - lists all the groups the user belongs to',
  154. ' editcount - adds the user\'s edit count',
  155. ' registration - adds the user\'s registration timestamp',
  156. ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
  157. ),
  158. 'users' => 'A list of users to obtain the same information for'
  159. );
  160. }
  161. public function getDescription() {
  162. return 'Get information about a list of users';
  163. }
  164. protected function getExamples() {
  165. return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount';
  166. }
  167. public function getVersion() {
  168. return __CLASS__ . ': $Id: ApiQueryUsers.php 50094 2009-05-01 06:24:09Z tstarling $';
  169. }
  170. }