2
0

ViewQuery.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Functions concerned with viewing queries, and is used for debugging.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Show the database queries for debugging
  18. * What this does:
  19. * - Toggles the session variable 'view_queries'.
  20. * - Views a list of queries and analyzes them.
  21. * - Requires the admin_forum permission.
  22. * - Is accessed via ?action=viewquery.
  23. * - Strings in this function have not been internationalized.
  24. */
  25. function ViewQuery()
  26. {
  27. global $scripturl, $settings, $context, $db_connection, $boarddir, $smcFunc, $txt, $db_show_debug;
  28. // We should have debug mode enabled, as well as something to display!
  29. if (!isset($db_show_debug) || $db_show_debug !== true || !isset($_SESSION['debug']))
  30. fatal_lang_error('no_access', false);
  31. // Don't allow except for administrators.
  32. isAllowedTo('admin_forum');
  33. // If we're just hiding/showing, do it now.
  34. if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'hide')
  35. {
  36. $_SESSION['view_queries'] = $_SESSION['view_queries'] == 1 ? 0 : 1;
  37. if (strpos($_SESSION['old_url'], 'action=viewquery') !== false)
  38. redirectexit();
  39. else
  40. redirectexit($_SESSION['old_url']);
  41. }
  42. call_integration_hook('integrate_egg_nog');
  43. $query_id = isset($_REQUEST['qq']) ? (int) $_REQUEST['qq'] - 1 : -1;
  44. echo '<!DOCTYPE html>
  45. <html', $context['right_to_left'] ? ' dir="rtl"' : '', '>
  46. <head>
  47. <title>', $context['forum_name_html_safe'], '</title>
  48. <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/index', $context['theme_variant'], '.css?alp21" />
  49. <style type="text/css">
  50. body
  51. {
  52. margin: 1ex;
  53. }
  54. body, td, th, .normaltext
  55. {
  56. font-size: x-small;
  57. }
  58. .smalltext
  59. {
  60. font-size: xx-small;
  61. }
  62. </style>
  63. </head>
  64. <body id="help_popup">
  65. <div class="tborder windowbg description">';
  66. foreach ($_SESSION['debug'] as $q => $query_data)
  67. {
  68. // Fix the indentation....
  69. $query_data['q'] = ltrim(str_replace("\r", '', $query_data['q']), "\n");
  70. $query = explode("\n", $query_data['q']);
  71. $min_indent = 0;
  72. foreach ($query as $line)
  73. {
  74. preg_match('/^(\t*)/', $line, $temp);
  75. if (strlen($temp[0]) < $min_indent || $min_indent == 0)
  76. $min_indent = strlen($temp[0]);
  77. }
  78. foreach ($query as $l => $dummy)
  79. $query[$l] = substr($dummy, $min_indent);
  80. $query_data['q'] = implode("\n", $query);
  81. // Make the filenames look a bit better.
  82. if (isset($query_data['f']))
  83. $query_data['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $query_data['f']);
  84. $is_select_query = substr(trim($query_data['q']), 0, 6) == 'SELECT';
  85. if ($is_select_query)
  86. $select = $query_data['q'];
  87. elseif (preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
  88. {
  89. $is_select_query = true;
  90. $select = $matches[1];
  91. }
  92. elseif (preg_match('~^CREATE TEMPORARY TABLE .+?(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
  93. {
  94. $is_select_query = true;
  95. $select = $matches[1];
  96. }
  97. // Temporary tables created in earlier queries are not explainable.
  98. if ($is_select_query)
  99. {
  100. foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
  101. if (strpos($select, $tmp) !== false)
  102. {
  103. $is_select_query = false;
  104. break;
  105. }
  106. }
  107. echo '
  108. <div id="qq', $q, '" style="margin-bottom: 2ex;">
  109. <a', $is_select_query ? ' href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '"' : '', ' style="font-weight: bold; text-decoration: none;">
  110. ', nl2br(str_replace("\t", '&nbsp;&nbsp;&nbsp;', $smcFunc['htmlspecialchars']($query_data['q']))), '
  111. </a><br />';
  112. if (!empty($query_data['f']) && !empty($query_data['l']))
  113. echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']);
  114. if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at']))
  115. echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8));
  116. else
  117. echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8));
  118. echo '
  119. </div>';
  120. // Explain the query.
  121. if ($query_id == $q && $is_select_query)
  122. {
  123. $result = $smcFunc['db_query']('', '
  124. EXPLAIN ' . $select,
  125. array(
  126. )
  127. );
  128. if ($result === false)
  129. {
  130. echo '
  131. <table border="1" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">
  132. <tr><td>', $smcFunc['db_error']($db_connection), '</td></tr>
  133. </table>';
  134. continue;
  135. }
  136. echo '
  137. <table border="1" rules="all" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">';
  138. $row = $smcFunc['db_fetch_assoc']($result);
  139. echo '
  140. <tr>
  141. <th>' . implode('</th>
  142. <th>', array_keys($row)) . '</th>
  143. </tr>';
  144. $smcFunc['db_data_seek']($result, 0);
  145. while ($row = $smcFunc['db_fetch_assoc']($result))
  146. {
  147. echo '
  148. <tr>
  149. <td>' . implode('</td>
  150. <td>', $row) . '</td>
  151. </tr>';
  152. }
  153. $smcFunc['db_free_result']($result);
  154. echo '
  155. </table>';
  156. }
  157. }
  158. echo '
  159. </div>
  160. </body>
  161. </html>';
  162. obExit(false);
  163. }
  164. ?>