1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller; |
6
|
|
|
|
7
|
|
|
use App\Model\Edit; |
8
|
|
|
use App\Model\GlobalContribs; |
9
|
|
|
use App\Repository\EditRepository; |
10
|
|
|
use App\Repository\GlobalContribsRepository; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This controller serves the search form and results for the Global Contributions tool. |
17
|
|
|
*/ |
18
|
|
|
class GlobalContribsController extends XtoolsController |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritDoc |
23
|
|
|
* @codeCoverageIgnore |
24
|
|
|
*/ |
25
|
|
|
public function getIndexRoute(): string |
26
|
|
|
{ |
27
|
|
|
return 'GlobalContribs'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* GlobalContribs can be very slow, especially for wide IP ranges, so limit to max 500 results. |
32
|
|
|
* @inheritDoc |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
*/ |
35
|
|
|
public function maxLimit(): int |
36
|
|
|
{ |
37
|
|
|
return 500; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The search form. |
42
|
|
|
* @Route("/globalcontribs", name="GlobalContribs") |
43
|
|
|
* @Route("/ec-latestglobal", name="EditCounterLatestGlobalIndex") |
44
|
|
|
* @Route("/ec-latestglobal-contributions", name="EditCounterLatestGlobalContribsIndex") |
45
|
|
|
* @Route("/ec-latestglobaledits", name="EditCounterLatestGlobalEditsIndex") |
46
|
|
|
* @return Response |
47
|
|
|
*/ |
48
|
|
|
public function indexAction(): Response |
49
|
|
|
{ |
50
|
|
|
// Redirect if username is given. |
51
|
|
|
if (isset($this->params['username'])) { |
52
|
|
|
return $this->redirectToRoute('GlobalContribsResult', $this->params); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// FIXME: Nasty hack until T226072 is resolved. |
56
|
|
|
$project = $this->projectRepo->getProject($this->i18n->getLang().'.wikipedia'); |
57
|
|
|
if (!$project->exists()) { |
58
|
|
|
$project = $this->projectRepo->getProject($this->getParameter('central_auth_project')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->render('globalContribs/index.html.twig', array_merge([ |
62
|
|
|
'xtPage' => 'GlobalContribs', |
63
|
|
|
'xtPageTitle' => 'tool-globalcontribs', |
64
|
|
|
'xtSubtitle' => 'tool-globalcontribs-desc', |
65
|
|
|
'project' => $project, |
66
|
|
|
|
67
|
|
|
// Defaults that will get overridden if in $this->params. |
68
|
|
|
'namespace' => 'all', |
69
|
|
|
'start' => '', |
70
|
|
|
'end' => '', |
71
|
|
|
], $this->params)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param GlobalContribsRepository $globalContribsRepo |
76
|
|
|
* @param EditRepository $editRepo |
77
|
|
|
* @return GlobalContribs |
78
|
|
|
* @codeCoverageIgnore |
79
|
|
|
*/ |
80
|
|
|
public function getGlobalContribs( |
81
|
|
|
GlobalContribsRepository $globalContribsRepo, |
82
|
|
|
EditRepository $editRepo |
83
|
|
|
): GlobalContribs { |
84
|
|
|
return new GlobalContribs( |
85
|
|
|
$globalContribsRepo, |
86
|
|
|
$this->pageRepo, |
87
|
|
|
$this->userRepo, |
88
|
|
|
$editRepo, |
89
|
|
|
$this->user, |
|
|
|
|
90
|
|
|
$this->namespace, |
91
|
|
|
$this->start, |
92
|
|
|
$this->end, |
93
|
|
|
$this->offset, |
94
|
|
|
$this->limit |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Display the latest global edits tool. First two routes are legacy. |
100
|
|
|
* @Route( |
101
|
|
|
* "/ec-latestglobal-contributions/{project}/{username}", |
102
|
|
|
* name="EditCounterLatestGlobalContribs", |
103
|
|
|
* requirements={ |
104
|
|
|
* "username"="(ipr-.+\/\d+[^\/])|([^\/]+)", |
105
|
|
|
* }, |
106
|
|
|
* defaults={ |
107
|
|
|
* "project"="", |
108
|
|
|
* "namespace"="all" |
109
|
|
|
* } |
110
|
|
|
* ) |
111
|
|
|
* @Route( |
112
|
|
|
* "/ec-latestglobal/{project}/{username}", |
113
|
|
|
* name="EditCounterLatestGlobal", |
114
|
|
|
* requirements={ |
115
|
|
|
* "username"="(ipr-.+\/\d+[^\/])|([^\/]+)", |
116
|
|
|
* }, |
117
|
|
|
* defaults={ |
118
|
|
|
* "project"="", |
119
|
|
|
* "namespace"="all" |
120
|
|
|
* } |
121
|
|
|
* ), |
122
|
|
|
* @Route( |
123
|
|
|
* "/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}", |
124
|
|
|
* name="GlobalContribsResult", |
125
|
|
|
* requirements={ |
126
|
|
|
* "username"="(ipr-.+\/\d+[^\/])|([^\/]+)", |
127
|
|
|
* "namespace"="|all|\d+", |
128
|
|
|
* "start"="|\d*|\d{4}-\d{2}-\d{2}", |
129
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
130
|
|
|
* "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}", |
131
|
|
|
* }, |
132
|
|
|
* defaults={ |
133
|
|
|
* "namespace"="all", |
134
|
|
|
* "start"=false, |
135
|
|
|
* "end"=false, |
136
|
|
|
* "offset"=false, |
137
|
|
|
* } |
138
|
|
|
* ), |
139
|
|
|
* @param GlobalContribsRepository $globalContribsRepo |
140
|
|
|
* @param EditRepository $editRepo |
141
|
|
|
* @return Response |
142
|
|
|
* @codeCoverageIgnore |
143
|
|
|
*/ |
144
|
|
|
public function resultsAction(GlobalContribsRepository $globalContribsRepo, EditRepository $editRepo): Response |
145
|
|
|
{ |
146
|
|
|
$globalContribs = $this->getGlobalContribs($globalContribsRepo, $editRepo); |
147
|
|
|
$defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project')); |
148
|
|
|
|
149
|
|
|
return $this->render('globalContribs/result.html.twig', [ |
150
|
|
|
'xtTitle' => $this->user->getUsername(), |
|
|
|
|
151
|
|
|
'xtPage' => 'GlobalContribs', |
152
|
|
|
'is_sub_request' => $this->isSubRequest, |
153
|
|
|
'user' => $this->user, |
154
|
|
|
'project' => $defaultProject, |
155
|
|
|
'gc' => $globalContribs, |
156
|
|
|
]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/************************ API endpoints ************************/ |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get global edits made by a user, IP or IP range. |
163
|
|
|
* @Route( |
164
|
|
|
* "/api/user/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}", |
165
|
|
|
* name="UserApiGlobalContribs", |
166
|
|
|
* requirements={ |
167
|
|
|
* "username"="(ipr-.+\/\d+[^\/])|([^\/]+)", |
168
|
|
|
* "namespace"="|all|\d+", |
169
|
|
|
* "start"="|\d*|\d{4}-\d{2}-\d{2}", |
170
|
|
|
* "end"="|\d{4}-\d{2}-\d{2}", |
171
|
|
|
* "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}", |
172
|
|
|
* }, |
173
|
|
|
* defaults={ |
174
|
|
|
* "namespace"="all", |
175
|
|
|
* "start"=false, |
176
|
|
|
* "end"=false, |
177
|
|
|
* "offset"=false, |
178
|
|
|
* "limit"=50, |
179
|
|
|
* }, |
180
|
|
|
* ) |
181
|
|
|
* @param GlobalContribsRepository $globalContribsRepo |
182
|
|
|
* @param EditRepository $editRepo |
183
|
|
|
* @return JsonResponse |
184
|
|
|
* @codeCoverageIgnore |
185
|
|
|
*/ |
186
|
|
|
public function resultsApiAction( |
187
|
|
|
GlobalContribsRepository $globalContribsRepo, |
188
|
|
|
EditRepository $editRepo |
189
|
|
|
): JsonResponse { |
190
|
|
|
$this->recordApiUsage('user/globalcontribs'); |
191
|
|
|
|
192
|
|
|
$globalContribs = $this->getGlobalContribs($globalContribsRepo, $editRepo); |
193
|
|
|
$defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project')); |
194
|
|
|
$this->project = $defaultProject; |
195
|
|
|
|
196
|
|
|
$results = $globalContribs->globalEdits(); |
197
|
|
|
$results = array_map(function (Edit $edit) { |
198
|
|
|
return $edit->getForJson(true, true); |
199
|
|
|
}, array_values($results)); |
200
|
|
|
$results = $this->addFullPageTitlesAndContinue('globalcontribs', [], $results); |
201
|
|
|
|
202
|
|
|
return $this->getFormattedApiResponse($results); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|