0ccd44bf51fd7bd7d66848318ace71b8d3f816ed
[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 $this->creationSort = $wgRequest->getBool( 'creationSort' );
57
58 $this->requestedUser = '';
59 if ( $un != '' ) {
60 $username = Title::makeTitleSafe( NS_USER, $un );
61 if( ! is_null( $username ) ) {
62 $this->requestedUser = $username->getText();
63 }
64 }
65 parent::__construct();
66 }
67
68
69 function getIndexField() {
70 return $this->creationSort ? 'user_id' : 'user_name';
71 }
72
73 function getQueryInfo() {
74 global $wgUser;
75 $dbr = wfGetDB( DB_SLAVE );
76 $conds = array();
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;
82 $useIndex = '';
83 } else {
84 $useIndex = $dbr->useIndexClause( $this->creationSort ? 'PRIMARY' : 'user_name');
85 }
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 ) );
90 } else {
91 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
92 }
93 }
94 if( $this->editsOnly ) {
95 $conds[] = 'user_editcount > 0';
96 }
97
98 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
99
100 $query = array(
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 ",
103 'fields' => array(
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
111 ),
112 'options' => array('GROUP BY' => $this->creationSort ? 'user_id' : 'user_name'),
113 'conds' => $conds
114 );
115
116 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
117 return $query;
118 }
119
120 function formatRow( $row ) {
121 global $wgLang;
122
123 if ($row->user_id == 0) #Bug 16487
124 return '';
125
126 $userPage = Title::makeTitle( NS_USER, $row->user_name );
127 $name = $this->getSkin()->link( $userPage, htmlspecialchars( $userPage->getText() ) );
128
129 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
130 $list = array();
131 foreach( self::getGroups( $row->user_id ) as $group )
132 $list[] = self::buildGroupLink( $group );
133 $groups = $wgLang->commaList( $list );
134 } elseif( $row->numgroups == 1 ) {
135 $groups = self::buildGroupLink( $row->singlegroup );
136 } else {
137 $groups = '';
138 }
139
140 $item = wfSpecialList( $name, $groups );
141 if( $row->ipb_deleted ) {
142 $item = "<span class=\"deleted\">$item</span>";
143 }
144
145 global $wgEdititis;
146 if ( $wgEdititis ) {
147 $editCount = $wgLang->formatNum( $row->edits );
148 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
149 } else {
150 $edits = '';
151 }
152
153 $created = '';
154 # Some rows may be NULL
155 if( $row->creation ) {
156 $d = $wgLang->date( wfTimestamp( TS_MW, $row->creation ), true );
157 $t = $wgLang->time( wfTimestamp( TS_MW, $row->creation ), true );
158 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
159 $created = htmlspecialchars( $created );
160 }
161
162 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
163 return "<li>{$item}{$edits}{$created}</li>";
164 }
165
166 function getBody() {
167 if( !$this->mQueryDone ) {
168 $this->doQuery();
169 }
170 $this->mResult->rewind();
171 $batch = new LinkBatch;
172 while ( $row = $this->mResult->fetchObject() ) {
173 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
174 }
175 $batch->execute();
176 $this->mResult->rewind();
177 return parent::getBody();
178 }
179
180 function getPageHeader( ) {
181 global $wgScript, $wgRequest;
182 $self = $this->getTitle();
183
184 # Form tag
185 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
186 Xml::fieldset( wfMsg( 'listusers' ) ) .
187 Xml::hidden( 'title', $self->getPrefixedDbKey() );
188
189 # Username field
190 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
191 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
192
193 # Group drop-down list
194 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
195 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
196 Xml::option( wfMsg( 'group-all' ), '' );
197 foreach( $this->getAllGroups() as $group => $groupText )
198 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
199 $out .= Xml::closeElement( 'select' ) . '<br />';
200 $out .= Xml::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly );
201 $out .= '&nbsp;';
202 $out .= Xml::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort );
203 $out .= '<br />';
204
205 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
206
207 # Submit button and form bottom
208 $out .= Xml::hidden( 'limit', $this->mLimit );
209 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
210 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
211 $out .= Xml::closeElement( 'fieldset' ) .
212 Xml::closeElement( 'form' );
213
214 return $out;
215 }
216
217 /**
218 * Get a list of all explicit groups
219 * @return array
220 */
221 function getAllGroups() {
222 $result = array();
223 foreach( User::getAllGroups() as $group ) {
224 $result[$group] = User::getGroupName( $group );
225 }
226 asort( $result );
227 return $result;
228 }
229
230 /**
231 * Preserve group and username offset parameters when paging
232 * @return array
233 */
234 function getDefaultQuery() {
235 $query = parent::getDefaultQuery();
236 if( $this->requestedGroup != '' )
237 $query['group'] = $this->requestedGroup;
238 if( $this->requestedUser != '' )
239 $query['username'] = $this->requestedUser;
240 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
241 return $query;
242 }
243
244 /**
245 * Get a list of groups the specified user belongs to
246 *
247 * @param $uid Integer: user id
248 * @return array
249 */
250 protected static function getGroups( $uid ) {
251 $user = User::newFromId( $uid );
252 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
253 return $groups;
254 }
255
256 /**
257 * Format a link to a group description page
258 *
259 * @param $group String: group name
260 * @return string
261 */
262 protected static function buildGroupLink( $group ) {
263 static $cache = array();
264 if( !isset( $cache[$group] ) )
265 $cache[$group] = User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupMember( $group ) ) );
266 return $cache[$group];
267 }
268 }
269
270 /**
271 * constructor
272 * $par string (optional) A group to list users from
273 */
274 function wfSpecialListusers( $par = null ) {
275 global $wgRequest, $wgOut;
276
277 $up = new UsersPager($par);
278
279 # getBody() first to check, if empty
280 $usersbody = $up->getBody();
281 $s = XML::openElement( 'div', array('class' => 'mw-spcontent') );
282 $s .= $up->getPageHeader();
283 if( $usersbody ) {
284 $s .= $up->getNavigationBar();
285 $s .= '<ul>' . $usersbody . '</ul>';
286 $s .= $up->getNavigationBar() ;
287 } else {
288 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
289 };
290 $s .= XML::closeElement( 'div' );
291 $wgOut->addHTML( $s );
292 }