用kyotocabinet备份MediaWiki数据

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
package jay;

import static java.lang.System.out;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import kyotocabinet.Cursor;
import kyotocabinet.DB;

public class TakeBack {
static Connection conn;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://DBServer:3306/wiki", "user", "passwd");
} catch (Exception e) {
e.printStackTrace();
}
}

static void showHashDB() {
DB db = new DB();
if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
System.err.println("open error: " + db.error());
}

Cursor cur = db.cursor();
cur.jump();
String[] rec;
while ((rec = cur.get_str(true)) != null) {
System.out.println(rec[0] + ":" + rec[1]);
break;
}
cur.disable();
if (!db.close()) {
System.err.println("close error: " + db.error());
}
}

static void backupMediawiki() throws Exception {
DB db = new DB();
if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
System.err.println("open error: " + db.error());
}

String sql = "SELECT page_title, old_text FROM `page` p, revision r, text t WHERE page_latest = r.rev_id and r.rev_text_id = t.old_id and page_namespace = 0";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
InputStream in = rs.getBlob(1).getBinaryStream();
byte[] bytes = new byte[89200000];
int len = in.read(bytes);
String title = new String(bytes, 0, len, "UTF-8");
in.close();

in = rs.getBlob(2).getBinaryStream();
len = in.read(bytes);
if (len == -1) {
out.println("标题:" + title + " 没有内容。");
continue;
}
String content = new String(bytes, 0, len, "UTF-8");
in.close();
// out.println("title: " + title + " content: " + content);

db.set(title, content);
}

rs.close();
stmt.close();
conn.close();
if (!db.close()) {
System.err.println("close error: " + db.error());
}
}

public static void main(String[] args) throws Exception {
backupMediawiki();
showHashDB();
}
}