Call useIndexClause, not raw FORCE INDEX.
[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
37 class UsersPager extends AlphabeticPager {
38
39 function __construct($group=null) {
40 global $wgRequest;
41 $this->requestedGroup = $group != "" ? $group : $wgRequest->getVal( 'group' );
42 $un = $wgRequest->getText( 'username' );
43 $this->requestedUser = '';
44 if ( $un != '' ) {
45 $username = Title::makeTitleSafe( NS_USER, $un );
46 if( ! is_null( $username ) ) {
47 $this->requestedUser = $username->getText();
48 }
49 }
50 parent::__construct();
51 }
52
53
54 function getIndexField() {
55 return 'user_name';
56 }
57
58 function getQueryInfo() {
59 $dbr = wfGetDB( DB_SLAVE );
60 $conds=array();
61 // don't show hidden names
62 $conds[]='ipb_deleted IS NULL OR ipb_deleted = 0';
63 if ($this->requestedGroup != "") {
64 $conds['ug_group'] = $this->requestedGroup;
65 $useIndex = '';
66 } else {
67 $useIndex = $dbr->useIndexClause('user_name');
68 }
69 if ($this->requestedUser != "") {
70 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
71 }
72
73 list ($user,$user_groups,$ipblocks) = $dbr->tableNamesN('user','user_groups','ipblocks');
74
75 $query = array(
76 'tables' => " $user $useIndex LEFT JOIN $user_groups ON user_id=ug_user
77 LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_auto=0 ",
78 'fields' => array('user_name',
79 'MAX(user_id) AS user_id',
80 'COUNT(ug_group) AS numgroups',
81 'MAX(ug_group) AS singlegroup'),
82 'options' => array('GROUP BY' => 'user_name'),
83 'conds' => $conds
84 );
85
86 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
87 return $query;
88 }
89
90 function formatRow( $row ) {
91 $userPage = Title::makeTitle( NS_USER, $row->user_name );
92 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
93
94 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
95 $list = array();
96 foreach( self::getGroups( $row->user_id ) as $group )
97 $list[] = self::buildGroupLink( $group );
98 $groups = implode( ', ', $list );
99 } elseif( $row->numgroups == 1 ) {
100 $groups = self::buildGroupLink( $row->singlegroup );
101 } else {
102 $groups = '';
103 }
104
105 $item = wfSpecialList( $name, $groups );
106 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
107 return "<li>{$item}</li>";
108 }
109
110 function getBody() {
111 if (!$this->mQueryDone) {
112 $this->doQuery();
113 }
114 $batch = new LinkBatch;
115
116 $this->mResult->rewind();
117
118 while ( $row = $this->mResult->fetchObject() ) {
119 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
120 }
121 $batch->execute();
122 $this->mResult->rewind();
123 return parent::getBody();
124 }
125
126 function getPageHeader( ) {
127 global $wgScript, $wgRequest;
128 $self = $this->getTitle();
129
130 # Form tag
131 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
132 '<fieldset>' .
133 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
134 $out .= Xml::hidden( 'title', $self->getPrefixedDbKey() );
135
136 # Username field
137 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
138 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
139
140 # Group drop-down list
141 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
142 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
143 Xml::option( wfMsg( 'group-all' ), '' );
144 foreach( $this->getAllGroups() as $group => $groupText )
145 $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
146 $out .= Xml::closeElement( 'select' ) . ' ';
147
148 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
149
150 # Submit button and form bottom
151 if( $this->mLimit )
152 $out .= Xml::hidden( 'limit', $this->mLimit );
153 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
154 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
155 $out .= '</fieldset>' .
156 Xml::closeElement( 'form' );
157
158 return $out;
159 }
160
161 function getAllGroups() {
162 $result = array();
163 foreach( User::getAllGroups() as $group ) {
164 $result[$group] = User::getGroupName( $group );
165 }
166 return $result;
167 }
168
169 /**
170 * Preserve group and username offset parameters when paging
171 * @return array
172 */
173 function getDefaultQuery() {
174 $query = parent::getDefaultQuery();
175 if( $this->requestedGroup != '' )
176 $query['group'] = $this->requestedGroup;
177 if( $this->requestedUser != '' )
178 $query['username'] = $this->requestedUser;
179 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
180 return $query;
181 }
182
183 /**
184 * Get a list of groups the specified user belongs to
185 *
186 * @param int $uid
187 * @return array
188 */
189 protected static function getGroups( $uid ) {
190 $dbr = wfGetDB( DB_SLAVE );
191 $groups = array();
192 $res = $dbr->select( 'user_groups', 'ug_group', array( 'ug_user' => $uid ), __METHOD__ );
193 if( $res && $dbr->numRows( $res ) > 0 ) {
194 while( $row = $dbr->fetchObject( $res ) )
195 $groups[] = $row->ug_group;
196 $dbr->freeResult( $res );
197 }
198 return $groups;
199 }
200
201 /**
202 * Format a link to a group description page
203 *
204 * @param string $group
205 * @return string
206 */
207 protected static function buildGroupLink( $group ) {
208 static $cache = array();
209 if( !isset( $cache[$group] ) )
210 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
211 return $cache[$group];
212 }
213 }
214
215 /**
216 * constructor
217 * $par string (optional) A group to list users from
218 */
219 function wfSpecialListusers( $par = null ) {
220 global $wgRequest, $wgOut;
221
222 $up = new UsersPager($par);
223
224 # getBody() first to check, if empty
225 $usersbody = $up->getBody();
226 $s = $up->getPageHeader();
227 if( $usersbody ) {
228 $s .= $up->getNavigationBar();
229 $s .= '<ul>' . $usersbody . '</ul>';
230 $s .= $up->getNavigationBar() ;
231 } else {
232 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
233 };
234
235 $wgOut->addHTML( $s );
236 }