* Add a special page to list active users
[mediawiki.git] / includes / SpecialListusers.php
blob6c279bbfff1b6bd0c4dbb5186a33be070bf2190b
1 <?php
3 # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
4 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
6 # © 2006 Rob Church <robchur@gmail.com>
8 # http://www.mediawiki.org/
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # http://www.gnu.org/copyleft/gpl.html
24 /**
26 * @addtogroup SpecialPage
29 /**
30 * This class is used to get a list of user. The ones with specials
31 * rights (sysop, bureaucrat, developer) will have them displayed
32 * next to their names.
34 * @addtogroup SpecialPage
37 class UsersPager extends AlphabeticPager {
39 function __construct($group=null) {
40 global $wgRequest;
41 $this->requestedGroup = $group != "" ? $group : $wgRequest->getVal( 'group' );
42 $un = $wgRequest->getText( 'username' );
43 $this->requestedUser = '';
44 if ( $un != '' ) {
45 $username = Title::makeTitleSafe( NS_USER, $un );
46 if( ! is_null( $username ) ) {
47 $this->requestedUser = $username->getText();
50 parent::__construct();
54 function getIndexField() {
55 return 'user_name';
58 function getQueryInfo() {
59 $dbr = wfGetDB( DB_SLAVE );
60 $conds=array();
61 // don't show hidden names
62 $conds[]='ipb_deleted IS NULL OR ipb_deleted = 0';
63 if ($this->requestedGroup != "") {
64 $conds['ug_group'] = $this->requestedGroup;
65 $useIndex = '';
66 } else {
67 $useIndex = $dbr->useIndexClause('user_name');
69 if ($this->requestedUser != "") {
70 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
73 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
75 $query = array(
76 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
77 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_auto=0 ",
78 'fields' => array('user_name',
79 'MAX(user_id) AS user_id',
80 'COUNT(ug_group) AS numgroups',
81 'MAX(ug_group) AS singlegroup',
82 'MAX(ipb_user) AS blocked'),
83 'options' => array('GROUP BY' => 'user_name'),
84 'conds' => $conds
87 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
88 return $query;
91 function formatRow( $row ) {
92 $userPage = Title::makeTitle( NS_USER, $row->user_name );
93 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
95 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
96 $list = array();
97 foreach( self::getGroups( $row->user_id ) as $group )
98 $list[] = self::buildGroupLink( $group );
99 $groups = implode( ', ', $list );
100 } elseif( $row->numgroups == 1 ) {
101 $groups = self::buildGroupLink( $row->singlegroup );
102 } else {
103 $groups = '';
106 $item = wfSpecialList( $name, $groups );
107 $blocked = $row->blocked ? ' '.wfMsg('listusers-blocked') : '';
109 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
110 return "<li>{$item}{$blocked}</li>";
113 function getBody() {
114 if (!$this->mQueryDone) {
115 $this->doQuery();
117 $batch = new LinkBatch;
119 $this->mResult->rewind();
121 while ( $row = $this->mResult->fetchObject() ) {
122 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
124 $batch->execute();
125 $this->mResult->rewind();
126 return parent::getBody();
129 function getPageHeader( ) {
130 global $wgScript, $wgRequest;
131 $self = $this->getTitle();
133 # Form tag
134 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
135 '<fieldset>' .
136 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
137 $out .= Xml::hidden( 'title', $self->getPrefixedDbKey() );
139 # Username field
140 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
141 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
143 # Group drop-down list
144 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
145 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
146 Xml::option( wfMsg( 'group-all' ), '' );
147 foreach( $this->getAllGroups() as $group => $groupText )
148 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
149 $out .= Xml::closeElement( 'select' ) . ' ';
151 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
153 # Submit button and form bottom
154 if( $this->mLimit )
155 $out .= Xml::hidden( 'limit', $this->mLimit );
156 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
157 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
158 $out .= '</fieldset>' .
159 Xml::closeElement( 'form' );
161 return $out;
164 function getAllGroups() {
165 $result = array();
166 foreach( User::getAllGroups() as $group ) {
167 $result[$group] = User::getGroupName( $group );
169 return $result;
173 * Preserve group and username offset parameters when paging
174 * @return array
176 function getDefaultQuery() {
177 $query = parent::getDefaultQuery();
178 if( $this->requestedGroup != '' )
179 $query['group'] = $this->requestedGroup;
180 if( $this->requestedUser != '' )
181 $query['username'] = $this->requestedUser;
182 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
183 return $query;
187 * Get a list of groups the specified user belongs to
189 * @param int $uid
190 * @return array
192 protected static function getGroups( $uid ) {
193 $dbr = wfGetDB( DB_SLAVE );
194 $groups = array();
195 $res = $dbr->select( 'user_groups', 'ug_group', array( 'ug_user' => $uid ), __METHOD__ );
196 if( $res && $dbr->numRows( $res ) > 0 ) {
197 while( $row = $dbr->fetchObject( $res ) )
198 $groups[] = $row->ug_group;
199 $dbr->freeResult( $res );
201 return $groups;
205 * Format a link to a group description page
207 * @param string $group
208 * @return string
210 protected static function buildGroupLink( $group ) {
211 static $cache = array();
212 if( !isset( $cache[$group] ) )
213 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
214 return $cache[$group];
219 * constructor
220 * $par string (optional) A group to list users from
222 function wfSpecialListusers( $par = null ) {
223 global $wgRequest, $wgOut;
225 $up = new UsersPager($par);
227 # getBody() first to check, if empty
228 $usersbody = $up->getBody();
229 $s = $up->getPageHeader();
230 if( $usersbody ) {
231 $s .= $up->getNavigationBar();
232 $s .= '<ul>' . $usersbody . '</ul>';
233 $s .= $up->getNavigationBar() ;
234 } else {
235 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
238 $wgOut->addHTML( $s );