ViewQuery()   F
last analyzed

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 160
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 76
nc 122900
nop 0
dl 0
loc 160
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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