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
26 * @ingroup SpecialPage
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 * @ingroup SpecialPage
36 class UsersPager
extends AlphabeticPager
{
38 function __construct( $par=null ) {
40 $parms = explode( '/', ($par = ( $par !== null ) ?
$par : '' ) );
41 $symsForAll = array( '*', 'user' );
42 if ( $parms[0] != '' && ( in_array( $par, User
::getAllGroups() ) ||
in_array( $par, $symsForAll ) ) ) {
43 $this->requestedGroup
= $par;
44 $un = $wgRequest->getText( 'username' );
45 } else if ( count( $parms ) == 2 ) {
46 $this->requestedGroup
= $parms[0];
49 $this->requestedGroup
= $wgRequest->getVal( 'group' );
50 $un = ( $par != '' ) ?
$par : $wgRequest->getText( 'username' );
52 if ( in_array( $this->requestedGroup
, $symsForAll ) ) {
53 $this->requestedGroup
= '';
55 $this->editsOnly
= $wgRequest->getBool( 'editsOnly' );
56 $this->creationSort
= $wgRequest->getBool( 'creationSort' );
58 $this->requestedUser
= '';
60 $username = Title
::makeTitleSafe( NS_USER
, $un );
61 if( ! is_null( $username ) ) {
62 $this->requestedUser
= $username->getText();
65 parent
::__construct();
69 function getIndexField() {
70 return $this->creationSort ?
'user_id' : 'user_name';
73 function getQueryInfo() {
75 $dbr = wfGetDB( DB_SLAVE
);
77 // Don't show hidden names
78 if( !$wgUser->isAllowed('hideuser') )
79 $conds[] = 'ipb_deleted IS NULL';
80 if( $this->requestedGroup
!= '' ) {
81 $conds['ug_group'] = $this->requestedGroup
;
84 $useIndex = $dbr->useIndexClause( $this->creationSort ?
'PRIMARY' : 'user_name');
86 if( $this->requestedUser
!= '' ) {
87 # Sorted either by account creation or name
88 if( $this->creationSort
) {
89 $conds[] = 'user_id >= ' . intval( User
::idFromName( $this->requestedUser
) );
91 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
94 if( $this->editsOnly
) {
95 $conds[] = 'user_editcount > 0';
98 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
101 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
102 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_deleted=1 AND ipb_auto=0 ",
104 $this->creationSort ?
'MAX(user_name) AS user_name' : 'user_name',
105 $this->creationSort ?
'user_id' : 'MAX(user_id) AS user_id',
106 'MAX(user_editcount) AS edits',
107 'COUNT(ug_group) AS numgroups',
108 'MAX(ug_group) AS singlegroup', // the usergroup if there is only one
109 'MIN(user_registration) AS creation',
110 'MAX(ipb_deleted) AS ipb_deleted' // block/hide status
112 'options' => array('GROUP BY' => $this->creationSort ?
'user_id' : 'user_name'),
116 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
120 function formatRow( $row ) {
123 $userPage = Title
::makeTitle( NS_USER
, $row->user_name
);
124 $name = $this->getSkin()->link( $userPage, htmlspecialchars( $userPage->getText() ) );
126 if( $row->numgroups
> 1 ||
( $this->requestedGroup
&& $row->numgroups
== 1 ) ) {
128 foreach( self
::getGroups( $row->user_id
) as $group )
129 $list[] = self
::buildGroupLink( $group );
130 $groups = $wgLang->commaList( $list );
131 } elseif( $row->numgroups
== 1 ) {
132 $groups = self
::buildGroupLink( $row->singlegroup
);
137 $item = wfSpecialList( $name, $groups );
138 if( $row->ipb_deleted
) {
139 $item = "<span class=\"deleted\">$item</span>";
144 $editCount = $wgLang->formatNum( $row->edits
);
145 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
151 # Some rows may be NULL
152 if( $row->creation
) {
153 $d = $wgLang->date( wfTimestamp( TS_MW
, $row->creation
), true );
154 $t = $wgLang->time( wfTimestamp( TS_MW
, $row->creation
), true );
155 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
156 $created = htmlspecialchars( $created );
159 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
160 return "<li>{$item}{$edits}{$created}</li>";
164 if( !$this->mQueryDone
) {
167 $this->mResult
->rewind();
168 $batch = new LinkBatch
;
169 while ( $row = $this->mResult
->fetchObject() ) {
170 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
173 $this->mResult
->rewind();
174 return parent
::getBody();
177 function getPageHeader( ) {
178 global $wgScript, $wgRequest;
179 $self = $this->getTitle();
182 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
183 Xml
::fieldset( wfMsg( 'listusers' ) ) .
184 Xml
::hidden( 'title', $self->getPrefixedDbKey() );
187 $out .= Xml
::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
188 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
190 # Group drop-down list
191 $out .= Xml
::label( wfMsg( 'group' ), 'group' ) . ' ' .
192 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
193 Xml
::option( wfMsg( 'group-all' ), '' );
194 foreach( $this->getAllGroups() as $group => $groupText )
195 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
196 $out .= Xml
::closeElement( 'select' ) . '<br/>';
197 $out .= Xml
::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly
);
199 $out .= Xml
::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort
);
202 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
204 # Submit button and form bottom
205 $out .= Xml
::hidden( 'limit', $this->mLimit
);
206 $out .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
207 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
208 $out .= Xml
::closeElement( 'fieldset' ) .
209 Xml
::closeElement( 'form' );
215 * Get a list of all explicit groups
218 function getAllGroups() {
220 foreach( User
::getAllGroups() as $group ) {
221 $result[$group] = User
::getGroupName( $group );
228 * Preserve group and username offset parameters when paging
231 function getDefaultQuery() {
232 $query = parent
::getDefaultQuery();
233 if( $this->requestedGroup
!= '' )
234 $query['group'] = $this->requestedGroup
;
235 if( $this->requestedUser
!= '' )
236 $query['username'] = $this->requestedUser
;
237 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
242 * Get a list of groups the specified user belongs to
244 * @param $uid Integer: user id
247 protected static function getGroups( $uid ) {
248 $user = User
::newFromId( $uid );
249 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
254 * Format a link to a group description page
256 * @param $group String: group name
259 protected static function buildGroupLink( $group ) {
260 static $cache = array();
261 if( !isset( $cache[$group] ) )
262 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group ) ) );
263 return $cache[$group];
269 * $par string (optional) A group to list users from
271 function wfSpecialListusers( $par = null ) {
272 global $wgRequest, $wgOut;
274 $up = new UsersPager($par);
276 # getBody() first to check, if empty
277 $usersbody = $up->getBody();
278 $s = XML
::openElement( 'div', array('class' => 'mw-spcontent') );
279 $s .= $up->getPageHeader();
281 $s .= $up->getNavigationBar();
282 $s .= '<ul>' . $usersbody . '</ul>';
283 $s .= $up->getNavigationBar() ;
285 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
287 $s .= XML
::closeElement( 'div' );
288 $wgOut->addHTML( $s );