9896c24888226604589be80573e601c015e97867
[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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 */
22
23 /**
24 * @file
25 */
26
27 /**
28 * This class is used to get a list of user. The ones with specials
29 * rights (sysop, bureaucrat, developer) will have them displayed
30 * next to their names.
31 *
32 * @ingroup SpecialPage
33 */
34 class UsersPager extends AlphabeticPager {
35
36 function __construct( $par=null ) {
37 global $wgRequest;
38 $parms = explode( '/', ($par = ( $par !== null ) ? $par : '' ) );
39 $symsForAll = array( '*', 'user' );
40 if ( $parms[0] != '' && ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) ) ) {
41 $this->requestedGroup = $par;
42 $un = $wgRequest->getText( 'username' );
43 } else if ( count( $parms ) == 2 ) {
44 $this->requestedGroup = $parms[0];
45 $un = $parms[1];
46 } else {
47 $this->requestedGroup = $wgRequest->getVal( 'group' );
48 $un = ( $par != '' ) ? $par : $wgRequest->getText( 'username' );
49 }
50 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
51 $this->requestedGroup = '';
52 }
53 $this->editsOnly = $wgRequest->getBool( 'editsOnly' );
54 $this->creationSort = $wgRequest->getBool( 'creationSort' );
55
56 $this->requestedUser = '';
57 if ( $un != '' ) {
58 $username = Title::makeTitleSafe( NS_USER, $un );
59 if( ! is_null( $username ) ) {
60 $this->requestedUser = $username->getText();
61 }
62 }
63 parent::__construct();
64 }
65
66
67 function getIndexField() {
68 return $this->creationSort ? 'user_id' : 'user_name';
69 }
70
71 function getQueryInfo() {
72 global $wgUser;
73 $dbr = wfGetDB( DB_SLAVE );
74 $conds = array();
75 // Don't show hidden names
76 if( !$wgUser->isAllowed('hideuser') )
77 $conds[] = 'ipb_deleted IS NULL';
78 if( $this->requestedGroup != '' ) {
79 $conds['ug_group'] = $this->requestedGroup;
80 $useIndex = '';
81 } else {
82 $useIndex = $dbr->useIndexClause( $this->creationSort ? 'PRIMARY' : 'user_name');
83 }
84 if( $this->requestedUser != '' ) {
85 # Sorted either by account creation or name
86 if( $this->creationSort ) {
87 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
88 } else {
89 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
90 }
91 }
92 if( $this->editsOnly ) {
93 $conds[] = 'user_editcount > 0';
94 }
95
96 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
97
98 $query = array(
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 ",
101 'fields' => array(
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', // the usergroup if there is only one
107 'MIN(user_registration) AS creation',
108 'MAX(ipb_deleted) AS ipb_deleted' // block/hide status
109 ),
110 'options' => array('GROUP BY' => $this->creationSort ? 'user_id' : 'user_name'),
111 'conds' => $conds
112 );
113
114 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
115 return $query;
116 }
117
118 function formatRow( $row ) {
119 global $wgLang;
120
121 if ($row->user_id == 0) #Bug 16487
122 return '';
123
124 $userPage = Title::makeTitle( NS_USER, $row->user_name );
125 $name = $this->getSkin()->link( $userPage, htmlspecialchars( $userPage->getText() ) );
126
127 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
128 $list = array();
129 foreach( self::getGroups( $row->user_id ) as $group )
130 $list[] = self::buildGroupLink( $group );
131 $groups = $wgLang->commaList( $list );
132 } elseif( $row->numgroups == 1 ) {
133 $groups = self::buildGroupLink( $row->singlegroup );
134 } else {
135 $groups = '';
136 }
137
138 $item = wfSpecialList( $name, $groups );
139 if( $row->ipb_deleted ) {
140 $item = "<span class=\"deleted\">$item</span>";
141 }
142
143 global $wgEdititis;
144 if ( $wgEdititis ) {
145 $editCount = $wgLang->formatNum( $row->edits );
146 $edits = ' [' . wfMsgExt( 'usereditcount', array( 'parsemag', 'escape' ), $editCount ) . ']';
147 } else {
148 $edits = '';
149 }
150
151 $created = '';
152 # Some rows may be NULL
153 if( $row->creation ) {
154 $d = $wgLang->date( wfTimestamp( TS_MW, $row->creation ), true );
155 $t = $wgLang->time( wfTimestamp( TS_MW, $row->creation ), true );
156 $created = ' (' . wfMsg( 'usercreated', $d, $t ) . ')';
157 $created = htmlspecialchars( $created );
158 }
159
160 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
161 return "<li>{$item}{$edits}{$created}</li>";
162 }
163
164 function getBody() {
165 if( !$this->mQueryDone ) {
166 $this->doQuery();
167 }
168 $this->mResult->rewind();
169 $batch = new LinkBatch;
170 while ( $row = $this->mResult->fetchObject() ) {
171 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
172 }
173 $batch->execute();
174 $this->mResult->rewind();
175 return parent::getBody();
176 }
177
178 function getPageHeader( ) {
179 global $wgScript;
180 $self = $this->getTitle();
181
182 # Form tag
183 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
184 Xml::fieldset( wfMsg( 'listusers' ) ) .
185 Xml::hidden( 'title', $self->getPrefixedDbKey() );
186
187 # Username field
188 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
189 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
190
191 # Group drop-down list
192 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
193 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
194 Xml::option( wfMsg( 'group-all' ), '' );
195 foreach( $this->getAllGroups() as $group => $groupText )
196 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
197 $out .= Xml::closeElement( 'select' ) . '<br />';
198 $out .= Xml::checkLabel( wfMsg('listusers-editsonly'), 'editsOnly', 'editsOnly', $this->editsOnly );
199 $out .= '&#160;';
200 $out .= Xml::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort );
201 $out .= '<br />';
202
203 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
204
205 # Submit button and form bottom
206 $out .= Xml::hidden( 'limit', $this->mLimit );
207 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
208 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
209 $out .= Xml::closeElement( 'fieldset' ) .
210 Xml::closeElement( 'form' );
211
212 return $out;
213 }
214
215 /**
216 * Get a list of all explicit groups
217 * @return array
218 */
219 function getAllGroups() {
220 $result = array();
221 foreach( User::getAllGroups() as $group ) {
222 $result[$group] = User::getGroupName( $group );
223 }
224 asort( $result );
225 return $result;
226 }
227
228 /**
229 * Preserve group and username offset parameters when paging
230 * @return array
231 */
232 function getDefaultQuery() {
233 $query = parent::getDefaultQuery();
234 if( $this->requestedGroup != '' )
235 $query['group'] = $this->requestedGroup;
236 if( $this->requestedUser != '' )
237 $query['username'] = $this->requestedUser;
238 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
239 return $query;
240 }
241
242 /**
243 * Get a list of groups the specified user belongs to
244 *
245 * @param $uid Integer: user id
246 * @return array
247 */
248 protected static function getGroups( $uid ) {
249 $user = User::newFromId( $uid );
250 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
251 return $groups;
252 }
253
254 /**
255 * Format a link to a group description page
256 *
257 * @param $group String: group name
258 * @return string
259 */
260 protected static function buildGroupLink( $group ) {
261 static $cache = array();
262 if( !isset( $cache[$group] ) )
263 $cache[$group] = User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupMember( $group ) ) );
264 return $cache[$group];
265 }
266 }
267
268 /**
269 * constructor
270 * $par string (optional) A group to list users from
271 */
272 function wfSpecialListusers( $par = null ) {
273 global $wgOut;
274
275 $up = new UsersPager($par);
276
277 # getBody() first to check, if empty
278 $usersbody = $up->getBody();
279 $s = Xml::openElement( 'div', array('class' => 'mw-spcontent') );
280 $s .= $up->getPageHeader();
281 if( $usersbody ) {
282 $s .= $up->getNavigationBar();
283 $s .= '<ul>' . $usersbody . '</ul>';
284 $s .= $up->getNavigationBar() ;
285 } else {
286 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
287 };
288 $s .= Xml::closeElement( 'div' );
289 $wgOut->addHTML( $s );
290 }