dd220865eb7bbab549aa439804b8d77e09528c6e
[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 * @package MediaWiki
27 * @subpackage SpecialPage
28 */
29
30 /**
31 * This class is used to get a list of user. The ones with specials
32 * rights (sysop, bureaucrat, developer) will have them displayed
33 * next to their names.
34 *
35 * @package MediaWiki
36 * @subpackage SpecialPage
37 */
38 class ListUsersPage extends QueryPage {
39 var $requestedGroup = '';
40 var $requestedUser = '';
41
42 function getName() {
43 return 'Listusers';
44 }
45 function isSyndicated() { return false; }
46
47 /**
48 * Not expensive, this class won't work properly with the caching system anyway
49 */
50 function isExpensive() {
51 return false;
52 }
53
54 /**
55 * Fetch user page links and cache their existence
56 */
57 function preprocessResults( &$db, &$res ) {
58 $batch = new LinkBatch;
59 while ( $row = $db->fetchObject( $res ) ) {
60 $batch->addObj( Title::makeTitleSafe( $row->namespace, $row->title ) );
61 }
62 $batch->execute();
63
64 // Back to start for display
65 if( $db->numRows( $res ) > 0 ) {
66 // If there are no rows we get an error seeking.
67 $db->dataSeek( $res, 0 );
68 }
69 }
70
71 /**
72 * Show a drop down list to select a group as well as a user name
73 * search box.
74 * @todo localize
75 */
76 function getPageHeader( ) {
77 $self = $this->getTitle();
78
79 # Form tag
80 $out = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
81
82 # Group drop-down list
83 $out .= wfElement( 'label', array( 'for' => 'group' ), wfMsg( 'group' ) ) . ' ';
84 $out .= wfOpenElement( 'select', array( 'name' => 'group', 'id' => 'group' ) );
85 $out .= wfElement( 'option', array( 'value' => '' ), wfMsg( 'group-all' ) ); # Item for "all groups"
86 $groups = User::getAllGroups();
87 foreach( $groups as $group ) {
88 $attribs = array( 'value' => $group );
89 if( $group == $this->requestedGroup )
90 $attribs['selected'] = 'selected';
91 $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) );
92 }
93 $out .= wfCloseElement( 'select' ) . ' ';;# . wfElement( 'br' );
94
95 # Username field
96 $out .= wfElement( 'label', array( 'for' => 'username' ), wfMsg( 'listusersfrom' ) ) . ' ';
97 $out .= wfElement( 'input', array( 'type' => 'text', 'id' => 'username', 'name' => 'username',
98 'value' => $this->requestedUser ) ) . ' ';
99
100 # Preserve offset and limit
101 if( $this->offset )
102 $out .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'offset', 'value' => $this->offset ) );
103 if( $this->limit )
104 $out .= wfElement( 'input', array( 'type' => 'hidden', 'name' => 'limit', 'value' => $this->limit ) );
105
106 # Submit button and form bottom
107 $out .= wfElement( 'input', array( 'type' => 'submit', 'value' => wfMsg( 'allpagessubmit' ) ) );
108 $out .= wfCloseElement( 'form' );
109
110 return $out;
111 }
112
113 function getSQL() {
114 global $wgDBtype;
115 $dbr =& wfGetDB( DB_SLAVE );
116 $user = $dbr->tableName( 'user' );
117 $user_groups = $dbr->tableName( 'user_groups' );
118
119 // We need to get an 'atomic' list of users, so that we
120 // don't break the list half-way through a user's group set
121 // and so that lists by group will show all group memberships.
122 //
123 // On MySQL 4.1 we could use GROUP_CONCAT to grab group
124 // assignments together with users pretty easily. On other
125 // versions, it's not so easy to do it consistently.
126 // For now we'll just grab the number of memberships, so
127 // we can then do targetted checks on those who are in
128 // non-default groups as we go down the list.
129
130 $userspace = NS_USER;
131 $sql = "SELECT 'Listusers' as type, $userspace AS namespace, user_name AS title, " .
132 "user_name as value, user_id, COUNT(ug_group) as numgroups " .
133 "FROM $user ".
134 "LEFT JOIN $user_groups ON user_id=ug_user " .
135 $this->userQueryWhere( $dbr ) .
136 " GROUP BY user_name";
137 if ( $wgDBtype != 'mysql' ) {
138 $sql .= ",user_id";
139 }
140 return $sql;
141 }
142
143 function userQueryWhere( &$dbr ) {
144 $conds = $this->userQueryConditions( $dbr );
145 return empty( $conds )
146 ? ""
147 : "WHERE " . $dbr->makeList( $conds, LIST_AND );
148 }
149
150 function userQueryConditions( $dbr ) {
151 $conds = array();
152 if( $this->requestedGroup != '' ) {
153 $conds['ug_group'] = $this->requestedGroup;
154 }
155 if( $this->requestedUser != '' ) {
156 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
157 }
158 return $conds;
159 }
160
161 function linkParameters() {
162 $conds = array();
163 if( $this->requestedGroup != '' ) {
164 $conds['group'] = $this->requestedGroup;
165 }
166 if( $this->requestedUser != '' ) {
167 $conds['username'] = $this->requestedUser;
168 }
169 return $conds;
170 }
171
172 function sortDescending() {
173 return false;
174 }
175
176 function formatResult( $skin, $result ) {
177 $userPage = Title::makeTitle( $result->namespace, $result->title );
178 $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
179 $groups = null;
180
181 if( !isset( $result->numgroups ) || $result->numgroups > 0 ) {
182 $dbr =& wfGetDB( DB_SLAVE );
183 $result = $dbr->select( 'user_groups',
184 array( 'ug_group' ),
185 array( 'ug_user' => $result->user_id ),
186 'ListUsersPage::formatResult' );
187 $groups = array();
188 while( $row = $dbr->fetchObject( $result ) ) {
189 $groups[$row->ug_group] = User::getGroupMember( $row->ug_group );
190 }
191 $dbr->freeResult( $result );
192
193 if( count( $groups ) > 0 ) {
194 foreach( $groups as $group => $desc ) {
195 $list[] = User::makeGroupLinkHTML( $group, $desc );
196 }
197 $groups = implode( ', ', $list );
198 } else {
199 $groups = '';
200 }
201
202 }
203
204 return wfSpecialList( $name, $groups );
205 }
206 }
207
208 /**
209 * constructor
210 * $par string (optional) A group to list users from
211 */
212 function wfSpecialListusers( $par = null ) {
213 global $wgRequest;
214
215 list( $limit, $offset ) = wfCheckLimits();
216
217
218 $slu = new ListUsersPage();
219
220 /**
221 * Get some parameters
222 */
223 $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
224 $slu->requestedGroup = $groupTarget;
225
226 # 'Validate' the username first
227 $username = $wgRequest->getText( 'username', '' );
228 $user = User::newFromName( $username );
229 $slu->requestedUser = is_object( $user ) ? $user->getName() : '';
230
231 return $slu->doQuery( $offset, $limit );
232 }
233
234 ?>