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