*only sanitize IPs, preserve mName
[lhc/web/wiklou.git] / includes / 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 *
26 * @addtogroup 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 * @addtogroup SpecialPage
35 */
36
37 class UsersPager extends AlphabeticPager {
38
39 function __construct($group=null) {
40 global $wgRequest;
41 $this->requestedGroup = $group != "" ? $group : $wgRequest->getVal( 'group' );
42 $un = $wgRequest->getText( 'username' );
43 $this->requestedUser = '';
44 if ( $un != '' ) {
45 $username = Title::makeTitleSafe( NS_USER, $un );
46 if( ! is_null( $username ) ) {
47 $this->requestedUser = $username->getText();
48 }
49 }
50 parent::__construct();
51 }
52
53
54 function getIndexField() {
55 return 'user_name';
56 }
57
58 function getQueryInfo() {
59 $conds=array();
60 // don't show hidden names
61 $conds[]='ipb_deleted IS NULL OR ipb_deleted = 0';
62 if ($this->requestedGroup != "") {
63 $conds['ug_group'] = $this->requestedGroup;
64 }
65 if ($this->requestedUser != "") {
66 $conds[] = 'user_name >= ' . wfGetDB()->addQuotes( $this->requestedUser );
67 }
68
69 list ($user,$user_groups,$ipblocks) = wfGetDB()->tableNamesN('user','user_groups','ipblocks');
70
71 return array(
72 'tables' => " $user LEFT JOIN $user_groups ON user_id=ug_user LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_auto=0 ",
73 'fields' => array('user_name',
74 'MAX(user_id) AS user_id',
75 'COUNT(ug_group) AS numgroups',
76 'MAX(ug_group) AS singlegroup'),
77 'options' => array('GROUP BY' => 'user_name'),
78 'conds' => $conds
79 );
80
81 }
82
83 function formatRow( $row ) {
84 $userPage = Title::makeTitle( NS_USER, $row->user_name );
85 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
86
87 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
88 $list = array();
89 foreach( self::getGroups( $row->user_id ) as $group )
90 $list[] = self::buildGroupLink( $group );
91 $groups = implode( ', ', $list );
92 } elseif( $row->numgroups == 1 ) {
93 $groups = self::buildGroupLink( $row->singlegroup );
94 } else {
95 $groups = '';
96 }
97
98 return '<li>' . wfSpecialList( $name, $groups ) . '</li>';
99 }
100
101 function getBody() {
102 if (!$this->mQueryDone) {
103 $this->doQuery();
104 }
105 $batch = new LinkBatch;
106 $db = $this->mDb;
107
108 $this->mResult->rewind();
109
110 while ( $row = $this->mResult->fetchObject() ) {
111 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
112 }
113 $batch->execute();
114 $this->mResult->rewind();
115 return parent::getBody();
116 }
117
118 function getPageHeader( ) {
119 global $wgRequest;
120 $self = $this->getTitle();
121
122 # Form tag
123 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $self->getLocalUrl() ) ) .
124 '<fieldset>' .
125 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
126
127 # Username field
128 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
129 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
130
131 if( $this->mLimit )
132 $out .= Xml::hidden( 'limit', $this->mLimit );
133
134 # Group drop-down list
135 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
136 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
137 Xml::option( wfMsg( 'group-all' ), '' ); # Item for "all groups"
138
139 $groups = User::getAllGroups();
140 foreach( $groups as $group ) {
141 $attribs = array( 'value' => $group );
142 $attribs['selected'] = ( $group == $this->requestedGroup ) ? 'selected' : '';
143 $out .= Xml::option( User::getGroupName( $group ), $attribs['value'], $attribs['selected'] );
144 }
145 $out .= Xml::closeElement( 'select' ) . ' ';
146
147 # Submit button and form bottom
148 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
149 '</fieldset>' .
150 Xml::closeElement( 'form' );
151
152 return $out;
153 }
154
155 /**
156 * Preserve group and username offset parameters when paging
157 * @return array
158 */
159 function getDefaultQuery() {
160 $query = parent::getDefaultQuery();
161 if( $this->requestedGroup != '' )
162 $query['group'] = $this->requestedGroup;
163 if( $this->requestedUser != '' )
164 $query['username'] = $this->requestedUser;
165 return $query;
166 }
167
168 /**
169 * Get a list of groups the specified user belongs to
170 *
171 * @param int $uid
172 * @return array
173 */
174 private static function getGroups( $uid ) {
175 $dbr = wfGetDB( DB_SLAVE );
176 $groups = array();
177 $res = $dbr->select( 'user_groups', 'ug_group', array( 'ug_user' => $uid ), __METHOD__ );
178 if( $res && $dbr->numRows( $res ) > 0 ) {
179 while( $row = $dbr->fetchObject( $res ) )
180 $groups[] = $row->ug_group;
181 $dbr->freeResult( $res );
182 }
183 return $groups;
184 }
185
186 /**
187 * Format a link to a group description page
188 *
189 * @param string $group
190 * @return string
191 */
192 private static function buildGroupLink( $group ) {
193 static $cache = array();
194 if( !isset( $cache[$group] ) )
195 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
196 return $cache[$group];
197 }
198 }
199
200 /**
201 * constructor
202 * $par string (optional) A group to list users from
203 */
204 function wfSpecialListusers( $par = null ) {
205 global $wgRequest, $wgOut;
206
207 list( $limit, $offset ) = wfCheckLimits();
208
209 $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
210
211 $up = new UsersPager($par);
212
213 # getBody() first to check, if empty
214 $usersbody = $up->getBody();
215 $s = $up->getPageHeader();
216 if( $usersbody ) {
217 $s .= $up->getNavigationBar();
218 $s .= '<ul>' . $usersbody . '</ul>';
219 $s .= $up->getNavigationBar() ;
220 } else {
221 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
222 };
223
224 $wgOut->addHTML( $s );
225 }
226
227 ?>