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