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() {
74 $dbr = wfGetDB( DB_SLAVE
);
76 // Don't show hidden names
77 $conds[] = 'ipb_deleted IS NULL';
78 if( $this->requestedGroup
!= '' ) {
79 $conds['ug_group'] = $this->requestedGroup
;
82 $useIndex = $dbr->useIndexClause( $this->creationSort ?
'PRIMARY' : 'user_name');
84 if( $this->requestedUser
!= '' ) {
85 # Sorted either by account creation or name
86 if( $this->creationSort
) {
87 $conds[] = 'user_id >= ' . User
::idFromName( $this->requestedUser
);
89 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser
);
92 if( $this->editsOnly
) {
93 $conds[] = 'user_editcount > 0';
96 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
99 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
100 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_deleted=1 AND ipb_auto=0 ",
102 $this->creationSort ?
'MAX(user_name) AS user_name' : 'user_name',
103 $this->creationSort ?
'user_id' : 'MAX(user_id) AS user_id',
104 'MAX(user_editcount) AS edits',
105 'COUNT(ug_group) AS numgroups',
106 'MAX(ug_group) AS singlegroup',
107 'MIN(user_registration) AS creation'),
108 'options' => array('GROUP BY' => $this->creationSort ?
'user_id' : 'user_name'),
112 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
116 function formatRow( $row ) {
119 $userPage = Title
::makeTitle( NS_USER
, $row->user_name
);
120 $name = $this->getSkin()->link( $userPage, htmlspecialchars( $userPage->getText() ) );
122 if( $row->numgroups
> 1 ||
( $this->requestedGroup
&& $row->numgroups
== 1 ) ) {
124 foreach( self
::getGroups( $row->user_id
) as $group )
125 $list[] = self
::buildGroupLink( $group );
126 $groups = $wgLang->commaList( $list );
127 } elseif( $row->numgroups
== 1 ) {
128 $groups = self
::buildGroupLink( $row->singlegroup
);
133 $item = wfSpecialList( $name, $groups );
137 $editCount = $wgLang->formatNum( $row->edits
);
138 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
144 # Some rows may be NULL
145 if( $row->creation
) {
146 $d = $wgLang->date( wfTimestamp( TS_MW
, $row->creation
), true );
147 $t = $wgLang->time( wfTimestamp( TS_MW
, $row->creation
), true );
148 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
149 $created = htmlspecialchars( $created );
152 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
153 return "<li>{$item}{$edits}{$created}</li>";
157 if( !$this->mQueryDone
) {
160 $this->mResult
->rewind();
161 $batch = new LinkBatch
;
162 while ( $row = $this->mResult
->fetchObject() ) {
163 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
166 $this->mResult
->rewind();
167 return parent
::getBody();
170 function getPageHeader( ) {
171 global $wgScript, $wgRequest;
172 $self = $this->getTitle();
175 $out = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
176 Xml
::fieldset( wfMsg( 'listusers' ) ) .
177 Xml
::hidden( 'title', $self->getPrefixedDbKey() );
180 $out .= Xml
::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
181 Xml
::input( 'username', 20, $this->requestedUser
, array( 'id' => 'offset' ) ) . ' ';
183 # Group drop-down list
184 $out .= Xml
::label( wfMsg( 'group' ), 'group' ) . ' ' .
185 Xml
::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
186 Xml
::option( wfMsg( 'group-all' ), '' );
187 foreach( $this->getAllGroups() as $group => $groupText )
188 $out .= Xml
::option( $groupText, $group, $group == $this->requestedGroup
);
189 $out .= Xml
::closeElement( 'select' ) . '<br/>';
190 $out .= Xml
::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly
);
192 $out .= Xml
::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort
);
195 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
197 # Submit button and form bottom
198 $out .= Xml
::hidden( 'limit', $this->mLimit
);
199 $out .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
200 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
201 $out .= Xml
::closeElement( 'fieldset' ) .
202 Xml
::closeElement( 'form' );
208 * Get a list of all explicit groups
211 function getAllGroups() {
213 foreach( User
::getAllGroups() as $group ) {
214 $result[$group] = User
::getGroupName( $group );
221 * Preserve group and username offset parameters when paging
224 function getDefaultQuery() {
225 $query = parent
::getDefaultQuery();
226 if( $this->requestedGroup
!= '' )
227 $query['group'] = $this->requestedGroup
;
228 if( $this->requestedUser
!= '' )
229 $query['username'] = $this->requestedUser
;
230 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
235 * Get a list of groups the specified user belongs to
240 protected static function getGroups( $uid ) {
241 $user = User
::newFromId( $uid );
242 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
247 * Format a link to a group description page
249 * @param string $group
252 protected static function buildGroupLink( $group ) {
253 static $cache = array();
254 if( !isset( $cache[$group] ) )
255 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupMember( $group ) ) );
256 return $cache[$group];
262 * $par string (optional) A group to list users from
264 function wfSpecialListusers( $par = null ) {
265 global $wgRequest, $wgOut;
267 $up = new UsersPager($par);
269 # getBody() first to check, if empty
270 $usersbody = $up->getBody();
271 $s = XML
::openElement( 'div', array('class' => 'mw-spcontent') );
272 $s .= $up->getPageHeader();
274 $s .= $up->getNavigationBar();
275 $s .= '<ul>' . $usersbody . '</ul>';
276 $s .= $up->getNavigationBar() ;
278 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
280 $s .= XML
::closeElement( 'div' );
281 $wgOut->addHTML( $s );