Passed
Pull Request — main (#442)
by MusikAnimal
08:40 queued 04:21
created

GlobalContribsController::getGlobalContribs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Controller;
6
7
use App\Model\Edit;
8
use App\Model\GlobalContribs;
9
use App\Repository\EditRepository;
10
use App\Repository\GlobalContribsRepository;
11
use Symfony\Component\HttpFoundation\JsonResponse;
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
    /**
23
     * Get the name of the tool's index route. This is also the name of the associated model.
24
     * @return string
25
     * @codeCoverageIgnore
26
     */
27
    public function getIndexRoute(): string
28
    {
29
        return 'GlobalContribs';
30
    }
31
32
    /**
33
     * GlobalContribs can be very slow, especially for wide IP ranges, so limit to max 500 results.
34
     * @inheritDoc
35
     */
36
    public function maxLimit(): int
37
    {
38
        return 500;
39
    }
40
41
    /**
42
     * The search form.
43
     * @Route("/globalcontribs", name="GlobalContribs")
44
     * @Route("/ec-latestglobal", name="EditCounterLatestGlobalIndex")
45
     * @Route("/ec-latestglobal-contributions", name="EditCounterLatestGlobalContribsIndex")
46
     * @Route("/ec-latestglobaledits", name="EditCounterLatestGlobalEditsIndex")
47
     * @return Response
48
     */
49
    public function indexAction(): Response
50
    {
51
        // Redirect if username is given.
52
        if (isset($this->params['username'])) {
53
            return $this->redirectToRoute('GlobalContribsResult', $this->params);
54
        }
55
56
        // FIXME: Nasty hack until T226072 is resolved.
57
        $project = $this->projectRepo->getProject($this->i18n->getLang().'.wikipedia');
58
        if (!$project->exists()) {
59
            $project = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
60
        }
61
62
        return $this->render('globalContribs/index.html.twig', array_merge([
63
            'xtPage' => 'GlobalContribs',
64
            'xtPageTitle' => 'tool-globalcontribs',
65
            'xtSubtitle' => 'tool-globalcontribs-desc',
66
            'project' => $project,
67
68
            // Defaults that will get overridden if in $this->params.
69
            'namespace' => 'all',
70
            'start' => '',
71
            'end' => '',
72
        ], $this->params));
73
    }
74
75
    /**
76
     * @param GlobalContribsRepository $globalContribsRepo
77
     * @param EditRepository $editRepo
78
     * @return GlobalContribs
79
     */
80
    public function getGlobalContribs(
81
        GlobalContribsRepository $globalContribsRepo,
82
        EditRepository $editRepo
83
    ): GlobalContribs {
84
        return new GlobalContribs(
85
            $globalContribsRepo,
86
            $this->pageRepo,
87
            $this->userRepo,
88
            $editRepo,
89
            $this->user,
0 ignored issues
show
Bug introduced by
It seems like $this->user can also be of type null; however, parameter $user of App\Model\GlobalContribs::__construct() does only seem to accept App\Model\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            /** @scrutinizer ignore-type */ $this->user,
Loading history...
90
            $this->namespace,
91
            $this->start,
92
            $this->end,
93
            $this->offset,
94
            $this->limit
95
        );
96
    }
97
98
    /**
99
     * Display the latest global edits tool. First two routes are legacy.
100
     * @Route(
101
     *     "/ec-latestglobal-contributions/{project}/{username}",
102
     *     name="EditCounterLatestGlobalContribs",
103
     *     requirements={
104
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
105
     *     },
106
     *     defaults={
107
     *         "project"="",
108
     *         "namespace"="all"
109
     *     }
110
     * )
111
     * @Route(
112
     *     "/ec-latestglobal/{project}/{username}",
113
     *     name="EditCounterLatestGlobal",
114
     *     requirements={
115
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
116
     *     },
117
     *     defaults={
118
     *         "project"="",
119
     *         "namespace"="all"
120
     *     }
121
     * ),
122
     * @Route(
123
     *     "/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
124
     *     name="GlobalContribsResult",
125
     *     requirements={
126
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
127
     *         "namespace"="|all|\d+",
128
     *         "start"="|\d*|\d{4}-\d{2}-\d{2}",
129
     *         "end"="|\d{4}-\d{2}-\d{2}",
130
     *         "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
131
     *     },
132
     *     defaults={
133
     *         "namespace"="all",
134
     *         "start"=false,
135
     *         "end"=false,
136
     *         "offset"=false,
137
     *     }
138
     * ),
139
     * @param GlobalContribsRepository $globalContribsRepo
140
     * @param EditRepository $editRepo
141
     * @return Response
142
     * @codeCoverageIgnore
143
     */
144
    public function resultsAction(GlobalContribsRepository $globalContribsRepo, EditRepository $editRepo): Response
145
    {
146
        $globalContribs = $this->getGlobalContribs($globalContribsRepo, $editRepo);
147
        $defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
148
149
        return $this->render('globalContribs/result.html.twig', [
150
            'xtTitle' => $this->user->getUsername(),
0 ignored issues
show
Bug introduced by
The method getUsername() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
            'xtTitle' => $this->user->/** @scrutinizer ignore-call */ getUsername(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
            'xtPage' => 'GlobalContribs',
152
            'is_sub_request' => $this->isSubRequest,
153
            'user' => $this->user,
154
            'project' => $defaultProject,
155
            'gc' => $globalContribs,
156
        ]);
157
    }
158
159
    /************************ API endpoints ************************/
160
161
    /**
162
     * Get global edits made by a user, IP or IP range.
163
     * @Route(
164
     *     "/api/user/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
165
     *     name="UserApiGlobalContribs",
166
     *     requirements={
167
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
168
     *         "namespace"="|all|\d+",
169
     *         "start"="|\d*|\d{4}-\d{2}-\d{2}",
170
     *         "end"="|\d{4}-\d{2}-\d{2}",
171
     *         "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
172
     *     },
173
     *     defaults={
174
     *         "namespace"="all",
175
     *         "start"=false,
176
     *         "end"=false,
177
     *         "offset"=false,
178
     *         "limit"=50,
179
     *     },
180
     * )
181
     * @param GlobalContribsRepository $globalContribsRepo
182
     * @param EditRepository $editRepo
183
     * @return JsonResponse
184
     */
185
    public function resultsApiAction(
186
        GlobalContribsRepository $globalContribsRepo,
187
        EditRepository $editRepo
188
    ): JsonResponse {
189
        $this->recordApiUsage('user/globalcontribs');
190
191
        $globalContribs = $this->getGlobalContribs($globalContribsRepo, $editRepo);
192
        $defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
193
        $this->project = $defaultProject;
194
195
        $results = $globalContribs->globalEdits();
196
        $results = array_map(function (Edit $edit) {
197
            return $edit->getForJson(true, true);
198
        }, array_values($results));
199
        $results = $this->addFullPageTitlesAndContinue('globalcontribs', [], $results);
200
201
        return $this->getFormattedApiResponse($results);
202
    }
203
}
204