-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.php
59 lines (51 loc) · 1.36 KB
/
cli.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
<?php
/* Only Command-line Execution Allowed */
if (!isset($argv[1]))
exit;
require('/usr/share/nginx/tg.pe/database.php');
$db = new MyDB();
switch ($argv[1]) {
case 'build':
$sql = 'CREATE TABLE main (' .
'code TEXT NOT NULL UNIQUE, ' .
'url TEXT NOT NULL, ' .
'author TEXT NOT NULL, ' .
'created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, ' .
'modified_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL)';
$stmt = $db->pdo->prepare($sql);
$stmt->execute();
break;
case 'dump':
$sql = "SELECT * FROM main";
$stmt = $db->pdo->prepare($sql);
$stmt->execute();
while ($data = $stmt->fetch())
printf("%-5s %-30s %10s %s\n", $data['code'], $data['url'], $data['author'], $data['created_at']);
break;
case 'export':
$sql = "SELECT * FROM main";
$stmt = $db->pdo->prepare($sql);
$stmt->execute();
$result = [];
while ($data = $stmt->fetch(PDO::FETCH_ASSOC))
$result[] = $data;
echo json_encode($result, JSON_PRETTY_PRINT);
break;
case 'import':
$json = file_get_contents($argv[2]);
$json = json_decode($json, true);
foreach ($json as $data) {
$sql = 'INSERT INTO main(' .
join(', ', array_keys($data)) .
') VALUES (:' .
join(', :', array_keys($data)) .
')';
$stmt = $db->pdo->prepare($sql);
foreach ($data as $k => $v)
$stmt->bindValue(":$k", $v);
$stmt->execute();
}
break;
default:
echo "Unknown argument: {$argv[1]}";
}