Fix stomach-churning case of edititis in r41350: made optional.
[lhc/web/wiklou.git] / includes / specials / SpecialListusers.php
1 <?php
2
3 # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
4 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
5 #
6 # © 2006 Rob Church <robchur@gmail.com>
7 #
8 # http://www.mediawiki.org/
9 #
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.
14 #
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.
19 #
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 /**
25 * @file
26 * @ingroup SpecialPage
27 */
28
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.
33 *
34 * @ingroup SpecialPage
35 */
36 class UsersPager extends AlphabeticPager {
37
38 function __construct( $par=null ) {
39 global $wgRequest;
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];
47 $un = $parms[1];
48 } else {
49 $this->requestedGroup = $wgRequest->getVal( 'group' );
50 $un = ( $par != '' ) ? $par : $wgRequest->getText( 'username' );
51 }
52 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
53 $this->requestedGroup = '';
54 }
55 $this->editsOnly = $wgRequest->getBool( 'editsOnly' );
56
57 $this->requestedUser = '';
58 if ( $un != '' ) {
59 $username = Title::makeTitleSafe( NS_USER, $un );
60 if( ! is_null( $username ) ) {
61 $this->requestedUser = $username->getText();
62 }
63 }
64 parent::__construct();
65 }
66
67
68 function getIndexField() {
69 return 'user_name';
70 }
71
72 function getQueryInfo() {
73 $dbr = wfGetDB( DB_SLAVE );
74 $conds = array();
75 // Don't show hidden names
76 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
77 if ($this->requestedGroup != "") {
78 $conds['ug_group'] = $this->requestedGroup;
79 $useIndex = '';
80 } else {
81 $useIndex = $dbr->useIndexClause('user_name');
82 }
83 if ($this->requestedUser != "") {
84 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
85 }
86 if( $this->editsOnly ) {
87 $conds[] = 'user_editcount > 0';
88 }
89
90 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
91
92 $query = array(
93 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
94 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_auto=0 ",
95 'fields' => array('user_name',
96 'MAX(user_id) AS user_id',
97 'MAX(user_editcount) AS edits',
98 'COUNT(ug_group) AS numgroups',
99 'MAX(ug_group) AS singlegroup'),
100 'options' => array('GROUP BY' => 'user_name'),
101 'conds' => $conds
102 );
103
104 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
105 return $query;
106 }
107
108 function formatRow( $row ) {
109 global $wgLang;
110
111 $userPage = Title::makeTitle( NS_USER, $row->user_name );
112 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
113
114 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
115 $list = array();
116 foreach( self::getGroups( $row->user_id ) as $group )
117 $list[] = self::buildGroupLink( $group );
118 $groups = implode( ', ', $list );
119 } elseif( $row->numgroups == 1 ) {
120 $groups = self::buildGroupLink( $row->singlegroup );
121 } else {
122 $groups = '';
123 }
124
125 $item = wfSpecialList( $name, $groups );
126
127 global $wgEdititis;
128 if ( $wgEdititis ) {
129 $editCount = $wgLang->formatNum( $row->edits );
130 $edits = ' [' . wfMsgExt( 'usereditcount', 'parsemag', $editCount ) . ']';
131 } else {
132 $edits = '';
133 }
134 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
135 return "<li>{$item}{$edits}</li>";
136 }
137
138 function getBody() {
139 if( !$this->mQueryDone ) {
140 $this->doQuery();
141 }
142 $this->mResult->rewind();
143 $batch = new LinkBatch;
144 while ( $row = $this->mResult->fetchObject() ) {
145 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
146 }
147 $batch->execute();
148 $this->mResult->rewind();
149 return parent::getBody();
150 }
151
152 function getPageHeader( ) {
153 global $wgScript, $wgRequest;
154 $self = $this->getTitle();
155
156 # Form tag
157 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
158 '<fieldset>' .
159 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
160 $out .= Xml::hidden( 'title', $self->getPrefixedDbKey() );
161
162 # Username field
163 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
164 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
165
166 # Group drop-down list
167 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
168 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
169 Xml::option( wfMsg( 'group-all' ), '' );
170 foreach( $this->getAllGroups() as $group => $groupText )
171 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
172 $out .= Xml::closeElement( 'select' ) . '<br/>';
173 $out .= Xml::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly );
174 $out .= '&nbsp;';
175
176 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
177
178 # Submit button and form bottom
179 if( $this->mLimit )
180 $out .= Xml::hidden( 'limit', $this->mLimit );
181 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
182 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
183 $out .= '</fieldset>' .
184 Xml::closeElement( 'form' );
185
186 return $out;
187 }
188
189 function getAllGroups() {
190 $result = array();
191 foreach( User::getAllGroups() as $group ) {
192 $result[$group] = User::getGroupName( $group );
193 }
194 return $result;
195 }
196
197 /**
198 * Preserve group and username offset parameters when paging
199 * @return array
200 */
201 function getDefaultQuery() {
202 $query = parent::getDefaultQuery();
203 if( $this->requestedGroup != '' )
204 $query['group'] = $this->requestedGroup;
205 if( $this->requestedUser != '' )
206 $query['username'] = $this->requestedUser;
207 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
208 return $query;
209 }
210
211 /**
212 * Get a list of groups the specified user belongs to
213 *
214 * @param int $uid
215 * @return array
216 */
217 protected static function getGroups( $uid ) {
218 $dbr = wfGetDB( DB_SLAVE );
219 $groups = array();
220 $res = $dbr->select( 'user_groups', 'ug_group', array( 'ug_user' => $uid ), __METHOD__ );
221 if( $res && $dbr->numRows( $res ) > 0 ) {
222 while( $row = $dbr->fetchObject( $res ) )
223 $groups[] = $row->ug_group;
224 $dbr->freeResult( $res );
225 }
226 return $groups;
227 }
228
229 /**
230 * Format a link to a group description page
231 *
232 * @param string $group
233 * @return string
234 */
235 protected static function buildGroupLink( $group ) {
236 static $cache = array();
237 if( !isset( $cache[$group] ) )
238 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
239 return $cache[$group];
240 }
241 }
242
243 /**
244 * constructor
245 * $par string (optional) A group to list users from
246 */
247 function wfSpecialListusers( $par = null ) {
248 global $wgRequest, $wgOut;
249
250 $up = new UsersPager($par);
251
252 # getBody() first to check, if empty
253 $usersbody = $up->getBody();
254 $s = XML::openElement( 'div', array('class' => 'mw-spcontent') );
255 $s .= $up->getPageHeader();
256 if( $usersbody ) {
257 $s .= $up->getNavigationBar();
258 $s .= '<ul>' . $usersbody . '</ul>';
259 $s .= $up->getNavigationBar() ;
260 } else {
261 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
262 };
263 $s .= XML::closeElement( 'div' );
264 $wgOut->addHTML( $s );
265 }