Merge "phpcs: Fix WhiteSpace.LanguageConstructSpacing warnings"
[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 string $par 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 = $this->getDatabase();
95
96 $conds = array( 'rc_user > 0' ); // Users - no anons
97 $conds[] = 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' );
98 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes(
99 $dbr->timestamp( wfTimestamp( TS_UNIX ) - $this->RCMaxAge*24*3600 ) );
100
101 if ( $this->requestedUser != '' ) {
102 $conds[] = 'rc_user_text >= ' . $dbr->addQuotes( $this->requestedUser );
103 }
104
105 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
106 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
107 'ipblocks', '1', array( 'rc_user=ipb_user', 'ipb_deleted' => 1 )
108 ) . ')';
109 }
110
111 return array(
112 'tables' => 'recentchanges',
113 'fields' => array(
114 'user_name' => 'rc_user_text', // for Pager inheritance
115 'rc_user_text', // for Pager
116 'user_id' => 'MAX(rc_user)', // Postgres
117 'recentedits' => 'COUNT(*)'
118 ),
119 'options' => array(
120 'GROUP BY' => array( 'rc_user_text' ),
121 'USE INDEX' => array( 'recentchanges' => 'rc_user_text' )
122 ),
123 'join_conds' => array( // check for suppression blocks
124 'ipblocks' => array( 'LEFT JOIN',
125 array( 'rc_user=ipb_user', 'ipb_deleted' => 1 ) ),
126 ),
127 'conds' => $conds
128 );
129 }
130
131 function doBatchLookups() {
132 $uids = array();
133 foreach ( $this->mResult as $row ) {
134 $uids[] = $row->user_id;
135 }
136 // Fetch the block status of the user for showing "(blocked)" text and for
137 // striking out names of suppressed users when privileged user views the list.
138 // Although the first query already hits the block table for un-privileged, this
139 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
140 $dbr = $this->getDatabase();
141 $res = $dbr->select( 'ipblocks',
142 array( 'ipb_user', 'MAX(ipb_deleted) AS block_status' ),
143 array( 'ipb_user' => $uids ),
144 __METHOD__,
145 array( 'GROUP BY' => array( 'ipb_user' ) )
146 );
147 $this->blockStatusByUid = array();
148 foreach ( $res as $row ) {
149 $this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
150 }
151 $this->mResult->seek( 0 );
152 }
153
154 function formatRow( $row ) {
155 $userName = $row->user_name;
156
157 $ulinks = Linker::userLink( $row->user_id, $userName );
158 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
159
160 $lang = $this->getLanguage();
161
162 $list = array();
163 $user = User::newFromId( $row->user_id );
164
165 // User right filter
166 foreach( $this->hideRights as $right ) {
167 // Calling User::getRights() within the loop so that
168 // if the hideRights() filter is empty, we don't have to
169 // trigger the lazy-init of the big userrights array in the
170 // User object
171 if ( in_array( $right, $user->getRights() ) ) {
172 return '';
173 }
174 }
175
176 // User group filter
177 // Note: This is a different loop than for user rights,
178 // because we're reusing it to build the group links
179 // at the same time
180 foreach( $user->getGroups() as $group ) {
181 if ( in_array( $group, $this->hideGroups ) ) {
182 return '';
183 }
184 $list[] = self::buildGroupLink( $group, $userName );
185 }
186
187 $groups = $lang->commaList( $list );
188
189 $item = $lang->specialList( $ulinks, $groups );
190
191 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
192 if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
193 $item = "<span class=\"deleted\">$item</span>";
194 }
195 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
196 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
197 $blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
198
199 return Html::rawElement( 'li', array(), "{$item} [{$count}]{$blocked}" );
200 }
201
202 function getPageHeader() {
203 global $wgScript;
204
205 $self = $this->getTitle();
206 $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
207
208 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); # Form tag
209 $out .= Xml::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
210 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
211
212 $out .= Xml::inputLabel( $this->msg( 'activeusers-from' )->text(),
213 'username', 'offset', 20, $this->requestedUser ) . '<br />';# Username field
214
215 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
216 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ) );
217
218 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidesysops' )->text(),
219 'hidesysops', 'hidesysops', $this->opts->getValue( 'hidesysops' ) ) . '<br />';
220
221 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n";# Submit button and form bottom
222 $out .= Xml::closeElement( 'fieldset' );
223 $out .= Xml::closeElement( 'form' );
224
225 return $out;
226 }
227 }
228
229 /**
230 * @ingroup SpecialPage
231 */
232 class SpecialActiveUsers extends SpecialPage {
233
234 /**
235 * Constructor
236 */
237 public function __construct() {
238 parent::__construct( 'Activeusers' );
239 }
240
241 /**
242 * Show the special page
243 *
244 * @param $par Mixed: parameter passed to the page or null
245 */
246 public function execute( $par ) {
247 global $wgActiveUserDays;
248
249 $this->setHeaders();
250 $this->outputHeader();
251
252 $out = $this->getOutput();
253 $out->wrapWikiMsg( "<div class='mw-activeusers-intro'>\n$1\n</div>",
254 array( 'activeusers-intro', $this->getLanguage()->formatNum( $wgActiveUserDays ) ) );
255
256 $up = new ActiveUsersPager( $this->getContext(), null, $par );
257
258 # getBody() first to check, if empty
259 $usersbody = $up->getBody();
260
261 $out->addHTML( $up->getPageHeader() );
262 if ( $usersbody ) {
263 $out->addHTML(
264 $up->getNavigationBar() .
265 Html::rawElement( 'ul', array(), $usersbody ) .
266 $up->getNavigationBar()
267 );
268 } else {
269 $out->addWikiMsg( 'activeusers-noresult' );
270 }
271 }
272
273 protected function getGroupName() {
274 return 'users';
275 }
276 }