Skip to content

Commit 02ccc26

Browse files
added simple import function
1 parent f705e67 commit 02ccc26

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TODO
2727
* Make delete a POST request
2828
* Better error handling
2929
* Move or Copy key to different server
30-
* Importing of databases (json and redis commands)
30+
* Importing JSON
3131
* JSON export with seperate objects based on your seperator
3232

3333

import.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
require_once 'common.inc.php';
4+
5+
6+
7+
8+
if (isset($_POST['commands'])) {
9+
$commands = explode("\n", $_POST['commands']);
10+
11+
foreach ($commands as $command) {
12+
// The command and it's arguments are always seperated by a space
13+
$command = explode(' ', trim($command), 2);
14+
15+
if (!isset($command[1])) {
16+
// It's just an empty line or simple command which we don't support.
17+
continue;
18+
}
19+
20+
// Is the key enclosed in ""?
21+
if ($command[1][0] == '"') {
22+
// We can't just explode since we might have \" inside the key. So do a preg_split on " with a negative lookbehind assertions to make sure it isn't a \".
23+
$key = preg_split('/(?<!\\\\)"/', substr($command[1], 1), 2);
24+
25+
// Strip the seperating space
26+
$key[1] = substr($key[1], 1);
27+
} else {
28+
$key = explode(' ', $command[1], 2);
29+
}
30+
31+
switch ($command[0]) {
32+
case 'SET': {
33+
// Trim the optional "" acount th value.
34+
$redis->set($key[0], trim($key[1], '"'));
35+
break;
36+
}
37+
38+
case 'HSET': {
39+
if ($key[1][0] == '"') {
40+
// See preg_split above.
41+
$hkey = preg_split('/(?<!\\\\)"/', substr($key[1], 1), 2);
42+
43+
// Strip the seperating space
44+
$hkey[1] = substr($hkey[1], 1);
45+
} else {
46+
$hkey = explode(' ', $key[1], 2);
47+
}
48+
49+
$redis->hSet($key[0], $hkey[0], trim($hkey[1], '"'));
50+
break;
51+
}
52+
53+
case 'RPUSH': {
54+
$redis->rPush($key[0], trim($key[1], '"'));
55+
break;
56+
}
57+
58+
case 'SADD': {
59+
$redis->sAdd($key[0], trim($key[1], '"'));
60+
break;
61+
}
62+
63+
case 'ZADD': {
64+
if ($key[1][0] == '"') {
65+
// See preg_split ebove.
66+
$score = preg_split('/(?<!\\\\)"/', substr($key[1], 1), 2);
67+
68+
// Strip the seperating space
69+
$score[1] = substr($score[1], 1);
70+
} else {
71+
$score = explode(' ', $key[1], 2);
72+
}
73+
74+
$redis->zAdd($key[0], $score[0], trim($score[1], '"'));
75+
break;
76+
}
77+
78+
// We ignore commands we don't know (Could produce a warning).
79+
}
80+
}
81+
82+
83+
84+
// Refresh the top so the key tree is updated.
85+
require 'header.inc.php';
86+
87+
?>
88+
<script>
89+
top.location.href = top.location.pathname+'?overview&s=<?php echo $server['id']?>';
90+
</script>
91+
<?php
92+
93+
require 'footer.inc.php';
94+
die;
95+
}
96+
97+
98+
99+
100+
$page['css'][] = 'frame';
101+
$page['js'][] = 'frame';
102+
103+
require 'header.inc.php';
104+
105+
?>
106+
<h2>Import</h2>
107+
<form action="<?php echo format_html($_SERVER['REQUEST_URI'])?>" method="post">
108+
109+
<p>
110+
<label for="commands">Commands:<br>
111+
<br>
112+
<span class="info">
113+
Valid are:<br>
114+
SET<br>
115+
HSET<br>
116+
RPUSH<br>
117+
SADD<br>
118+
ZADD
119+
</span>
120+
</label>
121+
<textarea name="commands" id="commands" cols="80" rows="20"></textarea>
122+
</p>
123+
124+
<p>
125+
<input type="submit" class="button" value="Import">
126+
</p>
127+
128+
</form>
129+
<?php
130+
131+
require 'footer.inc.php';
132+
133+
?>

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function print_namespace($item, $name, $fullkey, $islast) {
164164
<p>
165165
<a href="?info&amp;s=<?php echo $server['id']?>"><img src="images/info.png" width="16" height="16" title="Info" alt="[I]"></a>
166166
<a href="?export&amp;s=<?php echo $server['id']?>"><img src="images/export.png" width="16" height="16" title="Export" alt="[E]"></a>
167+
<a href="?import&amp;s=<?php echo $server['id']?>"><img src="images/import.png" width="16" height="16" title="Import" alt="[I]"></a>
167168
</p>
168169

169170
<p>

0 commit comments

Comments
 (0)