|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
|
14
|
|
|
|
|
15
|
|
|
use craft\db\Query; |
|
|
|
|
|
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
|
|
17
|
|
|
use craft\web\Controller; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use yii\web\ForbiddenHttpException; |
|
|
|
|
|
|
20
|
|
|
use yii\web\Response; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
|
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
24
|
|
|
* @package Webperf |
|
|
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
|
|
27
|
|
|
class TablesController extends Controller |
|
28
|
|
|
{ |
|
29
|
|
|
// Constants |
|
30
|
|
|
// ========================================================================= |
|
31
|
|
|
|
|
32
|
|
|
// Protected Properties |
|
33
|
|
|
// ========================================================================= |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @var bool|array |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
protected $allowAnonymous = []; |
|
39
|
|
|
|
|
40
|
|
|
// Public Methods |
|
41
|
|
|
// ========================================================================= |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Handle requests for the dashboard statistics table |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $sort |
|
|
|
|
|
|
47
|
|
|
* @param int $page |
|
|
|
|
|
|
48
|
|
|
* @param int $per_page |
|
|
|
|
|
|
49
|
|
|
* @param string $filter |
|
|
|
|
|
|
50
|
|
|
* @param null $siteId |
|
|
|
|
|
|
51
|
|
|
* |
|
52
|
|
|
* @return Response |
|
53
|
|
|
* @throws ForbiddenHttpException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function actionPagesIndex( |
|
56
|
|
|
string $sort = 'pageLoad|desc', |
|
57
|
|
|
int $page = 1, |
|
58
|
|
|
int $per_page = 20, |
|
59
|
|
|
$filter = '', |
|
60
|
|
|
$siteId = 0 |
|
61
|
|
|
): Response { |
|
62
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
|
63
|
|
|
$data = []; |
|
64
|
|
|
$sortField = 'pageLoad'; |
|
65
|
|
|
$sortType = 'DESC'; |
|
66
|
|
|
// Figure out the sorting type |
|
67
|
|
|
if ($sort !== '') { |
|
68
|
|
|
if (strpos($sort, '|') === false) { |
|
69
|
|
|
$sortField = $sort; |
|
70
|
|
|
} else { |
|
71
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
// Query the db table |
|
75
|
|
|
$offset = ($page - 1) * $per_page; |
|
76
|
|
|
$query = (new Query()) |
|
77
|
|
|
->select([ |
|
|
|
|
|
|
78
|
|
|
'url', |
|
79
|
|
|
'MIN(title) AS title', |
|
80
|
|
|
'COUNT(url) AS cnt', |
|
81
|
|
|
'AVG(pageLoad) AS pageLoad', |
|
82
|
|
|
'AVG(craftDbCnt) AS craftDbCnt', |
|
83
|
|
|
'AVG(craftTwigCnt) AS craftTwigCnt', |
|
84
|
|
|
'AVG(craftOtherCnt) AS craftOtherCnt', |
|
85
|
|
|
'AVG(craftTotalMemory) AS craftTotalMemory', |
|
86
|
|
|
]) |
|
|
|
|
|
|
87
|
|
|
->from(['{{%webperf_data_samples}}']) |
|
88
|
|
|
->offset($offset); |
|
89
|
|
|
if ((int)$siteId !== 0) { |
|
90
|
|
|
$query->where(['siteId' => $siteId]); |
|
91
|
|
|
} |
|
92
|
|
|
if ($filter !== '') { |
|
93
|
|
|
$query->where(['like', 'url', $filter]); |
|
94
|
|
|
$query->orWhere(['like', 'title', $filter]); |
|
95
|
|
|
} |
|
96
|
|
|
$query |
|
97
|
|
|
->orderBy("{$sortField} {$sortType}") |
|
98
|
|
|
->groupBy('url') |
|
99
|
|
|
->limit($per_page); |
|
100
|
|
|
|
|
101
|
|
|
$stats = $query->all(); |
|
102
|
|
|
if ($stats) { |
|
103
|
|
|
// Decode any emojis in the title |
|
104
|
|
|
foreach ($stats as &$stat) { |
|
105
|
|
|
if (!empty($stat['title'])) { |
|
106
|
|
|
$stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
// Format the data for the API |
|
110
|
|
|
$data['data'] = $stats; |
|
111
|
|
|
$query = (new Query()) |
|
112
|
|
|
->from(['{{%webperf_data_samples}}']) |
|
113
|
|
|
->groupBy('url'); |
|
114
|
|
|
if ($filter !== '') { |
|
115
|
|
|
$query->where(['like', 'url', $filter]); |
|
116
|
|
|
$query->orWhere(['like', 'title', $filter]); |
|
117
|
|
|
} |
|
118
|
|
|
$count = $query->count(); |
|
119
|
|
|
$data['links']['pagination'] = [ |
|
120
|
|
|
'total' => $count, |
|
121
|
|
|
'per_page' => $per_page, |
|
122
|
|
|
'current_page' => $page, |
|
123
|
|
|
'last_page' => ceil($count / $per_page), |
|
124
|
|
|
'next_page_url' => null, |
|
125
|
|
|
'prev_page_url' => null, |
|
126
|
|
|
'from' => $offset + 1, |
|
127
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->asJson($data); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Handle requests for the dashboard redirects table |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $sort |
|
|
|
|
|
|
138
|
|
|
* @param int $page |
|
|
|
|
|
|
139
|
|
|
* @param int $per_page |
|
|
|
|
|
|
140
|
|
|
* @param string $filter |
|
|
|
|
|
|
141
|
|
|
* @param null $siteId |
|
|
|
|
|
|
142
|
|
|
* |
|
143
|
|
|
* @return Response |
|
144
|
|
|
* @throws ForbiddenHttpException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function actionRedirects( |
|
147
|
|
|
string $sort = 'hitCount|desc', |
|
148
|
|
|
int $page = 1, |
|
149
|
|
|
int $per_page = 20, |
|
150
|
|
|
$filter = '', |
|
151
|
|
|
$siteId = 0 |
|
152
|
|
|
): Response { |
|
153
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:redirects'); |
|
154
|
|
|
$data = []; |
|
155
|
|
|
$sortField = 'hitCount'; |
|
156
|
|
|
$sortType = 'DESC'; |
|
157
|
|
|
// Figure out the sorting type |
|
158
|
|
|
if ($sort !== '') { |
|
159
|
|
|
if (strpos($sort, '|') === false) { |
|
160
|
|
|
$sortField = $sort; |
|
161
|
|
|
} else { |
|
162
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
// Query the db table |
|
166
|
|
|
$offset = ($page - 1) * $per_page; |
|
167
|
|
|
$query = (new Query()) |
|
168
|
|
|
->from(['{{%retour_static_redirects}}']) |
|
169
|
|
|
->offset($offset) |
|
170
|
|
|
->limit($per_page) |
|
171
|
|
|
->orderBy("{$sortField} {$sortType}"); |
|
172
|
|
|
if ((int)$siteId !== 0) { |
|
173
|
|
|
$query->where(['siteId' => $siteId]); |
|
174
|
|
|
} |
|
175
|
|
|
if ($filter !== '') { |
|
176
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
|
177
|
|
|
$query->orWhere(['like', 'redirectDestUrl', $filter]); |
|
178
|
|
|
} |
|
179
|
|
|
$redirects = $query->all(); |
|
180
|
|
|
// Add in the `deleteLink` field |
|
181
|
|
|
foreach ($redirects as &$redirect) { |
|
182
|
|
|
$redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']); |
|
183
|
|
|
$redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']); |
|
184
|
|
|
} |
|
185
|
|
|
// Format the data for the API |
|
186
|
|
|
if ($redirects) { |
|
187
|
|
|
$data['data'] = $redirects; |
|
188
|
|
|
$query = (new Query()) |
|
189
|
|
|
->from(['{{%retour_static_redirects}}']); |
|
190
|
|
|
if ($filter !== '') { |
|
191
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
|
192
|
|
|
$query->orWhere(['like', 'redirectDestUrl', $filter]); |
|
193
|
|
|
} |
|
194
|
|
|
$count = $query->count(); |
|
195
|
|
|
$data['links']['pagination'] = [ |
|
196
|
|
|
'total' => $count, |
|
197
|
|
|
'per_page' => $per_page, |
|
198
|
|
|
'current_page' => $page, |
|
199
|
|
|
'last_page' => ceil($count / $per_page), |
|
200
|
|
|
'next_page_url' => null, |
|
201
|
|
|
'prev_page_url' => null, |
|
202
|
|
|
'from' => $offset + 1, |
|
203
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
|
204
|
|
|
]; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $this->asJson($data); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Protected Methods |
|
211
|
|
|
// ========================================================================= |
|
212
|
|
|
} |
|
213
|
|
|
|