PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/includes/SpecialListusers.php

https://code.google.com/
PHP | 234 lines | 131 code | 33 blank | 70 comment | 20 complexity | 0115cd385edd4b2bf3f65811d2484967 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
  3. # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
  4. #
  5. # Š 2006 Rob Church <robchur@gmail.com>
  6. #
  7. # http://www.mediawiki.org/
  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. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. # http://www.gnu.org/copyleft/gpl.html
  23. /**
  24. *
  25. * @package MediaWiki
  26. * @subpackage SpecialPage
  27. */
  28. /**
  29. * This class is used to get a list of user. The ones with specials
  30. * rights (sysop, bureaucrat, developer) will have them displayed
  31. * next to their names.
  32. *
  33. * @package MediaWiki
  34. * @subpackage SpecialPage
  35. */
  36. class ListUsersPage extends QueryPage {
  37. var $requestedGroup = '';
  38. var $requestedUser = '';
  39. function getName() {
  40. return 'Listusers';
  41. }
  42. function isSyndicated() { return false; }
  43. /**
  44. * Not expensive, this class won't work properly with the caching system anyway
  45. */
  46. function isExpensive() {
  47. return false;
  48. }
  49. /**
  50. * Fetch user page links and cache their existence
  51. */
  52. function preprocessResults( &$db, &$res ) {
  53. $batch = new LinkBatch;
  54. while ( $row = $db->fetchObject( $res ) ) {
  55. $batch->addObj( Title::makeTitleSafe( $row->namespace, $row->title ) );
  56. }
  57. $batch->execute();
  58. // Back to start for display
  59. if( $db->numRows( $res ) > 0 ) {
  60. // If there are no rows we get an error seeking.
  61. $db->dataSeek( $res, 0 );
  62. }
  63. }
  64. /**
  65. * Show a drop down list to select a group as well as a user name
  66. * search box.
  67. * @todo localize
  68. */
  69. function getPageHeader( ) {
  70. $self = $this->getTitle();
  71. # Form tag
  72. $out = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
  73. # Group drop-down list
  74. $out .= wfElement( 'label', array( 'for' => 'group' ), wfMsg( 'group' ) ) . ' ';
  75. $out .= wfOpenElement( 'select', array( 'name' => 'group' ) );
  76. $out .= wfElement( 'option', array( 'value' => '' ), wfMsg( 'group-all' ) ); # Item for "all groups"
  77. $groups = User::getAllGroups();
  78. foreach( $groups as $group ) {
  79. $attribs = array( 'value' => $group );
  80. if( $group == $this->requestedGroup )
  81. $attribs['selected'] = 'selected';
  82. $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) );
  83. }
  84. $out .= wfCloseElement( 'select' ) . ' ';;# . wfElement( 'br' );
  85. # Username field
  86. $out .= wfElement( 'label', array( 'for' => 'username' ), wfMsg( 'listusersfrom' ) ) . ' ';
  87. $out .= wfElement( 'input', array( 'type' => 'text', 'id' => 'username', 'name' => 'username',
  88. 'value' => $this->requestedUser ) ) . ' ';
  89. # Preserve offset and limit
  90. if( $this->offset )
  91. $out .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'offset', 'value' => $this->offset ) );
  92. if( $this->limit )
  93. $out .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'limit', 'value' => $this->limit ) );
  94. # Submit button and form bottom
  95. $out .= wfElement( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'allpagessubmit' ) ) );
  96. $out .= wfCloseElement( 'form' );
  97. return $out;
  98. }
  99. function getSQL() {
  100. global $wgDBtype;
  101. $dbr =& wfGetDB( DB_SLAVE );
  102. $user = $dbr->tableName( 'user' );
  103. $user_groups = $dbr->tableName( 'user_groups' );
  104. // We need to get an 'atomic' list of users, so that we
  105. // don't break the list half-way through a user's group set
  106. // and so that lists by group will show all group memberships.
  107. //
  108. // On MySQL 4.1 we could use GROUP_CONCAT to grab group
  109. // assignments together with users pretty easily. On other
  110. // versions, it's not so easy to do it consistently.
  111. // For now we'll just grab the number of memberships, so
  112. // we can then do targetted checks on those who are in
  113. // non-default groups as we go down the list.
  114. $userspace = NS_USER;
  115. $sql = "SELECT 'Listusers' as type, $userspace AS namespace, user_name AS title, " .
  116. "user_name as value, user_id, COUNT(ug_group) as numgroups " .
  117. "FROM $user ".
  118. "LEFT JOIN $user_groups ON user_id=ug_user " .
  119. $this->userQueryWhere( $dbr ) .
  120. " GROUP BY user_name";
  121. if ( $wgDBtype != 'mysql' ) {
  122. $sql .= ",user_id";
  123. }
  124. return $sql;
  125. }
  126. function userQueryWhere( &$dbr ) {
  127. $conds = $this->userQueryConditions( $dbr );
  128. return empty( $conds )
  129. ? ""
  130. : "WHERE " . $dbr->makeList( $conds, LIST_AND );
  131. }
  132. function userQueryConditions( $dbr ) {
  133. $conds = array();
  134. if( $this->requestedGroup != '' ) {
  135. $conds['ug_group'] = $this->requestedGroup;
  136. }
  137. if( $this->requestedUser != '' ) {
  138. $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
  139. }
  140. return $conds;
  141. }
  142. function linkParameters() {
  143. $conds = array();
  144. if( $this->requestedGroup != '' ) {
  145. $conds['group'] = $this->requestedGroup;
  146. }
  147. if( $this->requestedUser != '' ) {
  148. $conds['username'] = $this->requestedUser;
  149. }
  150. return $conds;
  151. }
  152. function sortDescending() {
  153. return false;
  154. }
  155. function formatResult( $skin, $result ) {
  156. $userPage = Title::makeTitle( $result->namespace, $result->title );
  157. $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
  158. $groups = null;
  159. if( !isset( $result->numgroups ) || $result->numgroups > 0 ) {
  160. $dbr =& wfGetDB( DB_SLAVE );
  161. $result = $dbr->select( 'user_groups',
  162. array( 'ug_group' ),
  163. array( 'ug_user' => $result->user_id ),
  164. 'ListUsersPage::formatResult' );
  165. $groups = array();
  166. while( $row = $dbr->fetchObject( $result ) ) {
  167. $groups[$row->ug_group] = User::getGroupMember( $row->ug_group );
  168. }
  169. $dbr->freeResult( $result );
  170. if( count( $groups ) > 0 ) {
  171. foreach( $groups as $group => $desc ) {
  172. $list[] = User::makeGroupLinkHTML( $group, $desc );
  173. }
  174. $groups = implode( ', ', $list );
  175. } else {
  176. $groups = '';
  177. }
  178. }
  179. return wfSpecialList( $name, $groups );
  180. }
  181. }
  182. /**
  183. * constructor
  184. * $par string (optional) A group to list users from
  185. */
  186. function wfSpecialListusers( $par = null ) {
  187. global $wgRequest;
  188. list( $limit, $offset ) = wfCheckLimits();
  189. $slu = new ListUsersPage();
  190. /**
  191. * Get some parameters
  192. */
  193. $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
  194. $slu->requestedGroup = $groupTarget;
  195. # 'Validate' the username first
  196. $username = $wgRequest->getText( 'username', '' );
  197. $user = User::newFromName( $username );
  198. $slu->requestedUser = is_object( $user ) ? $user->getName() : '';
  199. return $slu->doQuery( $offset, $limit );
  200. }
  201. ?>