]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialListusers.php
MediaWiki 1.15.4-scripts
[autoinstallsdev/mediawiki.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 = $wgLang->commaList( $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, 'id' => 'mw-listusers-form' ) ) .
175                         Xml::fieldset( wfMsg( 'listusers' ) ) .
176                         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 .= Xml::closeElement( 'fieldset' ) .
201                         Xml::closeElement( 'form' );
202
203                 return $out;
204         }
205
206         /**
207          * Get a list of all explicit groups
208          * @return array
209          */
210         function getAllGroups() {
211                 $result = array();
212                 foreach( User::getAllGroups() as $group ) {
213                         $result[$group] = User::getGroupName( $group );
214                 }
215                 asort( $result );
216                 return $result;
217         }
218
219         /**
220          * Preserve group and username offset parameters when paging
221          * @return array
222          */
223         function getDefaultQuery() {
224                 $query = parent::getDefaultQuery();
225                 if( $this->requestedGroup != '' )
226                         $query['group'] = $this->requestedGroup;
227                 if( $this->requestedUser != '' )
228                         $query['username'] = $this->requestedUser;
229                 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
230                 return $query;
231         }
232
233         /**
234          * Get a list of groups the specified user belongs to
235          *
236          * @param int $uid
237          * @return array
238          */
239         protected static function getGroups( $uid ) {
240                 $user = User::newFromId( $uid );
241                 $groups = array_diff( $user->getEffectiveGroups(), $user->getImplicitGroups() );
242                 return $groups;
243         }
244
245         /**
246          * Format a link to a group description page
247          *
248          * @param string $group
249          * @return string
250          */
251         protected static function buildGroupLink( $group ) {
252                 static $cache = array();
253                 if( !isset( $cache[$group] ) )
254                         $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
255                 return $cache[$group];
256         }
257 }
258
259 /**
260  * constructor
261  * $par string (optional) A group to list users from
262  */
263 function wfSpecialListusers( $par = null ) {
264         global $wgRequest, $wgOut;
265
266         $up = new UsersPager($par);
267
268         # getBody() first to check, if empty
269         $usersbody = $up->getBody();
270         $s = XML::openElement( 'div', array('class' => 'mw-spcontent') );
271         $s .= $up->getPageHeader();
272         if( $usersbody ) {
273                 $s .=   $up->getNavigationBar();
274                 $s .=   '<ul>' . $usersbody . '</ul>';
275                 $s .=   $up->getNavigationBar() ;
276         } else {
277                 $s .=   '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
278         };
279         $s .= XML::closeElement( 'div' );
280         $wgOut->addHTML( $s );
281 }