Processes.php 10 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. $url_params = array();
  57. $show_full_sql = ! empty($_REQUEST['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($_REQUEST['order_by_field'])
  113. && ! empty($_REQUEST['sort_order']))
  114. || (! empty($_REQUEST['showExecuting']))
  115. ) {
  116. $sql_query = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` ';
  117. }
  118. if (! empty($_REQUEST['showExecuting'])) {
  119. $sql_query .= ' WHERE state != "" ';
  120. }
  121. if (!empty($_REQUEST['order_by_field']) && !empty($_REQUEST['sort_order'])) {
  122. $sql_query .= ' ORDER BY '
  123. . Util::backquote($_REQUEST['order_by_field'])
  124. . ' ' . $_REQUEST['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($_REQUEST['order_by_field'])
  135. && ! empty($_REQUEST['sort_order'])
  136. && ($_REQUEST['order_by_field'] == $column['order_by_field']);
  137. $column['sort_order'] = 'ASC';
  138. if ($is_sorted && $_REQUEST['sort_order'] === 'ASC') {
  139. $column['sort_order'] = 'DESC';
  140. }
  141. if (isset($_REQUEST['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 ($_REQUEST['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', __('Truncate Shown Queries'));
  167. } else {
  168. $retval .= Util::getImage('s_fulltext', __('Show Full Queries'));
  169. }
  170. $retval .= '</a>';
  171. }
  172. $retval .= '</th>';
  173. }
  174. $retval .= '</tr>';
  175. $retval .= '</thead>';
  176. $retval .= '<tbody>';
  177. while ($process = $GLOBALS['dbi']->fetchAssoc($result)) {
  178. $retval .= self::getHtmlForServerProcessItem(
  179. $process,
  180. $show_full_sql
  181. );
  182. }
  183. $retval .= '</tbody>';
  184. $retval .= '</table>';
  185. $retval .= '</div>';
  186. return $retval;
  187. }
  188. /**
  189. * Returns the html for the list filter
  190. *
  191. * @return string
  192. */
  193. public static function getHtmlForProcessListFilter()
  194. {
  195. $showExecuting = '';
  196. if (! empty($_REQUEST['showExecuting'])) {
  197. $showExecuting = ' checked="checked"';
  198. }
  199. $url_params = array(
  200. 'ajax_request' => true,
  201. 'full' => (isset($_REQUEST['full']) ? $_REQUEST['full'] : ''),
  202. 'column_name' => (isset($_REQUEST['column_name']) ? $_REQUEST['column_name'] : ''),
  203. 'order_by_field'
  204. => (isset($_REQUEST['order_by_field']) ? $_REQUEST['order_by_field'] : ''),
  205. 'sort_order' => (isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : ''),
  206. );
  207. $retval = '';
  208. $retval .= '<fieldset id="tableFilter">';
  209. $retval .= '<legend>' . __('Filters') . '</legend>';
  210. $retval .= '<form action="server_status_processes.php">';
  211. $retval .= Url::getHiddenInputs($url_params);
  212. $retval .= '<input type="submit" value="' . __('Refresh') . '" />';
  213. $retval .= '<div class="formelement">';
  214. $retval .= '<input' . $showExecuting . ' type="checkbox" name="showExecuting"'
  215. . ' id="showExecuting" class="autosubmit"/>';
  216. $retval .= '<label for="showExecuting">';
  217. $retval .= __('Show only active');
  218. $retval .= '</label>';
  219. $retval .= '</div>';
  220. $retval .= '</form>';
  221. $retval .= '</fieldset>';
  222. return $retval;
  223. }
  224. /**
  225. * Prints Every Item of Server Process
  226. *
  227. * @param array $process data of Every Item of Server Process
  228. * @param bool $show_full_sql show full sql or not
  229. *
  230. * @return string
  231. */
  232. public static function getHtmlForServerProcessItem(array $process, $show_full_sql)
  233. {
  234. // Array keys need to modify due to the way it has used
  235. // to display column values
  236. if ((! empty($_REQUEST['order_by_field']) && ! empty($_REQUEST['sort_order']))
  237. || (! empty($_REQUEST['showExecuting']))
  238. ) {
  239. foreach (array_keys($process) as $key) {
  240. $new_key = ucfirst(mb_strtolower($key));
  241. if ($new_key !== $key) {
  242. $process[$new_key] = $process[$key];
  243. unset($process[$key]);
  244. }
  245. }
  246. }
  247. $url_params = array(
  248. 'kill' => $process['Id'],
  249. 'ajax_request' => true
  250. );
  251. $kill_process = 'server_status_processes.php' . Url::getCommon($url_params);
  252. $retval = '<tr>';
  253. $retval .= '<td><a class="ajax kill_process" href="' . $kill_process . '">'
  254. . __('Kill') . '</a></td>';
  255. $retval .= '<td class="value">' . $process['Id'] . '</td>';
  256. $retval .= '<td>' . htmlspecialchars($process['User']) . '</td>';
  257. $retval .= '<td>' . htmlspecialchars($process['Host']) . '</td>';
  258. $retval .= '<td>' . ((! isset($process['db'])
  259. || strlen($process['db']) === 0)
  260. ? '<i>' . __('None') . '</i>'
  261. : htmlspecialchars($process['db'])) . '</td>';
  262. $retval .= '<td>' . htmlspecialchars($process['Command']) . '</td>';
  263. $retval .= '<td class="value">' . $process['Time'] . '</td>';
  264. $processStatusStr = empty($process['State']) ? '---' : $process['State'];
  265. $retval .= '<td>' . $processStatusStr . '</td>';
  266. $processProgress = empty($process['Progress']) ? '---' : $process['Progress'];
  267. $retval .= '<td>' . $processProgress . '</td>';
  268. $retval .= '<td>';
  269. if (empty($process['Info'])) {
  270. $retval .= '---';
  271. } else {
  272. $retval .= Util::formatSql($process['Info'], ! $show_full_sql);
  273. }
  274. $retval .= '</td>';
  275. $retval .= '</tr>';
  276. return $retval;
  277. }
  278. }