/
QueryInformation.php
138 lines (129 loc) · 4.5 KB
/
QueryInformation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Adminpanel\Modules\Debug;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger;
use TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule;
use TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface;
use TYPO3\CMS\Adminpanel\ModuleApi\ModuleData;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Admin Panel Query Information module for showing SQL Queries
*
* @internal
*/
class QueryInformation extends AbstractSubModule implements DataProviderInterface
{
/**
* Identifier for this Sub-module,
* for example "preview" or "cache"
*
* @return string
*/
public function getIdentifier(): string
{
return 'debug_queryinformation';
}
/**
* Sub-Module label
*
* @return string
*/
public function getLabel(): string
{
return $this->getLanguageService()->sL(
'LLL:EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf:submodule.queryInformation.label'
);
}
/**
* @param ServerRequestInterface $request
* @return \TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
* @throws \Doctrine\DBAL\Exception
*/
public function getDataToStore(ServerRequestInterface $request): ModuleData
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$logger = $connection->getConfiguration()->getSQLLogger();
$data = [];
if ($logger instanceof DoctrineSqlLogger) {
$queries = $logger->getQueries();
$data['queries'] = $this->groupQueries($queries);
$data['totalTime'] = array_sum(array_column($queries, 'executionMS')) * 1000;
}
return new ModuleData($data);
}
/**
* @param \TYPO3\CMS\Adminpanel\ModuleApi\ModuleData $data
* @return string Returns content of admin panel
*/
public function getContent(ModuleData $data): string
{
$view = new StandaloneView();
$view->setTemplatePathAndFilename(
'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/QueryInformation.html'
);
$this->getLanguageService()->includeLLFile('EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf');
$view->assignMultiple($data->getArrayCopy());
$view->assign('languageKey', $this->getBackendUser()->user['lang']);
return $view->render();
}
/**
* @param array $queries
* @return array
*/
protected function groupQueries(array $queries): array
{
$groupedQueries = [];
foreach ($queries as $query) {
$backtraceString = json_encode($query['backtrace']);
if ($backtraceString === false) {
// skip entry if it can't be encoded
continue;
}
$identifier = sha1($query['sql']) . sha1($backtraceString);
if (is_array($query['params'])) {
foreach ($query['params'] as $k => $param) {
if (is_array($param)) {
$query['params'][$k] = implode(',', $param);
}
}
}
if (isset($groupedQueries[$identifier])) {
$groupedQueries[$identifier]['count']++;
$groupedQueries[$identifier]['time'] += ($query['executionMS'] * 1000);
$groupedQueries[$identifier]['queries'][] = $query;
} else {
$groupedQueries[$identifier] = [
'sql' => $query['sql'],
'time' => $query['executionMS'] * 1000,
'count' => 1,
'queries' => [
$query,
],
];
}
}
uasort(
$groupedQueries,
static function ($a, $b) {
return $b['time'] <=> $a['time'];
}
);
return $groupedQueries;
}
}