Console.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Used to render the console of PMA's pages
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use function count;
  8. use function sprintf;
  9. /**
  10. * Class used to output the console
  11. */
  12. class Console
  13. {
  14. /**
  15. * Whether to display anything
  16. *
  17. * @access private
  18. * @var bool
  19. */
  20. private $isEnabled;
  21. /**
  22. * Whether we are servicing an ajax request.
  23. *
  24. * @access private
  25. * @var bool
  26. */
  27. private $isAjax;
  28. /** @var Relation */
  29. private $relation;
  30. /** @var Template */
  31. public $template;
  32. /**
  33. * Creates a new class instance
  34. */
  35. public function __construct()
  36. {
  37. global $dbi;
  38. $this->isEnabled = true;
  39. $this->relation = new Relation($dbi);
  40. $this->template = new Template();
  41. }
  42. /**
  43. * Set the ajax flag to indicate whether
  44. * we are servicing an ajax request
  45. *
  46. * @param bool $isAjax Whether we are servicing an ajax request
  47. */
  48. public function setAjax(bool $isAjax): void
  49. {
  50. $this->isAjax = $isAjax;
  51. }
  52. /**
  53. * Disables the rendering of the footer
  54. */
  55. public function disable(): void
  56. {
  57. $this->isEnabled = false;
  58. }
  59. /**
  60. * Renders the bookmark content
  61. *
  62. * @access public
  63. */
  64. public static function getBookmarkContent(): string
  65. {
  66. global $dbi;
  67. $template = new Template();
  68. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  69. if ($cfgBookmark) {
  70. $bookmarks = Bookmark::getList(
  71. $dbi,
  72. $GLOBALS['cfg']['Server']['user']
  73. );
  74. $count_bookmarks = count($bookmarks);
  75. if ($count_bookmarks > 0) {
  76. $welcomeMessage = sprintf(
  77. _ngettext(
  78. 'Showing %1$d bookmark (both private and shared)',
  79. 'Showing %1$d bookmarks (both private and shared)',
  80. $count_bookmarks
  81. ),
  82. $count_bookmarks
  83. );
  84. } else {
  85. $welcomeMessage = __('No bookmarks');
  86. }
  87. return $template->render('console/bookmark_content', [
  88. 'welcome_message' => $welcomeMessage,
  89. 'bookmarks' => $bookmarks,
  90. ]);
  91. }
  92. return '';
  93. }
  94. /**
  95. * Returns the list of JS scripts required by console
  96. *
  97. * @return array list of scripts
  98. */
  99. public function getScripts(): array
  100. {
  101. return ['console.js'];
  102. }
  103. /**
  104. * Renders the console
  105. *
  106. * @access public
  107. */
  108. public function getDisplay(): string
  109. {
  110. if (! $this->isAjax && $this->isEnabled) {
  111. $cfgBookmark = Bookmark::getParams(
  112. $GLOBALS['cfg']['Server']['user']
  113. );
  114. $image = Html\Generator::getImage('console', __('SQL Query Console'));
  115. $_sql_history = $this->relation->getHistory(
  116. $GLOBALS['cfg']['Server']['user']
  117. );
  118. $bookmarkContent = static::getBookmarkContent();
  119. return $this->template->render('console/display', [
  120. 'cfg_bookmark' => $cfgBookmark,
  121. 'image' => $image,
  122. 'sql_history' => $_sql_history,
  123. 'bookmark_content' => $bookmarkContent,
  124. ]);
  125. }
  126. return '';
  127. }
  128. }