Completed
Push — master ( ba71b0...2b3d16 )
by cam
04:32
created

tracer.php ➔ trace_query_end()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 18
nop 5
dl 0
loc 22
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
if (!defined('_ECRIRE_INC_VERSION')) {
14
	return;
15
}
16
17
// https://code.spip.net/@trace_query_start
18
function trace_query_start() {
19
	static $trace = '?';
20
	if ($trace === '?' or defined('_DEBUG_TRACE_QUERIES')) {
21
		// gare au bouclage sur calcul de droits au premier appel
22
		// A fortiori quand on demande une trace
23
		if (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES) {
24
			$trace = true;
25
		}
26
		else {
27
			include_spip('inc/autoriser');
28
			$trace = (isset($_GET['var_profile']) and autoriser('debug'));
29
		}
30
	}
31
32
	return $trace ? microtime() : 0;
33
}
34
35
// https://code.spip.net/@trace_query_end
36
function trace_query_end($query, $start, $result, $erreur, $serveur = '') {
37
	static $trace = '?';
38
	if ($trace === '?') {
39
		$trace = isset($_GET['var_profile']) and (autoriser('debug'));
40
	}
41
	if ($start) {
42
		$end = microtime();
43
		list($usec, $sec) = explode(" ", $start);
44
		list($usec2, $sec2) = explode(" ", $end);
45
		$dt = $sec2 + $usec2 - $sec - $usec;
46
		pipeline('trig_trace_query', ['query' => $query, 'start' => $start, 'end' => $end, 'time' => $dt, 'result' => $result, 'erreur' => $erreur, 'serveur' => $serveur]);
47
		if ($trace) {
48
			trace_query_chrono($dt, $query, $result, $serveur);
49
		}
50
	}
51
	// tracer les erreurs, sauf pour select, c'est fait dans abstract_sql
52
	if ($trace and $erreur and !preg_match('/^select\b/i', $query)) {
53
		erreur_squelette(array(sql_errno($serveur), $erreur, $query));
54
	}
55
56
	return $result;
57
}
58
59
// https://code.spip.net/@trace_query_chrono
60
function trace_query_chrono($dt, $query, $result, $serveur = '') {
61
	include_spip('inc/filtres_mini');
62
	static $tt = 0, $nb = 0;
63
64
	$x = _request('var_mode_objet');
65
	if (isset($GLOBALS['debug']['aucasou'])) {
66
		list(, $boucle, $serveur, $contexte) = $GLOBALS['debug']['aucasou'];
67
		if ($x and !preg_match("/$boucle\$/", $x)) {
68
			return;
69
		}
70
		if ($serveur) {
71
			$boucle .= " ($serveur)";
72
		}
73
		$boucle = "<b>$boucle</b>";
74
	} else {
75
		if ($x) {
76
			return;
77
		}
78
		$boucle = $contexte = '';
79
	}
80
81
	$tt += $dt;
82
	$nb++;
83
84
	$q = preg_replace('/([a-z)`])\s+([A-Z])/', "$1\n<br />$2", spip_htmlentities($query));
85
	$e = sql_explain($query, $serveur);
86
	$r = str_replace('Resource id ', '', (is_object($result) ? get_class($result) : $result));
87
	$GLOBALS['tableau_des_temps'][] = array($dt, $nb, $boucle, $q, $e, $r, $contexte);
88
}
89
90
91
function chrono_requete($temps) {
92
	$total = 0;
93
	$hors = "<i>" . _T('zbug_hors_compilation') . "</i>";
94
	$t = $q = $n = $d = array();
95
	// Totaliser les temps et completer le Explain
96
	foreach ($temps as $key => $v) {
97
		list($dt, $nb, $boucle, $query, $explain, $res, $contexte) = $v;
98
		if (is_array($contexte)) {
99
			$k = ($contexte[0] . " $boucle");
100
			include_spip('public/compiler');
101
			$env = reconstruire_contexte_compil($contexte);
102
		} else {
103
			$k = $env = $boucle;
104
		}
105
106
		$total += $dt;
107
		$t[$key] = $dt;
108
		$q[$key] = $nb;
109
		if (!isset($d[$k])) {
110
			$d[$k] = 0;
111
			$n[$k] = 0;
112
		}
113
		$d[$k] += $dt;
114
		++$n[$k];
115
116
		if (!is_array($explain)) {
117
			$explain = array();
118
		}
119
		foreach ($explain as $j => $v) {
120
			$explain[$j] = "<tr><th>$j</th><td>"
121
				. str_replace(';', '<br />', $v)
122
				. "</td></tr>";
123
		}
124
		$e = "<table class='explain'>"
125
			. "<caption>"
126
			. $query
127
			. "</caption>"
128
			. "<tr><th>Time</th><td>$dt</td></tr>"
129
			. "<tr><th>Order</th><td>$nb</td></tr>"
130
			. "<tr><th>Res</th><td>$res</td></tr>"
131
			. join('', $explain)
132
			. "</table>";
133
134
		$temps[$key] = array($e, $env, $k);
135
	}
136
	// Trier par temps d'execution decroissant
137
	array_multisort($t, SORT_DESC, $q, $temps);
138
	arsort($d);
139
	$i = 1;
140
	$t = array();
141
	// Fabriquer les liens de navigations dans le tableau des temps
142
	foreach ($temps as $k => $v) {
143
		$titre = strip_tags($v[2]);
144
		$href = quote_amp($GLOBALS['REQUEST_URI']) . "#req$i";
145
		$href = str_replace("\\'", '&#39;', $href);
146
147
		if (!isset($t[$v[2]])) {
148
			$t[$v[2]] = array();
149
		}
150
		$t[$v[2]][] = "<span class='spip-debug-arg'> "
151
			. "<a title='$titre' href='$href'>$i</a>"
152
			. '</span>'
153
			. ((count($t[$v[2]]) % 10 == 9) ? "<br />" : '');
154
		$i++;
155
	}
156
157
	if ($d['']) {
158
		$d[$hors] = $d[''];
159
		$n[$hors] = $n[''];
160
		$t[$hors] = $t[''];
161
	}
162
	unset($d['']);
163
	// Fabriquer le tableau des liens de navigation dans le grand tableau
164
	foreach ($d as $k => $v) {
165
		$d[$k] = $n[$k] . "</td><td>$k</td><td class='time'>$v</td><td class='liste-reqs'>"
166
			. join('', $t[$k]);
167
	}
168
169
	$navigation = array(
170
		_T('zbug_statistiques'),
171
		"<tr><td>"
172
		. join("</td></tr>\n<tr><td>", $d)
173
		. "</td></tr>\n"
174
		. (# _request('var_mode_objet') ? '' :
175
		("<tr><td>" . count($temps) . "</td><td>" . _T('info_total') . '</td><td class="time">' . $total . "</td><td></td></tr>"))
176
	);
177
178
	return array($temps, $navigation);
179
}
180