server_status_processes.lib.php 10 KB

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