1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<?php
namespace MediaWiki\CheckUser;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\Subquery;
class CompareService extends ChangeService {
/** @var int */
private $limit;
/**
* @param ILoadBalancer $loadBalancer
* @param UserManager $userManager
* @param int $limit Maximum number of rows to access (T245499)
*/
public function __construct(
ILoadBalancer $loadBalancer,
UserManager $userManager,
$limit = 100000
) {
parent::__construct( $loadBalancer, $userManager );
$this->limit = $limit;
}
/**
* Get edits made from an ip
*
* @param string $ipHex
* @param string|null $excludeUser
* @return int
*/
public function getTotalEditsFromIp(
string $ipHex,
string $excludeUser = null
) : int {
$db = $this->loadBalancer->getConnectionRef( DB_REPLICA );
$conds = [
'cuc_ip_hex' => $ipHex,
'cuc_type' => [ RC_EDIT, RC_NEW ],
];
if ( $excludeUser ) {
$conds[] = 'cuc_user_text != ' . $db->addQuotes( $excludeUser );
}
return $db->selectRowCount( 'cu_changes', '*', $conds, __METHOD__ );
}
/**
* Get the compare query info
*
* @param string[] $targets
* @param string[] $excludeTargets
* @param string $start
* @return array
*/
public function getQueryInfo( array $targets, array $excludeTargets, string $start ): array {
$db = $this->loadBalancer->getConnectionRef( DB_REPLICA );
if ( $targets === [] ) {
throw new \LogicException( 'Cannot get query info when $targets is empty.' );
}
$limit = (int)( $this->limit / count( $targets ) );
$sqlText = [];
foreach ( $targets as $target ) {
$info = $this->getQueryInfoForSingleTarget( $target, $excludeTargets, $start, $limit );
if ( $info !== null ) {
if ( !$db->unionSupportsOrderAndLimit() ) {
unset( $info['options']['ORDER BY'], $info['options']['LIMIT'] );
}
$sqlText[] = $db->selectSQLText(
$info['tables'],
$info['fields'],
$info['conds'],
__METHOD__,
$info['options']
);
}
}
$derivedTable = $db->unionQueries( $sqlText, $db::UNION_DISTINCT );
return [
'tables' => [ 'a' => new Subquery( $derivedTable ) ],
'fields' => [
'cuc_user' => 'a.cuc_user',
'cuc_user_text' => 'a.cuc_user_text',
'cuc_ip' => 'a.cuc_ip',
'cuc_ip_hex' => 'a.cuc_ip_hex',
'cuc_agent' => 'a.cuc_agent',
'first_edit' => 'MIN(a.cuc_timestamp)',
'last_edit' => 'MAX(a.cuc_timestamp)',
'total_edits' => 'count(*)',
],
'options' => [
'GROUP BY' => [
'cuc_user_text',
'cuc_ip_hex',
'cuc_agent',
],
],
];
}
/**
* Get the query info for a single target.
*
* For the main investigation, this becomes a subquery that contributes to a derived
* table, used by getQueryInfo.
*
* For a limit check, this query is used to check whether the number of results for
* the target exceed the limit-per-target in getQueryInfo.
*
* @param string $target
* @param string[] $excludeTargets
* @param string $start
* @param int $limitPerTarget
* @param bool $limitCheck
* @return array|null Return null for invalid target
*/
public function getQueryInfoForSingleTarget(
string $target,
array $excludeTargets,
string $start,
int $limitPerTarget,
$limitCheck = false
) : ?array {
if ( $limitCheck ) {
$orderBy = null;
$offset = $limitPerTarget;
$limit = 1;
} else {
$orderBy = 'cuc_timestamp DESC';
$offset = null;
$limit = $limitPerTarget;
}
$conds = $this->buildTargetConds( $target );
if ( $conds === [] ) {
return null;
}
$conds = array_merge(
$conds,
$this->buildExcludeTargetsConds( $excludeTargets ),
$this->buildStartConds( $start )
);
$conds['cuc_type'] = [ RC_EDIT, RC_NEW ];
return [
'tables' => 'cu_changes',
'fields' => [
'cuc_id',
'cuc_user',
'cuc_user_text',
'cuc_ip',
'cuc_ip_hex',
'cuc_agent',
'cuc_timestamp',
],
'conds' => $conds,
'options' => [
'ORDER BY' => $orderBy,
'LIMIT' => $limit,
'OFFSET' => $offset,
],
];
}
/**
* Check if we have incomplete data for any of the targets.
*
* @param string[] $targets
* @param string[] $excludeTargets
* @param string $start
* @return string[]
*/
public function getTargetsOverLimit(
array $targets,
array $excludeTargets,
string $start
) : array {
if ( $targets === [] ) {
return $targets;
}
$db = $this->loadBalancer->getConnectionRef( DB_REPLICA );
// If the database does not support order and limit on a UNION
// then none of the targets can be over the limit.
if ( !$db->unionSupportsOrderAndLimit() ) {
return [];
}
$targetsOverLimit = [];
$offset = (int)( $this->limit / count( $targets ) );
foreach ( $targets as $target ) {
$info = $this->getQueryInfoForSingleTarget( $target, $excludeTargets, $start, $offset, true );
if ( $info !== null ) {
$limitCheck = $db->select(
$info['tables'],
$info['fields'],
$info['conds'],
__METHOD__,
$info['options']
);
if ( $limitCheck->numRows() > 0 ) {
$targetsOverLimit[] = $target;
}
}
}
return $targetsOverLimit;
}
}
|