-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoar.php
211 lines (193 loc) · 5.52 KB
/
soar.php
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
<?php
class Soar {
private $config = [
'report-type' => 'json',
'allow-online-as-test' => 'true',
'sampling' => 'false',
];
private $cmd = '';
public function __construct($config = []) {
$this->config($config);
$this->cmd = $this->getCmd();
$this->cmd .= ' ' . $this->buildConfig();
}
/**
* @return string cmd
*/
private function getCmd() {
defined('PHP_OS') or define('PHP_OS', 'Linux');
if (DIRECTORY_SEPARATOR == '\\') {
$cmd = 'soar.windows-amd64.exe';
} else {
$cmd = stristr(PHP_OS, 'darwin') ? 'soar.darwin-amd64' : 'soar.linux-amd64';
}
return __DIR__ . '/bin/' . $cmd;
}
/**
* 设置soar 配置
* @param array $config
*/
public function config($config) {
$this->config = array_merge($this->config, $config);
}
private function buildConfig() {
$options = [];
foreach ($this->config as $key => $val) {
$options[] = "-{$key}={$val}";
}
return implode(' ', $options);
}
/**
* return array
*/
public function analysis($sql) {
$sql = trim(preg_replace('/^explain/i', '', trim($sql)));
$f = proc_open($this->cmd, [
['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']
], $pipes);
fwrite($pipes[0], $sql);
fclose($pipes[0]);
$data = stream_get_contents($pipes[1]);
fclose($pipes[1]);
return $this->config['report-type'] == 'json' ? (json_decode($data, true) ?: [[]]) : $data;
}
}
/**
* format array to html for phpmyadmin
*/
class SoarHtml {
private $r;
private $config;
private $columns = [
'Item', 'Level', 'Summary', 'Content', 'Case'
];
public function __construct($arr) {
$this->r = $arr;
$this->parseResult();
}
/**
* 解析结, 设置分数和 explain解读, 并按照level排序
*/
private function parseResult() {
$total = 100;
$explainItem = [];
$analysis = [];
foreach ($this->r as $key => $val) {
$num = intval(str_replace(['L', 'l'], '', $val['Severity']));
$total -= $num * 5;
if (strpos($key, 'EXP') !== false) {
$explainItem = $val;
} else {
$val['Level'] = $num;
$analysis[] = $val;
}
}
usort($analysis, function ($a, $b) {
return $b['Level'] - $a['Level'];
});
$this->config['num'] = $total < 0 ? 0 : $total;
$this->config['explain'] = $explainItem;
$this->config['analysis'] = $analysis;
}
/**
* 分数html
*/
public function asNumHtml() {
$margin = '20px 0px 0px 0px';
$verArr = explode('.', PMA_VERSION);
$ver = "{$verArr[0]}.{$verArr[1]}";
switch ($ver) {
case '4.8';
$margin = '20px 0px 10px 0px';
break;
case '4.7';
case '4.6';
$margin = '20px 0px 0px 0px';
break;
case '4.5';
case '4.4';
case '4.3';
case '4.2';
case '4.1';
case '4.0';
$margin = '0px 0px 0px 0px';
break;
}
return "<h3 style=\"margin:{$margin}\">评分:{$this->config['num']}分</h3>";
}
/**
* expalin html
*/
public function asExplainHtml() {
if ($this->config['explain']) {
$html = $this->config['explain']['Case'];
$html = preg_replace('/####(.+?)\n/', '<h4 style="margin:5px 20px;">$1</h4>', $html);
$html = preg_replace('/###(.+?)\n/', '<h3 style="margin:10px 0px;">$1:</h3>', $html);
$html = preg_replace('/\* (.+?)\n/', '<ul style="margin:0px;">$1</ul>', $html);
$html = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $html);
$html = "<div style=\"margin-bottom:20px;\">{$html}</div>";
return $html;
}
return '';
}
/**
* sql 建议 html
*/
public function asItemHtml() {
$html = '';
if ($this->config['analysis']) {
$html .= '<h3 style="margin:10px 0px;">SQL建议与优化:</h3>';
$html .= '<table class="table_results ajax pma_table" data-uniqueid="18066"><thead><tr>';
foreach ($this->columns as $column) {
$html .= '<th class="draggable"><span>' . $column . '</span></th>';
}
$html .= '<td class="print_ignore"><span></span></td></tr></thead><tbody>';
foreach ($this->config['analysis'] as $index => $item) {
$class = ($index % 2) ? 'even' : 'odd';
$html .= '<tr class="' . $class . '">';
foreach ($this->columns as $column) {
$html .= "<td data-decimals=\"0\" data-type=\"string\" class=\"data text\"><span>{$item[$column]}</span></td>";
}
$html .= '</tr>';
}
}
$html .= '</tbody></table>';
return $html;
}
}
/**
* 检测是否有phpmyadmin 环境
*/
if (defined('PMA_VERSION')) {
$db = $GLOBALS['db'];
global $cfg;
$host = $cfg['Server']['host'];
$user = $cfg['Server']['user'];
$pwd = $cfg['Server']['password'];
$port = $cfg['Server']['port'] ?: '3306';
//如果密码含特殊字符, 则读取配置文件
if (preg_match('/[@:\/]/', $pwd)) {
$file = __DIR__. '/bin/soar.yaml';
$content = "test-dsn:\n addr: '{$host}:{$port}'\n schema: '{$db}'\n user: '{$user}'\n password: '{$pwd}'\n disable: false";
file_put_contents($file, $content);
$soar = new Soar();
} else {
$dsn = "{$user}:{$pwd}@{$host}:{$port}/$db";
$soar = new Soar(['test-dsn' => $dsn]);
}
$GLOBALS['soar'] = $soar;
/**
* 格式化sql
*/
function get_analyzed_sql($sqlParser) {
$sql = '';
foreach ($sqlParser->tokens as $val) {
if (!is_null($val)) $sql .= $val->token;
}
return $sql;
}
}
################## demo ###################
//$soar = new Soar(['test-dsn' => 'xxx']);
//$r = $soar->analysis($sql);
//print_r($r);