-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump-markdown.php
142 lines (122 loc) · 3.82 KB
/
dump-markdown.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
<?php
/*
* AdminerDumpMarkdown - dump to MARKDOWN format v0.7 (October 14th, 2020)
*
* @link https://github.com/fthiella/adminer-plugin-dump-markdown
* @author Federico Thiella, https://fthiella.github.io/
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*
*/
class AdminerDumpMarkdown {
private $type = 'markdown';
private $format = 'Markdown';
function _format_value($s, $l, $c) {
return (strlen(utf8_decode($s)) > $l) ? substr($s, 0, $l) : $s.str_repeat($c, $l-strlen(utf8_decode($s)));
}
function _map($array, $width, $c) {
foreach ($array as $k => &$v) $v = $this->_format_value($v, $width[$k], $c);
return $array;
}
function _map_header($array) {
foreach ($array as $k => &$v) $v = $k;
return $array;
}
function _map_mtable($array) {
foreach ($array as $k => &$v) $v = '-';
return $array;
}
function _markdown_row($row, $column_width, $separator, $filler) {
return implode($separator, $this->_map($row, $column_width, $filler));
}
function _markdown_table($rows, $column_width) {
$content = $this->_markdown_row($this->_map_header($rows[0]), $column_width, " | ", " ") . "\n";
$content .= $this->_markdown_row($this->_map_mtable($rows[0]), $column_width, "-|-", "-") . "\n";
foreach ($rows as $row) {
$content .= $this->_markdown_row($row, $column_width, " | ", " ") . "\n";
}
return $content;
}
function _bool($value) {
return $value == 1 ? 'Yes' : 'No';
}
function dumpFormat() {
return array($this->type => $this->format);
}
function dumpDatabase($db) {
if ($_POST["format"] == $this->type) {
echo '# ' . $db . "\n\n";
return true;
}
}
/* export table structure */
function dumpTable($table, $style, $is_view = false) {
if ($_POST["format"] == $this->type) {
echo '## ' . addcslashes($table, "\n\"\\") . "\n\n";
if ($style) {
echo "### table structure\n\n";
$field_rows = array();
$field_width = (['Column name' => 11, 'Type' => 4, 'Comment' => 7, 'Null' => 4, 'AI' => 2]);
foreach (fields($table) as $field) {
$new_row = [
'Column name' => $field['field'],
'Type' => $field['full_type'],
'Comment' => $field['comment'],
'Null' => $this->_bool($field['null']),
'AI' => $this->_bool($field['auto_increment'])
];
array_push($field_rows, $new_row);
foreach ($new_row as $key => $val) {
$field_width[$key] = max($field_width[$key], strlen(utf8_decode($new_row[$key])));
}
}
echo $this->_markdown_table($field_rows, $field_width);
echo "\n";
}
return true;
}
}
/* export table data */
function dumpData($table, $style, $query) {
if ($_POST["format"] == $this->type) {
echo "### table data\n\n";
$connection = connection();
$result = $connection->query($query, 1);
if ($result) {
$rn = 0;
$sample_rows = array();
$column_width = array();
while ($row = $result->fetch_assoc()) {
switch(true) {
case $rn==0:
foreach ($row as $key => $val) {
$column_width[$key] = strlen(utf8_decode($key));
}
case $rn<100:
$sample_rows[$rn]=$row;
foreach ($row as $key => $val) {
$column_width[$key] = max($column_width[$key], strlen(utf8_decode($row[$key])));
}
break;
case $rn==100:
echo $this->_markdown_table($sample_rows, $column_width);
default:
echo $this->_markdown_row($row, $column_width, " | ", " ") . "\n";
}
$rn++;
}
if ($rn<100) {
echo $this->_markdown_table($sample_rows, $column_width);
}
echo "\n";
}
return true;
}
}
function dumpHeaders($identifier, $multi_table = false) {
if ($_POST["format"] == $this->type) {
header("Content-Type: text/text; charset=utf-8");
return "md";
}
}
}