-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminerUuidPlugin.php
More file actions
202 lines (164 loc) · 5.85 KB
/
Copy pathAdminerUuidPlugin.php
File metadata and controls
202 lines (164 loc) · 5.85 KB
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
<?php
/**
* This Adminer plugin will format binary-stored UUIDs to human-readable UUID format.
*
* @link https://github.com/burithetech/adminer-uuid-plugin
* @author Peter Burian, https://buri.tech/
* @version 1.0
* @license https://opensource.org/licenses/MIT, The MIT License (MIT)
*/
class AdminerUuidPlugin extends Adminer
{
private const ERROR_PREFIX = 'UuidPlugin:';
private array $options = [
'convert_to_lowercase' => true,
'applicable_to' => [
'table_names' => '/.*/',
'column_names' => '/.*/',
'column_types' => '/^binary\(16\)$/i',
'comments' => '/.*/'
],
];
public function __construct(array $options = [])
{
$this->parseOptions($options);
}
public function selectSearchPrint($where, $columns, $indexes) {
$uuidFieldsToPrint = array_filter(
fields($this->getSelectedTable()),
fn (array $field) => $this->isFieldSuitable($field)
);
$uuidColumnsToPrint = array_column($uuidFieldsToPrint, 'field');
$originalWhereConditions = $this->getWhereConditions();
$temporaryWhereConditions = array_map(function (array $condition) use ($uuidColumnsToPrint) {
if (!in_array($condition['col'], $uuidColumnsToPrint)) {
return $condition;
}
return array_replace($condition, [
'val' => $this->formatBinaryUuid($condition['val']),
]);
}, $originalWhereConditions);
$this->setWhereConditions($temporaryWhereConditions);
parent::selectSearchPrint($where, $columns, $indexes);
$this->setWhereConditions($originalWhereConditions);
}
public function selectSearchProcess($fields, $indexes) {
$uuidColumnsToProcess = array_filter(
$fields,
fn (array $field) => $this->isFieldSuitable($field)
);
$where = array_map(function (array $condition) use ($uuidColumnsToProcess) {
if (!array_key_exists($condition['col'], $uuidColumnsToProcess)) {
return $condition;
}
return array_replace($condition, [
'val' => str_replace('-', '', $condition['val']),
]);
}, $this->getWhereConditions());
$this->setWhereConditions($where);
return parent::selectSearchProcess($fields, $indexes);
}
public function rowDescriptions($rows, $foreignKeys)
{
$eligibleFields = [];
foreach (fields($this->getSelectedTable()) as $field) {
if (!$this->isFieldSuitable($field)) {
continue;
}
$eligibleFields[] = $field['field'];
}
return array_map(function (array $row) use ($eligibleFields) {
foreach ($row as $columnKey => $columnValue) {
if (!in_array($columnKey, $eligibleFields)) {
continue;
}
$row[$columnKey] = $this->formatBinaryUuid($columnValue);
}
return $row;
}, $rows);
}
public function getSelectedTable(): string
{
return $_GET['select'] ?? throw new Exception(sprintf(
'%s No table selected.',
self::ERROR_PREFIX
));
}
private function parseOptions(array $options): void
{
// options: applicable_to
foreach ($options['applicable_to'] ?? [] as $context => $regEx) {
if (!array_key_exists($context, $this->options['applicable_to'])) {
continue;
}
if (!is_string($regEx)) {
continue;
}
// validate regex
if (@preg_match($regEx, '') === false) {
throw new Exception(sprintf('%s Invalid reg-ex in options->applicable_to->%s',
self::ERROR_PREFIX,
$context
));
}
$options['applicable_to'][$context] = $regEx;
}
// options: convert_to_lowercase
if (array_key_exists('convert_to_lowercase', $options)) {
if (is_bool($options['convert_to_lowercase'])) {
$this->options['convert_to_lowercase'] = $options['convert_to_lowercase'];
}
}
}
private function formatBinaryUuid(string $binary): string
{
if (strlen($binary) !== 32) {
return $binary;
}
$uuid = sprintf(
'%s-%s-%s-%s-%s',
substr($binary, 0, 8),
substr($binary, 8, 4),
substr($binary, 12, 4),
substr($binary, 16, 4),
substr($binary, 20, 12)
);
if ($this->options['convert_to_lowercase']) {
return strtolower($uuid);
}
return strtoupper($uuid);
}
/**
* @return array<array{col: string, op: string, val: string}>
*/
public function getWhereConditions(): array
{
return $_GET['where'] ?? [];
}
/**
* @param array<array{col: string, op: string, val: string}> $conditions
*/
public function setWhereConditions(array $conditions): void
{
$_GET['where'] = $conditions;
}
/**
* @param array{field: string, full_type: string, comment: string} $field
*/
private function isFieldSuitable(array $field): bool {
$applicableToOptions = $this->options['applicable_to'];
if (!preg_match($applicableToOptions['table_names'], $this->getSelectedTable())) {
return false;
}
if (!preg_match($applicableToOptions['column_names'], $field['field'])) {
return false;
}
if (!preg_match($applicableToOptions['column_types'], $field['full_type'])) {
return false;
}
if (!preg_match($applicableToOptions['comments'], $field['comment'])) {
return false;
}
return true;
}
}