ViewQuery.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 2013 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, $user_info, $settings, $context, $db_connection;
  28. global $modSettings, $boarddir, $smcFunc, $txt, $db_show_debug;
  29. // We should have debug mode enabled, as well as something to display!
  30. if (!isset($db_show_debug) || $db_show_debug !== true || !isset($_SESSION['debug']))
  31. fatal_lang_error('no_access', false);
  32. // Don't allow except for administrators.
  33. isAllowedTo('admin_forum');
  34. // If we're just hiding/showing, do it now.
  35. if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'hide')
  36. {
  37. $_SESSION['view_queries'] = $_SESSION['view_queries'] == 1 ? 0 : 1;
  38. if (strpos($_SESSION['old_url'], 'action=viewquery') !== false)
  39. redirectexit();
  40. else
  41. redirectexit($_SESSION['old_url']);
  42. }
  43. call_integration_hook('integrate_egg_nog');
  44. $query_id = isset($_REQUEST['qq']) ? (int) $_REQUEST['qq'] - 1 : -1;
  45. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  46. <html xmlns="http://www.w3.org/1999/xhtml"', $context['right_to_left'] ? ' dir="rtl"' : '', '>
  47. <head>
  48. <title>', $context['forum_name_html_safe'], '</title>
  49. <link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/index', $context['theme_variant'], '.css?alp21" />
  50. <style type="text/css">
  51. body
  52. {
  53. margin: 1ex;
  54. }
  55. body, td, th, .normaltext
  56. {
  57. font-size: x-small;
  58. }
  59. .smalltext
  60. {
  61. font-size: xx-small;
  62. }
  63. </style>
  64. </head>
  65. <body id="help_popup">
  66. <div class="tborder windowbg description">';
  67. foreach ($_SESSION['debug'] as $q => $query_data)
  68. {
  69. // Fix the indentation....
  70. $query_data['q'] = ltrim(str_replace("\r", '', $query_data['q']), "\n");
  71. $query = explode("\n", $query_data['q']);
  72. $min_indent = 0;
  73. foreach ($query as $line)
  74. {
  75. preg_match('/^(\t*)/', $line, $temp);
  76. if (strlen($temp[0]) < $min_indent || $min_indent == 0)
  77. $min_indent = strlen($temp[0]);
  78. }
  79. foreach ($query as $l => $dummy)
  80. $query[$l] = substr($dummy, $min_indent);
  81. $query_data['q'] = implode("\n", $query);
  82. // Make the filenames look a bit better.
  83. if (isset($query_data['f']))
  84. $query_data['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $query_data['f']);
  85. $is_select_query = substr(trim($query_data['q']), 0, 6) == 'SELECT';
  86. if ($is_select_query)
  87. $select = $query_data['q'];
  88. elseif (preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
  89. {
  90. $is_select_query = true;
  91. $select = $matches[1];
  92. }
  93. elseif (preg_match('~^CREATE TEMPORARY TABLE .+?(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
  94. {
  95. $is_select_query = true;
  96. $select = $matches[1];
  97. }
  98. // Temporary tables created in earlier queries are not explainable.
  99. if ($is_select_query)
  100. {
  101. foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
  102. if (strpos($select, $tmp) !== false)
  103. {
  104. $is_select_query = false;
  105. break;
  106. }
  107. }
  108. echo '
  109. <div id="qq', $q, '" style="margin-bottom: 2ex;">
  110. <a', $is_select_query ? ' href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '"' : '', ' style="font-weight: bold; text-decoration: none;">
  111. ', nl2br(str_replace("\t", '&nbsp;&nbsp;&nbsp;', $smcFunc['htmlspecialchars']($query_data['q']))), '
  112. </a><br />';
  113. if (!empty($query_data['f']) && !empty($query_data['l']))
  114. echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']);
  115. if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at']))
  116. echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8));
  117. else
  118. echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8));
  119. echo '
  120. </div>';
  121. // Explain the query.
  122. if ($query_id == $q && $is_select_query)
  123. {
  124. $result = $smcFunc['db_query']('', '
  125. EXPLAIN ' . $select,
  126. array(
  127. )
  128. );
  129. if ($result === false)
  130. {
  131. echo '
  132. <table border="1" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">
  133. <tr><td>', $smcFunc['db_error']($db_connection), '</td></tr>
  134. </table>';
  135. continue;
  136. }
  137. echo '
  138. <table border="1" rules="all" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">';
  139. $row = $smcFunc['db_fetch_assoc']($result);
  140. echo '
  141. <tr>
  142. <th>' . implode('</th>
  143. <th>', array_keys($row)) . '</th>
  144. </tr>';
  145. $smcFunc['db_data_seek']($result, 0);
  146. while ($row = $smcFunc['db_fetch_assoc']($result))
  147. {
  148. echo '
  149. <tr>
  150. <td>' . implode('</td>
  151. <td>', $row) . '</td>
  152. </tr>';
  153. }
  154. $smcFunc['db_free_result']($result);
  155. echo '
  156. </table>';
  157. }
  158. }
  159. echo '
  160. </div>
  161. </body>
  162. </html>';
  163. obExit(false);
  164. }
  165. ?>