-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_tables.php
More file actions
87 lines (71 loc) · 2.81 KB
/
Copy pathview_tables.php
File metadata and controls
87 lines (71 loc) · 2.81 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
<?php
declare(strict_types=1);
/**
* Table browser for one database, with bulk maintenance actions.
*/
require __DIR__ . '/lib/bootstrap.php';
use DbManager\Page;
use DbManager\Schema;
use DbManager\View;
[$pdo, $database] = Page::database();
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
$operation = request_str('operation');
$selected = $_POST['tables'] ?? [];
Page::handle(
static function () use ($pdo, $database, $operation, $selected): string {
if (!is_array($selected) || $selected === []) {
throw new RuntimeException(__('tables.no_selection'));
}
$done = 0;
$failed = 0;
$lastError = '';
foreach ($selected as $name) {
try {
$table = Schema::requireTable($pdo, $database, (string) $name);
if ($operation === 'drop') {
Schema::dropTable($pdo, $table);
} elseif ($operation === 'optimize') {
Schema::maintain($pdo, $table, 'optimize');
} else {
throw new RuntimeException(__('error.unknown_operation', [':name' => $operation]));
}
$done++;
} catch (Throwable $exception) {
$failed++;
$lastError = $exception->getMessage();
}
}
if ($failed > 0) {
flash('warning', __('tables.failed', [':count' => $failed, ':detail' => $lastError]));
}
return $operation === 'drop'
? __('tables.dropped', [':count' => $done])
: __('tables.optimized', [':count' => $done]);
},
url('view_tables.php', ['db' => $database])
);
}
$tables = Schema::tables($pdo, $database);
$totalSize = array_sum(array_column($tables, 'size'));
$actions = '<a class="btn" href="' . e(url('export.php', ['db' => $database])) . '">'
. icon('download') . '<span>' . e(__('tables.export_db')) . '</span></a>'
. '<a class="btn" href="' . e(url('import_database.php', ['db' => $database])) . '">'
. icon('upload') . '<span>' . e(__('common.import')) . '</span></a>'
. '<a class="btn btn--primary" href="' . e(url('create_table.php', ['db' => $database])) . '">'
. icon('plus') . '<span>' . e(__('tables.new_table')) . '</span></a>';
View::render(
'tables',
[
'database' => $database,
'tables' => $tables,
],
[
'title' => __('tables.title', [':database' => $database]),
'subtitle' => __('tables.subtitle', [
':count' => format_number(count($tables)),
':size' => format_bytes($totalSize),
]),
'active' => 'databases',
'actions' => $actions,
]
);