|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the SimpleEditCounterController class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace AppBundle\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
15
|
|
|
use Xtools\SimpleEditCounter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This controller handles the Simple Edit Counter tool. |
|
19
|
|
|
*/ |
|
20
|
|
|
class SimpleEditCounterController extends XtoolsController |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the tool's shortname. |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @codeCoverageIgnore |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getToolShortname() |
|
29
|
|
|
{ |
|
30
|
|
|
return 'sc'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The Simple Edit Counter search form. |
|
35
|
|
|
* @Route("/sc", name="sc") |
|
36
|
|
|
* @Route("/sc", name="SimpleEditCounter") |
|
37
|
|
|
* @Route("/sc/", name="SimpleEditCounterSlash") |
|
38
|
|
|
* @Route("/sc/index.php", name="SimpleEditCounterIndexPhp") |
|
39
|
|
|
* @Route("/sc/{project}", name="SimpleEditCounterProject") |
|
40
|
|
|
* @param Request $request The HTTP request. |
|
41
|
|
|
* @return Response |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function indexAction(Request $request) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$params = $this->parseQueryParams($request); |
|
46
|
|
|
|
|
47
|
|
|
// Redirect if project and username are given. |
|
48
|
1 |
|
if (isset($params['project']) && isset($params['username'])) { |
|
49
|
|
|
return $this->redirectToRoute('SimpleEditCounterResult', $params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Convert the given project (or default project) into a Project instance. |
|
53
|
1 |
|
$params['project'] = $this->getProjectFromQuery($params); |
|
54
|
|
|
|
|
55
|
|
|
// Show the form. |
|
56
|
1 |
|
return $this->render('simpleEditCounter/index.html.twig', [ |
|
57
|
1 |
|
'xtPageTitle' => 'tool-sc', |
|
58
|
1 |
|
'xtSubtitle' => 'tool-sc-desc', |
|
59
|
1 |
|
'xtPage' => 'sc', |
|
60
|
1 |
|
'project' => $params['project'], |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Display the |
|
66
|
|
|
* @Route("/sc/{project}/{username}", name="SimpleEditCounterResult") |
|
67
|
|
|
* @param Request $request The HTTP request. |
|
68
|
|
|
* @return Response |
|
69
|
|
|
* @codeCoverageIgnore |
|
70
|
|
|
*/ |
|
71
|
|
|
public function resultAction(Request $request) |
|
72
|
|
|
{ |
|
73
|
|
|
$ret = $this->validateProjectAndUser($request); |
|
74
|
|
|
if ($ret instanceof RedirectResponse) { |
|
75
|
|
|
return $ret; |
|
76
|
|
|
} else { |
|
77
|
|
|
list($project, $user) = $ret; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$sec = new SimpleEditCounter($this->container, $project, $user); |
|
81
|
|
|
$sec->prepareData(); |
|
82
|
|
|
|
|
83
|
|
|
// Assign the values and display the template |
|
84
|
|
|
return $this->render('simpleEditCounter/result.html.twig', [ |
|
85
|
|
|
'xtPage' => 'sc', |
|
86
|
|
|
'xtTitle' => $user->getUsername(), |
|
87
|
|
|
'user' => $user, |
|
88
|
|
|
'project' => $project, |
|
89
|
|
|
'sec' => $sec, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/************************ API endpoints ************************/ |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* API endpoint for the Simple Edit Counter. |
|
97
|
|
|
* @Route("/api/user/simple_editcount/{project}/{username}", name="SimpleEditCounterApi") |
|
98
|
|
|
* @param Request $request |
|
99
|
|
|
* @return Response |
|
100
|
|
|
* @codeCoverageIgnore |
|
101
|
|
|
*/ |
|
102
|
|
|
public function simpleEditCounterApiAction(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->recordApiUsage('user/simple_editcount'); |
|
105
|
|
|
|
|
106
|
|
|
// Here we do want to impose the max edit count restriction. Even though the |
|
107
|
|
|
// query is very 'simple', it can still run too slow for an API. |
|
108
|
|
|
$ret = $this->validateProjectAndUser($request, 'sc'); |
|
109
|
|
|
if ($ret instanceof RedirectResponse) { |
|
110
|
|
|
return $ret; |
|
111
|
|
|
} else { |
|
112
|
|
|
list($project, $user) = $ret; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$sec = new SimpleEditCounter($this->container, $project, $user); |
|
116
|
|
|
$sec->prepareData(); |
|
117
|
|
|
|
|
118
|
|
|
$response = new JsonResponse(); |
|
119
|
|
|
$response->setEncodingOptions(JSON_NUMERIC_CHECK); |
|
120
|
|
|
$response->setData($sec->getData()); |
|
121
|
|
|
return $response; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|