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