|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Model; |
|
6
|
|
|
|
|
7
|
|
|
use App\Repository\AutoEditsRepository; |
|
8
|
|
|
use App\Repository\EditRepository; |
|
9
|
|
|
use App\Repository\PageRepository; |
|
10
|
|
|
use App\Repository\UserRepository; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* AutoEdits returns statistics about automated edits made by a user. |
|
14
|
|
|
*/ |
|
15
|
|
|
class AutoEdits extends Model |
|
16
|
|
|
{ |
|
17
|
|
|
protected EditRepository $editRepo; |
|
18
|
|
|
protected PageRepository $pageRepo; |
|
19
|
|
|
protected UserRepository $userRepo; |
|
20
|
|
|
|
|
21
|
|
|
/** @var null|string The tool we're searching for when fetching (semi-)automated edits. */ |
|
22
|
|
|
protected ?string $tool; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Edit[] The list of non-automated contributions. */ |
|
25
|
|
|
protected array $nonAutomatedEdits; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Edit[] The list of automated contributions. */ |
|
28
|
|
|
protected array $automatedEdits; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int Total number of edits. */ |
|
31
|
|
|
protected int $editCount; |
|
32
|
|
|
|
|
33
|
|
|
/** @var int Total number of non-automated edits. */ |
|
34
|
|
|
protected int $automatedCount; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array Counts of known automated tools used by the given user. */ |
|
37
|
|
|
protected array $toolCounts; |
|
38
|
|
|
|
|
39
|
|
|
/** @var int Total number of edits made with the tools. */ |
|
40
|
|
|
protected int $toolsTotal; |
|
41
|
|
|
|
|
42
|
|
|
/** @var int Default number of results to show per page when fetching (non-)automated edits. */ |
|
43
|
|
|
public const RESULTS_PER_PAGE = 50; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor for the AutoEdits class. |
|
47
|
|
|
* @param AutoEditsRepository $repository |
|
48
|
|
|
* @param EditRepository $editRepo |
|
49
|
|
|
* @param PageRepository $pageRepo |
|
50
|
|
|
* @param UserRepository $userRepo |
|
51
|
|
|
* @param Project $project |
|
52
|
|
|
* @param User $user |
|
53
|
|
|
* @param int|string $namespace Namespace ID or 'all' |
|
54
|
|
|
* @param false|int $start Start date as Unix timestamp. |
|
55
|
|
|
* @param false|int $end End date as Unix timestamp. |
|
56
|
|
|
* @param null $tool The tool we're searching for when fetching (semi-)automated edits. |
|
|
|
|
|
|
57
|
|
|
* @param false|int $offset Unix timestamp. Used for pagination. |
|
58
|
|
|
* @param int|null $limit Number of results to return. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
AutoEditsRepository $repository, |
|
62
|
|
|
EditRepository $editRepo, |
|
63
|
|
|
PageRepository $pageRepo, |
|
64
|
|
|
UserRepository $userRepo, |
|
65
|
|
|
Project $project, |
|
66
|
|
|
User $user, |
|
67
|
|
|
$namespace = 0, |
|
68
|
|
|
$start = false, |
|
69
|
|
|
$end = false, |
|
70
|
|
|
$tool = null, |
|
71
|
|
|
$offset = false, |
|
72
|
|
|
?int $limit = self::RESULTS_PER_PAGE |
|
73
|
|
|
) { |
|
74
|
|
|
$this->repository = $repository; |
|
75
|
|
|
$this->editRepo = $editRepo; |
|
76
|
|
|
$this->pageRepo = $pageRepo; |
|
77
|
|
|
$this->userRepo = $userRepo; |
|
78
|
|
|
$this->project = $project; |
|
79
|
|
|
$this->user = $user; |
|
80
|
|
|
$this->namespace = $namespace; |
|
81
|
|
|
$this->start = $start; |
|
82
|
|
|
$this->end = $end; |
|
83
|
|
|
$this->tool = $tool; |
|
84
|
|
|
$this->offset = $offset; |
|
85
|
|
|
$this->limit = $limit ?? self::RESULTS_PER_PAGE; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* The tool we're limiting the results to when fetching |
|
90
|
|
|
* (semi-)automated contributions. |
|
91
|
|
|
* @return null|string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getTool(): ?string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->tool; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the raw edit count of the user. |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getEditCount(): int |
|
103
|
|
|
{ |
|
104
|
|
|
if (!isset($this->editCount)) { |
|
105
|
|
|
$this->editCount = $this->user->countEdits( |
|
|
|
|
|
|
106
|
|
|
$this->project, |
|
107
|
|
|
$this->namespace, |
|
108
|
|
|
$this->start, |
|
109
|
|
|
$this->end |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->editCount; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the number of edits this user made using semi-automated tools. |
|
118
|
|
|
* This is not the same as self::getToolCounts because the regex can overlap. |
|
119
|
|
|
* @return int Result of query, see below. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getAutomatedCount(): int |
|
122
|
|
|
{ |
|
123
|
|
|
if (isset($this->automatedCount)) { |
|
124
|
|
|
return $this->automatedCount; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->automatedCount = $this->repository->countAutomatedEdits( |
|
|
|
|
|
|
128
|
|
|
$this->project, |
|
129
|
|
|
$this->user, |
|
130
|
|
|
$this->namespace, |
|
131
|
|
|
$this->start, |
|
132
|
|
|
$this->end |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
return $this->automatedCount; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get the percentage of all edits made using automated tools. |
|
140
|
|
|
* @return float |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAutomatedPercentage(): float |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->getEditCount() > 0 |
|
145
|
|
|
? ($this->getAutomatedCount() / $this->getEditCount()) * 100 |
|
146
|
|
|
: 0; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get non-automated contributions for this user. |
|
151
|
|
|
* @param bool $raw Wether to return raw data from the database, or get Edit objects. |
|
152
|
|
|
* @return string[]|Edit[] |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getNonAutomatedEdits(bool $raw = false): array |
|
155
|
|
|
{ |
|
156
|
|
|
if (isset($this->nonAutomatedEdits)) { |
|
157
|
|
|
return $this->nonAutomatedEdits; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$revs = $this->repository->getNonAutomatedEdits( |
|
|
|
|
|
|
161
|
|
|
$this->project, |
|
162
|
|
|
$this->user, |
|
163
|
|
|
$this->namespace, |
|
164
|
|
|
$this->start, |
|
165
|
|
|
$this->end, |
|
166
|
|
|
$this->offset, |
|
167
|
|
|
$this->limit |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
if ($raw) { |
|
171
|
|
|
return $revs; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->nonAutomatedEdits = Edit::getEditsFromRevs( |
|
175
|
|
|
$this->pageRepo, |
|
176
|
|
|
$this->editRepo, |
|
177
|
|
|
$this->userRepo, |
|
178
|
|
|
$this->project, |
|
179
|
|
|
$this->user, |
|
|
|
|
|
|
180
|
|
|
$revs |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
return $this->nonAutomatedEdits; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get automated contributions for this user. |
|
188
|
|
|
* @param bool $raw Whether to return raw data from the database, or get Edit objects. |
|
189
|
|
|
* @return Edit[] |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getAutomatedEdits(bool $raw = false): array |
|
192
|
|
|
{ |
|
193
|
|
|
if (isset($this->automatedEdits)) { |
|
194
|
|
|
return $this->automatedEdits; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$revs = $this->repository->getAutomatedEdits( |
|
|
|
|
|
|
198
|
|
|
$this->project, |
|
199
|
|
|
$this->user, |
|
200
|
|
|
$this->namespace, |
|
201
|
|
|
$this->start, |
|
202
|
|
|
$this->end, |
|
203
|
|
|
$this->tool, |
|
204
|
|
|
$this->offset |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
if ($raw) { |
|
208
|
|
|
return $revs; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$this->automatedEdits = Edit::getEditsFromRevs( |
|
212
|
|
|
$this->pageRepo, |
|
213
|
|
|
$this->editRepo, |
|
214
|
|
|
$this->userRepo, |
|
215
|
|
|
$this->project, |
|
216
|
|
|
$this->user, |
|
|
|
|
|
|
217
|
|
|
$revs |
|
218
|
|
|
); |
|
219
|
|
|
|
|
220
|
|
|
return $this->automatedEdits; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get counts of known automated tools used by the given user. |
|
225
|
|
|
* @return array Each tool that they used along with the count and link: |
|
226
|
|
|
* [ |
|
227
|
|
|
* 'Twinkle' => [ |
|
228
|
|
|
* 'count' => 50, |
|
229
|
|
|
* 'link' => 'Wikipedia:Twinkle', |
|
230
|
|
|
* ], |
|
231
|
|
|
* ] |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getToolCounts(): array |
|
234
|
|
|
{ |
|
235
|
|
|
if (isset($this->toolCounts)) { |
|
236
|
|
|
return $this->toolCounts; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$this->toolCounts = $this->repository->getToolCounts( |
|
|
|
|
|
|
240
|
|
|
$this->project, |
|
241
|
|
|
$this->user, |
|
242
|
|
|
$this->namespace, |
|
243
|
|
|
$this->start, |
|
244
|
|
|
$this->end |
|
245
|
|
|
); |
|
246
|
|
|
|
|
247
|
|
|
return $this->toolCounts; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get a list of all available tools for the Project. |
|
252
|
|
|
* @return array |
|
253
|
|
|
*/ |
|
254
|
|
|
public function getAllTools(): array |
|
255
|
|
|
{ |
|
256
|
|
|
return $this->repository->getTools($this->project); |
|
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Get the combined number of edits made with each tool. This is calculated separately from |
|
261
|
|
|
* self::getAutomatedCount() because the regex can sometimes overlap, and the counts are actually different. |
|
262
|
|
|
* @return int |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getToolsTotal(): int |
|
265
|
|
|
{ |
|
266
|
|
|
if (!isset($this->toolsTotal)) { |
|
267
|
|
|
$this->toolsTotal = array_reduce($this->getToolCounts(), function ($a, $b) { |
|
268
|
|
|
return $a + $b['count']; |
|
269
|
|
|
}); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $this->toolsTotal; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @return bool |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getUseSandbox(): bool |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->repository->getUseSandbox(); |
|
|
|
|
|
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|