1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190:
<?php
if (!defined('SMF'))
die('No direct access...');
function ViewQuery()
{
global $scripturl, $settings, $context, $db_connection, $boarddir, $smcFunc, $txt, $db_show_debug;
if (!isset($db_show_debug) || $db_show_debug !== true || !isset($_SESSION['debug']))
fatal_lang_error('no_access', false);
isAllowedTo('admin_forum');
if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'hide')
{
$_SESSION['view_queries'] = $_SESSION['view_queries'] == 1 ? 0 : 1;
if (strpos($_SESSION['old_url'], 'action=viewquery') !== false)
redirectexit();
else
redirectexit($_SESSION['old_url']);
}
call_integration_hook('integrate_egg_nog');
$query_id = isset($_REQUEST['qq']) ? (int) $_REQUEST['qq'] - 1 : -1;
echo '<!DOCTYPE html>
<html', $context['right_to_left'] ? ' dir="rtl"' : '', '>
<head>
<title>', $context['forum_name_html_safe'], '</title>
<link rel="stylesheet" href="', $settings['theme_url'], '/css/index', $context['theme_variant'], '.css?alp21">
<style>
body
{
margin: 1ex;
}
body, td, th, .normaltext
{
font-size: x-small;
}
.smalltext
{
font-size: xx-small;
}
</style>
</head>
<body id="help_popup">
<div class="tborder windowbg description">';
foreach ($_SESSION['debug'] as $q => $query_data)
{
$query_data['q'] = ltrim(str_replace("\r", '', $query_data['q']), "\n");
$query = explode("\n", $query_data['q']);
$min_indent = 0;
foreach ($query as $line)
{
preg_match('/^(\t*)/', $line, $temp);
if (strlen($temp[0]) < $min_indent || $min_indent == 0)
$min_indent = strlen($temp[0]);
}
foreach ($query as $l => $dummy)
$query[$l] = substr($dummy, $min_indent);
$query_data['q'] = implode("\n", $query);
if (isset($query_data['f']))
$query_data['f'] = preg_replace('~^' . preg_quote($boarddir, '~') . '~', '...', $query_data['f']);
$is_select_query = substr(trim($query_data['q']), 0, 6) == 'SELECT';
if ($is_select_query)
$select = $query_data['q'];
elseif (preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
elseif (preg_match('~^CREATE TEMPORARY TABLE .+?(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
if ($is_select_query)
{
foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
if (strpos($select, $tmp) !== false)
{
$is_select_query = false;
break;
}
}
echo '
<div id="qq', $q, '" style="margin-bottom: 2ex;">
<a', $is_select_query ? ' href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '"' : '', ' style="font-weight: bold; text-decoration: none;">
', nl2br(str_replace("\t", ' ', $smcFunc['htmlspecialchars']($query_data['q']))), '
</a><br>';
if (!empty($query_data['f']) && !empty($query_data['l']))
echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']);
if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at']))
echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8));
else
echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8));
echo '
</div>';
if ($query_id == $q && $is_select_query)
{
$result = $smcFunc['db_query']('', '
EXPLAIN ' . ($smcFunc['db_title'] === POSTGRE_TITLE ? 'ANALYZE ' : '') . $select,
array(
)
);
if ($result === false)
{
echo '
<table>
<tr><td>', $smcFunc['db_error']($db_connection), '</td></tr>
</table>';
continue;
}
echo '
<table>';
$row = $smcFunc['db_fetch_assoc']($result);
echo '
<tr>
<th>' . implode('</th>
<th>', array_keys($row)) . '</th>
</tr>';
$smcFunc['db_data_seek']($result, 0);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
echo '
<tr>
<td>' . implode('</td>
<td>', $row) . '</td>
</tr>';
}
$smcFunc['db_free_result']($result);
echo '
</table>';
}
}
echo '
</div>
</body>
</html>';
obExit(false);
}
?>