E_STRICT
[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 $dbr = wfGetDB( DB_SLAVE );
75 $conds = array();
76 // Don't show hidden names
77 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
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 >= ' . 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_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',
107 'MIN(user_registration) AS creation'),
108 'options' => array('GROUP BY' => $this->creationSort ? 'user_id' : 'user_name'),
109 'conds' => $conds
110 );
111
112 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
113 return $query;
114 }
115
116 function formatRow( $row ) {
117 global $wgLang;
118
119 $userPage = Title::makeTitle( NS_USER, $row->user_name );
120 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
121
122 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
123 $list = array();
124 foreach( self::getGroups( $row->user_id ) as $group )
125 $list[] = self::buildGroupLink( $group );
126 $groups = implode( ', ', $list );
127 } elseif( $row->numgroups == 1 ) {
128 $groups = self::buildGroupLink( $row->singlegroup );
129 } else {
130 $groups = '';
131 }
132
133 $item = wfSpecialList( $name, $groups );
134
135 global $wgEdititis;
136 if ( $wgEdititis ) {
137 $editCount = $wgLang->formatNum( $row->edits );
138 $edits = ' [' . wfMsgExt( 'usereditcount', 'parsemag', $editCount ) . ']';
139 } else {
140 $edits = '';
141 }
142
143 $created = '';
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 = ' (' . wfMsgHtml( 'usercreated', $d, $t ) . ')';
149 }
150
151 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
152 return "<li>{$item}{$edits}{$created}</li>";
153 }
154
155 function getBody() {
156 if( !$this->mQueryDone ) {
157 $this->doQuery();
158 }
159 $this->mResult->rewind();
160 $batch = new LinkBatch;
161 while ( $row = $this->mResult->fetchObject() ) {
162 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
163 }
164 $batch->execute();
165 $this->mResult->rewind();
166 return parent::getBody();
167 }
168
169 function getPageHeader( ) {
170 global $wgScript, $wgRequest;
171 $self = $this->getTitle();
172
173 # Form tag
174 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
175 '<fieldset>' .
176 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
177 $out .= Xml::hidden( 'title', $self->getPrefixedDbKey() );
178
179 # Username field
180 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
181 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
182
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 );
191 $out .= '&nbsp;';
192 $out .= Xml::checkLabel( wfMsg('listusers-creationsort'), 'creationSort', 'creationSort', $this->creationSort );
193 $out .= '<br/>';
194
195 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
196
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 .= '</fieldset>' .
202 Xml::closeElement( 'form' );
203
204 return $out;
205 }
206
207 function getAllGroups() {
208 $result = array();
209 foreach( User::getAllGroups() as $group ) {
210 $result[$group] = User::getGroupName( $group );
211 }
212 return $result;
213 }
214
215 /**
216 * Preserve group and username offset parameters when paging
217 * @return array
218 */
219 function getDefaultQuery() {
220 $query = parent::getDefaultQuery();
221 if( $this->requestedGroup != '' )
222 $query['group'] = $this->requestedGroup;
223 if( $this->requestedUser != '' )
224 $query['username'] = $this->requestedUser;
225 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
226 return $query;
227 }
228
229 /**
230 * Get a list of groups the specified user belongs to
231 *
232 * @param int $uid
233 * @return array
234 */
235 protected static function getGroups( $uid ) {
236 $user = User::newFromId( $uid );
237 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
238 return $groups;
239 }
240
241 /**
242 * Format a link to a group description page
243 *
244 * @param string $group
245 * @return string
246 */
247 protected static function buildGroupLink( $group ) {
248 static $cache = array();
249 if( !isset( $cache[$group] ) )
250 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
251 return $cache[$group];
252 }
253 }
254
255 /**
256 * constructor
257 * $par string (optional) A group to list users from
258 */
259 function wfSpecialListusers( $par = null ) {
260 global $wgRequest, $wgOut;
261
262 $up = new UsersPager($par);
263
264 # getBody() first to check, if empty
265 $usersbody = $up->getBody();
266 $s = XML::openElement( 'div', array('class' => 'mw-spcontent') );
267 $s .= $up->getPageHeader();
268 if( $usersbody ) {
269 $s .= $up->getNavigationBar();
270 $s .= '<ul>' . $usersbody . '</ul>';
271 $s .= $up->getNavigationBar() ;
272 } else {
273 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
274 };
275 $s .= XML::closeElement( 'div' );
276 $wgOut->addHTML( $s );
277 }