Passed
Push — master ( e6ce91...429907 )
by MusikAnimal
04:46
created

TopEditsController::topEditsUserApiAction()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 0
dl 0
loc 29
ccs 0
cts 0
cp 0
crap 20
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the TopEditsController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Xtools\TopEdits;
14
use Xtools\TopEditsRepository;
15
16
/**
17
 * The Top Edits tool.
18
 */
19
class TopEditsController extends XtoolsController
20
{
21
    /**
22
     * Get the name of the tool's index route.
23
     * This is also the name of the associated model.
24
     * @return string
25
     * @codeCoverageIgnore
26
     */
27
    public function getIndexRoute()
28
    {
29
        return 'TopEdits';
30
    }
31
32
    /**
33
     * TopEditsController constructor.
34
     * @param RequestStack $requestStack
35
     * @param ContainerInterface $container
36
     */
37 1
    public function __construct(RequestStack $requestStack, ContainerInterface $container)
38
    {
39 1
        $this->tooHighEditCountAction = $this->getIndexRoute();
40
41 1
        parent::__construct($requestStack, $container);
42 1
    }
43
44
    /**
45
     * Display the form.
46
     * @Route("/topedits", name="topedits")
47
     * @Route("/topedits", name="TopEdits")
48
     * @Route("/topedits/", name="topEditsSlash")
49
     * @Route("/topedits/index.php", name="TopEditsIndex")
50
     * @Route("/topedits/{project}", name="TopEditsProject")
51
     * @return Response
52
     */
53 1
    public function indexAction()
54
    {
55
        // Redirect if at minimum project and username are provided.
56 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
57
            return $this->redirectToRoute('TopEditsResult', $this->params);
58
        }
59
60 1
        return $this->render('topedits/index.html.twig', array_merge([
61 1
            'xtPageTitle' => 'tool-topedits',
62
            'xtSubtitle' => 'tool-topedits-desc',
63
            'xtPage' => 'topedits',
64
65
            // Defaults that will get overriden if in $params.
66
            'namespace' => 0,
67
            'page' => '',
68
            'username' => '',
69 1
        ], $this->params, ['project' => $this->project]));
70
    }
71
72
    /**
73
     * Display the results.
74
     * @Route("/topedits/{project}/{username}/{namespace}/{page}", name="TopEditsResult",
75
     *     requirements = {"page"="|.+", "namespace" = "|all|\d+"},
76
     *     defaults = {"page" = "", "namespace" = "all"}
77
     * )
78
     * @return Response
79
     * @codeCoverageIgnore
80
     */
81
    public function resultAction()
82
    {
83
        if (empty($this->page)) {
84
            return $this->namespaceTopEdits();
85
        } else {
86
            return $this->singlePageTopEdits();
87
        }
88
    }
89
90
    /**
91
     * List top edits by this user for all pages in a particular namespace.
92
     * @return Response
93
     * @codeCoverageIgnore
94
     */
95
    public function namespaceTopEdits()
96
    {
97
        // Make sure they've opted in to see this data.
98
        if (!$this->project->userHasOptedIn($this->user)) {
99
            $optedInPage = $this->project
100
                ->getRepository()
101
                ->getPage($this->project, $this->project->userOptInPage($this->user));
102
103
            return $this->render('topedits/result_namespace.html.twig', [
104
                'xtPage' => 'topedits',
105
                'xtTitle' => $this->user->getUsername(),
106
                'project' => $this->project,
107
                'user' => $this->user,
108
                'namespace' => $this->namespace,
109
                'opted_in_page' => $optedInPage,
110
                'is_sub_request' => $this->isSubRequest,
111
            ]);
112
        }
113
114
        /**
115
         * Max number of rows per namespace to show. `null` here will cause to
116
         * use the TopEdits default.
117
         * @var int
118
         */
119
        $limit = $this->isSubRequest ? 10 : null;
120
121
        $topEdits = new TopEdits($this->project, $this->user, null, $this->namespace, $limit);
122
        $topEditsRepo = new TopEditsRepository();
123
        $topEditsRepo->setContainer($this->container);
124
        $topEdits->setRepository($topEditsRepo);
125
126
        $topEdits->prepareData();
127
128
        $ret = [
129
            'xtPage' => 'topedits',
130
            'xtTitle' => $this->user->getUsername(),
131
            'project' => $this->project,
132
            'user' => $this->user,
133
            'namespace' => $this->namespace,
134
            'te' => $topEdits,
135
            'is_sub_request' => $this->isSubRequest,
136
        ];
137
138
        // Output the relevant format template.
139
        return $this->getFormattedResponse($this->request, 'topedits/result_namespace', $ret);
140
    }
141
142
    /**
143
     * List top edits by this user for a particular page.
144
     * @return Response
145
     * @codeCoverageIgnore
146
     */
147
    protected function singlePageTopEdits()
148
    {
149
        // FIXME: add pagination.
150
        $topEdits = new TopEdits($this->project, $this->user, $this->page);
151
        $topEditsRepo = new TopEditsRepository();
152
        $topEditsRepo->setContainer($this->container);
153
        $topEdits->setRepository($topEditsRepo);
154
155
        $topEdits->prepareData();
156
157
        // Send all to the template.
158
        return $this->render('topedits/result_article.html.twig', [
159
            'xtPage' => 'topedits',
160
            'xtTitle' => $this->user->getUsername() . ' - ' . $this->page->getTitle(),
161
            'project' => $this->project,
162
            'user' => $this->user,
163
            'page' => $this->page,
164
            'te' => $topEdits,
165
        ]);
166
    }
167
168
    /************************ API endpoints ************************/
169
170
    /**
171
     * Get the all edits of a user to a specific page, maximum 1000.
172
     * @Route("/api/user/topedits/{project}/{username}/{namespace}/{page}", name="UserApiTopEditsArticle",
173
     *     requirements = {"page"="|.+", "namespace"="|\d+|all"},
174
     *     defaults={"page"="", "namespace"="all"}
175
     * )
176
     * @Route("/api/user/top_edits/{project}/{username}/{namespace}/{page}", name="UserApiTopEditsArticleUnderscored",
177
     *     requirements={"page"="|.+", "namespace"="|\d+|all"},
178
     *     defaults={"page"="", "namespace"="all"}
179
     * )
180
     * @return JsonResponse
181
     * @codeCoverageIgnore
182
     */
183
    public function topEditsUserApiAction()
184
    {
185
        $this->recordApiUsage('user/topedits');
186
187
        if (!$this->project->userHasOptedIn($this->user)) {
188
            return new JsonResponse(
189
                [
190
                    'error' => 'User:'.$this->user->getUsername().' has not opted in to detailed statistics.'
191
                ],
192
                Response::HTTP_FORBIDDEN
193
            );
194
        }
195
196
        $limit = isset($this->page) ? 1000 : 20;
197
        $topEdits = new TopEdits($this->project, $this->user, null, $this->namespace, $limit);
198
        $topEditsRepo = new TopEditsRepository();
199
        $topEditsRepo->setContainer($this->container);
200
        $topEdits->setRepository($topEditsRepo);
201
202
        if (isset($this->page)) {
203
            $topEdits->setPage($this->page);
204
            $topEdits->prepareData(false);
205
        } else {
206
            // Do format the results.
207
            $topEdits->prepareData();
208
        }
209
210
        return $this->getFormattedApiResponse([
211
            'top_edits' => $topEdits->getTopEdits(),
212
        ]);
213
    }
214
}
215