-
Notifications
You must be signed in to change notification settings - Fork 64
/
ApiController.php
235 lines (213 loc) · 7.7 KB
/
ApiController.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
<?php
/**
* This file contains only the ApiController class.
*/
namespace AppBundle\Controller;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Debug\Exception\FatalErrorException;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use Xtools\ProjectRepository;
use Xtools\UserRepository;
use Xtools\Page;
use Xtools\PagesRepository;
use Xtools\Edit;
use DateTime;
/**
* Serves the external API of XTools.
*/
class ApiController extends FOSRestController
{
/**
* Get domain name, URL, and API URL of the given project.
* @Rest\Get("/api/project/normalize/{project}")
* @param string $project Project database name, URL, or domain name.
* @return View
*/
public function normalizeProject($project)
{
$proj = ProjectRepository::getProject($project, $this->container);
if (!$proj->exists()) {
return new View(
[
'error' => "$project is not a valid project",
],
Response::HTTP_NOT_FOUND
);
}
return new View(
[
'domain' => $proj->getDomain(),
'url' => $proj->getUrl(),
'api' => $proj->getApiUrl(),
'database' => $proj->getDatabaseName(),
],
Response::HTTP_OK
);
}
/**
* Get all namespaces of the given project. This endpoint also does the same thing
* as the /project/normalize endpoint, returning other basic info about the project.
* @Rest\Get("/api/project/namespaces/{project}")
* @param string $project The project name.
* @return View
*/
public function namespaces($project)
{
$proj = ProjectRepository::getProject($project, $this->container);
if (!$proj->exists()) {
return new View(
[
'error' => "$project is not a valid project",
],
Response::HTTP_NOT_FOUND
);
}
return new View(
[
'domain' => $proj->getDomain(),
'url' => $proj->getUrl(),
'api' => $proj->getApiUrl(),
'database' => $proj->getDatabaseName(),
'namespaces' => $proj->getNamespaces(),
],
Response::HTTP_OK
);
}
/**
* Count the number of automated edits the given user has made.
* @Rest\Get(
* "/api/user/automated_editcount/{project}/{username}/{namespace}/{start}/{end}/{tools}",
* requirements={"start" = "|\d{4}-\d{2}-\d{2}", "end" = "|\d{4}-\d{2}-\d{2}"}
* )
* @param Request $request The HTTP request.
* @param string $project
* @param string $username
* @param int|string $namespace ID of the namespace, or 'all' for all namespaces
* @param string $start In the format YYYY-MM-DD
* @param string $end In the format YYYY-MM-DD
* @param string $tools Non-blank to show which tools were used and how many times.
*/
public function automatedEditCount(
Request $request,
$project,
$username,
$namespace = 'all',
$start = '',
$end = '',
$tools = ''
) {
$project = ProjectRepository::getProject($project, $this->container);
$user = UserRepository::getUser($username, $this->container);
$res = [
'project' => $project->getDomain(),
'username' => $user->getUsername(),
'total_editcount' => $user->countEdits($project, $namespace, $start, $end),
];
if ($tools != '') {
$tools = $user->getAutomatedCounts($project, $namespace, $start, $end);
$res['automated_editcount'] = 0;
foreach ($tools as $tool) {
$res['automated_editcount'] += $tool['count'];
}
$res['automated_tools'] = $tools;
} else {
$res['automated_editcount'] = $user->countAutomatedEdits($project, $namespace, $start, $end);
}
$res['nonautomated_editcount'] = $res['total_editcount'] - $res['automated_editcount'];
$view = View::create()->setStatusCode(Response::HTTP_OK);
$view->setData($res);
return $view->setFormat('json');
}
/**
* Get non-automated edits for the given user.
* @Rest\Get(
* "/api/user/nonautomated_edits/{project}/{username}/{namespace}/{start}/{end}/{offset}",
* requirements={
* "start" = "|\d{4}-\d{2}-\d{2}",
* "end" = "|\d{4}-\d{2}-\d{2}",
* "offset" = "\d*"
* }
* )
* @param Request $request The HTTP request.
* @param string $project
* @param string $username
* @param int|string $namespace ID of the namespace, or 'all' for all namespaces
* @param string $start In the format YYYY-MM-DD
* @param string $end In the format YYYY-MM-DD
* @param int $offset For pagination, offset results by N edits
* @return View
*/
public function nonautomatedEdits(
Request $request,
$project,
$username,
$namespace,
$start = '',
$end = '',
$offset = 0
) {
$twig = $this->container->get('twig');
$project = ProjectRepository::getProject($project, $this->container);
$user = UserRepository::getUser($username, $this->container);
// Reject if they've made too many edits.
if ($user->hasTooManyEdits($project)) {
if ($request->query->get('format') !== 'html') {
return new View(
[
'error' => 'Unable to show any data. User has made over ' .
$user->maxEdits() . ' edits.',
],
Response::HTTP_FORBIDDEN
);
}
$edits = [];
} else {
$edits = $user->getNonautomatedEdits($project, $namespace, $start, $end, $offset);
}
$view = View::create()->setStatusCode(Response::HTTP_OK);
if ($request->query->get('format') === 'html') {
if ($edits) {
$edits = array_map(function ($attrs) use ($project, $username) {
$page = $project->getRepository()
->getPage($project, $attrs['full_page_title']);
$pageTitles[] = $attrs['full_page_title'];
$attrs['id'] = $attrs['rev_id'];
$attrs['username'] = $username;
return new Edit($page, $attrs);
}, $edits);
}
$twig = $this->container->get('twig');
$view->setTemplate('api/nonautomated_edits.html.twig');
$view->setTemplateData([
'edits' => $edits,
'project' => $project,
'maxEdits' => $user->maxEdits(),
]);
$view->setFormat('html');
} else {
$res = [
'project' => $project->getDomain(),
'username' => $user->getUsername(),
];
if ($namespace != '' && $namespace !== 'all') {
$res['namespace'] = $namespace;
}
if ($start != '') {
$res['start'] = $start;
}
if ($end != '') {
$res['end'] = $end;
}
$res['offset'] = $offset;
$res['nonautomated_edits'] = $edits;
$view->setData($res)->setFormat('json');
}
return $view;
}
}