Processes.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying processes list
  5. *
  6. * @usedby server_status_processes.php
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. namespace PhpMyAdmin\Server\Status;
  11. use PhpMyAdmin\Message;
  12. use PhpMyAdmin\Server\Status\Data;
  13. use PhpMyAdmin\Util;
  14. use PhpMyAdmin\Url;
  15. /**
  16. * PhpMyAdmin\Server\Status\Processes class
  17. *
  18. * @package PhpMyAdmin
  19. */
  20. class Processes
  21. {
  22. /**
  23. * Prints html for auto refreshing processes list
  24. *
  25. * @return string
  26. */
  27. public static function getHtmlForProcessListAutoRefresh()
  28. {
  29. $notice = Message::notice(
  30. __(
  31. 'Note: Enabling the auto refresh here might cause '
  32. . 'heavy traffic between the web server and the MySQL server.'
  33. )
  34. )->getDisplay();
  35. $retval = $notice . '<div class="tabLinks">';
  36. $retval .= '<label>' . __('Refresh rate') . ': ';
  37. $retval .= Data::getHtmlForRefreshList(
  38. 'refreshRate',
  39. 5,
  40. Array(2, 3, 4, 5, 10, 20, 40, 60, 120, 300, 600, 1200)
  41. );
  42. $retval .= '</label>';
  43. $retval .= '<a id="toggleRefresh" href="#">';
  44. $retval .= Util::getImage('play') . __('Start auto refresh');
  45. $retval .= '</a>';
  46. $retval .= '</div>';
  47. return $retval;
  48. }
  49. /**
  50. * Prints Server Process list
  51. *
  52. * @return string
  53. */
  54. public static function getHtmlForServerProcesslist()
  55. {
  56. $show_full_sql = ! empty($_POST['full']);
  57. // This array contains display name and real column name of each
  58. // sortable column in the table
  59. $sortable_columns = array(
  60. array(
  61. 'column_name' => __('ID'),
  62. 'order_by_field' => 'Id'
  63. ),
  64. array(
  65. 'column_name' => __('User'),
  66. 'order_by_field' => 'User'
  67. ),
  68. array(
  69. 'column_name' => __('Host'),
  70. 'order_by_field' => 'Host'
  71. ),
  72. array(
  73. 'column_name' => __('Database'),
  74. 'order_by_field' => 'db'
  75. ),
  76. array(
  77. 'column_name' => __('Command'),
  78. 'order_by_field' => 'Command'
  79. ),
  80. array(
  81. 'column_name' => __('Time'),
  82. 'order_by_field' => 'Time'
  83. ),
  84. array(
  85. 'column_name' => __('Status'),
  86. 'order_by_field' => 'State'
  87. ),
  88. array(
  89. 'column_name' => __('Progress'),
  90. 'order_by_field' => 'Progress'
  91. ),
  92. array(
  93. 'column_name' => __('SQL query'),
  94. 'order_by_field' => 'Info'
  95. )
  96. );
  97. $sortableColCount = count($sortable_columns);
  98. $sql_query = $show_full_sql
  99. ? 'SHOW FULL PROCESSLIST'
  100. : 'SHOW PROCESSLIST';
  101. if ((! empty($_POST['order_by_field'])
  102. && ! empty($_POST['sort_order']))
  103. || (! empty($_POST['showExecuting']))
  104. ) {
  105. $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ';
  106. }
  107. if (! empty($_POST['showExecuting'])) {
  108. $sql_query .= ' WHERE state != "" ';
  109. }
  110. if (!empty($_POST['order_by_field']) && !empty($_POST['sort_order'])) {
  111. $sql_query .= ' ORDER BY '
  112. . Util::backquote($_POST['order_by_field'])
  113. . ' ' . $_POST['sort_order'];
  114. }
  115. $result = $GLOBALS['dbi']->query($sql_query);
  116. $retval = '<div class="responsivetable">';
  117. $retval .= '<table id="tableprocesslist" '
  118. . 'class="data clearfloat noclick sortable">';
  119. $retval .= '<thead>';
  120. $retval .= '<tr>';
  121. $retval .= '<th>' . __('Processes') . '</th>';
  122. foreach ($sortable_columns as $column) {
  123. $is_sorted = ! empty($_POST['order_by_field'])
  124. && ! empty($_POST['sort_order'])
  125. && ($_POST['order_by_field'] == $column['order_by_field']);
  126. $column['sort_order'] = 'ASC';
  127. if ($is_sorted && $_POST['sort_order'] === 'ASC') {
  128. $column['sort_order'] = 'DESC';
  129. }
  130. if (isset($_POST['showExecuting'])) {
  131. $column['showExecuting'] = 'on';
  132. }
  133. $retval .= '<th>';
  134. $columnUrl = Url::getCommon($column, '', false);
  135. $retval .= '<a href="server_status_processes.php" data-post="' . $columnUrl . '" class="sortlink">';
  136. $retval .= $column['column_name'];
  137. if ($is_sorted) {
  138. $asc_display_style = 'inline';
  139. $desc_display_style = 'none';
  140. if ($_POST['sort_order'] === 'DESC') {
  141. $desc_display_style = 'inline';
  142. $asc_display_style = 'none';
  143. }
  144. $retval .= '<img class="icon ic_s_desc soimg" alt="'
  145. . __('Descending') . '" title="" src="themes/dot.gif" '
  146. . 'style="display: ' . $desc_display_style . '" />';
  147. $retval .= '<img class="icon ic_s_asc soimg hide" alt="'
  148. . __('Ascending') . '" title="" src="themes/dot.gif" '
  149. . 'style="display: ' . $asc_display_style . '" />';
  150. }
  151. $retval .= '</a>';
  152. if (0 === --$sortableColCount) {
  153. $url_params = array();
  154. if ($show_full_sql) {
  155. $url_params['full'] = '';
  156. } else {
  157. $url_params['full'] = 1;
  158. }
  159. if (isset($_POST['showExecuting'])) {
  160. $url_params['showExecuting'] = 'on';
  161. }
  162. if (isset($_POST['order_by_field'])) {
  163. $url_params['order_by_field'] = $_POST['order_by_field'];
  164. }
  165. if (isset($_POST['sort_order'])) {
  166. $url_params['sort_order'] = $_POST['sort_order'];
  167. }
  168. $retval .= '<a href="server_status_processes.php" data-post="' . Url::getCommon($url_params, '', false) . '" >';
  169. if ($show_full_sql) {
  170. $retval .= Util::getImage('s_partialtext',
  171. __('Truncate Shown Queries'), ['class' => 'icon_fulltext']);
  172. } else {
  173. $retval .= Util::getImage('s_fulltext',
  174. __('Show Full Queries'), ['class' => 'icon_fulltext']);
  175. }
  176. $retval .= '</a>';
  177. }
  178. $retval .= '</th>';
  179. }
  180. $retval .= '</tr>';
  181. $retval .= '</thead>';
  182. $retval .= '<tbody>';
  183. while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
  184. $retval .= self::getHtmlForServerProcessItem(
  185. $process,
  186. $show_full_sql
  187. );
  188. }
  189. $retval .= '</tbody>';
  190. $retval .= '</table>';
  191. $retval .= '</div>';
  192. return $retval;
  193. }
  194. /**
  195. * Returns the html for the list filter
  196. *
  197. * @return string
  198. */
  199. public static function getHtmlForProcessListFilter()
  200. {
  201. $showExecuting = '';
  202. if (! empty($_POST['showExecuting'])) {
  203. $showExecuting = ' checked="checked"';
  204. }
  205. $url_params = array(
  206. 'ajax_request' => true,
  207. 'full' => (isset($_POST['full']) ? $_POST['full'] : ''),
  208. 'column_name' => (isset($_POST['column_name']) ? $_POST['column_name'] : ''),
  209. 'order_by_field'
  210. => (isset($_POST['order_by_field']) ? $_POST['order_by_field'] : ''),
  211. 'sort_order' => (isset($_POST['sort_order']) ? $_POST['sort_order'] : ''),
  212. );
  213. $retval = '';
  214. $retval .= '<fieldset id="tableFilter">';
  215. $retval .= '<legend>' . __('Filters') . '</legend>';
  216. $retval .= '<form action="server_status_processes.php" method="post">';
  217. $retval .= Url::getHiddenInputs($url_params);
  218. $retval .= '<input type="submit" value="' . __('Refresh') . '" />';
  219. $retval .= '<div class="formelement">';
  220. $retval .= '<input' . $showExecuting . ' type="checkbox" name="showExecuting"'
  221. . ' id="showExecuting" class="autosubmit"/>';
  222. $retval .= '<label for="showExecuting">';
  223. $retval .= __('Show only active');
  224. $retval .= '</label>';
  225. $retval .= '</div>';
  226. $retval .= '</form>';
  227. $retval .= '</fieldset>';
  228. return $retval;
  229. }
  230. /**
  231. * Prints Every Item of Server Process
  232. *
  233. * @param array $process data of Every Item of Server Process
  234. * @param bool $show_full_sql show full sql or not
  235. *
  236. * @return string
  237. */
  238. public static function getHtmlForServerProcessItem(array $process, $show_full_sql)
  239. {
  240. // Array keys need to modify due to the way it has used
  241. // to display column values
  242. if ((! empty($_POST['order_by_field']) && ! empty($_POST['sort_order']))
  243. || (! empty($_POST['showExecuting']))
  244. ) {
  245. foreach (array_keys($process) as $key) {
  246. $new_key = ucfirst(mb_strtolower($key));
  247. if ($new_key !== $key) {
  248. $process[$new_key] = $process[$key];
  249. unset($process[$key]);
  250. }
  251. }
  252. }
  253. $retval = '<tr>';
  254. $retval .= '<td><a class="ajax kill_process" href="server_status_processes.php"'
  255. . ' data-post="' . Url::getCommon(['kill' => $process['Id']], '', false) . '">'
  256. . __('Kill') . '</a></td>';
  257. $retval .= '<td class="value">' . $process['Id'] . '</td>';
  258. $retval .= '<td>' . htmlspecialchars($process['User']) . '</td>';
  259. $retval .= '<td>' . htmlspecialchars($process['Host']) . '</td>';
  260. $retval .= '<td>' . ((! isset($process['db'])
  261. || strlen($process['db']) === 0)
  262. ? '<i>' . __('None') . '</i>'
  263. : htmlspecialchars($process['db'])) . '</td>';
  264. $retval .= '<td>' . htmlspecialchars($process['Command']) . '</td>';
  265. $retval .= '<td class="value">' . $process['Time'] . '</td>';
  266. $processStatusStr = empty($process['State']) ? '---' : $process['State'];
  267. $retval .= '<td>' . $processStatusStr . '</td>';
  268. $processProgress = empty($process['Progress']) ? '---' : $process['Progress'];
  269. $retval .= '<td>' . $processProgress . '</td>';
  270. $retval .= '<td>';
  271. if (empty($process['Info'])) {
  272. $retval .= '---';
  273. } else {
  274. $retval .= Util::formatSql($process['Info'], ! $show_full_sql);
  275. }
  276. $retval .= '</td>';
  277. $retval .= '</tr>';
  278. return $retval;
  279. }
  280. }