1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xtools; |
4
|
|
|
|
5
|
|
|
use AppBundle\Helper\AutomatedEditsHelper; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DateTime; |
8
|
|
|
use Mediawiki\Api\SimpleRequest; |
9
|
|
|
|
10
|
|
|
class EditCounterRepository extends Repository |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get revision counts for the given user. |
15
|
|
|
* @param User $user The user. |
16
|
|
|
* @returns string[] With keys: 'deleted', 'live', 'total', 'first', 'last', '24h', '7d', '30d', |
17
|
|
|
* '365d', 'small', 'large', 'with_comments', and 'minor_edits'. |
18
|
|
|
*/ |
19
|
|
|
public function getRevisionCounts(Project $project, User $user) |
20
|
|
|
{ |
21
|
|
|
// Set up cache. |
22
|
|
|
$cacheKey = 'revisioncounts.' . $project->getDatabaseName() . '.' . $user->getUsername(); |
23
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
24
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Prepare the queries and execute them. |
28
|
|
|
$archiveTable = $this->getTableName($project->getDatabaseName(), 'archive'); |
29
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
30
|
|
|
$queries = [ |
31
|
|
|
'deleted' => "SELECT COUNT(ar_id) FROM $archiveTable |
32
|
|
|
WHERE ar_user = :userId", |
33
|
|
|
'live' => "SELECT COUNT(rev_id) FROM $revisionTable |
34
|
|
|
WHERE rev_user = :userId", |
35
|
|
|
'day' => "SELECT COUNT(rev_id) FROM $revisionTable |
36
|
|
|
WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY)", |
37
|
|
|
'week' => "SELECT COUNT(rev_id) FROM $revisionTable |
38
|
|
|
WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 WEEK)", |
39
|
|
|
'month' => "SELECT COUNT(rev_id) FROM $revisionTable |
40
|
|
|
WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 MONTH)", |
41
|
|
|
'year' => "SELECT COUNT(rev_id) FROM $revisionTable |
42
|
|
|
WHERE rev_user = :userId AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 YEAR)", |
43
|
|
|
'small' => "SELECT COUNT(rev_id) FROM $revisionTable |
44
|
|
|
WHERE rev_user = :userId AND rev_len < 20", |
45
|
|
|
'large' => "SELECT COUNT(rev_id) FROM $revisionTable |
46
|
|
|
WHERE rev_user = :userId AND rev_len > 1000", |
47
|
|
|
'with_comments' => "SELECT COUNT(rev_id) FROM $revisionTable |
48
|
|
|
WHERE rev_user = :userId AND rev_comment = ''", |
49
|
|
|
'minor' => "SELECT COUNT(rev_id) FROM $revisionTable |
50
|
|
|
WHERE rev_user = :userId AND rev_minor_edit = 1", |
51
|
|
|
'average_size' => "SELECT AVG(rev_len) FROM $revisionTable |
52
|
|
|
WHERE rev_user = :userId", |
53
|
|
|
]; |
54
|
|
|
$this->stopwatch->start($cacheKey); |
55
|
|
|
$revisionCounts = []; |
56
|
|
|
foreach ($queries as $varName => $query) { |
57
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($query); |
58
|
|
|
$userId = $user->getId($project); |
59
|
|
|
$resultQuery->bindParam("userId", $userId); |
60
|
|
|
$resultQuery->execute(); |
61
|
|
|
$val = $resultQuery->fetchColumn(); |
62
|
|
|
$revisionCounts[$varName] = $val ?: 0; |
63
|
|
|
$this->stopwatch->lap($cacheKey); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
67
|
|
|
$this->stopwatch->stop($cacheKey); |
68
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
69
|
|
|
->set($revisionCounts) |
70
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
71
|
|
|
$this->cache->save($cacheItem); |
72
|
|
|
|
73
|
|
|
return $revisionCounts; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the first and last revision dates (in MySQL YYYYMMDDHHMMSS format). |
78
|
|
|
* @return string[] With keys 'first' and 'last'. |
79
|
|
|
*/ |
80
|
|
|
public function getRevisionDates(Project $project, User $user) |
81
|
|
|
{ |
82
|
|
|
// Set up cache. |
83
|
|
|
$cacheKey = 'revisiondates.' . $project->getDatabaseName() . '.' . $user->getUsername(); |
84
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
85
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->stopwatch->start($cacheKey); |
89
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
90
|
|
|
$query = "(SELECT 'first' AS `key`, rev_timestamp AS `date` FROM $revisionTable |
91
|
|
|
WHERE rev_user = :userId ORDER BY rev_timestamp ASC LIMIT 1) |
92
|
|
|
UNION |
93
|
|
|
(SELECT 'last' AS `key`, rev_timestamp AS `date` FROM $revisionTable |
94
|
|
|
WHERE rev_user = :userId ORDER BY rev_timestamp DESC LIMIT 1)"; |
95
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($query); |
96
|
|
|
$userId = $user->getId($project); |
97
|
|
|
$resultQuery->bindParam("userId", $userId); |
98
|
|
|
$resultQuery->execute(); |
99
|
|
|
$result = $resultQuery->fetchAll(); |
100
|
|
|
$revisionDates = []; |
101
|
|
|
foreach ($result as $res) { |
102
|
|
|
$revisionDates[$res['key']] = $res['date']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
106
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
107
|
|
|
->set($revisionDates) |
108
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
109
|
|
|
$this->cache->save($cacheItem); |
110
|
|
|
$this->stopwatch->stop($cacheKey); |
111
|
|
|
return $revisionDates; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get page counts for the given user, both for live and deleted pages/revisions. |
116
|
|
|
* @param Project $project The project. |
117
|
|
|
* @param User $user The user. |
118
|
|
|
* @return int[] With keys: edited-live, edited-deleted, created-live, created-deleted. |
119
|
|
|
*/ |
120
|
|
|
public function getPageCounts(Project $project, User $user) |
121
|
|
|
{ |
122
|
|
|
// Set up cache. |
123
|
|
|
$cacheKey = 'pagecounts.'.$project->getDatabaseName().'.'.$user->getUsername(); |
124
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
125
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
126
|
|
|
} |
127
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
128
|
|
|
|
129
|
|
|
// Build and execute query. |
130
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
131
|
|
|
$archiveTable = $this->getTableName($project->getDatabaseName(), 'archive'); |
132
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare(" |
133
|
|
|
(SELECT 'edited-live' AS source, COUNT(DISTINCT rev_page) AS value |
134
|
|
|
FROM $revisionTable |
135
|
|
|
WHERE rev_user_text=:username) |
136
|
|
|
UNION |
137
|
|
|
(SELECT 'edited-deleted' AS source, COUNT(DISTINCT ar_page_id) AS value |
138
|
|
|
FROM $archiveTable |
139
|
|
|
WHERE ar_user_text=:username) |
140
|
|
|
UNION |
141
|
|
|
(SELECT 'created-live' AS source, COUNT(DISTINCT rev_page) AS value |
142
|
|
|
FROM $revisionTable |
143
|
|
|
WHERE rev_user_text=:username AND rev_parent_id=0) |
144
|
|
|
UNION |
145
|
|
|
(SELECT 'created-deleted' AS source, COUNT(DISTINCT ar_page_id) AS value |
146
|
|
|
FROM $archiveTable |
147
|
|
|
WHERE ar_user_text=:username AND ar_parent_id=0) |
148
|
|
|
"); |
149
|
|
|
$username = $user->getUsername(); |
150
|
|
|
$resultQuery->bindParam("username", $username); |
151
|
|
|
$resultQuery->execute(); |
152
|
|
|
$results = $resultQuery->fetchAll(); |
153
|
|
|
|
154
|
|
|
$pageCounts = array_combine( |
155
|
|
|
array_map(function ($e) { |
156
|
|
|
return $e['source']; |
157
|
|
|
}, $results), |
158
|
|
|
array_map(function ($e) { |
159
|
|
|
return $e['value']; |
160
|
|
|
}, $results) |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
164
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
165
|
|
|
->set($pageCounts) |
166
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
167
|
|
|
$this->cache->save($cacheItem); |
168
|
|
|
$this->stopwatch->stop($cacheKey); |
169
|
|
|
return $pageCounts; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get log totals for a user. |
174
|
|
|
* @param Project $project The project. |
175
|
|
|
* @param User $user The user. |
176
|
|
|
* @return integer[] Keys are "<log>-<action>" strings, values are counts. |
177
|
|
|
*/ |
178
|
|
|
public function getLogCounts(Project $project, User $user) |
179
|
|
|
{ |
180
|
|
|
// Set up cache. |
181
|
|
|
$cacheKey = 'logcounts.'.$project->getDatabaseName().'.'.$user->getUsername(); |
182
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
183
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
184
|
|
|
} |
185
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
186
|
|
|
|
187
|
|
|
// Query. |
188
|
|
|
$sql = "SELECT CONCAT(log_type, '-', log_action) AS source, COUNT(log_id) AS value |
189
|
|
|
FROM " . $this->getTableName($project->getDatabaseName(), 'logging') . " |
190
|
|
|
WHERE log_user = :userId |
191
|
|
|
GROUP BY log_type, log_action"; |
192
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
193
|
|
|
$userId = $user->getId($project); |
194
|
|
|
$resultQuery->bindParam('userId', $userId); |
195
|
|
|
$resultQuery->execute(); |
196
|
|
|
$results = $resultQuery->fetchAll(); |
197
|
|
|
$logCounts = array_combine( |
198
|
|
|
array_map(function ($e) { |
199
|
|
|
return $e['source']; |
200
|
|
|
}, $results), |
201
|
|
|
array_map(function ($e) { |
202
|
|
|
return $e['value']; |
203
|
|
|
}, $results) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
// Make sure there is some value for each of the wanted counts. |
207
|
|
|
$requiredCounts = [ |
208
|
|
|
'thanks-thank', |
209
|
|
|
'review-approve', |
210
|
|
|
'patrol-patrol', |
211
|
|
|
'block-block', |
212
|
|
|
'block-unblock', |
213
|
|
|
'protect-protect', |
214
|
|
|
'protect-unprotect', |
215
|
|
|
'move-move', |
216
|
|
|
'delete-delete', |
217
|
|
|
'delete-revision', |
218
|
|
|
'delete-restore', |
219
|
|
|
'import-import', |
220
|
|
|
'upload-upload', |
221
|
|
|
'upload-overwrite', |
222
|
|
|
]; |
223
|
|
|
foreach ($requiredCounts as $req) { |
224
|
|
|
if (!isset($logCounts[$req])) { |
225
|
|
|
$logCounts[$req] = 0; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Add Commons upload count, if applicable. |
230
|
|
|
$logCounts['files_uploaded_commons'] = 0; |
231
|
|
|
if ($this->isLabs()) { |
232
|
|
|
$sql = "SELECT COUNT(log_id) FROM commonswiki_p.logging_userindex |
233
|
|
|
WHERE log_type = 'upload' AND log_action = 'upload' AND log_user = :userId"; |
234
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
235
|
|
|
$resultQuery->bindParam('userId', $userId); |
236
|
|
|
$resultQuery->execute(); |
237
|
|
|
$logCounts['files_uploaded_commons'] = $resultQuery->fetchColumn(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
241
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
242
|
|
|
->set($logCounts) |
243
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
244
|
|
|
$this->cache->save($cacheItem); |
245
|
|
|
$this->stopwatch->stop($cacheKey); |
246
|
|
|
|
247
|
|
|
return $logCounts; |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get a user's total edit count on one or more project. |
252
|
|
|
* Requires the CentralAuth extension to be installed on the project. |
253
|
|
|
* |
254
|
|
|
* @param string $username The username. |
|
|
|
|
255
|
|
|
* @param Project $project The project to start from. |
256
|
|
|
* @return mixed[]|boolean Array of total edit counts, or false if none could be found. |
257
|
|
|
*/ |
258
|
|
|
public function getRevisionCountsAllProjects(User $user, Project $project) |
259
|
|
|
{ |
260
|
|
|
// Set up cache and stopwatch. |
261
|
|
|
$cacheKey = 'globalRevisionCounts.'.$user->getUsername(); |
262
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
263
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
264
|
|
|
} |
265
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
266
|
|
|
|
267
|
|
|
// Load all projects, so it doesn't have to request metadata about each one as it goes. |
268
|
|
|
$project->getRepository()->getAll(); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$api = $this->getMediawikiApi($project); |
271
|
|
|
$params = [ |
272
|
|
|
'meta' => 'globaluserinfo', |
273
|
|
|
'guiprop' => 'editcount|merged', |
274
|
|
|
'guiuser' => $user->getUsername(), |
275
|
|
|
]; |
276
|
|
|
$query = new SimpleRequest('query', $params); |
277
|
|
|
$result = $api->getRequest($query); |
278
|
|
|
$out = []; |
279
|
|
|
if (isset($result['query']['globaluserinfo']['merged'])) { |
280
|
|
|
foreach ($result['query']['globaluserinfo']['merged'] as $merged) { |
281
|
|
|
$proj = ProjectRepository::getProject($merged['wiki'], $this->container); |
282
|
|
|
$out[] = [ |
283
|
|
|
'project' => $proj, |
284
|
|
|
'total' => $merged['editcount'], |
285
|
|
|
]; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
if (empty($out)) { |
289
|
|
|
$out = $this->getRevisionCountsAllProjectsNoCentralAuth($user, $project, $cacheKey); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
293
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
294
|
|
|
->set($out) |
295
|
|
|
->expiresAfter(new DateInterval('PT10M')); |
296
|
|
|
$this->cache->save($cacheItem); |
297
|
|
|
$this->stopwatch->stop($cacheKey); |
298
|
|
|
|
299
|
|
|
return $out; |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get total edit counts for the top 10 projects for this user. |
304
|
|
|
* @param string $username The username. |
|
|
|
|
305
|
|
|
* @return string[] Elements are arrays with 'dbName', 'url', 'name', and 'total'. |
306
|
|
|
*/ |
307
|
|
|
protected function getRevisionCountsAllProjectsNoCentralAuth( |
308
|
|
|
User $user, |
309
|
|
|
Project $project, |
310
|
|
|
$stopwatchName |
311
|
|
|
) { |
312
|
|
|
$allProjects = $project->getRepository()->getAll(); |
|
|
|
|
313
|
|
|
$topEditCounts = []; |
314
|
|
|
foreach ($allProjects as $projectMeta) { |
315
|
|
|
$revisionTableName = $this->getTableName($projectMeta['dbName'], 'revision'); |
316
|
|
|
$sql = "SELECT COUNT(rev_id) FROM $revisionTableName WHERE rev_user_text=:username"; |
317
|
|
|
$stmt = $this->getProjectsConnection()->prepare($sql); |
318
|
|
|
$stmt->bindParam("username", $user->getUsername()); |
|
|
|
|
319
|
|
|
$stmt->execute(); |
320
|
|
|
$total = (int)$stmt->fetchColumn(); |
321
|
|
|
$project = ProjectRepository::getProject($projectMeta['dbName'], $this->container); |
322
|
|
|
$topEditCounts[] = [ |
323
|
|
|
'project' => $project, |
324
|
|
|
'total' => $total, |
325
|
|
|
]; |
326
|
|
|
$this->stopwatch->lap($stopwatchName); |
327
|
|
|
} |
328
|
|
|
return $topEditCounts; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the given user's total edit counts per namespace on the given project. |
333
|
|
|
* @param Project $project The project. |
334
|
|
|
* @param User $user The user. |
335
|
|
|
* @return integer[] Array keys are namespace IDs, values are the edit counts. |
336
|
|
|
*/ |
337
|
|
|
public function getNamespaceTotals(Project $project, User $user) |
338
|
|
|
{ |
339
|
|
|
// Cache? |
340
|
|
|
$userId = $user->getId($project); |
341
|
|
|
$cacheKey = "ec.namespacetotals.{$project->getDatabaseName()}.$userId"; |
342
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
343
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
344
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// Query. |
348
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
349
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
350
|
|
|
$sql = "SELECT page_namespace, COUNT(rev_id) AS total |
351
|
|
|
FROM $revisionTable r JOIN $pageTable p on r.rev_page = p.page_id |
352
|
|
|
WHERE r.rev_user = :id |
353
|
|
|
GROUP BY page_namespace"; |
354
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
355
|
|
|
$resultQuery->bindParam(":id", $userId); |
356
|
|
|
$resultQuery->execute(); |
357
|
|
|
$results = $resultQuery->fetchAll(); |
358
|
|
|
$namespaceTotals = array_combine(array_map(function ($e) { |
359
|
|
|
return $e['page_namespace']; |
360
|
|
|
}, $results), array_map(function ($e) { |
361
|
|
|
return $e['total']; |
362
|
|
|
}, $results)); |
363
|
|
|
|
364
|
|
|
// Cache and return. |
365
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
366
|
|
|
$cacheItem->set($namespaceTotals); |
367
|
|
|
$cacheItem->expiresAfter(new DateInterval('PT15M')); |
368
|
|
|
$this->cache->save($cacheItem); |
369
|
|
|
$this->stopwatch->stop($cacheKey); |
370
|
|
|
return $namespaceTotals; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Get revisions by this user. |
375
|
|
|
* @param Project $project |
376
|
|
|
* @param User $user |
377
|
|
|
* @param DateTime $oldest |
378
|
|
|
* @return array|mixed |
379
|
|
|
*/ |
380
|
|
|
public function getRevisions(Project $project, User $user, $oldest = null) |
381
|
|
|
{ |
382
|
|
|
$username = $user->getUsername(); |
383
|
|
|
$cacheKey = "globalcontribs.{$project->getDatabaseName()}.$username"; |
384
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
385
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
386
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
387
|
|
|
} |
388
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
389
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
390
|
|
|
$whereTimestamp = ($oldest instanceof DateTime) |
391
|
|
|
? ' AND rev_timestamp > '.$oldest->getTimestamp() |
392
|
|
|
: ''; |
393
|
|
|
$sql = "SELECT rev_id, rev_timestamp, UNIX_TIMESTAMP(rev_timestamp) AS unix_timestamp, " |
394
|
|
|
." rev_minor_edit, rev_deleted, rev_len, rev_parent_id, rev_comment, " |
395
|
|
|
." page_title, page_namespace " |
396
|
|
|
."FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)" |
397
|
|
|
." WHERE rev_user_text LIKE :username $whereTimestamp" |
398
|
|
|
." ORDER BY rev_timestamp DESC"; |
399
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
400
|
|
|
$resultQuery->bindParam(":username", $username); |
401
|
|
|
$resultQuery->execute(); |
402
|
|
|
$revisions = $resultQuery->fetchAll(); |
403
|
|
|
|
404
|
|
|
// Cache this. |
405
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
406
|
|
|
$cacheItem->set($revisions); |
407
|
|
|
$cacheItem->expiresAfter(new DateInterval('PT15M')); |
408
|
|
|
$this->cache->save($cacheItem); |
409
|
|
|
|
410
|
|
|
$this->stopwatch->stop($cacheKey); |
411
|
|
|
return $revisions; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Get data for a bar chart of monthly edit totals per namespace. |
416
|
|
|
* @param string $username The username. |
|
|
|
|
417
|
|
|
* @return string[] |
418
|
|
|
*/ |
419
|
|
View Code Duplication |
public function getMonthCounts(Project $project, User $user) |
|
|
|
|
420
|
|
|
{ |
421
|
|
|
$username = $user->getUsername(); |
422
|
|
|
$cacheKey = "monthcounts.$username"; |
423
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
424
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
425
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
429
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
430
|
|
|
$sql = |
431
|
|
|
"SELECT " |
432
|
|
|
. " YEAR(rev_timestamp) AS `year`," |
433
|
|
|
. " MONTH(rev_timestamp) AS `month`," |
434
|
|
|
. " page_namespace," |
435
|
|
|
. " COUNT(rev_id) AS `count` " |
436
|
|
|
. " FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)" |
437
|
|
|
. " WHERE rev_user_text = :username" |
438
|
|
|
. " GROUP BY YEAR(rev_timestamp), MONTH(rev_timestamp), page_namespace " |
439
|
|
|
. " ORDER BY rev_timestamp DESC"; |
440
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
441
|
|
|
$resultQuery->bindParam(":username", $username); |
442
|
|
|
$resultQuery->execute(); |
443
|
|
|
$totals = $resultQuery->fetchAll(); |
444
|
|
|
|
445
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
446
|
|
|
$cacheItem->expiresAfter(new DateInterval('PT10M')); |
447
|
|
|
$cacheItem->set($totals); |
448
|
|
|
$this->cache->save($cacheItem); |
449
|
|
|
|
450
|
|
|
$this->stopwatch->stop($cacheKey); |
451
|
|
|
return $totals; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Get yearly edit totals for this user, grouped by namespace. |
456
|
|
|
* @param Project $project |
457
|
|
|
* @param User $user |
458
|
|
|
* @return string[] ['<namespace>' => ['<year>' => 'total', ... ], ... ] |
459
|
|
|
*/ |
460
|
|
View Code Duplication |
public function getYearCounts(Project $project, User $user) |
|
|
|
|
461
|
|
|
{ |
462
|
|
|
$username = $user->getUsername(); |
463
|
|
|
$cacheKey = "yearcounts.$username"; |
464
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
465
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
466
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
470
|
|
|
$pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
471
|
|
|
$sql = "SELECT " |
472
|
|
|
. " YEAR(rev_timestamp) AS `year`," |
473
|
|
|
. " page_namespace," |
474
|
|
|
. " COUNT(rev_id) AS `count` " |
475
|
|
|
. " FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)" |
476
|
|
|
. " WHERE rev_user_text = :username" |
477
|
|
|
. " GROUP BY YEAR(rev_timestamp), page_namespace " |
478
|
|
|
. " ORDER BY rev_timestamp DESC "; |
479
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
480
|
|
|
$resultQuery->bindParam(":username", $username); |
481
|
|
|
$resultQuery->execute(); |
482
|
|
|
$totals = $resultQuery->fetchAll(); |
483
|
|
|
|
484
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
485
|
|
|
$cacheItem->set($totals); |
486
|
|
|
$cacheItem->expiresAfter(new DateInterval('P10M')); |
487
|
|
|
$this->cache->save($cacheItem); |
488
|
|
|
|
489
|
|
|
$this->stopwatch->stop($cacheKey); |
490
|
|
|
return $totals; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Get data for the timecard chart, with totals grouped by day and to the nearest two-hours. |
495
|
|
|
* @param Project $project |
496
|
|
|
* @param User $user |
497
|
|
|
* @return string[] |
498
|
|
|
*/ |
499
|
|
|
public function getTimeCard(Project $project, User $user) |
500
|
|
|
{ |
501
|
|
|
$username = $user->getUsername(); |
502
|
|
|
$cacheKey = "timecard.".$username; |
503
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
504
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
505
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$hourInterval = 2; |
509
|
|
|
$xCalc = "ROUND(HOUR(rev_timestamp)/$hourInterval) * $hourInterval"; |
510
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
511
|
|
|
$sql = "SELECT " |
512
|
|
|
. " DAYOFWEEK(rev_timestamp) AS `y`, " |
513
|
|
|
. " $xCalc AS `x`, " |
514
|
|
|
. " COUNT(rev_id) AS `r` " |
515
|
|
|
. " FROM $revisionTable" |
516
|
|
|
. " WHERE rev_user_text = :username" |
517
|
|
|
. " GROUP BY DAYOFWEEK(rev_timestamp), $xCalc "; |
518
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
519
|
|
|
$resultQuery->bindParam(":username", $username); |
520
|
|
|
$resultQuery->execute(); |
521
|
|
|
$totals = $resultQuery->fetchAll(); |
522
|
|
|
// Scale the radii: get the max, then scale each radius. |
523
|
|
|
// This looks inefficient, but there's a max of 72 elements in this array. |
524
|
|
|
$max = 0; |
525
|
|
|
foreach ($totals as $total) { |
526
|
|
|
$max = max($max, $total['r']); |
527
|
|
|
} |
528
|
|
|
foreach ($totals as &$total) { |
529
|
|
|
$total['r'] = round($total['r'] / $max * 100); |
530
|
|
|
} |
531
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
532
|
|
|
$cacheItem->expiresAfter(new DateInterval('PT10M')); |
533
|
|
|
$cacheItem->set($totals); |
534
|
|
|
$this->cache->save($cacheItem); |
535
|
|
|
|
536
|
|
|
$this->stopwatch->stop($cacheKey); |
537
|
|
|
return $totals; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Get a summary of automated edits made by the given user in their last 1000 edits. |
542
|
|
|
* Will cache the result for 10 minutes. |
543
|
|
|
* @param User $user The user. |
544
|
|
|
* @return integer[] Array of edit counts, keyed by all tool names from |
545
|
|
|
* app/config/semi_automated.yml |
546
|
|
|
* @TODO This currently uses AutomatedEditsHelper but that could probably be refactored. |
547
|
|
|
*/ |
548
|
|
|
public function countAutomatedRevisions(Project $project, User $user) |
549
|
|
|
{ |
550
|
|
|
$userId = $user->getId($project); |
551
|
|
|
$cacheKey = "automatedEdits.".$project->getDatabaseName().'.'.$userId; |
552
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
553
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
554
|
|
|
$this->log->debug("Using cache for $cacheKey"); |
555
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** @var AutomatedEditsHelper $automatedEditsHelper */ |
559
|
|
|
$automatedEditsHelper = $this->container->get("app.automated_edits_helper"); |
560
|
|
|
|
561
|
|
|
// Get the most recent 1000 edit summaries. |
562
|
|
|
$revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
563
|
|
|
$sql = "SELECT rev_comment FROM $revisionTable |
564
|
|
|
WHERE rev_user=:userId ORDER BY rev_timestamp DESC LIMIT 1000"; |
565
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
566
|
|
|
$resultQuery->bindParam("userId", $userId); |
567
|
|
|
$resultQuery->execute(); |
568
|
|
|
$results = $resultQuery->fetchAll(); |
569
|
|
|
$editCounts = []; |
570
|
|
|
foreach ($results as $result) { |
571
|
|
|
$toolName = $automatedEditsHelper->getTool($result['rev_comment']); |
572
|
|
|
if ($toolName) { |
573
|
|
|
if (!isset($editCounts[$toolName])) { |
574
|
|
|
$editCounts[$toolName] = 0; |
575
|
|
|
} |
576
|
|
|
$editCounts[$toolName]++; |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
arsort($editCounts); |
580
|
|
|
|
581
|
|
|
// Cache for 10 minutes. |
582
|
|
|
$this->log->debug("Saving $cacheKey to cache", [$editCounts]); |
583
|
|
|
$cacheItem = $this->cache->getItem($cacheKey); |
584
|
|
|
$cacheItem->set($editCounts); |
585
|
|
|
$cacheItem->expiresAfter(new DateInterval('PT10M')); |
586
|
|
|
$this->cache->save($cacheItem); |
587
|
|
|
|
588
|
|
|
$this->stopwatch->stop($cacheKey); |
589
|
|
|
return $editCounts; |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.