/
GlobalContribsRepository.php
346 lines (304 loc) · 12.8 KB
/
GlobalContribsRepository.php
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
declare(strict_types = 1);
namespace App\Repository;
use App\Model\Project;
use App\Model\User;
use Doctrine\Persistence\ManagerRegistry;
use GuzzleHttp\Client;
use PDO;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Wikimedia\IPUtils;
/**
* A GlobalContribsRepository is responsible for retrieving information from the database for the GlobalContribs tool.
* @codeCoverageIgnore
*/
class GlobalContribsRepository extends Repository
{
protected ProjectRepository $projectRepo;
/** @var Project CentralAuth project (meta.wikimedia for WMF installation). */
protected Project $caProject;
public function __construct(
ManagerRegistry $managerRegistry,
CacheItemPoolInterface $cache,
Client $guzzle,
LoggerInterface $logger,
ParameterBagInterface $parameterBag,
bool $isWMF,
int $queryTimeout,
ProjectRepository $projectRepo,
string $centralAuthProject
) {
$this->caProject = new Project($centralAuthProject);
$this->projectRepo = $projectRepo;
$this->caProject->setRepository($this->projectRepo);
parent::__construct($managerRegistry, $cache, $guzzle, $logger, $parameterBag, $isWMF, $queryTimeout);
}
/**
* Get a user's edit count for each project.
* @see GlobalContribsRepository::globalEditCountsFromCentralAuth()
* @see GlobalContribsRepository::globalEditCountsFromDatabases()
* @param User $user The user.
* @return mixed[] Elements are arrays with 'project' (Project), and 'total' (int). Null if anon (too slow).
*/
public function globalEditCounts(User $user): ?array
{
if ($user->isAnon()) {
return null;
}
// Get the edit counts from CentralAuth or database.
$editCounts = $this->globalEditCountsFromCentralAuth($user);
// Pre-populate all projects' metadata, to prevent each project call from fetching it.
$this->caProject->getRepository()->getAll();
// Compile the output.
$out = [];
foreach ($editCounts as $editCount) {
$project = new Project($editCount['dbName']);
$project->setRepository($this->projectRepo);
// Make sure the project exists (new projects may not yet be on db replicas).
if ($project->exists()) {
$out[] = [
'dbName' => $editCount['dbName'],
'total' => $editCount['total'],
'project' => $project,
];
}
}
return $out;
}
/**
* Get a user's total edit count on one or more project.
* Requires the CentralAuth extension to be installed on the project.
* @param User $user The user.
* @return array|null Elements are arrays with 'dbName' (string), and 'total' (int). Null for logged out users.
*/
protected function globalEditCountsFromCentralAuth(User $user): ?array
{
if (true === $user->isAnon()) {
return null;
}
// Set up cache.
$cacheKey = $this->getCacheKey(func_get_args(), 'gc_globaleditcounts');
if ($this->cache->hasItem($cacheKey)) {
return $this->cache->getItem($cacheKey)->get();
}
$params = [
'meta' => 'globaluserinfo',
'guiprop' => 'editcount|merged',
'guiuser' => $user->getUsername(),
];
$result = $this->executeApiRequest($this->caProject, $params);
if (!isset($result['query']['globaluserinfo']['merged'])) {
return [];
}
$out = [];
foreach ($result['query']['globaluserinfo']['merged'] as $result) {
$out[] = [
'dbName' => $result['wiki'],
'total' => $result['editcount'],
];
}
// Cache and return.
return $this->setCache($cacheKey, $out);
}
/**
* Loop through the given dbNames and create Project objects for each.
* @param array $dbNames
* @return Project[] Keyed by database name.
*/
private function formatProjects(array $dbNames): array
{
$projects = [];
foreach ($dbNames as $dbName) {
$projects[$dbName] = $this->projectRepo->getProject($dbName);
}
return $projects;
}
/**
* Get all Projects on which the user has made at least one edit.
* @param User $user
* @return Project[]
*/
public function getProjectsWithEdits(User $user): array
{
if ($user->isAnon()) {
$dbNames = array_keys($this->getDbNamesAndActorIds($user));
} else {
$dbNames = [];
foreach ($this->globalEditCountsFromCentralAuth($user) as $projectMeta) {
if ($projectMeta['total'] > 0) {
$dbNames[] = $projectMeta['dbName'];
}
}
}
return $this->formatProjects($dbNames);
}
/**
* Get projects that the user has made at least one edit on, and the associated actor ID.
* @param User $user
* @param string[] $dbNames Loop over these projects instead of all of them.
* @return array Keys are database names, values are actor IDs.
*/
public function getDbNamesAndActorIds(User $user, ?array $dbNames = null): array
{
// Check cache.
$cacheKey = $this->getCacheKey(func_get_args(), 'gc_db_names_actor_ids');
if ($this->cache->hasItem($cacheKey)) {
return $this->cache->getItem($cacheKey)->get();
}
if (!$dbNames) {
$dbNames = array_column($this->caProject->getRepository()->getAll(), 'dbName');
}
if ($user->isIpRange()) {
$username = $user->getIpSubstringFromCidr().'%';
$whereClause = "actor_name LIKE :actor";
} else {
$username = $user->getUsername();
$whereClause = "actor_name = :actor";
}
$queriesBySlice = [];
foreach ($dbNames as $dbName) {
$slice = $this->getDbList()[$dbName];
// actor_revision table only includes users who have made at least one edit.
$actorTable = $this->getTableName($dbName, 'actor', 'revision');
$queriesBySlice[$slice][] = "SELECT '$dbName' AS `dbName`, actor_id " .
"FROM $actorTable WHERE $whereClause";
}
$actorIds = [];
foreach ($queriesBySlice as $slice => $queries) {
$sql = implode(' UNION ', $queries);
$resultQuery = $this->executeProjectsQuery($slice, $sql, [
'actor' => $username,
]);
while ($row = $resultQuery->fetchAssociative()) {
$actorIds[$row['dbName']] = (int)$row['actor_id'];
}
}
return $this->setCache($cacheKey, $actorIds);
}
/**
* Get revisions by this user across the given Projects.
* @param string[] $dbNames Database names of projects to iterate over.
* @param User $user The user.
* @param int|string $namespace Namespace ID or 'all' for all namespaces.
* @param int|false $start Unix timestamp or false.
* @param int|false $end Unix timestamp or false.
* @param int $limit The maximum number of revisions to fetch from each project.
* @param int|false $offset Unix timestamp. Used for pagination.
* @return array
*/
public function getRevisions(
array $dbNames,
User $user,
$namespace = 'all',
$start = false,
$end = false,
int $limit = 31, // One extra to know whether there should be another page.
$offset = false
): array {
// Check cache.
$cacheKey = $this->getCacheKey(func_get_args(), 'gc_revisions');
if ($this->cache->hasItem($cacheKey)) {
return $this->cache->getItem($cacheKey)->get();
}
// Just need any Connection to use the ->quote() method.
$quoteConn = $this->getProjectsConnection('s1');
$username = $quoteConn->quote($user->getUsername(), PDO::PARAM_STR);
// IP range handling.
$startIp = '';
$endIp = '';
if ($user->isIpRange()) {
[$startIp, $endIp] = IPUtils::parseRange($user->getUsername());
$startIp = $quoteConn->quote($startIp, PDO::PARAM_STR);
$endIp = $quoteConn->quote($endIp, PDO::PARAM_STR);
}
// Fetch actor IDs (for IP ranges, it strips trailing zeros and uses a LIKE query).
$actorIds = $this->getDbNamesAndActorIds($user, $dbNames);
if (!$actorIds) {
return [];
}
$namespaceCond = 'all' === $namespace
? ''
: 'AND page_namespace = '.(int)$namespace;
$revDateConditions = $this->getDateConditions($start, $end, $offset, 'revs.', 'rev_timestamp');
// Assemble queries.
$queriesBySlice = [];
$projectRepo = $this->caProject->getRepository();
foreach ($dbNames as $dbName) {
if (isset($actorIds[$dbName])) {
$revisionTable = $projectRepo->getTableName($dbName, 'revision');
$pageTable = $projectRepo->getTableName($dbName, 'page');
$commentTable = $projectRepo->getTableName($dbName, 'comment', 'revision');
$actorTable = $projectRepo->getTableName($dbName, 'actor', 'revision');
$tagTable = $projectRepo->getTableName($dbName, 'change_tag');
$tagDefTable = $projectRepo->getTableName($dbName, 'change_tag_def');
if ($user->isIpRange()) {
$ipcTable = $projectRepo->getTableName($dbName, 'ip_changes');
$ipcJoin = "JOIN $ipcTable ON revs.rev_id = ipc_rev_id";
$whereClause = "ipc_hex BETWEEN $startIp AND $endIp";
$username = 'actor_name';
} else {
$ipcJoin = '';
$whereClause = 'revs.rev_actor = '.$actorIds[$dbName];
}
$slice = $this->getDbList()[$dbName];
$queriesBySlice[$slice][] = "
SELECT
'$dbName' AS dbName,
revs.rev_id AS id,
revs.rev_timestamp AS timestamp,
UNIX_TIMESTAMP(revs.rev_timestamp) AS unix_timestamp,
revs.rev_minor_edit AS minor,
revs.rev_deleted AS deleted,
revs.rev_len AS length,
(CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change,
revs.rev_parent_id AS parent_id,
revs.rev_deleted AS rev_deleted,
$username AS username,
page.page_title,
page.page_namespace,
comment_text AS comment,
(
SELECT 1
FROM $tagTable
WHERE ct_rev_id = revs.rev_id
AND ct_tag_id = (
SELECT ctd_id
FROM $tagDefTable
WHERE ctd_name = 'mw-reverted'
)
LIMIT 1
) AS reverted
FROM $revisionTable AS revs
$ipcJoin
JOIN $pageTable AS page ON (rev_page = page_id)
JOIN $actorTable ON (actor_id = revs.rev_actor)
LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id)
LEFT OUTER JOIN $commentTable ON revs.rev_comment_id = comment_id
WHERE $whereClause
$namespaceCond
$revDateConditions";
}
}
// Re-assemble into UNIONed queries, executing as many per slice as possible.
$revisions = [];
foreach ($queriesBySlice as $slice => $queries) {
$sql = "SELECT * FROM ((\n" . join("\n) UNION (\n", $queries) . ")) a ORDER BY timestamp DESC LIMIT $limit";
$revisions = array_merge($revisions, $this->executeProjectsQuery($slice, $sql)->fetchAllAssociative());
}
// If there are more than $limit results, re-sort by timestamp.
if (count($revisions) > $limit) {
usort($revisions, function ($a, $b) {
if ($a['unix_timestamp'] === $b['unix_timestamp']) {
return 0;
}
return $a['unix_timestamp'] > $b['unix_timestamp'] ? -1 : 1;
});
// Truncate size to $limit.
$revisions = array_slice($revisions, 0, $limit);
}
// Cache and return.
return $this->setCache($cacheKey, $revisions);
}
}