This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathphpBB_to_Flarum.php
430 lines (360 loc) · 14.3 KB
/
phpBB_to_Flarum.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
// Original script by robrotheram from discuss.flarum.org
// Modified by VIRUXE
// Modified by Reflic
// Modified by TidbitSoftware
set_time_limit(0);
ini_set('memory_limit', -1);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
$servername = "localhost";
$username = "user";
$password = "password";
$exportDBName = "PHPforums";
$importDBName = "flarum";
$exportDBPrefix = "phpbb_";
$importDBPrefix = ""; // Leave blank if you did not supply a table prefix when setting up Flarum. Otherwise, supply the full table prefix (i.e. including "_", if used).
// Establish a connection to the server where the PHPBB database exists
$exportDbConnection = new mysqli($servername, $username, $password, $exportDBName);
// Check connection
if ($exportDbConnection->connect_error)
die("Export - Connection failed: " . $exportDbConnection->connect_error);
else
{
echo "Export - Connected successfully<br>\n";
if(!$exportDbConnection->set_charset("utf8"))
{
printf("Error loading character set utf8: %s\n", $exportDbConnection->error);
exit();
}
else
printf("Current character set: %s\n", $exportDbConnection->character_set_name());
}
// Establish a connection to the server where the Flarum database exists
$importDbConnection = new mysqli($servername, $username, $password, $importDBName);
// Check connection
if ($importDbConnection->connect_error)
die("Import - Connection failed: " . $importDbConnection->connect_error);
else
{
echo "Import - Connected successfully<br>\n";
if(!$importDbConnection->set_charset("utf8"))
{
printf("Error loading character set utf8: %s\n", $importDbConnection->error);
exit();
}
else
printf("Current character set: %s\n", $importDbConnection->character_set_name());
}
//Disable foreing keys check
$importDbConnection->query("SET FOREIGN_KEY_CHECKS=0");
//Convert Users
echo "<hr>Step 1 - Users<hr>";
$result = $exportDbConnection->query("SELECT user_id, from_unixtime(user_regdate) as user_regdate, username_clean, user_email FROM ${exportDBPrefix}users");
$totalUsers = $result->num_rows;
if ($totalUsers)
{
$i = 0;
$usersIgnored = 0;
while($row = $result->fetch_assoc())
{
$i++;
if($row["user_email"] != NULL)
{
$username = $row["username_clean"];
$usernameHasSpace = strpos($username, " ");
if($usernameHasSpace > 0)
{
$formatedUsername = str_replace(" ", NULL, $username);
}
else{
$formatedUsername = $username;
}
$id = $row['user_id'];
$email = $row['user_email'];
$password = sha1(md5(time()));
$jointime = $row['user_regdate'];
$query = "INSERT INTO " . $importDBPrefix . "users (id, username, email, password, joined_at, is_email_confirmed) VALUES ( '$id', '$formatedUsername', '$email', '$password', '$jointime', 1)";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
}
else {
$usersIgnored++;
}
}
echo $i-$usersIgnored . ' out of '. $totalUsers .' Total Users converted';
}
else
echo "Something went wrong.";
//Convert Categories to Tags
echo "<hr>Step 2 - Categories<hr>";
$result = $exportDbConnection->query("SELECT forum_id, forum_name, forum_desc FROM ${exportDBPrefix}forums");
$totalCategories = $result->num_rows;
if ($totalCategories)
{
$i = 1;
while($row = $result->fetch_assoc())
{
$id = $row["forum_id"];
$name = mysql_escape_mimic($row["forum_name"]);
$description = mysql_escape_mimic(strip_tags(stripBBCode($row["forum_desc"])));
$color = rand_color();
$position = $i;
$slug = mysql_escape_mimic(slugify($row["forum_name"]));
$query = "INSERT INTO " . $importDBPrefix . "tags (id, name, description, slug, color, position) VALUES ( '$id', '$name', '$description', '$slug', '$color', '$position')";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL Assumption id Confict now trying a update <br/>\n";
$queryupdate = "UPDATE " . $importDBPrefix . "tags SET name = '$name', description = '$description', slug = '$slug' WHERE id = '$id' ;";
$res = $importDbConnection->query($queryupdate);
if($res === false) { echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n"; }
}
$i++;
}
echo $totalCategories . ' Categories converted.';
}
else
echo "Something went wrong.";
echo "<hr>Step 3 - Topics<hr>";
$topicsQuery = $exportDbConnection->query("SELECT topic_id, topic_poster, forum_id, topic_title, topic_time FROM ${exportDBPrefix}topics ORDER BY topic_id DESC;");
$topicCount = $topicsQuery->num_rows;
if($topicCount)
{
$curTopicCount = 0;
$insertString = "INSERT INTO " . $importDBPrefix . "posts (id, user_id, discussion_id, created_at, type, content) VALUES \n";
// Loop trough all PHPBB topics
$topictotal = $topicsQuery->num_rows;
$i = 1;
while($topic = $topicsQuery->fetch_assoc())
{
// Convert posts per topic
$participantsArr = [];
$lastPosterID = 0;
$sqlQuery = sprintf("SELECT * FROM ${exportDBPrefix}posts WHERE topic_id = %d;", $topic["topic_id"]);
$postsQuery = $exportDbConnection->query($sqlQuery);
$postCount = $postsQuery->num_rows;
if($postCount)
{
$curPost = 0;
//fwrite($sqlScript_posts, $insertString);
while($post = $postsQuery->fetch_assoc())
{
$curPost++;
$posterID = 0;
$date = new DateTime();
$date->setTimestamp($post["post_time"]);
$postDate = $date->format('Y-m-d H:i:s');
$postText = formatText($exportDbConnection, $post['post_text']);
if($post['post_id'] == 913){echo $postText;}
if(empty($post['post_username']))// If the post_username field has text it means it's a "ghost" post. Therefore we should set the poster id to 0 so Flarum knows it's an invalid user
{
$posterID = $post['poster_id'];
// Add to the array only if unique
if(!in_array($posterID, $participantsArr))
$participantsArr[] = $posterID;
}
if($curPost == $postCount)// Check if it's the last post in the discussion and save the poster id
$lastPosterID = $posterID;
// Write post values to SQL Script
//fwrite($sqlScript_posts, sprintf("\t(%d, %d, %d, '%s', 'comment', '%s')%s\n", $post['post_id'], $posterID, $topic['topic_id'], $postDate, $postText, $curPost != $postCount ? "," : ";"));
// Execute the insert query in the desired database.
$formattedValuesStr = sprintf("(%d, %d, %d, '%s', 'comment', '%s');", $post['post_id'], $posterID, $topic['topic_id'], $postDate, $postText);
$query = $insertString . $formattedValuesStr;
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
}
}
//else
// echo "<br>\nTopic ". $topic['topic_id'] ." has zero posts.<br>\n";
// Convert topic to Flarum format
//
// This needs to be done at the end because we need to get the post count first
//
$date = new DateTime();
$date->setTimestamp($topic["topic_time"]);
$discussionDate = $date->format('Y-m-d H:i:s');
$topicTitle = $exportDbConnection->real_escape_string($topic["topic_title"]);
// Link Discussion/Topic to a Tag/Category
$topicid = $topic["topic_id"];
$forumid = $topic["forum_id"];
$query = "INSERT INTO " . $importDBPrefix . "discussion_tag (discussion_id, tag_id) VALUES( '$topicid', '$forumid')";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
// Check for parent forums
$parentForum = $exportDbConnection->query("SELECT parent_id FROM ${exportDBPrefix}forums WHERE forum_id = " . $topic["forum_id"]);
$result = $parentForum->fetch_assoc();
if($result['parent_id'] > 0){
$topicid = $topic["topic_id"];
$parentid = $result['parent_id'];
$query = "INSERT INTO " . $importDBPrefix . "discussion_tag (discussion_id, tag_id) VALUES( '$topicid', '$parentid')";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
}
if($lastPosterID == 0)// Just to make sure it displays an actual username if the topic doesn't have posts? Not sure about this.
$lastPosterID = $topic["topic_poster"];
$slug = mysql_escape_mimic(slugify($topicTitle));
$count = count($participantsArr);
$poster = $topic["topic_poster"];
$query = "INSERT INTO " . $importDBPrefix . "discussions (id, title, slug, created_at, comment_count, participant_count, first_post_id, last_post_id, user_id, last_posted_user_id, last_posted_at) VALUES( '$topicid', '$topicTitle', '$slug', '$discussionDate', '$postCount', '$count', 1, 1, '$poster', '$lastPosterID', '$discussionDate')";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
$i++;
}
}
// Convert user posted topics to user discussions?
echo "<hr> User Discussions<hr/>";
$result = $exportDbConnection->query("SELECT user_id, topic_id FROM ${exportDBPrefix}topics_posted");
if ($result->num_rows > 0)
{
$total = $result->num_rows;
$i = 1;
while($row = $result->fetch_assoc()) {
$comma = $i == $total ? ";" : ",";
$userID = $row["user_id"];
$topicID = $row["topic_id"];
$query = "INSERT INTO " . $importDBPrefix . "discussion_user (user_id, discussion_id) VALUES ( '$userID', '$topicID')";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
$i++;
}
echo "Success";
}
else
echo "Table is empty";
// Convert user posted topics to user discussions?
echo "<hr>last Step Update User table<hr/>";
$result = $importDbConnection->query("SELECT id FROM users");
if ($result->num_rows > 0)
{
$total = $result->num_rows;
$i = 1;
while($row = $result->fetch_assoc()) {
$comma = $i == $total ? ";" : ",";
$userID = $row["id"];
$res = $importDbConnection->query("select * from users_discussions where user_id = '$userID' ");
$numTopics = $res->num_rows;
$res1 = $importDbConnection->query("select * from posts where user_id = '$userID' ");
$numPosts = $res1->num_rows;
$query = "UPDATE " . $importDBPrefix . "users SET discussions_count = '$numTopics', comment_count = '$numPosts' WHERE id = '$userID' ";
$res = $importDbConnection->query($query);
if($res === false) {
echo "Wrong SQL: " . $query . " Error: " . $importDbConnection->error . " <br/>\n";
}
}
echo "Success";
}
else
echo "Table is empty";
// Close connection to the database
$exportDbConnection->close();
$importDbConnection->close();
// Functions
function print_r2($val)
{
echo '<pre>';
print_r($val);
echo '</pre>';
}
function slugify($text)
{
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
return 'n-a';
return $text;
}
function rand_color()
{
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
function mysql_escape_mimic($inp)
{
if(is_array($inp))
return array_map(__METHOD__, $inp);
if(!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
// Formats PHPBB's text to Flarum's text format
function formatText($connection, $text)
{
$text = preg_replace('#\:\w+#', '', $text);
$text = convertBBCodeToHTML($text);
$text = str_replace(""","\"",$text);
$text = preg_replace('|[[\/\!]*?[^\[\]]*?]|si', '', $text);
$text = trimSmilies($text);
// Wrap text lines with paragraph tags
$explodedText = explode("\n", $text);
foreach ($explodedText as $key => $value)
{
if(strlen($value) > 1)// Only wrap in a paragraph tag if the line has actual text
$explodedText[$key] = '<p>' . $value . '</p>';
}
$text = implode("\n", $explodedText);
$wrapTag = strpos($text, '>') > 0 ? "r" : "t";// Posts with quotes need to be 'richtext'
$text = sprintf('<%s>%s</%s>', $wrapTag, $text, $wrapTag);
return $connection->real_escape_string($text);
}
// Used to convert Categories to Tags
function stripBBCode($text_to_search) {
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
function convertBBCodeToHTML($bbcode)
{
$bbcode = preg_replace('#\[b](.+)\[\/b]#', "<b>$1</b>", $bbcode);
$bbcode = preg_replace('#\[i](.+)\[\/i]#', "<i>$1</i>", $bbcode);
$bbcode = preg_replace('#\[u](.+)\[\/u]#', "<u>$1</u>", $bbcode);
$bbcode = preg_replace('#\[img](.+?)\[\/img]#is', "<img src='$1'\>", $bbcode);
$bbcode = preg_replace('#\[quote=(.+?)](.+?)\[\/quote]#is', "<QUOTE><i>></i>$2</QUOTE>", $bbcode);
$bbcode = preg_replace('#\[code:\w+](.+?)\[\/code:\w+]#is', "<CODE class='hljs'>$1<CODE>", $bbcode);
$bbcode = preg_replace('#\[pre](.+?)\[\/pre]#is', "<code>$1<code>", $bbcode);
$bbcode = preg_replace('#\[u](.+)\[\/u]#', "<u>$1</u>", $bbcode);
$bbcode = preg_replace('#\[\*](.+?)\[\/\*]#is', "<li>$1</li>", $bbcode);
$bbcode = preg_replace('#\[color=\#\w+](.+?)\[\/color]#is', "$1", $bbcode);
$bbcode = preg_replace('#\[url=(.+?)](.+?)\[\/url]#is', "<a href='$1'>$2</a>", $bbcode);
$bbcode = preg_replace('#\[url](.+?)\[\/url]#is', "<a href='$1'>$1</a>", $bbcode);
$bbcode = preg_replace('#\[list](.+?)\[\/list]#is', "<ul>$1</ul>", $bbcode);
$bbcode = preg_replace('#\[size=200](.+?)\[\/size]#is', "<h1>$1</h1>", $bbcode);
$bbcode = preg_replace('#\[size=170](.+?)\[\/size]#is', "<h2>$1</h2>", $bbcode);
$bbcode = preg_replace('#\[size=150](.+?)\[\/size]#is', "<h3>$1</h3>", $bbcode);
$bbcode = preg_replace('#\[size=120](.+?)\[\/size]#is', "<h4>$1</h4>", $bbcode);
$bbcode = preg_replace('#\[size=85](.+?)\[\/size]#is', "<h5>$1</h5>", $bbcode);
return $bbcode;
}
function trimSmilies($postText)
{
$startStr = "<!--";
$endStr = 'alt="';
$startStr1 = '" title';
$endStr1 = " -->";
$emoticonsCount = substr_count($postText, '<img src="{SMILIES_PATH}');
for ($i=0; $i < $emoticonsCount; $i++)
{
$startPos = strpos($postText, $startStr);
$endPos = strpos($postText, $endStr);
$postText = substr_replace($postText, NULL, $startPos, $endPos-$startPos+strlen($endStr));
$startPos1 = strpos($postText, $startStr1);
$endPos1 = strpos($postText, $endStr1);
$postText = substr_replace($postText, NULL, $startPos1, $endPos1-$startPos1+strlen($endStr1));
}
return $postText;
}
?>