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