Test Failed
Pull Request — main (#406)
by MusikAnimal
12:27 queued 07:43
created

GlobalContribsController::getIndexRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace AppBundle\Controller;
5
6
use AppBundle\Helper\I18nHelper;
7
use AppBundle\Model\GlobalContribs;
8
use AppBundle\Repository\GlobalContribsRepository;
9
use AppBundle\Repository\ProjectRepository;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpFoundation\RequestStack;
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
 * @codeCoverageIgnore
18
 */
19
class GlobalContribsController extends XtoolsController
20
{
21
    /**
22
     * Used to override properties set in XtoolsController.
23
     * @param RequestStack $requestStack
24
     * @param ContainerInterface $container
25
     * @param I18nHelper $i18n
26
     */
27
    public function __construct(RequestStack $requestStack, ContainerInterface $container, I18nHelper $i18n)
28
    {
29
        // GlobalContribs can be very slow, especially for wide IP ranges.
30
        $this->maxLimit = 500;
31
        parent::__construct($requestStack, $container, $i18n);
32
    }
33
34
    /**
35
     * Get the name of the tool's index route. This is also the name of the associated model.
36
     * @return string
37
     * @codeCoverageIgnore
38
     */
39
    public function getIndexRoute(): string
40
    {
41
        return 'GlobalContribs';
42
    }
43
44
    /**
45
     * The search form.
46
     * @Route("/globalcontribs", name="GlobalContribs")
47
     * @Route("/ec-latestglobal", name="EditCounterLatestGlobalIndex")
48
     * @Route("/ec-latestglobal-contributions", name="EditCounterLatestGlobalContribsIndex")
49
     * @Route("/ec-latestglobaledits", name="EditCounterLatestGlobalEditsIndex")
50
     * @return Response
51
     */
52
    public function indexAction(): Response
53
    {
54
        // Redirect if username is given.
55
        if (isset($this->params['username'])) {
56
            return $this->redirectToRoute('GlobalContribsResult', $this->params);
57
        }
58
59
        // FIXME: Nasty hack until T226072 is resolved.
60
        $project = ProjectRepository::getProject($this->i18n->getLang().'.wikipedia', $this->container);
61
        if (!$project->exists()) {
62
            $project = ProjectRepository::getProject(
63
                $this->container->getParameter('central_auth_project'),
64
                $this->container
65
            );
66
        }
67
68
        return $this->render('globalContribs/index.html.twig', array_merge([
69
            'xtPage' => 'GlobalContribs',
70
            'xtPageTitle' => 'tool-globalcontribs',
71
            'xtSubtitle' => 'tool-globalcontribs-desc',
72
            'project' => $project,
73
74
            // Defaults that will get overridden if in $this->params.
75
            'namespace' => 'all',
76
            'start' => '',
77
            'end' => '',
78
        ], $this->params));
79
    }
80
81
    /**
82
     * Display the latest global edits tool. First two routes are legacy.
83
     * @Route(
84
     *     "/ec-latestglobal-contributions/{project}/{username}",
85
     *     name="EditCounterLatestGlobalContribs",
86
     *     requirements={
87
     *         "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)",
88
     *     },
89
     *     defaults={
90
     *         "project"="",
91
     *         "namespace"="all"
92
     *     }
93
     * )
94
     * @Route(
95
     *     "/ec-latestglobal/{project}/{username}",
96
     *     name="EditCounterLatestGlobal",
97
     *     requirements={
98
     *         "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)",
99
     *     },
100
     *     defaults={
101
     *         "project"="",
102
     *         "namespace"="all"
103
     *     }
104
     * ),
105
     * @Route(
106
     *     "/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
107
     *     name="GlobalContribsResult",
108
     *     requirements={
109
     *         "username" = "(ipr-.+\/\d+[^\/])|([^\/]+)",
110
     *         "namespace" = "|all|\d+",
111
     *         "start" = "|\d*|\d{4}-\d{2}-\d{2}",
112
     *         "end" = "|\d{4}-\d{2}-\d{2}",
113
     *         "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
114
     *     },
115
     *     defaults={
116
     *         "namespace"="all",
117
     *         "start"=false,
118
     *         "end"=false,
119
     *         "offset"=false,
120
     *     }
121
     * ),
122
     * @return Response
123
     * @codeCoverageIgnore
124
     */
125
    public function resultsAction(): Response
126
    {
127
        $globalContribsRepo = new GlobalContribsRepository();
128
        $globalContribsRepo->setContainer($this->container);
129
        $globalContribs = new GlobalContribs(
130
            $this->user,
131
            $this->namespace,
132
            $this->start,
133
            $this->end,
134
            $this->offset,
135
            $this->limit
136
        );
137
        $globalContribs->setRepository($globalContribsRepo);
138
        $defaultProject = ProjectRepository::getProject(
139
            $this->container->getParameter('central_auth_project'),
140
            $this->container
141
        );
142
        $defaultProject->getRepository()->setContainer($this->container);
143
144
        return $this->render('globalContribs/result.html.twig', [
145
            'xtTitle' => $this->user->getUsername(),
146
            'xtPage' => 'GlobalContribs',
147
            'is_sub_request' => $this->isSubRequest,
148
            'user' => $this->user,
149
            'project' => $defaultProject,
150
            'gc' => $globalContribs,
151
        ]);
152
    }
153
}
154