1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace AppBundle\Controller; |
5
|
|
|
|
6
|
|
|
use AppBundle\Helper\I18nHelper; |
7
|
|
|
use AppBundle\Model\LargestPages; |
8
|
|
|
use AppBundle\Repository\LargestPagesRepository; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
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 Largest Pages tool. |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
*/ |
19
|
|
|
class LargestPagesController extends XtoolsController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get the name of the tool's index route. This is also the name of the associated model. |
23
|
|
|
* @return string |
24
|
|
|
* @codeCoverageIgnore |
25
|
|
|
*/ |
26
|
|
|
public function getIndexRoute(): string |
27
|
|
|
{ |
28
|
|
|
return 'LargestPages'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* LargestPagesController constructor. |
33
|
|
|
* @param RequestStack $requestStack |
34
|
|
|
* @param ContainerInterface $container |
35
|
|
|
* @param I18nHelper $i18n |
36
|
|
|
*/ |
37
|
|
|
public function __construct(RequestStack $requestStack, ContainerInterface $container, I18nHelper $i18n) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($requestStack, $container, $i18n); |
40
|
|
|
$this->params['include_pattern'] = $this->request->get('include_pattern', ''); |
41
|
|
|
$this->params['exclude_pattern'] = $this->request->get('exclude_pattern', ''); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The search form. |
46
|
|
|
* @Route("/largestpages", name="LargestPages") |
47
|
|
|
* @return Response |
48
|
|
|
*/ |
49
|
|
|
public function indexAction(): Response |
50
|
|
|
{ |
51
|
|
|
// Redirect if required params are given. |
52
|
|
|
if (isset($this->params['project'])) { |
53
|
|
|
return $this->redirectToRoute('LargestPagesResult', $this->params); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->render('largestPages/index.html.twig', array_merge([ |
57
|
|
|
'xtPage' => 'LargestPages', |
58
|
|
|
'xtPageTitle' => 'tool-largestpages', |
59
|
|
|
'xtSubtitle' => 'tool-largestpages-desc', |
60
|
|
|
|
61
|
|
|
// Defaults that will get overriden if in $this->params. |
62
|
|
|
'project' => $this->project, |
63
|
|
|
'namespace' => 'all', |
64
|
|
|
'include_pattern' => '', |
65
|
|
|
'exclude_pattern' => '', |
66
|
|
|
], $this->params)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Instantiate a LargestPages object. |
71
|
|
|
* @return LargestPages |
72
|
|
|
*/ |
73
|
|
|
protected function getLargestPages(): LargestPages |
74
|
|
|
{ |
75
|
|
|
$largestPagesRepo = new LargestPagesRepository(); |
76
|
|
|
$largestPagesRepo->setContainer($this->container); |
77
|
|
|
$largestPages = new LargestPages( |
78
|
|
|
$this->project, |
79
|
|
|
$this->namespace, |
80
|
|
|
$this->params['include_pattern'], |
81
|
|
|
$this->params['exclude_pattern'] |
82
|
|
|
); |
83
|
|
|
$largestPages->setRepository($largestPagesRepo); |
84
|
|
|
return $largestPages; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Display the largest pages on the requested project. |
89
|
|
|
* @Route( |
90
|
|
|
* "/largestpages/{project}/{namespace}", |
91
|
|
|
* name="LargestPagesResult", |
92
|
|
|
* defaults={ |
93
|
|
|
* "namespace"="all" |
94
|
|
|
* } |
95
|
|
|
* ) |
96
|
|
|
* @return Response |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
public function resultsAction(): Response |
100
|
|
|
{ |
101
|
|
|
$ret = [ |
102
|
|
|
'xtPage' => 'LargestPages', |
103
|
|
|
'xtTitle' => $this->project->getDomain(), |
104
|
|
|
'lp' => $this->getLargestPages(), |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
return $this->getFormattedResponse('largestPages/result', $ret); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/************************ API endpoints ************************/ |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the largest pages on the requested project. |
114
|
|
|
* @Route( |
115
|
|
|
* "/api/project/largest_pages/{project}/{namespace}", |
116
|
|
|
* name="ProjectApiLargestPages", |
117
|
|
|
* defaults={ |
118
|
|
|
* "namespace"="all" |
119
|
|
|
* } |
120
|
|
|
* ) |
121
|
|
|
* @return JsonResponse |
122
|
|
|
*/ |
123
|
|
|
public function resultsApiAction(): JsonResponse |
124
|
|
|
{ |
125
|
|
|
$this->recordApiUsage('project/largest_pages'); |
126
|
|
|
$lp = $this->getLargestPages(); |
127
|
|
|
|
128
|
|
|
$pages = []; |
129
|
|
|
foreach ($lp->getResults() as $index => $page) { |
130
|
|
|
$pages[] = [ |
131
|
|
|
'rank' => $index + 1, |
132
|
|
|
'page_title' => $page->getTitle(true), |
133
|
|
|
'length' => $page->getLength(), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->getFormattedApiResponse([ |
138
|
|
|
'pages' => $pages, |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|