* Made IndexPager extend ContextSource
[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 $groups;
44
45 function __construct( RequestContext $context = null, $group = null ) {
46 global $wgActiveUserDays;
47
48 parent::__construct( $context );
49
50 $this->RCMaxAge = $wgActiveUserDays;
51 $un = $this->getRequest()->getText( 'username' );
52 $this->requestedUser = '';
53 if ( $un != '' ) {
54 $username = Title::makeTitleSafe( NS_USER, $un );
55 if( !is_null( $username ) ) {
56 $this->requestedUser = $username->getText();
57 }
58 }
59
60 $this->setupOptions();
61 }
62
63 public function setupOptions() {
64 $this->opts = new FormOptions();
65
66 $this->opts->add( 'hidebots', false, FormOptions::BOOL );
67 $this->opts->add( 'hidesysops', false, FormOptions::BOOL );
68
69 $this->opts->fetchValuesFromRequest( $this->getRequest() );
70
71 $this->groups = array();
72 if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
73 $this->groups['bot'] = true;
74 }
75 if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
76 $this->groups['sysop'] = true;
77 }
78 }
79
80 function getIndexField() {
81 return 'rc_user_text';
82 }
83
84 function getQueryInfo() {
85 $dbr = wfGetDB( DB_SLAVE );
86 $conds = array( 'rc_user > 0' ); // Users - no anons
87 $conds[] = 'ipb_deleted IS NULL'; // don't show hidden names
88 $conds[] = "rc_log_type IS NULL OR rc_log_type != 'newusers'";
89 $conds[] = "rc_timestamp >= '{$dbr->timestamp( wfTimestamp( TS_UNIX ) - $this->RCMaxAge*24*3600 )}'";
90
91 if( $this->requestedUser != '' ) {
92 $conds[] = 'rc_user_text >= ' . $dbr->addQuotes( $this->requestedUser );
93 }
94
95 $query = array(
96 'tables' => array( 'recentchanges', 'user', 'ipblocks' ),
97 'fields' => array( 'rc_user_text AS user_name', // inheritance
98 'rc_user_text', // for Pager
99 'user_id',
100 'COUNT(*) AS recentedits',
101 'MAX(ipb_user) AS blocked'
102 ),
103 'options' => array(
104 'GROUP BY' => 'rc_user_text, user_id',
105 'USE INDEX' => array( 'recentchanges' => 'rc_user_text' )
106 ),
107 'join_conds' => array(
108 'user' => array( 'INNER JOIN', 'rc_user_text=user_name' ),
109 'ipblocks' => array( 'LEFT JOIN', 'user_id=ipb_user AND ipb_auto=0 AND ipb_deleted=1' ),
110 ),
111 'conds' => $conds
112 );
113 return $query;
114 }
115
116 function formatRow( $row ) {
117 $userName = $row->user_name;
118
119 $ulinks = Linker::userLink( $row->user_id, $userName );
120 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
121
122 $list = array();
123 foreach( self::getGroups( $row->user_id ) as $group ) {
124 if ( isset( $this->groups[$group] ) ) {
125 return;
126 }
127 $list[] = self::buildGroupLink( $group );
128 }
129 $groups = $this->getLang()->commaList( $list );
130
131 $item = wfSpecialList( $ulinks, $groups );
132 $count = wfMsgExt( 'activeusers-count',
133 array( 'parsemag' ),
134 $this->getLang()->formatNum( $row->recentedits ),
135 $userName,
136 $this->getLang()->formatNum( $this->RCMaxAge )
137 );
138 $blocked = $row->blocked ? ' ' . wfMsgExt( 'listusers-blocked', array( 'parsemag' ), $userName ) : '';
139
140 return Html::rawElement( 'li', array(), "{$item} [{$count}]{$blocked}" );
141 }
142
143 function getPageHeader() {
144 global $wgScript;
145
146 $self = $this->getTitle();
147 $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
148
149 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); # Form tag
150 $out .= Xml::fieldset( wfMsg( 'activeusers' ) ) . "\n";
151 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
152
153 $out .= Xml::inputLabel( wfMsg( 'activeusers-from' ), 'username', 'offset', 20, $this->requestedUser ) . '<br />';# Username field
154
155 $out .= Xml::checkLabel( wfMsg('activeusers-hidebots'), 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ) );
156
157 $out .= Xml::checkLabel( wfMsg('activeusers-hidesysops'), 'hidesysops', 'hidesysops', $this->opts->getValue( 'hidesysops' ) ) . '<br />';
158
159 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n";# Submit button and form bottom
160 $out .= Xml::closeElement( 'fieldset' );
161 $out .= Xml::closeElement( 'form' );
162
163 return $out;
164 }
165 }
166
167 /**
168 * @ingroup SpecialPage
169 */
170 class SpecialActiveUsers extends SpecialPage {
171
172 /**
173 * Constructor
174 */
175 public function __construct() {
176 parent::__construct( 'Activeusers' );
177 }
178
179 /**
180 * Show the special page
181 *
182 * @param $par Mixed: parameter passed to the page or null
183 */
184 public function execute( $par ) {
185 global $wgActiveUserDays;
186
187 $this->setHeaders();
188 $this->outputHeader();
189
190 $up = new ActiveUsersPager( $this->getContext() );
191
192 # getBody() first to check, if empty
193 $usersbody = $up->getBody();
194
195 $s = Html::rawElement( 'div', array( 'class' => 'mw-activeusers-intro' ),
196 wfMsgExt( 'activeusers-intro', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $wgActiveUserDays ) )
197 );
198
199 $s .= $up->getPageHeader();
200 if( $usersbody ) {
201 $s .= $up->getNavigationBar();
202 $s .= Html::rawElement( 'ul', array(), $usersbody );
203 $s .= $up->getNavigationBar();
204 } else {
205 $s .= Html::element( 'p', array(), wfMsg( 'activeusers-noresult' ) );
206 }
207
208 $this->getOutput()->addHTML( $s );
209 }
210
211 }