summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/DebugFormatter.php
blob: d9c84dbf6dea6959a4c13e74cf366538f369f2f2 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php

namespace SMW\Query;

use SMW\ProcessingErrorMsgHandler;
use SMWQuery as Query;

/**
 * @license GNU GPL v2+
 * @since 2.0
 *
 * @author mwjames
 * @author Markus Krötzsch
 */
class DebugFormatter {

	const JSON_FORMAT = 'json';

	/**
	 * @var boolean
	 */
	private static $explainFormat = '';

	/**
	 * @since 3.0
	 *
	 * @param string $explainFormat
	 */
	public static function setExplainFormat( $explainFormat ) {
		if ( $explainFormat === self::JSON_FORMAT ) {
			self::$explainFormat = $explainFormat;
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 *
	 * @return string
	 */
	public static function getFormat( $type ) {

		$format = '';

		// Use a more expressive explain output
		// https://dev.mysql.com/doc/refman/5.6/en/explain.html
		// https://mariadb.com/kb/en/mariadb/explain-formatjson-in-mysql/
		if ( $type === 'mysql' && self::$explainFormat === self::JSON_FORMAT ) {
			$format = 'FORMAT=json';
		}

		return $format;
	}

	/**
	 * Generate textual debug output that shows an arbitrary list of informative
	 * fields. Used for formatting query debug output.
	 *
	 * @note All strings given must be usable and safe in wiki and HTML
	 * contexts.
	 *
	 * @param $storeName string name of the storage backend for which this is generated
	 * @param $entries array of name => value of informative entries to display
	 * @param $query SMWQuery or null, if given add basic data about this query as well
	 *
	 * @return string
	 */
	public static function getStringFrom( $storeName, array $entries, Query $query = null ) {

		if ( $query instanceof Query ) {
			$preEntries = [];
			$preEntries['ASK Query'] = '<div class="smwpre">' . str_replace( '[', '&#91;', $query->getDescription()->getQueryString() ) . '</div>';
			$entries = array_merge( $preEntries, $entries );
			$entries['Query Metrics'] = 'Query-Size:' . $query->getDescription()->getSize() . '<br />' .
						'Query-Depth:' . $query->getDescription()->getDepth();
			$errors = '';

			$queryErrors = ProcessingErrorMsgHandler::normalizeAndDecodeMessages(
				$query->getErrors()
			);

			foreach ( $queryErrors as $error ) {
				$errors .= $error . '<br />';
			}

			if ( $errors === '' ) {
				$errors = 'None';
			}

			$entries['Errors and Warnings'] = $errors;
		}

		$result = '<div class="smw-debug" style="border: 5px dotted #ffcc00; background: #FFF0BD; padding: 20px; margin-bottom: 10px;">' .
		          "<div class='smw-column-header'><big>$storeName debug output</big></div>";

		foreach ( $entries as $header => $information ) {
			$result .= "<div class='smw-column-header'>$header</div>";

			if ( $information !== '' ) {
				$result .= "$information";
			}
		}

		$result .= '</div>';

		return $result;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $type
	 * @param array $rows
	 *
	 * @return string
	 */
	public static function prettifyExplain( $type, $res ) {

		$output = '';

		// https://dev.mysql.com/doc/refman/5.0/en/explain-output.html
		if ( $type === 'mysql' ) {
			$output .= '<div class="smwpre" style="word-break:normal;">' .
			'<table class="" style="border-spacing: 5px;"><tr>' .
			'<th style="text-align: left;">ID</th>'.
			'<th style="text-align: left;">select_type</th>'.
			'<th style="text-align: left;">table</th>'.
			'<th style="text-align: left;">type</th>'.
			'<th style="text-align: left;">possible_keys</th>'.
			'<th style="text-align: left;">key</th>'.
			'<th style="text-align: left;">key_len</th>'.
			'<th style="text-align: left;">ref</th>'.
			'<th style="text-align: left;">rows</th>'.
			'<th style="text-align: left;">Extra</th></tr>';

			foreach ( $res as $row ) {

				if ( isset( $row->EXPLAIN ) ) {
					return '<div class="smwpre">' . $row->EXPLAIN . '</div>';
				}

				$output .= "<tr><td>" . $row->id .
				"</td><td>" . $row->select_type .
				"</td><td>" . $row->table .
				"</td><td>" . $row->type  .
				"</td><td>" . $row->possible_keys .
				"</td><td>" . $row->key .
				"</td><td>" . $row->key_len .
				"</td><td>" . $row->ref .
				"</td><td>" . $row->rows .
				"</td><td>" . $row->Extra . "</td></tr>";
			}

			$output .= '</table></div>';
		}

		if ( $type === 'postgres' ) {
			$output .= '<div class="smwpre">';

			foreach ( $res as $row ) {
				foreach ( $row as $key => $value ) {
					$output .= str_replace( [ ' ', '->' ], [ '&nbsp;', '└── ' ], $value ) .'<br>';
				}
			}

			$output .= '</div>';
		}

		// SQlite doesn't support this
		if ( $type === 'sqlite' ) {
			$output .= 'Not supported.';
		}

		return $output;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $sparql
	 *
	 * @return string
	 */
	public static function prettifySparql( $sparql ) {

		$sparql =  str_replace(
			[
				'[',
				':',
				' ',
				'<',
				'>'
			],
			[
				'&#91;',
				'&#x003A;',
				'&#x0020;',
				'&#x3C;',
				'&#x3E;'
			],
			$sparql
		);

		return '<div class="smwpre">' . $sparql . '</div>';
	}

	/**
	 * @since 2.5
	 *
	 * @param string $sql
	 * @param string $alias
	 *
	 * @return string
	 */
	public static function prettifySql( $sql, $alias ) {

		$sql = str_replace(
			[
				"SELECT DISTINCT",
				"FROM",
				"INNER JOIN",
				"LEFT OUTER JOIN",
				"LEFT JOIN",
				"RIGHT JOIN",
				"WHERE",
				"ORDER BY",
				"GROUP BY",
				"LIMIT",
				"OFFSET",
				"AND $alias.smw_",
				",$alias.smw_",
				"AND (",
				"))",
				"(("
			],
			[
				"SELECT DISTINCT<br>&nbsp;",
				"<br>FROM<br>&nbsp;",
				"<br>INNER JOIN<br>&nbsp;",
				"<br>LEFT OUTER JOIN<br>&nbsp;",
				"<br>LEFT JOIN<br>&nbsp;",
				"<br>RIGHT JOIN<br>&nbsp;",
				"<br>WHERE<br>&nbsp;",
				"<br>ORDER BY<br>&nbsp;",
				"<br>GROUP BY<br>&nbsp;",
				"<br>LIMIT<br>&nbsp;",
				"<br>OFFSET<br>&nbsp;",
				"<br>&nbsp;&nbsp;AND $alias.smw_",
				",<br>&nbsp;&nbsp;$alias.smw_",
				"<br>&nbsp;&nbsp;&nbsp;AND (",
				")<br>&nbsp;&nbsp;)",
				"(<br>&nbsp;&nbsp;&nbsp;("
			],
			$sql
		);

		return '<div class="smwpre">' . $sql . '</div>';
	}

}