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