Completed
Branch master (715cbe)
by
unknown
51:55
created

ActiveUsersPager::formatRow()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 8
nop 1
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
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 string[]
40
	 */
41
	protected $groups;
42
43
	/**
44
	 * @var array
45
	 */
46
	private $blockStatusByUid;
47
48
	/**
49
	 * @param IContextSource $context
50
	 * @param FormOptions $opts
51
	 */
52
	function __construct( IContextSource $context = null, FormOptions $opts ) {
53
		parent::__construct( $context );
54
55
		$this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
0 ignored issues
show
Bug introduced by
The property RCMaxAge does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
		$this->requestedUser = '';
0 ignored issues
show
Bug introduced by
The property requestedUser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
57
58
		$un = $opts->getValue( 'username' );
59 View Code Duplication
		if ( $un != '' ) {
60
			$username = Title::makeTitleSafe( NS_USER, $un );
61
			if ( !is_null( $username ) ) {
62
				$this->requestedUser = $username->getText();
63
			}
64
		}
65
66
		$this->groups = $opts->getValue( 'groups' );
0 ignored issues
show
Documentation Bug introduced by
It seems like $opts->getValue('groups') of type * is incompatible with the declared type array<integer,string> of property $groups.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
	}
68
69
	function getIndexField() {
70
		return 'qcc_title';
71
	}
72
73
	function getQueryInfo() {
74
		$dbr = $this->getDatabase();
75
76
		$activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
77
		$timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
78
		$tables = [ 'querycachetwo', 'user', 'recentchanges' ];
79
		$conds = [
80
			'qcc_type' => 'activeusers',
81
			'qcc_namespace' => NS_USER,
82
			'user_name = qcc_title',
83
			'rc_user_text = qcc_title',
84
			'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
85
			'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
86
			'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
87
			'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
88
		];
89
		if ( $this->requestedUser != '' ) {
90
			$conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
91
		}
92
		if ( $this->groups !== [] ) {
93
			$tables[] = 'user_groups';
94
			$conds[] = 'ug_user = user_id';
95
			$conds['ug_group'] = $this->groups;
96
		}
97
		if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
98
			$conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
99
					'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
100
				) . ')';
101
		}
102
103
		if ( $dbr->implicitGroupby() ) {
104
			$options = [ 'GROUP BY' => [ 'qcc_title' ] ];
105
		} else {
106
			$options = [ 'GROUP BY' => [ 'user_name', 'user_id', 'qcc_title' ] ];
107
		}
108
109
		return [
110
			'tables' => $tables,
111
			'fields' => [ 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ],
112
			'options' => $options,
113
			'conds' => $conds
114
		];
115
	}
116
117
	function doBatchLookups() {
118
		parent::doBatchLookups();
119
120
		$uids = [];
121
		foreach ( $this->mResult as $row ) {
122
			$uids[] = $row->user_id;
123
		}
124
		// Fetch the block status of the user for showing "(blocked)" text and for
125
		// striking out names of suppressed users when privileged user views the list.
126
		// Although the first query already hits the block table for un-privileged, this
127
		// is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
128
		$dbr = $this->getDatabase();
129
		$res = $dbr->select( 'ipblocks',
130
			[ 'ipb_user', 'MAX(ipb_deleted) AS block_status' ],
131
			[ 'ipb_user' => $uids ],
132
			__METHOD__,
133
			[ 'GROUP BY' => [ 'ipb_user' ] ]
134
		);
135
		$this->blockStatusByUid = [];
136
		foreach ( $res as $row ) {
137
			$this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
138
		}
139
		$this->mResult->seek( 0 );
140
	}
141
142
	function formatRow( $row ) {
143
		$userName = $row->user_name;
144
145
		$ulinks = Linker::userLink( $row->user_id, $userName );
146
		$ulinks .= Linker::userToolLinks( $row->user_id, $userName );
147
148
		$lang = $this->getLanguage();
149
150
		$list = [];
151
		$user = User::newFromId( $row->user_id );
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
152
153
		$groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
154
		foreach ( $groups_list as $group ) {
155
			$list[] = self::buildGroupLink( $group, $userName );
156
		}
157
158
		$groups = $lang->commaList( $list );
159
160
		$item = $lang->specialList( $ulinks, $groups );
161
162
		$isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
163
		if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
164
			$item = "<span class=\"deleted\">$item</span>";
165
		}
166
		$count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
167
			->params( $userName )->numParams( $this->RCMaxAge )->escaped();
168
		$blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
169
170
		return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
171
	}
172
173
}
174