Some changes to Special:ListUsers and Special:ActiveUsers
[lhc/web/wiklou.git] / includes / specials / SpecialActiveusers.php
1 <?php
2 /**
3 * Implements Special:Activeusers
4 *
5 * Copyright © 2008 Aaron Schulz
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * This class is used to get a list of active users. The ones with specials
28 * rights (sysop, bureaucrat, developer) will have them displayed
29 * next to their names.
30 *
31 * @ingroup SpecialPage
32 */
33 class ActiveUsersPager extends UsersPager {
34
35 /**
36 * @var FormOptions
37 */
38 protected $opts;
39
40 /**
41 * @var Array
42 */
43 protected $hideGroups = array();
44
45 /**
46 * @var Array
47 */
48 protected $hideRights = array();
49
50 /**
51 * @param $context IContextSource
52 * @param $group null Unused
53 * @param $par string Parameter passed to the page
54 */
55 function __construct( IContextSource $context = null, $group = null, $par = null ) {
56 global $wgActiveUserDays;
57
58 parent::__construct( $context );
59
60 $this->RCMaxAge = $wgActiveUserDays;
61 $un = $this->getRequest()->getText( 'username', $par );
62 $this->requestedUser = '';
63 if ( $un != '' ) {
64 $username = Title::makeTitleSafe( NS_USER, $un );
65 if( !is_null( $username ) ) {
66 $this->requestedUser = $username->getText();
67 }
68 }
69
70 $this->setupOptions();
71 }
72
73 public function setupOptions() {
74 $this->opts = new FormOptions();
75
76 $this->opts->add( 'hidebots', false, FormOptions::BOOL );
77 $this->opts->add( 'hidesysops', false, FormOptions::BOOL );
78
79 $this->opts->fetchValuesFromRequest( $this->getRequest() );
80
81 if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
82 $this->hideRights[] = 'bot';
83 }
84 if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
85 $this->hideGroups[] = 'sysop';
86 }
87 }
88
89 function getIndexField() {
90 return 'rc_user_text';
91 }
92
93 function getQueryInfo() {
94 $dbr = wfGetDB( DB_SLAVE );
95 $conds = array( 'rc_user > 0' ); // Users - no anons
96 $conds[] = 'ipb_deleted IS NULL'; // don't show hidden names
97 $conds[] = 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' );
98 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX ) - $this->RCMaxAge*24*3600 ) );
99
100 if( $this->requestedUser != '' ) {
101 $conds[] = 'rc_user_text >= ' . $dbr->addQuotes( $this->requestedUser );
102 }
103
104 $query = array(
105 'tables' => array( 'recentchanges', 'user', 'ipblocks' ),
106 'fields' => array( 'user_name' => 'rc_user_text', // inheritance
107 'rc_user_text', // for Pager
108 'user_id',
109 'recentedits' => 'COUNT(*)',
110 'blocked' => 'MAX(ipb_user)'
111 ),
112 'options' => array(
113 'GROUP BY' => array( 'rc_user_text', 'user_id' ),
114 'USE INDEX' => array( 'recentchanges' => 'rc_user_text' )
115 ),
116 'join_conds' => array(
117 'user' => array( 'INNER JOIN', 'rc_user_text=user_name' ),
118 'ipblocks' => array( 'LEFT JOIN', array(
119 'user_id=ipb_user',
120 'ipb_auto' => 0,
121 'ipb_deleted' => 1
122 )),
123 ),
124 'conds' => $conds
125 );
126 return $query;
127 }
128
129 function formatRow( $row ) {
130 $userName = $row->user_name;
131
132 $ulinks = Linker::userLink( $row->user_id, $userName );
133 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
134
135 $lang = $this->getLanguage();
136
137 $list = array();
138 $user = User::newFromId( $row->user_id );
139
140 // User right filter
141 foreach( $this->hideRights as $right ) {
142 // Calling User::getRights() within the loop so that
143 // if the hideRights() filter is empty, we don't have to
144 // trigger the lazy-init of the big userrights array in the
145 // User object
146 if ( in_array( $right, $user->getRights() ) ) {
147 return '';
148 }
149 }
150
151 // User group filter
152 // Note: This is a different loop than for user rights,
153 // because we're reusing it to build the group links
154 // at the same time
155 foreach( $user->getGroups() as $group ) {
156 if ( in_array( $group, $this->hideGroups ) ) {
157 return '';
158 }
159 $list[] = self::buildGroupLink( $group, $userName );
160 }
161
162 $groups = $lang->commaList( $list );
163
164 $item = $lang->specialList( $ulinks, $groups );
165 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
166 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
167 $blocked = $row->blocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
168
169 return Html::rawElement( 'li', array(), "{$item} [{$count}]{$blocked}" );
170 }
171
172 function getPageHeader() {
173 global $wgScript;
174
175 $self = $this->getTitle();
176 $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
177
178 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); # Form tag
179 $out .= Xml::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
180 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
181
182 $out .= Xml::inputLabel( $this->msg( 'activeusers-from' )->text(),
183 'username', 'offset', 20, $this->requestedUser ) . '<br />';# Username field
184
185 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
186 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ) );
187
188 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidesysops' )->text(),
189 'hidesysops', 'hidesysops', $this->opts->getValue( 'hidesysops' ) ) . '<br />';
190
191 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n";# Submit button and form bottom
192 $out .= Xml::closeElement( 'fieldset' );
193 $out .= Xml::closeElement( 'form' );
194
195 return $out;
196 }
197 }
198
199 /**
200 * @ingroup SpecialPage
201 */
202 class SpecialActiveUsers extends SpecialPage {
203
204 /**
205 * Constructor
206 */
207 public function __construct() {
208 parent::__construct( 'Activeusers' );
209 }
210
211 /**
212 * Show the special page
213 *
214 * @param $par Mixed: parameter passed to the page or null
215 */
216 public function execute( $par ) {
217 global $wgActiveUserDays;
218
219 $this->setHeaders();
220 $this->outputHeader();
221
222 $out = $this->getOutput();
223 $out->wrapWikiMsg( "<div class='mw-activeusers-intro'>\n$1\n</div>",
224 array( 'activeusers-intro', $this->getLanguage()->formatNum( $wgActiveUserDays ) ) );
225
226 $up = new ActiveUsersPager( $this->getContext(), null, $par );
227
228 # getBody() first to check, if empty
229 $usersbody = $up->getBody();
230
231 $out->addHTML( $up->getPageHeader() );
232 if ( $usersbody ) {
233 $out->addHTML(
234 $up->getNavigationBar() .
235 Html::rawElement( 'ul', array(), $usersbody ) .
236 $up->getNavigationBar()
237 );
238 } else {
239 $out->addWikiMsg( 'activeusers-noresult' );
240 }
241 }
242
243 }