1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains only the AutomatedEditsController class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace AppBundle\Controller; |
9
|
|
|
|
10
|
|
|
use AppBundle\Helper\I18nHelper; |
11
|
|
|
use AppBundle\Model\AutoEdits; |
12
|
|
|
use AppBundle\Repository\AutoEditsRepository; |
13
|
|
|
use DateTime; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This controller serves the AutomatedEdits tool. |
23
|
|
|
*/ |
24
|
|
|
class AutomatedEditsController extends XtoolsController |
25
|
|
|
{ |
26
|
|
|
/** @var AutoEdits The AutoEdits instance. */ |
27
|
|
|
protected $autoEdits; |
28
|
|
|
|
29
|
|
|
/** @var array Data that is passed to the view. */ |
30
|
|
|
private $output; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the name of the tool's index route. |
34
|
|
|
* This is also the name of the associated model. |
35
|
|
|
* @return string |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
public function getIndexRoute(): string |
39
|
|
|
{ |
40
|
|
|
return 'AutoEdits'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* AutomatedEditsController constructor. |
45
|
|
|
* @param RequestStack $requestStack |
46
|
|
|
* @param ContainerInterface $container |
47
|
|
|
* @param I18nHelper $i18n |
48
|
|
|
*/ |
49
|
1 |
|
public function __construct(RequestStack $requestStack, ContainerInterface $container, I18nHelper $i18n) |
50
|
|
|
{ |
51
|
|
|
// This will cause the tool to redirect back to the index page, with an error, |
52
|
|
|
// if the user has too high of an edit count. |
53
|
1 |
|
$this->tooHighEditCountAction = $this->getIndexRoute(); |
54
|
|
|
|
55
|
1 |
|
parent::__construct($requestStack, $container, $i18n); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Display the search form. |
60
|
|
|
* @Route("/autoedits", name="AutoEdits") |
61
|
|
|
* @Route("/automatededits", name="AutoEditsLong") |
62
|
|
|
* @Route("/autoedits/index.php", name="AutoEditsIndexPhp") |
63
|
|
|
* @Route("/automatededits/index.php", name="AutoEditsLongIndexPhp") |
64
|
|
|
* @Route("/autoedits/{project}", name="AutoEditsProject") |
65
|
|
|
* @return Response |
66
|
|
|
*/ |
67
|
1 |
|
public function indexAction(): Response |
68
|
|
|
{ |
69
|
|
|
// Redirect if at minimum project and username are provided. |
70
|
1 |
|
if (isset($this->params['project']) && isset($this->params['username'])) { |
71
|
|
|
// If 'tool' param is given, redirect to corresponding action. |
72
|
|
|
$tool = $this->request->query->get('tool'); |
73
|
|
|
|
74
|
|
|
if ('all' === $tool) { |
75
|
|
|
unset($this->params['tool']); |
76
|
|
|
return $this->redirectToRoute('AutoEditsContributionsResult', $this->params); |
77
|
|
|
} elseif ('' != $tool && 'none' !== $tool) { |
78
|
|
|
$this->params['tool'] = $tool; |
79
|
|
|
return $this->redirectToRoute('AutoEditsContributionsResult', $this->params); |
80
|
|
|
} elseif ('none' === $tool) { |
81
|
|
|
unset($this->params['tool']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Otherwise redirect to the normal result action. |
85
|
|
|
return $this->redirectToRoute('AutoEditsResult', $this->params); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $this->render('autoEdits/index.html.twig', array_merge([ |
89
|
1 |
|
'xtPageTitle' => 'tool-autoedits', |
90
|
|
|
'xtSubtitle' => 'tool-autoedits-desc', |
91
|
|
|
'xtPage' => 'AutoEdits', |
92
|
|
|
|
93
|
|
|
// Defaults that will get overridden if in $this->params. |
94
|
|
|
'username' => '', |
95
|
|
|
'namespace' => 0, |
96
|
|
|
'start' => '', |
97
|
|
|
'end' => '', |
98
|
1 |
|
], $this->params, ['project' => $this->project])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set defaults, and instantiate the AutoEdits model. This is called at the top of every view action. |
103
|
|
|
* @codeCoverageIgnore |
104
|
|
|
*/ |
105
|
|
|
private function setupAutoEdits(): void |
106
|
|
|
{ |
107
|
|
|
$tool = $this->request->query->get('tool', null); |
108
|
|
|
$useSandbox = (bool)$this->request->query->get('usesandbox', false); |
109
|
|
|
|
110
|
|
|
if ($useSandbox && !$this->request->getSession()->get('logged_in_user')) { |
111
|
|
|
$this->addFlashMessage('danger', 'auto-edits-logged-out'); |
112
|
|
|
$useSandbox = false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$autoEditsRepo = new AutoEditsRepository($useSandbox); |
116
|
|
|
$autoEditsRepo->setContainer($this->container); |
117
|
|
|
|
118
|
|
|
$misconfigured = $autoEditsRepo->getInvalidTools($this->project); |
119
|
|
|
$helpLink = "https://w.wiki/ppr"; |
120
|
|
|
foreach ($misconfigured as $tool) { |
121
|
|
|
$this->addFlashMessage('warning', 'auto-edits-misconfiguration', [$tool, $helpLink]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Validate tool. |
125
|
|
|
// FIXME: instead of redirecting to index page, show result page listing all tools for that project, |
126
|
|
|
// clickable to show edits by the user, etc. |
127
|
|
|
if ($tool && !isset($autoEditsRepo->getTools($this->project)[$tool])) { |
128
|
|
|
$this->throwXtoolsException( |
129
|
|
|
$this->getIndexRoute(), |
130
|
|
|
'auto-edits-unknown-tool', |
131
|
|
|
[$tool], |
132
|
|
|
'tool' |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->autoEdits = new AutoEdits( |
137
|
|
|
$this->project, |
138
|
|
|
$this->user, |
139
|
|
|
$this->namespace, |
140
|
|
|
$this->start, |
141
|
|
|
$this->end, |
142
|
|
|
$tool, |
143
|
|
|
$this->offset, |
144
|
|
|
$this->limit |
145
|
|
|
); |
146
|
|
|
$this->autoEdits->setRepository($autoEditsRepo); |
147
|
|
|
|
148
|
|
|
$this->output = [ |
149
|
|
|
'xtPage' => 'AutoEdits', |
150
|
|
|
'xtTitle' => $this->user->getUsername(), |
151
|
|
|
'ae' => $this->autoEdits, |
152
|
|
|
'is_sub_request' => $this->isSubRequest, |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Display the results. |
158
|
|
|
* @Route( |
159
|
|
|
* "/autoedits/{project}/{username}/{namespace}/{start}/{end}/{offset}", name="AutoEditsResult", |
160
|
|
|
* requirements={ |
161
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
162
|
|
|
* "namespace"="|all|\d+", |
163
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
164
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
165
|
|
|
* "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}", |
166
|
|
|
* }, |
167
|
|
|
* defaults={"namespace"=0, "start"=false, "end"=false, "offset"=false} |
168
|
|
|
* ) |
169
|
|
|
* @return Response |
170
|
|
|
* @codeCoverageIgnore |
171
|
|
|
*/ |
172
|
|
|
public function resultAction(): Response |
173
|
|
|
{ |
174
|
|
|
// Will redirect back to index if the user has too high of an edit count. |
175
|
|
|
$this->setupAutoEdits(); |
176
|
|
|
|
177
|
|
|
if (in_array('bot', $this->user->getUserRights($this->project))) { |
178
|
|
|
$this->addFlashMessage('warning', 'auto-edits-bot'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->getFormattedResponse('autoEdits/result', $this->output); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get non-automated edits for the given user. |
186
|
|
|
* @Route( |
187
|
|
|
* "/nonautoedits-contributions/{project}/{username}/{namespace}/{start}/{end}/{offset}", |
188
|
|
|
* name="NonAutoEditsContributionsResult", |
189
|
|
|
* requirements={ |
190
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
191
|
|
|
* "namespace"="|all|\d+", |
192
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
193
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
194
|
|
|
* "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}", |
195
|
|
|
* }, |
196
|
|
|
* defaults={"namespace"=0, "start"=false, "end"=false, "offset"=false} |
197
|
|
|
* ) |
198
|
|
|
* @return Response|RedirectResponse |
199
|
|
|
* @codeCoverageIgnore |
200
|
|
|
*/ |
201
|
|
|
public function nonAutomatedEditsAction(): Response |
202
|
|
|
{ |
203
|
|
|
$this->setupAutoEdits(); |
204
|
|
|
|
205
|
|
|
return $this->getFormattedResponse('autoEdits/nonautomated_edits', $this->output); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get automated edits for the given user using the given tool. |
210
|
|
|
* @Route( |
211
|
|
|
* "/autoedits-contributions/{project}/{username}/{namespace}/{start}/{end}/{offset}", |
212
|
|
|
* name="AutoEditsContributionsResult", |
213
|
|
|
* requirements={ |
214
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
215
|
|
|
* "namespace"="|all|\d+", |
216
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
217
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
218
|
|
|
* "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}", |
219
|
|
|
* }, |
220
|
|
|
* defaults={"namespace"=0, "start"=false, "end"=false, "offset"=false} |
221
|
|
|
* ) |
222
|
|
|
* @return Response |
223
|
|
|
* @codeCoverageIgnore |
224
|
|
|
*/ |
225
|
|
|
public function automatedEditsAction(): Response |
226
|
|
|
{ |
227
|
|
|
$this->setupAutoEdits(); |
228
|
|
|
|
229
|
|
|
return $this->getFormattedResponse('autoEdits/automated_edits', $this->output); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/************************ API endpoints ************************/ |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get a list of the automated tools and their regex/tags/etc. |
236
|
|
|
* @Route("/api/user/automated_tools/{project}", name="UserApiAutoEditsTools") |
237
|
|
|
* @Route("/api/project/automated_tools/{project}", name="ProjectApiAutoEditsTools") |
238
|
|
|
* @return JsonResponse |
239
|
|
|
* @codeCoverageIgnore |
240
|
|
|
*/ |
241
|
|
|
public function automatedToolsApiAction(): JsonResponse |
242
|
|
|
{ |
243
|
|
|
$this->recordApiUsage('user/automated_tools'); |
244
|
|
|
|
245
|
|
|
$aeh = $this->container->get('app.automated_edits_helper'); |
246
|
|
|
return $this->getFormattedApiResponse($aeh->getTools($this->project)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Count the number of automated edits the given user has made. |
251
|
|
|
* @Route( |
252
|
|
|
* "/api/user/automated_editcount/{project}/{username}/{namespace}/{start}/{end}/{tools}", |
253
|
|
|
* name="UserApiAutoEditsCount", |
254
|
|
|
* requirements={ |
255
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
256
|
|
|
* "namespace"="|all|\d+", |
257
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
258
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}" |
259
|
|
|
* }, |
260
|
|
|
* defaults={"namespace"="all", "start"=false, "end"=false, "tools"=false} |
261
|
|
|
* ) |
262
|
|
|
* @return JsonResponse |
263
|
|
|
* @codeCoverageIgnore |
264
|
|
|
*/ |
265
|
|
|
public function automatedEditCountApiAction(): JsonResponse |
266
|
|
|
{ |
267
|
|
|
$this->recordApiUsage('user/automated_editcount'); |
268
|
|
|
|
269
|
|
|
$this->setupAutoEdits(); |
270
|
|
|
|
271
|
|
|
$ret = [ |
272
|
|
|
'total_editcount' => $this->autoEdits->getEditCount(), |
273
|
|
|
'automated_editcount' => $this->autoEdits->getAutomatedCount(), |
274
|
|
|
]; |
275
|
|
|
$ret['nonautomated_editcount'] = $ret['total_editcount'] - $ret['automated_editcount']; |
276
|
|
|
|
277
|
|
|
if (isset($this->params['tools'])) { |
278
|
|
|
$tools = $this->autoEdits->getToolCounts(); |
279
|
|
|
$ret['automated_tools'] = $tools; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this->getFormattedApiResponse($ret); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get non-automated edits for the given user. |
287
|
|
|
* @Route( |
288
|
|
|
* "/api/user/nonautomated_edits/{project}/{username}/{namespace}/{start}/{end}/{offset}", |
289
|
|
|
* name="UserApiNonAutoEdits", |
290
|
|
|
* requirements={ |
291
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
292
|
|
|
* "namespace"="|all|\d+", |
293
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
294
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
295
|
|
|
* "offset"="|\d{4}-?\d{2}-?\d{2}T?\d{2}:?\d{2}:?\d{2}" |
296
|
|
|
* }, |
297
|
|
|
* defaults={"namespace"=0, "start"=false, "end"=false, "offset"=false, "limit"=50} |
298
|
|
|
* ) |
299
|
|
|
* @return JsonResponse |
300
|
|
|
* @codeCoverageIgnore |
301
|
|
|
*/ |
302
|
|
|
public function nonAutomatedEditsApiAction(): JsonResponse |
303
|
|
|
{ |
304
|
|
|
$this->recordApiUsage('user/nonautomated_edits'); |
305
|
|
|
|
306
|
|
|
$this->setupAutoEdits(); |
307
|
|
|
|
308
|
|
|
$out = $this->addFullPageTitlesAndContinue( |
309
|
|
|
'nonautomated_edits', |
310
|
|
|
[], |
311
|
|
|
$this->autoEdits->getNonAutomatedEdits(true) |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
return $this->getFormattedApiResponse($out); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get (semi-)automated edits for the given user, optionally using the given tool. |
319
|
|
|
* @Route( |
320
|
|
|
* "/api/user/automated_edits/{project}/{username}/{namespace}/{start}/{end}/{offset}", |
321
|
|
|
* name="UserApiAutoEdits", |
322
|
|
|
* requirements={ |
323
|
|
|
* "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)", |
324
|
|
|
* "namespace"="|all|\d+", |
325
|
|
|
* "start"="|\d{4}-\d{2}-\d{2}", |
326
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
327
|
|
|
* "offset"="|\d{4}-?\d{2}-?\d{2}T?\d{2}:?\d{2}:?\d{2}", |
328
|
|
|
* }, |
329
|
|
|
* defaults={"namespace"=0, "start"=false, "end"=false, "offset"=false, "limit"=50} |
330
|
|
|
* ) |
331
|
|
|
* @return Response |
332
|
|
|
* @codeCoverageIgnore |
333
|
|
|
*/ |
334
|
|
|
public function automatedEditsApiAction(): Response |
335
|
|
|
{ |
336
|
|
|
$this->recordApiUsage('user/automated_edits'); |
337
|
|
|
|
338
|
|
|
$this->setupAutoEdits(); |
339
|
|
|
|
340
|
|
|
$extras = $this->autoEdits->getTool() |
341
|
|
|
? ['tool' => $this->autoEdits->getTool()] |
342
|
|
|
: []; |
343
|
|
|
|
344
|
|
|
$out = $this->addFullPageTitlesAndContinue( |
345
|
|
|
'automated_edits', |
346
|
|
|
$extras, |
347
|
|
|
$this->autoEdits->getAutomatedEdits(true) |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
return $this->getFormattedApiResponse($out); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|