Profiling.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use PhpMyAdmin\Utils\SessionCache;
  5. use function is_array;
  6. /**
  7. * Statement resource usage.
  8. */
  9. final class Profiling
  10. {
  11. public static function isSupported(DatabaseInterface $dbi): bool
  12. {
  13. if (SessionCache::has('profiling_supported')) {
  14. return SessionCache::get('profiling_supported');
  15. }
  16. /**
  17. * 5.0.37 has profiling but for example, 5.1.20 does not
  18. * (avoid a trip to the server for MySQL before 5.0.37)
  19. * and do not set a constant as we might be switching servers
  20. */
  21. if ($dbi->fetchValue('SELECT @@have_profiling')) {
  22. SessionCache::set('profiling_supported', true);
  23. return true;
  24. }
  25. SessionCache::set('profiling_supported', false);
  26. return false;
  27. }
  28. public static function enable(DatabaseInterface $dbi): void
  29. {
  30. if (! isset($_SESSION['profiling']) || ! self::isSupported($dbi)) {
  31. return;
  32. }
  33. $dbi->query('SET PROFILING=1;');
  34. }
  35. /** @return array<string, string>|null */
  36. public static function getInformation(DatabaseInterface $dbi): ?array
  37. {
  38. if (! isset($_SESSION['profiling']) || ! self::isSupported($dbi)) {
  39. return null;
  40. }
  41. $result = $dbi->fetchResult('SHOW PROFILE;');
  42. if (! is_array($result)) {
  43. return null;
  44. }
  45. return $result;
  46. }
  47. /**
  48. * Check if profiling was requested and remember it.
  49. */
  50. public static function check(DatabaseInterface $dbi, Response $response): void
  51. {
  52. if (isset($_REQUEST['profiling']) && self::isSupported($dbi)) {
  53. $_SESSION['profiling'] = true;
  54. } elseif (isset($_REQUEST['profiling_form'])) {
  55. // the checkbox was unchecked
  56. unset($_SESSION['profiling']);
  57. }
  58. if (! isset($_SESSION['profiling'])) {
  59. return;
  60. }
  61. $scripts = $response->getHeader()->getScripts();
  62. $scripts->addFiles([
  63. 'chart.js',
  64. 'vendor/jqplot/jquery.jqplot.js',
  65. 'vendor/jqplot/plugins/jqplot.pieRenderer.js',
  66. 'vendor/jqplot/plugins/jqplot.highlighter.js',
  67. 'vendor/jquery/jquery.tablesorter.js',
  68. ]);
  69. }
  70. }