Passed
Pull Request — main (#425)
by MusikAnimal
05:22 queued 01:08
created

GlobalContribsController::resultsApiAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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