‪TYPO3CMS  10.4
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['queries'] = $this->‪groupQueries($queries) ?? [];
73  $data['totalTime'] = array_sum(array_column($queries, 'executionMS')) * 1000;
74  }
75  return new ‪ModuleData($data);
76  }
77 
82  public function ‪getContent(‪ModuleData $data): string
83  {
84  $view = new ‪StandaloneView();
85  $view->setTemplatePathAndFilename(
86  'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/QueryInformation.html'
87  );
88  $this->‪getLanguageService()->‪includeLLFile('EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf');
89  $view->assignMultiple($data->getArrayCopy());
90  $view->assign('languageKey', $this->‪getBackendUser()->user['lang']);
91  return $view->render();
92  }
93 
98  protected function ‪groupQueries(array $queries): array
99  {
100  $groupedQueries = [];
101  foreach ($queries as $query) {
102  $backtraceString = json_encode($query['backtrace']);
103  if ($backtraceString === false) {
104  // skip entry if it can't be encoded
105  continue;
106  }
107  $identifier = sha1($query['sql']) . sha1($backtraceString);
108  if (is_array($query['params'])) {
109  foreach ($query['params'] as $k => $param) {
110  if (is_array($param)) {
111  $query['params'][$k] = implode(',', $param);
112  }
113  }
114  }
115  if (isset($groupedQueries[$identifier])) {
116  $groupedQueries[$identifier]['count']++;
117  $groupedQueries[$identifier]['time'] += ($query['executionMS'] * 1000);
118  $groupedQueries[$identifier]['queries'][] = $query;
119  } else {
120  $groupedQueries[$identifier] = [
121  'sql' => $query['sql'],
122  'time' => $query['executionMS'] * 1000,
123  'count' => 1,
124  'queries' => [
125  $query,
126  ],
127  ];
128  }
129  }
130  uasort(
131  $groupedQueries,
132  static function ($a, $b) {
133  return $b['time'] <=> $a['time'];
134  }
135  );
136  return $groupedQueries;
137  }
138 }
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getContent
‪string getContent(ModuleData $data)
Definition: QueryInformation.php:82
‪TYPO3\CMS\Core\Localization\LanguageService\includeLLFile
‪array includeLLFile($fileRef, $setGlobal=null, $mergeLocalOntoDefault=null)
Definition: LanguageService.php:297
‪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:194
‪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:34
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\groupQueries
‪array groupQueries(array $queries)
Definition: QueryInformation.php:98
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:27
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪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