‪TYPO3CMS  ‪main
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 {
40  public function ‪getIdentifier(): string
41  {
42  return 'debug_queryinformation';
43  }
44 
48  public function ‪getLabel(): string
49  {
50  return $this->‪getLanguageService()->sL(
51  'LLL:EXT:adminpanel/Resources/Private/Language/locallang_debug.xlf:submodule.queryInformation.label'
52  );
53  }
54 
55  public function ‪getDataToStore(ServerRequestInterface $request): ‪ModuleData
56  {
57  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
58  $connection = $connectionPool->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
59 
60  $loggingMiddleware = null;
61  foreach ($connection->getConfiguration()->getMiddlewares() as $middleware) {
62  if ($middleware instanceof ‪DoctrineSqlLoggingMiddleware) {
63  $loggingMiddleware = $middleware;
64  break;
65  }
66  }
67 
68  $data = [];
69  if ($loggingMiddleware !== null) {
70  $queries = $loggingMiddleware->getQueries();
71  $data['totalQueries'] = count($queries);
72  $data['queries'] = $this->‪groupQueries($queries);
73  $data['totalTime'] = array_sum(array_column($queries, 'executionMS')) * 1000;
74  }
75  return new ‪ModuleData($data);
76  }
77 
78  public function ‪getContent(‪ModuleData $data): string
79  {
80  $view = new ‪StandaloneView();
81  $view->setTemplatePathAndFilename(
82  'EXT:adminpanel/Resources/Private/Templates/Modules/Debug/QueryInformation.html'
83  );
84  $view->assignMultiple($data->getArrayCopy());
85  $view->assign('languageKey', $this->‪getBackendUser()->user['lang'] ?? null);
86  return $view->render();
87  }
88 
89  protected function ‪groupQueries(array $queries): array
90  {
91  $groupedQueries = [];
92  foreach ($queries as $query) {
93  $backtraceString = json_encode($query['backtrace']);
94  if ($backtraceString === false) {
95  // skip entry if it can't be encoded
96  continue;
97  }
98  ‪$identifier = sha1($query['sql']) . sha1($backtraceString);
99  if (is_array($query['params'])) {
100  foreach ($query['params'] as $k => $param) {
101  if (is_array($param)) {
102  $query['params'][$k] = implode(',', $param);
103  }
104  }
105  }
106  if (isset($groupedQueries[‪$identifier])) {
107  $groupedQueries[‪$identifier]['count']++;
108  $groupedQueries[‪$identifier]['time'] += ($query['executionMS'] * 1000);
109  $groupedQueries[‪$identifier]['queries'][] = $query;
110  } else {
111  $groupedQueries[‪$identifier] = [
112  'sql' => $query['sql'],
113  'time' => $query['executionMS'] * 1000,
114  'count' => 1,
115  'queries' => [
116  $query,
117  ],
118  ];
119  }
120  }
121  uasort(
122  $groupedQueries,
123  static function (array $a, array $b): int {
124  return $b['time'] <=> $a['time'];
125  }
126  );
127  return $groupedQueries;
128  }
129 }
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getIdentifier
‪getIdentifier()
Definition: QueryInformation.php:40
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getLabel
‪getLabel()
Definition: QueryInformation.php:48
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\getDataToStore
‪getDataToStore(ServerRequestInterface $request)
Definition: QueryInformation.php:55
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getBackendUser
‪getBackendUser()
Definition: AbstractSubModule.php:35
‪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\getContent
‪getContent(ModuleData $data)
Definition: QueryInformation.php:78
‪TYPO3\CMS\Adminpanel\Log\DoctrineSqlLoggingMiddleware
Definition: DoctrineSqlLoggingMiddleware.php:30
‪TYPO3\CMS\Adminpanel\Modules\Debug\QueryInformation\groupQueries
‪groupQueries(array $queries)
Definition: QueryInformation.php:89
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule\getLanguageService
‪getLanguageService()
Definition: AbstractSubModule.php:30
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleData
Definition: ModuleData.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule
Definition: AbstractSubModule.php:29
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37