Merge "Fix wrong @return type hints in Language::tsTo… methods"
[lhc/web/wiklou.git] / includes / specials / pagers / ActiveUsersPager.php
1 <?php
2 /**
3 * Copyright © 2008 Aaron Schulz
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Pager
22 */
23
24 /**
25 * This class is used to get a list of active users. The ones with specials
26 * rights (sysop, bureaucrat, developer) will have them displayed
27 * next to their names.
28 *
29 * @ingroup Pager
30 */
31 class ActiveUsersPager extends UsersPager {
32
33 /**
34 * @var FormOptions
35 */
36 protected $opts;
37
38 /**
39 * @var array
40 */
41 protected $hideGroups = [];
42
43 /**
44 * @var array
45 */
46 protected $hideRights = [];
47
48 /**
49 * @var array
50 */
51 private $blockStatusByUid;
52
53 /**
54 * @param IContextSource $context
55 * @param null $group Unused
56 * @param string $par Parameter passed to the page
57 */
58 function __construct( IContextSource $context = null, $group = null, $par = null ) {
59 parent::__construct( $context );
60
61 $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
62 $un = $this->getRequest()->getText( 'username', $par );
63 $this->requestedUser = '';
64 if ( $un != '' ) {
65 $username = Title::makeTitleSafe( NS_USER, $un );
66 if ( !is_null( $username ) ) {
67 $this->requestedUser = $username->getText();
68 }
69 }
70
71 $this->setupOptions();
72 }
73
74 public function setupOptions() {
75 $this->opts = new FormOptions();
76
77 $this->opts->add( 'hidebots', false, FormOptions::BOOL );
78 $this->opts->add( 'hidesysops', false, FormOptions::BOOL );
79
80 $this->opts->fetchValuesFromRequest( $this->getRequest() );
81
82 if ( $this->opts->getValue( 'hidebots' ) == 1 ) {
83 $this->hideRights[] = 'bot';
84 }
85 if ( $this->opts->getValue( 'hidesysops' ) == 1 ) {
86 $this->hideGroups[] = 'sysop';
87 }
88 }
89
90 function getIndexField() {
91 return 'qcc_title';
92 }
93
94 function getQueryInfo() {
95 $dbr = $this->getDatabase();
96
97 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
98 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
99 $conds = [
100 'qcc_type' => 'activeusers',
101 'qcc_namespace' => NS_USER,
102 'user_name = qcc_title',
103 'rc_user_text = qcc_title',
104 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
105 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
106 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
107 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
108 ];
109 if ( $this->requestedUser != '' ) {
110 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
111 }
112 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
113 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
114 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
115 ) . ')';
116 }
117
118 if ( $dbr->implicitGroupby() ) {
119 $options = [ 'GROUP BY' => [ 'qcc_title' ] ];
120 } else {
121 $options = [ 'GROUP BY' => [ 'user_name', 'user_id', 'qcc_title' ] ];
122 }
123
124 return [
125 'tables' => [ 'querycachetwo', 'user', 'recentchanges' ],
126 'fields' => [ 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ],
127 'options' => $options,
128 'conds' => $conds
129 ];
130 }
131
132 function doBatchLookups() {
133 parent::doBatchLookups();
134
135 $uids = [];
136 foreach ( $this->mResult as $row ) {
137 $uids[] = $row->user_id;
138 }
139 // Fetch the block status of the user for showing "(blocked)" text and for
140 // striking out names of suppressed users when privileged user views the list.
141 // Although the first query already hits the block table for un-privileged, this
142 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
143 $dbr = $this->getDatabase();
144 $res = $dbr->select( 'ipblocks',
145 [ 'ipb_user', 'MAX(ipb_deleted) AS block_status' ],
146 [ 'ipb_user' => $uids ],
147 __METHOD__,
148 [ 'GROUP BY' => [ 'ipb_user' ] ]
149 );
150 $this->blockStatusByUid = [];
151 foreach ( $res as $row ) {
152 $this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
153 }
154 $this->mResult->seek( 0 );
155 }
156
157 function formatRow( $row ) {
158 $userName = $row->user_name;
159
160 $ulinks = Linker::userLink( $row->user_id, $userName );
161 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
162
163 $lang = $this->getLanguage();
164
165 $list = [];
166 $user = User::newFromId( $row->user_id );
167
168 // User right filter
169 foreach ( $this->hideRights as $right ) {
170 // Calling User::getRights() within the loop so that
171 // if the hideRights() filter is empty, we don't have to
172 // trigger the lazy-init of the big userrights array in the
173 // User object
174 if ( in_array( $right, $user->getRights() ) ) {
175 return '';
176 }
177 }
178
179 // User group filter
180 // Note: This is a different loop than for user rights,
181 // because we're reusing it to build the group links
182 // at the same time
183 $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
184 foreach ( $groups_list as $group ) {
185 if ( in_array( $group, $this->hideGroups ) ) {
186 return '';
187 }
188 $list[] = self::buildGroupLink( $group, $userName );
189 }
190
191 $groups = $lang->commaList( $list );
192
193 $item = $lang->specialList( $ulinks, $groups );
194
195 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
196 if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
197 $item = "<span class=\"deleted\">$item</span>";
198 }
199 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
200 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
201 $blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
202
203 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
204 }
205
206 function getPageHeader() {
207 $self = $this->getTitle();
208 $limit = $this->mLimit ? Html::hidden( 'limit', $this->mLimit ) : '';
209
210 # Form tag
211 $out = Xml::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] );
212 $out .= Xml::fieldset( $this->msg( 'activeusers' )->text() ) . "\n";
213 $out .= Html::hidden( 'title', $self->getPrefixedDBkey() ) . $limit . "\n";
214
215 # Username field (with autocompletion support)
216 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
217 $out .= Xml::inputLabel(
218 $this->msg( 'activeusers-from' )->text(),
219 'username',
220 'offset',
221 20,
222 $this->requestedUser,
223 [
224 'class' => 'mw-ui-input-inline mw-autocomplete-user',
225 'tabindex' => 1,
226 ] + (
227 // Set autofocus on blank input
228 $this->requestedUser === '' ? [ 'autofocus' => '' ] : []
229 )
230 ) . '<br />';
231
232 $out .= Xml::checkLabel( $this->msg( 'activeusers-hidebots' )->text(),
233 'hidebots', 'hidebots', $this->opts->getValue( 'hidebots' ), [ 'tabindex' => 2 ] );
234
235 $out .= Xml::checkLabel(
236 $this->msg( 'activeusers-hidesysops' )->text(),
237 'hidesysops',
238 'hidesysops',
239 $this->opts->getValue( 'hidesysops' ),
240 [ 'tabindex' => 3 ]
241 ) . '<br />';
242
243 # Submit button and form bottom
244 $out .= Xml::submitButton(
245 $this->msg( 'activeusers-submit' )->text(),
246 [ 'tabindex' => 4 ]
247 ) . "\n";
248 $out .= Xml::closeElement( 'fieldset' );
249 $out .= Xml::closeElement( 'form' );
250
251 return $out;
252 }
253
254 }