Processes.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. $url_params = array();
  57. $show_full_sql = ! empty($_POST['full']);
  58. if ($show_full_sql) {
  59. $url_params['full'] = 1;
  60. $full_text_link = 'server_status_processes.php' . Url::getCommon(
  61. array(), '?'
  62. );
  63. } else {
  64. $full_text_link = 'server_status_processes.php' . Url::getCommon(
  65. array('full' => 1)
  66. );
  67. }
  68. // This array contains display name and real column name of each
  69. // sortable column in the table
  70. $sortable_columns = array(
  71. array(
  72. 'column_name' => __('ID'),
  73. 'order_by_field' => 'Id'
  74. ),
  75. array(
  76. 'column_name' => __('User'),
  77. 'order_by_field' => 'User'
  78. ),
  79. array(
  80. 'column_name' => __('Host'),
  81. 'order_by_field' => 'Host'
  82. ),
  83. array(
  84. 'column_name' => __('Database'),
  85. 'order_by_field' => 'db'
  86. ),
  87. array(
  88. 'column_name' => __('Command'),
  89. 'order_by_field' => 'Command'
  90. ),
  91. array(
  92. 'column_name' => __('Time'),
  93. 'order_by_field' => 'Time'
  94. ),
  95. array(
  96. 'column_name' => __('Status'),
  97. 'order_by_field' => 'State'
  98. ),
  99. array(
  100. 'column_name' => __('Progress'),
  101. 'order_by_field' => 'Progress'
  102. ),
  103. array(
  104. 'column_name' => __('SQL query'),
  105. 'order_by_field' => 'Info'
  106. )
  107. );
  108. $sortableColCount = count($sortable_columns);
  109. $sql_query = $show_full_sql
  110. ? 'SHOW FULL PROCESSLIST'
  111. : 'SHOW PROCESSLIST';
  112. if ((! empty($_POST['order_by_field'])
  113. && ! empty($_POST['sort_order']))
  114. || (! empty($_POST['showExecuting']))
  115. ) {
  116. $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ';
  117. }
  118. if (! empty($_POST['showExecuting'])) {
  119. $sql_query .= ' WHERE state != "" ';
  120. }
  121. if (!empty($_POST['order_by_field']) && !empty($_POST['sort_order'])) {
  122. $sql_query .= ' ORDER BY '
  123. . Util::backquote($_POST['order_by_field'])
  124. . ' ' . $_POST['sort_order'];
  125. }
  126. $result = $GLOBALS['dbi']->query($sql_query);
  127. $retval = '<div class="responsivetable">';
  128. $retval .= '<table id="tableprocesslist" '
  129. . 'class="data clearfloat noclick sortable">';
  130. $retval .= '<thead>';
  131. $retval .= '<tr>';
  132. $retval .= '<th>' . __('Processes') . '</th>';
  133. foreach ($sortable_columns as $column) {
  134. $is_sorted = ! empty($_POST['order_by_field'])
  135. && ! empty($_POST['sort_order'])
  136. && ($_POST['order_by_field'] == $column['order_by_field']);
  137. $column['sort_order'] = 'ASC';
  138. if ($is_sorted && $_POST['sort_order'] === 'ASC') {
  139. $column['sort_order'] = 'DESC';
  140. }
  141. if (isset($_POST['showExecuting'])) {
  142. $column['showExecuting'] = 'on';
  143. }
  144. $retval .= '<th>';
  145. $columnUrl = Url::getCommon($column);
  146. $retval .= '<a href="server_status_processes.php' . $columnUrl . '" class="sortlink">';
  147. $retval .= $column['column_name'];
  148. if ($is_sorted) {
  149. $asc_display_style = 'inline';
  150. $desc_display_style = 'none';
  151. if ($_POST['sort_order'] === 'DESC') {
  152. $desc_display_style = 'inline';
  153. $asc_display_style = 'none';
  154. }
  155. $retval .= '<img class="icon ic_s_desc soimg" alt="'
  156. . __('Descending') . '" title="" src="themes/dot.gif" '
  157. . 'style="display: ' . $desc_display_style . '" />';
  158. $retval .= '<img class="icon ic_s_asc soimg hide" alt="'
  159. . __('Ascending') . '" title="" src="themes/dot.gif" '
  160. . 'style="display: ' . $asc_display_style . '" />';
  161. }
  162. $retval .= '</a>';
  163. if (0 === --$sortableColCount) {
  164. $retval .= '<a href="' . $full_text_link . '">';
  165. if ($show_full_sql) {
  166. $retval .= Util::getImage('s_partialtext',
  167. __('Truncate Shown Queries'), ['class' => 'icon_fulltext']);
  168. } else {
  169. $retval .= Util::getImage('s_fulltext',
  170. __('Show Full Queries'), ['class' => 'icon_fulltext']);
  171. }
  172. $retval .= '</a>';
  173. }
  174. $retval .= '</th>';
  175. }
  176. $retval .= '</tr>';
  177. $retval .= '</thead>';
  178. $retval .= '<tbody>';
  179. while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
  180. $retval .= self::getHtmlForServerProcessItem(
  181. $process,
  182. $show_full_sql
  183. );
  184. }
  185. $retval .= '</tbody>';
  186. $retval .= '</table>';
  187. $retval .= '</div>';
  188. return $retval;
  189. }
  190. /**
  191. * Returns the html for the list filter
  192. *
  193. * @return string
  194. */
  195. public static function getHtmlForProcessListFilter()
  196. {
  197. $showExecuting = '';
  198. if (! empty($_POST['showExecuting'])) {
  199. $showExecuting = ' checked="checked"';
  200. }
  201. $url_params = array(
  202. 'ajax_request' => true,
  203. 'full' => (isset($_POST['full']) ? $_POST['full'] : ''),
  204. 'column_name' => (isset($_POST['column_name']) ? $_POST['column_name'] : ''),
  205. 'order_by_field'
  206. => (isset($_POST['order_by_field']) ? $_POST['order_by_field'] : ''),
  207. 'sort_order' => (isset($_POST['sort_order']) ? $_POST['sort_order'] : ''),
  208. );
  209. $retval = '';
  210. $retval .= '<fieldset id="tableFilter">';
  211. $retval .= '<legend>' . __('Filters') . '</legend>';
  212. $retval .= '<form action="server_status_processes.php" method="post">';
  213. $retval .= Url::getHiddenInputs($url_params);
  214. $retval .= '<input type="submit" value="' . __('Refresh') . '" />';
  215. $retval .= '<div class="formelement">';
  216. $retval .= '<input' . $showExecuting . ' type="checkbox" name="showExecuting"'
  217. . ' id="showExecuting" class="autosubmit"/>';
  218. $retval .= '<label for="showExecuting">';
  219. $retval .= __('Show only active');
  220. $retval .= '</label>';
  221. $retval .= '</div>';
  222. $retval .= '</form>';
  223. $retval .= '</fieldset>';
  224. return $retval;
  225. }
  226. /**
  227. * Prints Every Item of Server Process
  228. *
  229. * @param array $process data of Every Item of Server Process
  230. * @param bool $show_full_sql show full sql or not
  231. *
  232. * @return string
  233. */
  234. public static function getHtmlForServerProcessItem(array $process, $show_full_sql)
  235. {
  236. // Array keys need to modify due to the way it has used
  237. // to display column values
  238. if ((! empty($_POST['order_by_field']) && ! empty($_POST['sort_order']))
  239. || (! empty($_POST['showExecuting']))
  240. ) {
  241. foreach (array_keys($process) as $key) {
  242. $new_key = ucfirst(mb_strtolower($key));
  243. if ($new_key !== $key) {
  244. $process[$new_key] = $process[$key];
  245. unset($process[$key]);
  246. }
  247. }
  248. }
  249. $retval = '<tr>';
  250. $retval .= '<td><a class="ajax kill_process" href="server_status_processes.php"'
  251. . ' data-post="' . Url::getCommon(['kill' => $process['Id']], '') . '">'
  252. . __('Kill') . '</a></td>';
  253. $retval .= '<td class="value">' . $process['Id'] . '</td>';
  254. $retval .= '<td>' . htmlspecialchars($process['User']) . '</td>';
  255. $retval .= '<td>' . htmlspecialchars($process['Host']) . '</td>';
  256. $retval .= '<td>' . ((! isset($process['db'])
  257. || strlen($process['db']) === 0)
  258. ? '<i>' . __('None') . '</i>'
  259. : htmlspecialchars($process['db'])) . '</td>';
  260. $retval .= '<td>' . htmlspecialchars($process['Command']) . '</td>';
  261. $retval .= '<td class="value">' . $process['Time'] . '</td>';
  262. $processStatusStr = empty($process['State']) ? '---' : $process['State'];
  263. $retval .= '<td>' . $processStatusStr . '</td>';
  264. $processProgress = empty($process['Progress']) ? '---' : $process['Progress'];
  265. $retval .= '<td>' . $processProgress . '</td>';
  266. $retval .= '<td>';
  267. if (empty($process['Info'])) {
  268. $retval .= '---';
  269. } else {
  270. $retval .= Util::formatSql($process['Info'], ! $show_full_sql);
  271. }
  272. $retval .= '</td>';
  273. $retval .= '</tr>';
  274. return $retval;
  275. }
  276. }