From 387222b63ac0cc5daacf15969fdb7d50329e375b Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms <tleilax+studip@gmail.com> Date: Tue, 28 May 2024 10:19:47 +0000 Subject: [PATCH] refine debugbar integration, re #4220 Merge request studip/studip!3050 --- app/controllers/debugbar.php | 11 ++++- config/config_defaults.inc.php | 2 +- lib/bootstrap-definitions.php | 6 +-- lib/classes/Debug/TraceableStudipPDO.php | 24 +++++++++++ lib/classes/StudipPDO.class.php | 21 --------- templates/debug/db-log.php | 55 ------------------------ templates/debug/trace-row.php | 13 ------ templates/footer.php | 24 ----------- 8 files changed, 37 insertions(+), 119 deletions(-) create mode 100644 lib/classes/Debug/TraceableStudipPDO.php delete mode 100644 templates/debug/db-log.php delete mode 100644 templates/debug/trace-row.php diff --git a/app/controllers/debugbar.php b/app/controllers/debugbar.php index a80000d86d8..59627f27964 100644 --- a/app/controllers/debugbar.php +++ b/app/controllers/debugbar.php @@ -1,17 +1,24 @@ <?php final class DebugbarController extends Trails_Controller { + public function __construct( + Trails\Dispatcher $dispatcher, + private readonly DebugBar\DebugBar $debugbar + ) { + parent::__construct($dispatcher); + } + public function css_action(): void { $this->set_content_type('text/css;charset=utf-8'); $this->render_nothing(); - app()->get(DebugBar\DebugBar::class)->getJavascriptRenderer()->dumpCssAssets(); + $this->debugbar->getJavascriptRenderer()->dumpCssAssets(); } public function js_action(): void { $this->set_content_type('text/javascript;charset=utf-8'); $this->render_nothing(); - app()->get(DebugBar\DebugBar::class)->getJavascriptRenderer()->setIncludeVendors(false)->dumpJsAssets(); + $this->debugbar->getJavascriptRenderer()->setIncludeVendors(false)->dumpJsAssets(); } } diff --git a/config/config_defaults.inc.php b/config/config_defaults.inc.php index de63800649a..78d53569a9f 100644 --- a/config/config_defaults.inc.php +++ b/config/config_defaults.inc.php @@ -14,7 +14,7 @@ $DB_STUDIP_HOST = $_ENV['MYSQL_HOST'] ?? 'localhost'; $DB_STUDIP_USER = $_ENV['MYSQL_USER'] ?? ''; $DB_STUDIP_PASSWORD = $_ENV['MYSQL_PASSWORD'] ?? ''; $DB_STUDIP_DATABASE = $_ENV['MYSQL_DATABASE'] ?? 'studip'; -$DEBUG_ALL_DB_QUERIES_WITH_TRACE = false; + /* // optional Stud.IP slave database $DB_STUDIP_SLAVE_HOST = "localhost"; diff --git a/lib/bootstrap-definitions.php b/lib/bootstrap-definitions.php index 2f59dc9bffc..8afb8ce5fbc 100644 --- a/lib/bootstrap-definitions.php +++ b/lib/bootstrap-definitions.php @@ -40,14 +40,14 @@ return [ // Future Improvements, not used/activated right now # $debugBar->addCollector(new MessagesCollector()); - # $debugBar->addCollector(new TimeDataCollector()); + $debugBar->addCollector(new TimeDataCollector()); $config = iterator_to_array(Config::getInstance()->getIterator()); ksort($config); $debugBar->addCollector(new DebugBar\DataCollector\ConfigCollector($config)); $pdo = $container->get(PDO::class); - if ($pdo instanceof DebugBar\DataCollector\PDO\TraceablePDO) { + if ($pdo instanceof Studip\Debug\TraceableStudipPDO) { $collector = new DebugBar\DataCollector\PDO\PDOCollector($pdo); $debugBar->addCollector($collector); } @@ -62,7 +62,7 @@ return [ ); if (Studip\Debug\DebugBar::isActivated()) { - $pdo = new DebugBar\DataCollector\PDO\TraceablePDO($pdo); + $pdo = new Studip\Debug\TraceableStudipPDO($pdo); } return $pdo; diff --git a/lib/classes/Debug/TraceableStudipPDO.php b/lib/classes/Debug/TraceableStudipPDO.php new file mode 100644 index 00000000000..6100c223ce2 --- /dev/null +++ b/lib/classes/Debug/TraceableStudipPDO.php @@ -0,0 +1,24 @@ +<?php +namespace Studip\Debug; + +use DebugBar\DataCollector\PDO\TraceablePDO; + +final class TraceableStudipPDO extends TraceablePDO +{ + /** + * Quotes a string for use in a query. + * + * @link http://php.net/manual/en/pdo.quote.php + * @param string $string The string to be quoted. + * @param int $parameter_type [optional] Provides a data type hint for drivers that have + * alternate quoting styles. + * @return string|bool A quoted string that is theoretically safe to pass into an SQL statement. + * Returns FALSE if the driver does not support quoting in this way. + */ + #[\ReturnTypeWillChange] + public function quote($string, $parameter_type = null) + { + return $this->pdo->quote($string, $parameter_type); + } + +} diff --git a/lib/classes/StudipPDO.class.php b/lib/classes/StudipPDO.class.php index 8fa3418bf95..8a1422b1a8e 100644 --- a/lib/classes/StudipPDO.class.php +++ b/lib/classes/StudipPDO.class.php @@ -23,7 +23,6 @@ class StudipPDO extends PDO // Counter for the queries sent to the database public $query_count = 0; - public $queries = []; /** * Verifies that the given SQL query only contains a single statement. @@ -42,26 +41,6 @@ class StudipPDO extends PDO // Count executed queries (this is placed here since this is the only // method that is executed on every call to the database) $this->query_count += 1; - - if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) { - $trace = debug_backtrace(); - - $classes = []; - if (isset($trace[2]['class']) && $trace[2]['class'] === 'SimpleORMap') { - $classes[] = 'sorm'; - } - if (isset($trace[1]) && $trace[1]['function'] === 'prepare') { - $classes[] = 'prepared'; - } - - $this->queries[] = [ - 'query' => implode("\n", array_filter(array_map('trim', explode("\n", $statement)))), - 'classes' => implode(' ', $classes), - 'trace' => $GLOBALS['DEBUG_ALL_DB_QUERIES_WITH_TRACE'] - ? array_slice($trace, 2) - : null, - ]; - } } /** diff --git a/templates/debug/db-log.php b/templates/debug/db-log.php deleted file mode 100644 index 979729503a1..00000000000 --- a/templates/debug/db-log.php +++ /dev/null @@ -1,55 +0,0 @@ -<? if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) : ?> - <style> - #all_db_queries td:first-child { - border-left: 4px solid transparent; - border-right: 4px solid red; - } - #all_db_queries .sorm td:first-child { - border-left-color: blue; - } - #all_db_queries .prepared td:first-child { - border-right-color: green; - } - #all_db_queries .query { - white-space: pre-wrap; - } - #all_db_queries ul { - counter-reset: queries -1; - } - #all_db_queries li:hover { - text-decoration: underline; - } - #all_db_queries li::before { - content: "#" counter(queries); - counter-increment: queries; - } - #all_db_queries li::before, - #all_db_queries span { - font-weight: lighter; - } - </style> - <div style="display: none;" id="all_db_queries"> - <table class="default"> - <tbody> - <? foreach ((array) DBManager::get()->queries as $query) : ?> - <tr class="<?= $query['classes'] ?>"> - <td> - <code class="query"><?= htmlReady($query['query']) ?></code> - </td> - <? if ($GLOBALS['DEBUG_ALL_DB_QUERIES_WITH_TRACE']) : ?> - <td> - <ul class="list-unstyled"> - <? foreach ($query['trace'] as $i => $row): ?> - <li> - <?= $this->render_partial('debug/trace-row.php', $row) ?> - </li> - <? endforeach; ?> - </ul> - </td> - <? endif ?> - </tr> - <? endforeach ?> - </tbody> - </table> - </div> -<? endif ?> diff --git a/templates/debug/trace-row.php b/templates/debug/trace-row.php deleted file mode 100644 index f3d29be0c87..00000000000 --- a/templates/debug/trace-row.php +++ /dev/null @@ -1,13 +0,0 @@ -<?php -$file = ltrim(str_replace($GLOBALS['STUDIP_BASE_PATH'], '', $file), '/'); -$trac = htmlReady("https://develop.studip.de/trac/browser/trunk/{$file}#L{$line}"); -?> -<code> - <?= htmlReady(sprintf( - '%s(%s)', - isset($class) ? "{$class}{$type}{$function}" : $function, - implode(', ', array_map(function ($arg) { return is_object($arg) ? get_class($arg) : (string) $arg; }, $args)) - )) ?> -</code> -<span>called at</span> -<a href="<?= $trac ?>" target="_blank"><?= htmlReady("{$file}:{$line}") ?></a> diff --git a/templates/footer.php b/templates/footer.php index 164b054cfff..63cb06471d6 100644 --- a/templates/footer.php +++ b/templates/footer.php @@ -9,29 +9,6 @@ htmlReady($GLOBALS['user']->perms)) ?> | <?= strftime('%x, %X') ?> - <? if (Studip\ENV === 'development'): ?> - [ - <? if (DBManager::get('studip') === DBManager::get('studip-slave')): ?> - <?= sprintf('%u db queries', DBManager::get('studip')->query_count) ?> - <? else: ?> - <?= sprintf( - 'M%u/S%u = %u db queries', - DBManager::get('studip')->query_count, - DBManager::get('studip-slave')->query_count, - DBManager::get('studip')->query_count + DBManager::get('studip-slave')->query_count - ) ?> - <? endif; ?> - / - <?= relsize(memory_get_peak_usage(true), false) ?> mem - / - <?= sprintf('%.5f sec', microtime(true) - $GLOBALS['STUDIP_STARTUP_TIME']) ?> - ] - <? if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) : ?> - <a href="" onClick="jQuery('#all_db_queries').toggle(); return false;"> - <?= Icon::create("code", "info_alt")->asImg(16, ['class' => "text-bottom"]) ?> - </a> - <? endif ?> - <? endif; ?> </div> <? endif; ?> @@ -57,5 +34,4 @@ <? endif; ?> </footer> <? endif; ?> -<?= $this->render_partial('debug/db-log.php') ?> <!-- Ende Footer --> -- GitLab