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