Remove never used stuff. There is some $rand = wfRandom calls that might
[lhc/web/wiklou.git] / includes / SpecialListusers.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
3 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
4 #
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21 /**
22 *
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26
27 /**
28 *
29 */
30 require_once('QueryPage.php');
31
32 /**
33 * This class is used to get a list of user. The ones with specials
34 * rights (sysop, bureaucrat, developer) will have them displayed
35 * next to their names.
36 *
37 * @package MediaWiki
38 * @subpackage SpecialPage
39 */
40 class ListUsersPage extends QueryPage {
41 var $requestedGroup = '';
42 var $requestedUser = '';
43
44 function getName() {
45 return 'Listusers';
46 }
47 function isSyndicated() { return false; }
48
49 /**
50 * Not expensive, this class won't work properly with the caching system anyway
51 */
52 function isExpensive() {
53 return false;
54 }
55
56 /**
57 * Fetch user page links and cache their existence
58 */
59 function preprocessResults( &$db, &$res ) {
60 global $wgLinkCache;
61
62 $batch = new LinkBatch;
63 while ( $row = $db->fetchObject( $res ) ) {
64 $batch->addObj( Title::makeTitleSafe( $row->namespace, $row->title ) );
65 }
66 $batch->execute( $wgLinkCache );
67
68 // Back to start for display
69 if( $db->numRows( $res ) > 0 ) {
70 // If there are no rows we get an error seeking.
71 $db->dataSeek( $res, 0 );
72 }
73 }
74
75 /**
76 * Show a drop down list to select a group as well as a user name
77 * search box.
78 * @todo localize
79 */
80 function getPageHeader( ) {
81 global $wgScript;
82
83 // Various variables used for the form
84 $action = htmlspecialchars( $wgScript );
85 $title = Title::makeTitle( NS_SPECIAL, 'Listusers' );
86 $special = htmlspecialchars( $title->getPrefixedDBkey() );
87
88 // form header
89 $out = '<form method="get" action="'.$action.'">' .
90 '<input type="hidden" name="title" value="'.$special.'" />' .
91 wfMsgHtml( 'groups-editgroup-name' ) . '<select name="group">';
92
93 // get all group names and IDs
94 $groups = User::getAllGroups();
95
96 // we want a default empty group
97 $out.= '<option value=""></option>';
98
99 // build the dropdown list menu using datas from the database
100 foreach ( $groups as $group ) {
101 $selected = ($group == $this->requestedGroup);
102 $out .= wfElement( 'option',
103 array_merge(
104 array( 'value' => $group ),
105 $selected ? array( 'selected' => 'selected' ) : array() ),
106 User::getGroupName( $group ) );
107 }
108 $out .= '</select> ';
109
110 $out .= wfMsgHtml( 'specialloguserlabel' ) . '<input type="text" name="username" /> ';
111
112 // OK button, end of form.
113 $out .= '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" /></form>';
114 // congratulations the form is now build
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
180 $userPage = Title::makeTitle( $result->namespace, $result->title );
181 $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
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[] = User::getGroupName( $row->ug_group );
192 }
193 $dbr->freeResult( $result );
194
195 if( count( $groups ) > 0 ) {
196 $name .= ' (' .
197 $skin->makeLink( wfMsgForContent( 'administrators' ),
198 htmlspecialchars( implode( ', ', $groups ) ) ) .
199 ')';
200 }
201 }
202
203 return $name;
204 }
205 }
206
207 /**
208 * constructor
209 * $par string (optional) A group to list users from
210 */
211 function wfSpecialListusers( $par = null ) {
212 global $wgRequest;
213
214 list( $limit, $offset ) = wfCheckLimits();
215
216
217 $slu = new ListUsersPage();
218
219 /**
220 * Get some parameters
221 */
222 $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
223 $slu->requestedGroup = $groupTarget;
224 $slu->requestedUser = $wgRequest->getVal('username');
225
226 return $slu->doQuery( $offset, $limit );
227 }
228
229 ?>