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