Fix for r85114, see code reviev
[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 SpecialActiveUsers extends QueryPage {
34
35 /**
36 * @var FormOptions
37 */
38 protected $opts;
39
40 /**
41 * @var Array
42 */
43 protected $groups;
44
45 function __construct( $group = null ) {
46 global $wgRequest, $wgActiveUserDays;
47 $this->RCMaxAge = $wgActiveUserDays;
48 $un = $wgRequest->getText( 'username' );
49 $this->requestedUser = '';
50 if ( $un != '' ) {
51 $username = Title::makeTitleSafe( NS_USER, $un );
52 if( !is_null( $username ) ) {
53 $this->requestedUser = $username->getText();
54 }
55 }
56
57 $this->setupOptions();
58
59 parent::__construct( 'Activeusers' );
60 }
61
62 public function setupOptions() {
63 global $wgRequest;
64
65 $this->opts = new FormOptions();
66
67 $this->opts->add( 'hidebots', false, FormOptions::BOOL );
68 $this->opts->add( 'hidesysops', false, FormOptions::BOOL );
69 $this->opts->add( 'limit', 0, FormOptions::INT );
70 $this->opts->add( 'offset', 0, FormOptions::INT );
71
72 $this->opts->fetchValuesFromRequest( $wgRequest );
73
74 $this->groups = array();
75 if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
76 $this->groups['bot'] = true;
77 }
78 if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
79 $this->groups['sysop'] = true;
80 }
81 }
82
83 function getOrderFields() {
84 return array( 'rc_user_text', 'user_id' );
85 }
86
87 function getQueryInfo() {
88 $dbr = wfGetDB( DB_SLAVE );
89 $conds = array( 'rc_user > 0' ); // Users - no anons
90 $conds[] = 'ipb_deleted IS NULL'; // don't show hidden names
91 $conds[] = "rc_log_type IS NULL OR rc_log_type != 'newusers'";
92 $conds[] = "rc_timestamp >= '{$dbr->timestamp( wfTimestamp( TS_UNIX ) - $this->RCMaxAge*24*3600 )}'";
93
94 if( $this->requestedUser != '' ) {
95 $conds[] = 'rc_user_text >= ' . $dbr->addQuotes( $this->requestedUser );
96 }
97
98 $query = array(
99 'tables' => array( 'recentchanges', 'user', 'ipblocks' ),
100 'fields' => array( 'rc_user_text AS user_name', // inheritance
101 'rc_user_text', // for Pager
102 'user_id',
103 'COUNT(*) AS recentedits',
104 'MAX(ipb_user) AS blocked'
105 ),
106 'options' => array(
107 'GROUP BY' => 'rc_user_text, user_id',
108 'USE INDEX' => array( 'recentchanges' => 'rc_user_text' )
109 ),
110 'join_conds' => array(
111 'user' => array( 'INNER JOIN', 'rc_user_text=user_name' ),
112 'ipblocks' => array( 'LEFT JOIN', 'user_id=ipb_user AND ipb_auto=0 AND ipb_deleted=1' ),
113 ),
114 'conds' => $conds
115 );
116 return $query;
117 }
118
119 function formatResult( $skin, $row ) {
120 global $wgLang;
121 $userName = $row->user_name;
122
123 $ulinks = $skin->userLink( $row->user_id, $userName );
124 $ulinks .= $skin->userToolLinks( $row->user_id, $userName );
125
126 $list = array();
127 $user = User::newFromId( $row->user_id );
128 foreach( $user->getEffectiveGroups() as $group ) {
129 if ( isset( $this->groups[$group] ) ) {
130 return false;
131 }
132 if( in_array( $group, array( '*', 'user' ) ) ){
133 continue;
134 }
135 $list[] = SpecialListUsers::buildGroupLink( $group );
136 }
137 $groups = $wgLang->commaList( $list );
138
139 $item = wfSpecialList( $ulinks, $groups );
140 $count = wfMsgExt( 'activeusers-count',
141 array( 'parsemag' ),
142 $wgLang->formatNum( $row->recentedits ),
143 $userName,
144 $wgLang->formatNum ( $this->RCMaxAge )
145 );
146 $blocked = $row->blocked ? ' ' . wfMsgExt( 'listusers-blocked', array( 'parsemag' ), $userName ) : '';
147
148 return "{$item} [{$count}]{$blocked}";
149 }
150
151 function linkParameters() {
152 return array(
153 'hidebots' => isset( $this->groups['bot'] ),
154 'hidesysops' => isset( $this->groups['sysop'] ),
155 'username' => $this->requestedUser,
156 );
157 }
158
159 function getPageHeader() {
160 global $wgScript, $wgActiveUserDays, $wgLang;
161
162 $self = $this->getTitle();
163 $limit = $this->opts->getValue( 'limit' )
164 ? Html::hidden( 'limit', $this->opts->getValue( 'limit' ) )
165 : '';
166 $offset = $this->opts->getValue( 'offset' )
167 ? Html::hidden( 'offset', $this->opts->getValue( 'offset' ) )
168 : '';
169
170 $out = Html::rawElement( 'div', array( 'class' => 'mw-activeusers-intro' ),
171 wfMsgExt( 'activeusers-intro', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgActiveUserDays ) )
172 );
173
174 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); # Form tag
175 $out .= Xml::fieldset( wfMsg( 'activeusers' ) ) . "\n";
176 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . $offset . "\n";
177
178 $out .= Xml::inputLabel( wfMsg( 'activeusers-from' ), 'username', 'offset', 20, $this->requestedUser ) . '<br />';# Username field
179
180 $out .= Xml::checkLabel( wfMsg('activeusers-hidebots'), 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ) );
181
182 $out .= Xml::checkLabel( wfMsg('activeusers-hidesysops'), 'hidesysops', 'hidesysops', $this->opts->getValue( 'hidesysops' ) ) . '<br />';
183
184 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n";# Submit button and form bottom
185 $out .= Xml::closeElement( 'fieldset' );
186 $out .= Xml::closeElement( 'form' );
187
188 return $out;
189 }
190 }