‪TYPO3CMS  11.5
QueryInformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
28 
35 {
42  public function ‪getIdentifier(): string
43  {
44  return 'debug_queryinformation';
45  }
46 
52  public function ‪getLabel(): string
53  {
54  return $this->‪getLanguageService()->‪sL(
55  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf:submodule.queryInformation.label'
56  );
57  }
58 
64  public function ‪getDataToStore(ServerRequestInterface $request): ‪ModuleData
65  {
66  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
67  $connection = $connectionPool->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
68  $logger = $connection->getConfiguration()->getSQLLogger();
69  $data = [];
70  if ($logger instanceof ‪DoctrineSqlLogger) {
71  $queries = $logger->getQueries();
72  $data['totalQueries'] = count($queries);
73  $data['queries'] = $this->‪groupQueries($queries);
74  $data['totalTime'] = array_sum(array_column($queries, 'executionMS')) * 1000;
75  }
76  return new ‪ModuleData($data);
77  }
78 
83  public function ‪getContent(‪ModuleData $data): string
84  {
85  $view = new ‪StandaloneView();
86  $view->setTemplatePathAndFilename(
87  'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/QueryInformation.html'
88  );
89  $this->‪getLanguageService()->‪includeLLFile('EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf');
90  $view->assignMultiple($data->getArrayCopy());
91  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
92  return $view->render();
93  }
94 
99  protected function ‪groupQueries(array $queries): array
100  {
101  $groupedQueries = [];
102  foreach ($queries as $query) {
103  $backtraceString = json_encode($query['backtrace']);
104  if ($backtraceString === false) {
105  // skip entry if it can't be encoded
106  continue;
107  }
108  $identifier = sha1($query['sql']) . sha1($backtraceString);
109  if (is_array($query['params'])) {
110  foreach ($query['params'] as $k => $param) {
111  if (is_array($param)) {
112  $query['params'][$k] = implode(',', $param);
113  }
114  }
115  }
116  if (isset($groupedQueries[$identifier])) {
117  $groupedQueries[$identifier]['count']++;
118  $groupedQueries[$identifier]['time'] += ($query['executionMS'] * 1000);
119  $groupedQueries[$identifier]['queries'][] = $query;
120  } else {
121  $groupedQueries[$identifier] = [
122  'sql' => $query['sql'],
123  'time' => $query['executionMS'] * 1000,
124  'count' => 1,
125  'queries' => [
126  $query,
127  ],
128  ];
129  }
130  }
131  uasort(
132  $groupedQueries,
133  static function ($a, $b) {
134  return $b['time'] <=> $a['time'];
135  }
136  );
137  return $groupedQueries;
138  }
139 }
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getContent
‪string getContent(ModuleData $data)
Definition: QueryInformation.php:83
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractSubModule.php:35
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getIdentifier
‪string getIdentifier()
Definition: QueryInformation.php:42
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLogger
Definition: DoctrineSqlLogger.php:29
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:30
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getBackendUser
‪getBackendUser()
Definition: AbstractSubModule.php:40
‪TYPO3\CMS\Core\Database\ConnectionPool\DEFAULT_CONNECTION_NAME
‪const DEFAULT_CONNECTION_NAME
Definition: ConnectionPool.php:50
‪TYPO3\CMS\Adminpanel\Modules\Debug
Definition: Events.php:18
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation
Definition: QueryInformation.php:35
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getLabel
‪string getLabel()
Definition: QueryInformation.php:52
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\groupQueries
‪array groupQueries(array $queries)
Definition: QueryInformation.php:99
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:26
‪TYPO3\CMS\Core\Localization\LanguageService\includeLLFile
‪array includeLLFile($fileRef)
Definition: LanguageService.php:271
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule
Definition: AbstractSubModule.php:29
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getDataToStore
‪TYPO3 CMS Adminpanel ModuleApi ModuleData getDataToStore(ServerRequestInterface $request)
Definition: QueryInformation.php:64