(bug 5378) General logs link in Special:Contributions
[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 $batch = new LinkBatch;
61 while ( $row = $db->fetchObject( $res ) ) {
62 $batch->addObj( Title::makeTitleSafe( $row->namespace, $row->title ) );
63 }
64 $batch->execute();
65
66 // Back to start for display
67 if( $db->numRows( $res ) > 0 ) {
68 // If there are no rows we get an error seeking.
69 $db->dataSeek( $res, 0 );
70 }
71 }
72
73 /**
74 * Show a drop down list to select a group as well as a user name
75 * search box.
76 * @todo localize
77 */
78 function getPageHeader( ) {
79 global $wgScript;
80
81 // Various variables used for the form
82 $action = htmlspecialchars( $wgScript );
83 $title = Title::makeTitle( NS_SPECIAL, 'Listusers' );
84 $special = htmlspecialchars( $title->getPrefixedDBkey() );
85
86 // form header
87 $out = '<form method="get" action="'.$action.'">' .
88 '<input type="hidden" name="title" value="'.$special.'" />' .
89 wfMsgHtml( 'groups-editgroup-name' ) . '<select name="group">';
90
91 // get group names
92 $groups = User::getAllGroups();
93
94 // we want a default empty group
95 $out.= '<option value=""></option>';
96
97 // build the dropdown list menu using datas from the database
98 foreach ( $groups as $group ) {
99 $selected = ($group == $this->requestedGroup);
100 $out .= wfElement( 'option',
101 array_merge(
102 array( 'value' => $group ),
103 $selected ? array( 'selected' => 'selected' ) : array() ),
104 User::getGroupName( $group ) );
105 }
106 $out .= '</select> ';
107
108 $out .= wfMsgHtml( 'specialloguserlabel' ) . '<input type="text" name="username" /> ';
109
110 // OK button, end of form.
111 $out .= '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" /></form>';
112 // congratulations the form is now build
113 return $out;
114 }
115
116 function getSQL() {
117 $dbr =& wfGetDB( DB_SLAVE );
118 $user = $dbr->tableName( 'user' );
119 $user_groups = $dbr->tableName( 'user_groups' );
120
121 // We need to get an 'atomic' list of users, so that we
122 // don't break the list half-way through a user's group set
123 // and so that lists by group will show all group memberships.
124 //
125 // On MySQL 4.1 we could use GROUP_CONCAT to grab group
126 // assignments together with users pretty easily. On other
127 // versions, it's not so easy to do it consistently.
128 // For now we'll just grab the number of memberships, so
129 // we can then do targetted checks on those who are in
130 // non-default groups as we go down the list.
131
132 $userspace = NS_USER;
133 $sql = "SELECT 'Listusers' as type, $userspace AS namespace, user_name AS title, " .
134 "user_name as value, user_id, COUNT(ug_group) as numgroups " .
135 "FROM $user ".
136 "LEFT JOIN $user_groups ON user_id=ug_user " .
137 $this->userQueryWhere( $dbr ) .
138 " GROUP BY user_name";
139
140 return $sql;
141 }
142
143 function userQueryWhere( &$dbr ) {
144 $conds = $this->userQueryConditions();
145 return empty( $conds )
146 ? ""
147 : "WHERE " . $dbr->makeList( $conds, LIST_AND );
148 }
149
150 function userQueryConditions() {
151 $conds = array();
152 if( $this->requestedGroup != '' ) {
153 $conds['ug_group'] = $this->requestedGroup;
154 }
155 if( $this->requestedUser != '' ) {
156 $conds['user_name'] = $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
180 if( !isset( $result->numgroups ) || $result->numgroups > 0 ) {
181 $dbr =& wfGetDB( DB_SLAVE );
182 $result = $dbr->select( 'user_groups',
183 array( 'ug_group' ),
184 array( 'ug_user' => $result->user_id ),
185 'ListUsersPage::formatResult' );
186 $groups = array();
187 while( $row = $dbr->fetchObject( $result ) ) {
188 $groups[] = User::getGroupName( $row->ug_group );
189 }
190 $dbr->freeResult( $result );
191
192 if( count( $groups ) > 0 ) {
193 $name .= ' (' .
194 $skin->makeLink( wfMsgForContent( 'administrators' ),
195 htmlspecialchars( implode( ', ', $groups ) ) ) .
196 ')';
197 }
198 }
199
200 return $name;
201 }
202 }
203
204 /**
205 * constructor
206 * $par string (optional) A group to list users from
207 */
208 function wfSpecialListusers( $par = null ) {
209 global $wgRequest;
210
211 list( $limit, $offset ) = wfCheckLimits();
212
213
214 $slu = new ListUsersPage();
215
216 /**
217 * Get some parameters
218 */
219 $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
220 $slu->requestedGroup = $groupTarget;
221 $slu->requestedUser = $wgRequest->getVal('username');
222
223 return $slu->doQuery( $offset, $limit );
224 }
225
226 ?>