Dernière activité 1 month ago

xiunobbs 转 flarum

xiuno2flarum.php Brut
1<?php
2
3/**
4 * # XiunoBBS 转 Flarum
5 * ## 使用说明
6 * ### 注意:本程序只转移 主题、帖子和版块,不转会员
7 *
8 * 1. 将本文件上传到两个网站共同的服务器(只要支持两个站的数据库连接均可)
9 * 2. 复制 xiunobbs 下的 conf/conf.php 到本目录,或者自己修改以下的引入路径
10 * 3. 复制 flarum 下的 config.php 到本目录,或者自己修改以下的引入路径
11 * 4. 执行 composer require s9e/text-formatter 安装转换的扩展
12 * 5. 再执行 php xiuno2flarum.php 命令行进行转换即可
13 *
14 * 注意:由于 Flarum 程序与 Markdown 兼容问题,本程序转换的数据支持不完美。
15 *
16 */
17
18// Get the autoloader
19include __DIR__ . '/vendor/autoload.php';
20
21// Use the Forum bundle. It supports BBCodes, emoticons and autolinking
22use s9e\TextFormatter\Bundles\Forum as TextFormatter;
23
24define('XIUNO_PATH', '');
25define('FLARUM_PATH', '');
26
27$xb_cfg = include 'conf.php';
28$fl_cfg = include 'config.php';
29
30$xn_cfg = $xb_cfg['db']['mysql']['master'];
31$dsn_xn = sprintf('mysql:dbname=%s;host=%s;charset=%s', $xn_cfg['name'], $xn_cfg['host'], $xn_cfg['charset']);
32$dbh_xn = new PDO($dsn_xn, $xn_cfg['user'], $xn_cfg['password']);
33
34$fm_cfg = $fl_cfg['database'];
35$dsn_fl = sprintf('mysql:dbname=%s;host=%s;charset=%s', $fm_cfg['database'], $fm_cfg['host'], $fm_cfg['charset']);
36$dbh_fl = new PDO($dsn_fl, $fm_cfg['username'], $fm_cfg['password']);
37
38$dbh_fl->exec('set @@foreign_key_checks=OFF');
39
40function date_at(string $time): string
41{
42 return date('Y-m-s h:i:s', $time);
43}
44
45function insert_tags()
46{
47 global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl;
48
49 $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'forum`');
50 $sth->execute();
51 // $result = $sth->fetchAll(PDO::FETCH_ASSOC);
52
53 $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'tags`');
54 // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'tags`');
55
56 while($row = $sth->fetch(PDO::FETCH_ASSOC))
57 {
58 //print_r($row);
59 $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'tags` (id,name,slug,description,position) VALUES (?,?,?,?,?)');
60
61 try {
62 $data = [
63 $row['fid'],
64 $row['name'],
65 'tag_' . $row['fid'],
66 $row['brief'],
67 $row['fid'],
68 ];
69
70 $dbh_fl->beginTransaction();
71 $stmt->execute($data);
72 $dbh_fl->commit();
73 // print $dbh_fl->lastInsertId();
74 } catch(PDOExecption $e) {
75 $dbh_fl->rollback();
76 print "Error!: " . $e->getMessage() . "\n";
77 exit;
78 }
79 }
80
81 print 'Insert table ' . $fm_cfg['prefix'] . "tags\n";
82}
83
84function insert_discussions()
85{
86 global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl;
87
88 $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'thread`');
89 $sth->execute();
90 // $result = $sth->fetchAll(PDO::FETCH_ASSOC);
91
92 $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'discussions`');
93 // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussions`');
94 $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussion_tag`');
95 $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussion_user`');
96
97 while($row = $sth->fetch(PDO::FETCH_ASSOC))
98 {
99 // print_r($row);
100 $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'discussions` (id,title,participant_count,created_at,user_id,first_post_id,last_posted_at,last_posted_user_id,last_post_id,slug,is_sticky) VALUES (?,?,1,FROM_UNIXTIME(?),?,?,FROM_UNIXTIME(?),?,?,"",?)');
101
102 try {
103 $data = [
104 $row['tid'],
105 $row['subject'],
106 $row['create_date'],
107 $row['uid'],
108 $row['firstpid'],
109 $row['last_date'],
110 $row['lastuid'],
111 $row['lastpid'],
112 $row['top'] > 0 ? 1 : 0,
113 ];
114
115 $dbh_fl->beginTransaction();
116 $stmt->execute($data);
117 $dbh_fl->commit();
118 // print $dbh_fl->lastInsertId();
119 } catch(PDOExecption $e) {
120 $dbh_fl->rollback();
121 print "Error!: " . $e->getMessage() . "\n";
122 exit;
123 }
124
125 $tag_sql = 'INSERT INTO `' . $fm_cfg['prefix'] . 'discussion_tag` (discussion_id,tag_id) VALUES (?,?)';
126 $stmt = $dbh_fl->prepare($tag_sql);
127 try {
128 $data = [
129 $row['tid'],
130 $row['fid'],
131 ];
132
133 $dbh_fl->beginTransaction();
134 $stmt->execute($data);
135 $dbh_fl->commit();
136 // print $dbh_fl->lastInsertId();
137 } catch(PDOExecption $e) {
138 $dbh_fl->rollback();
139 print "Error!: " . $e->getMessage() . "\n";
140 exit;
141 }
142
143 $tag_sql = 'INSERT INTO `' . $fm_cfg['prefix'] . 'discussion_user` (user_id,discussion_id,last_read_at,last_read_post_number) VALUES (?,?,FROM_UNIXTIME(?),1)';
144 $stmt = $dbh_fl->prepare($tag_sql);
145 try {
146 $data = [
147 $row['uid'],
148 $row['tid'],
149 $row['last_date'],
150 ];
151
152 $dbh_fl->beginTransaction();
153
154 $stmt->execute($data);
155 $dbh_fl->commit();
156 // print $dbh_fl->lastInsertId();
157 } catch(PDOExecption $e) {
158 $dbh_fl->rollback();
159 print "Error!: " . $e->getMessage() . "\n";
160 exit;
161 }
162 }
163
164 print 'Insert table ' . $fm_cfg['prefix'] . "discussions\n";
165}
166
167function insert_posts()
168{
169 global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl;
170
171 $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'post` ORDER BY `create_date` ASC');
172 $sth->execute();
173 // $result = $sth->fetchAll(PDO::FETCH_ASSOC);
174
175 $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'posts`');
176 // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'posts`');
177
178 $totals = [];
179 while($row = $sth->fetch(PDO::FETCH_ASSOC))
180 {
181 // print_r($row);
182 $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'posts` (id,discussion_id,number,created_at,user_id,type,content,ip_address) VALUES (?,?,?,FROM_UNIXTIME(?),?,"comment",?,?)');
183
184 try {
185 if (! isset($totals[$row['tid']])) {
186 $totals[$row['tid']] = [];
187 }
188 $totals[$row['tid']][] = 1;
189
190 // if ($row['isfirst'] === 1) {
191 // $number_id = 1;
192 // } else {
193 // $totals[$row['tid']][] = 1;
194 // }
195 $number_id = count($totals[$row['tid']]);
196
197 // XML representation, that's what you should store in your database
198 $xml = TextFormatter::parse($row['message']);
199
200 // HTML rendering, that's what you display to the user
201 $html = TextFormatter::render($xml);
202
203 $data = [
204 $row['pid'],
205 $row['tid'],
206 $number_id,
207 $row['create_date'],
208 $row['uid'],
209 // '<r>' . $row['message'] . '</r>',
210 // '<t>' . $row['message_fmt'] . '</t>',
211 '<t>' . $html . '</t>',
212 // $html,
213 long2ip($row['userip']),
214 ];
215
216 $dbh_fl->beginTransaction();
217 $stmt->execute($data);
218 $dbh_fl->commit();
219 // print $dbh_fl->lastInsertId();
220 } catch(PDOExecption $e) {
221 $dbh_fl->rollback();
222 print "Error!: " . $e->getMessage() . "\n";
223 exit;
224 }
225 }
226
227 print 'Insert table ' . $fm_cfg['prefix'] . "posts\n";
228}
229
230function fix_discussion_count()
231{
232 global $fm_cfg,$dbh_fl;
233
234 $sql = 'SELECT count(*) AS counts, `discussion_id` FROM `' . $fm_cfg['prefix'] . 'posts` GROUP BY `discussion_id`';
235 $sth = $dbh_fl->prepare($sql);
236 $sth->execute();
237
238 while($row = $sth->fetch(PDO::FETCH_ASSOC))
239 {
240 // print_r($row);
241 $stmt = $dbh_fl->prepare('UPDATE `' . $fm_cfg['prefix'] . 'discussions` SET `comment_count` = ?, post_number_index = ?, last_post_number = ? WHERE id = ?');
242
243 try {
244 $data = [
245 $row['counts'],
246 $row['counts'],
247 1,
248 $row['discussion_id'],
249 ];
250
251 $dbh_fl->beginTransaction();
252 $stmt->execute($data);
253 $dbh_fl->commit();
254 // print $dbh_fl->lastInsertId();
255 } catch(PDOExecption $e) {
256 $dbh_fl->rollback();
257 print "Error!: " . $e->getMessage() . "\n";
258 exit;
259 }
260 }
261
262 print 'Update table ' . $fm_cfg['prefix'] . "discussions counts\n";
263}
264
265
266function fix_tag_stat()
267{
268 global $fm_cfg,$dbh_fl;
269
270 $sql = 'SELECT *, count(*) AS counts FROM `'. $fm_cfg['prefix'] . 'discussions` GROUP BY `slug` DESC;';
271 $sth = $dbh_fl->prepare($sql);
272 $sth->execute();
273
274 while($row = $sth->fetch(PDO::FETCH_ASSOC))
275 {
276 // print_r($row);
277 $stmt = $dbh_fl->prepare('UPDATE `' . $fm_cfg['prefix'] . 'tags` SET `discussion_count` = ?, last_posted_at = ? , last_posted_discussion_id = ?, last_posted_user_id = ? WHERE slug = ?');
278
279 $last_uid = $row['last_posted_user_id'];
280 if ($last_uid == 0)
281 {
282 $last_uid = $row['user_id'];
283 }
284
285 try {
286 $data = [
287 $row['counts'],
288 $row['last_posted_at'],
289 $row['id'],
290 $last_uid,
291 $row['slug'],
292 ];
293
294 $dbh_fl->beginTransaction();
295 $stmt->execute($data);
296 $dbh_fl->commit();
297 // print $dbh_fl->lastInsertId();
298 } catch(PDOExecption $e) {
299 $dbh_fl->rollback();
300 print "Error!: " . $e->getMessage() . "\n";
301 exit;
302 }
303 }
304
305 print 'Update table ' . $fm_cfg['prefix'] . "tags stat\n";
306}
307
308insert_tags();
309insert_discussions();
310insert_posts();
311fix_discussion_count();
312fix_tag_stat();
313