-
Notifications
You must be signed in to change notification settings - Fork 3
/
smf2_to_flarum_alternative.php
687 lines (592 loc) · 25.5 KB
/
smf2_to_flarum_alternative.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<tt>
<?php
///////////////////////////////////////////////////////////////////////////////
// SMF2 to FLARUM migration script
//
// s9e\TextFormatter version
//
// This version performes a migration from SMF2 to Flarum using the
// TextFormatter parser from s9e\TextFormatter version by PHP
//
// (C) Marco Zambianchi - ISAA Technical Team (http://www.isaa.it)
// This file is shared under a Creative Commons "BY" license.
//
// Partially based on script phpbb_to_flarum by robrotheram, VIRUXE, Reflic
// https://github.com/robrotheram/phpbb_to_flarum
//
// License: MIT
///////////////////////////////////////////////////////////////////////////////
// Get the autoloader (unless you have already loaded Composer's)
include __DIR__ . '/vendor/autoload.php';
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
set_time_limit(60*60*24); // 1 day
error_reporting(E_ALL);
ini_set('display_errors',1);
$timestamp_start = time();
///////////////////////////////////////////////////////////////////////////////
// GENERAL SETTINGS ***TO BE TAILORED***
///////////////////////////////////////////////////////////////////////////////
// General settings are in smf2_to_flarum_settings.php
///////////////////////////////////////////////////////////////////////////////
include_once("smf2_to_flarum_settings.php");
///////////////////////////////////////////////////////////////////////////////
// AUXILIARY FUNCTIONS
///////////////////////////////////////////////////////////////////////////////
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;
}
// Generates a random color for Tags
function rand_color()
{
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
// Used to convert Categories to Tags
function stripBBCode($text_to_search) {
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
// We convert messages bodies into Flarum-compatible XML format
function newFormatText($connection,$text)
{
// We get rid of [html] bbcode, saving part of its content.
if (preg_match('/\[html]/si',$text))
{
$text = preg_replace('/\[html]/si', '', $text);
$text = preg_replace('/\[\/html]/si', '', $text);
$text = strip_tags($text,"<img>");
preg_match_all('#src="?(https?:\/\/(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)"?#is',$text,$matches);
$text = strip_tags($text);
$text = "[img]".$matches[1][0]."[/img]\n".$text;
}
// HTML line breaks to \n
$text = preg_replace('/(<br\s?\/?>)+/is', "\n", $text);
// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace('/[\r\n]{2,}/s', "\n", $text);
// Handle some special case here...
$text = preg_replace('/(&#039;|')+/',"'", $text);
// Force encoding conversion to UTF-8
$text = html_entity_decode($text,ENT_COMPAT|ENT_HTML401,'UTF-8');
//
$text = TextFormatter::parse($text);
// Delete all non converted bbcode
$text = preg_replace('/[[\/\!]*?[^\[\]]*?]/si', '', $text);
return $connection->real_escape_string($text);
}
// Older attachments may still use this function.
function getLegacyAttachmentFilename($filename, $attachment_id)
{
$clean_name = $filename;
// Remove international characters (windows-1252)
// These lines should never be needed again. Still, behave.
$clean_name = strtr($filename,"\x8a\x8e\x9a\x9e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xff",'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
$clean_name = strtr($clean_name, array("\xde" => 'TH', "\xfe" =>'th', "\xd0" => 'DH', "\xf0" => 'dh', "\xdf" => 'ss', "\x8c" => 'OE',"\x9c" => 'oe', "\xc6" => 'AE', "\xe6" => 'ae', "\xb5" => 'u'));
// Sorry, no spaces, dots, or anything else but letters allowed.
$clean_name = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $clean_name);
$enc_name = $attachment_id . '_' . strtr($clean_name, '.', '_') . md5($clean_name);
$clean_name = preg_replace('~\.[\.]+~', '.', $clean_name);
return $enc_name;
}
function generateRandomString($length = 16) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
///////////////////////////////////////////////////////////////////////////////
// DATABASE CONNECTIONS
///////////////////////////////////////////////////////////////////////////////
echo "<h2>DATABASE CONNECTIONS</h2>";
// Establish a connection to the server where the PHPBB database exists
$exportDbConnection = new mysqli($servername, $usrSMF, $pwdSMF, $dbSMF);
printf("Initial character set in EXPORT server: %s<br>", $exportDbConnection->character_set_name());
if (!$exportDbConnection->set_charset("utf8")) {
printf("Error loading character set utf8: %s<br>", $exportDbConnection->error);
} else {
printf("Current character set in EXPORT server: %s<br>", $exportDbConnection->character_set_name());
}
if ($do_import)
{
$importDbConnection = new mysqli($servername, $usrFlarum, $pwdFlarum, $dbFlarum);
printf("Initial character set in IMPORT server: %s<br>", $exportDbConnection->character_set_name());
if (!$importDbConnection->set_charset("utf8")) {
printf("Error loading character set utf8: %s<br>", $importDbConnection->error);
} else {
printf("Current character set in IMPORT server: %s<br>", $importDbConnection->character_set_name());
}
}
// SQL Dump file
if ($do_dump)
{
$fileDump = "SMF_dump.sql";
$fileHandler = fopen($fileDump,"w");
}
///////////////////////////////////////////////////////////////////////////////
// 0) SHOW SETTINGS
///////////////////////////////////////////////////////////////////////////////
// Export settings
echo "<h2>0) EXPORT/IMPORT SETTINGS</h2>";
if ($do_users) { echo "USERS export ENABLED<br>"; } else { echo "USERS export DISABLED<br>"; }
if ($do_tags) { echo "BOARDS export ENABLED<br>"; } else { echo "BOARDS export DISABLED<br>"; }
if ($do_posts) { echo "MESSAGES export ENABLED<br>"; } else { echo "MESSAGES export DISABLED<br>"; }
if ($do_import) { echo "DIRECT IMPORT in flarum ENABLED<br>"; } else { echo "DIRECT IMPORT in flarum DISABLED<br>"; }
if ($do_dump) { echo "SQL FILE DUMP ENABLED<br>"; } else { echo "SQL FILE DUMP DISABLED<br>"; }
if ($do_youtube_links) { echo "YOUTUBE EMBED ENABLED<br>"; } else { echo "YOUTUBE EMBED ENABLED<br>"; }
if ($do_dump) { echo "IMAGE ATTACHMENTS EXPORT ENABLED<br>"; } else { echo "IMAGE ATTACHMENTS EXPORT DISABLED<br>"; }
///////////////////////////////////////////////////////////////////////////////
// 1) USERS
///////////////////////////////////////////////////////////////////////////////
if ($do_users)
{
echo "<H2>1) SMF USERS TO FLARUM USERS</H2>";
$result = $exportDbConnection->query("
SELECT
".$table_prefix."members.*,
".$table_prefix."attachments.id_attach,
".$table_prefix."attachments.filename,
".$table_prefix."attachments.fileext,
".$table_prefix."attachments.file_hash,
".$table_prefix."attachments.id_folder
FROM ".$table_prefix."members
LEFT JOIN ".$table_prefix."attachments
ON ".$table_prefix."members.id_member=".$table_prefix."attachments.id_member
WHERE
".$table_prefix."attachments.attachment_type = 0"
);
if (!$result) echo $exportDbConnection->error;
$totalUsers = $result->num_rows;
if ($result)
{
if ($do_import)
{
$auxQuery = $importDbConnection->query("TRUNCATE users;");
$auxQuery = $importDbConnection->query("TRUNCATE users_groups;");
}
if ($do_dump)
{
$testW = fwrite($fileHandler,"TRUNCATE users;".PHP_EOL);
$testW = fwrite($fileHandler,"TRUNCATE users_groups;".PHP_EOL);
}
// if avatars dir doesn't exists we create it
if (!file_exists(__DIR__ . '/avatars')) mkdir(__DIR__ . '/avatars');
echo "Found $totalUsers users to export<br>";
$i = 0;
$usersIgnored = 0;
while($row = $result->fetch_assoc())
{
$i++;
// no email address, we skip
if(trim($row["email_address"]))
{
$username = $row['member_name'];
$id = $row['id_member'];
$email = $row['email_address'];
$password = sha1(md5(time())); //old password is deleted and changed with a random one
$jointime = date("Y-m-d H:i:s",$row['date_registered']);
echo sprintf("User %06d - %s\n<br>",$id,$username);
// Avatars are collected from SMF attachments table - Save location in flarum ./assets/avatars/7hgxrunbithyo20i.jpg
if (trim($row['file_hash']) == "")
{
$filename = getLegacyAttachmentFilename($row['filename'],$row['id_attach']);
} else {
$filename = $row['id_attach'] . "_" . $row['file_hash'];
}
$idx_src_dir = $row['id_folder'];
$src_dir = $attachments_dir[$idx_src_dir];
$src = $src_dir . '/' . $filename;
$dst = __DIR__.'/avatars/' . generateRandomString() . '.' . $row['fileext'];
// We make sure we have a unique filename
while (file_exists($dst)) $dst = __DIR__.'/avatars/' . generateRandomString() . '.' . $row['fileext'];
// If avatar not found or copy fails, we set avatar to NULL
$val = @copy($src,$dst);
//echo "Copy of $src to $dst ";
//if ($val) echo '<span style="color:green;">OK</span>';
$avatar = "'".basename($dst)."'";
//if (!$val) echo '<span style="color:red;">KO</span><br>';
if (!$val) $avatar = "NULL";
// We create the users table entries
$query = "INSERT INTO `users` (`id`, `username`, `email`, `password`, `join_time`, `avatar_path`, `is_activated`) VALUES ('$id', '$username', '$email', '$password', '$jointime', " . $avatar . ", '1');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We create the users_groups table entries, adding all new users to "members" group
$query = "INSERT INTO `users_groups` (`user_id`, `group_id`) VALUES ('$id', '3');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
}
else {
$usersIgnored++;
}
}
echo ($i-$usersIgnored) . ' of '. $totalUsers .' users converted';
}
else
echo "Users export error.";
$result->free_result();
echo "<hr>";
}
///////////////////////////////////////////////////////////////////////////////
// 2) SMF BOARDS AND SUB-BOARDS TO FLARUM TAGS
///////////////////////////////////////////////////////////////////////////////
if ($do_tags)
{
echo "<h2>2) SMF BOARDS AND SUB-BOARDS TO FLARUM TAGS...</h2>";
$result = $exportDbConnection->query("SELECT * FROM ".$table_prefix."boards ORDER BY id_parent, child_level;");
if (!$result) echo $exportDbConnection->error;
$totalBoards = $result->num_rows;
if ($totalBoards)
{
if ($do_import)
{
$auxQuery = $importDbConnection->query("TRUNCATE tags");
}
if ($do_dump)
{
$testW = fwrite($fileHandler,"TRUNCATE tags;".PHP_EOL);
}
$i = 1;
while($row = $result->fetch_assoc())
{
$id = $row["id_board"];
$name = html_entity_decode(addslashes($row["name"]));
$description = html_entity_decode(addslashes($row["description"]));
$color = rand_color();
$slug = slugify($row["name"]);
// We port only the first 2 layers of sub-boards, any deeper level is imported as secondary Tag
if ($row["child_level"] <= 1)
{
$parent_id = ($row["id_parent"] < 1) ? "NULL" : "'".$row["id_parent"]."'";
$position = "'$i'";
} else {
$parent_id = "NULL";
$position = "NULL";
}
echo sprintf("Tag %04d - %s (%s)\n<br>",$i,$name,$slug);
// Let's put some records in this Tags table!
$query = "INSERT IGNORE INTO tags (id, name, description, parent_id, slug, color, position) VALUES ( '$id', '$name', '$description', $parent_id, '$slug', '$color', $position);";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
$i++;
}
echo $totalBoards . ' boards and sub-boards converted.';
}
else
echo "Boards/Sub-boards export error.";
$result->close();
echo "<hr>";
}
///////////////////////////////////////////////////////////////////////////////
// 3) SMF TOPICS AND MESSAGES TO FLARUM DISCUSSIONS AND POSTS
///////////////////////////////////////////////////////////////////////////////
/*
*** Topic/Messages to Discussions/Posts mapping ***
FLARUM SMF
discussion.id topics.id_topic
discussion.title messages.subject
discussion.comments_count topics.num_replies
discussion.participants_count TBC
discussion.number_index BLANK
discussion.start_time messages.poster_time (from Timestamp to YYYY-MM-DD HH:II:SS)
discussion.start_user_id messages.id_member
discussion.start_post_id messages.id_msg (first topic only, of course)
discussion.last_time TBC
discussion.last_user_id topics.id_member_updated
discussion.last_post_id topics.id_last_msg
discussion.last_post_number messages.num_replies
discussion.hide_time NULL
discussion.hide_user_id NULL
discussion.is_locked topics.locked
discussion.is_sticky topics.is_sticky
*/
if ($do_posts)
{
echo "<h2>3) SMF TOPICS AND MESSAGES TO FLARUM DISCUSSIONS AND POSTS</h2>";
$result = $exportDbConnection->query("SELECT
topics.id_topic,
topics.id_board,
messages.id_msg,
messages.subject,
topics.num_replies,
from_unixtime(messages.poster_time) as poster_time,
messages.id_member,
messages.id_msg,
topics.id_member_updated,
messages.body,
topics.id_last_msg,
topics.locked,
topics.is_sticky,
messages.poster_ip
FROM ".$table_prefix."topics as topics INNER JOIN ".$table_prefix."messages as messages
ON topics.id_topic = messages.id_topic
ORDER BY topics.id_topic DESC, messages.poster_time $limit_topics;");
if (!$result) echo $exportDbConnection->error;
$totalPosts = $result->num_rows;
if ($result)
{
echo "Found $totalPosts messages to convert<br>";
// We empty the tables, for a clean import
if ($do_import)
{
$auxQuery = $importDbConnection->query("TRUNCATE discussions;");
$auxQuery = $importDbConnection->query("TRUNCATE posts;");
$auxQuery = $importDbConnection->query("TRUNCATE users_discussions;");
}
if ($do_dump)
{
$testW = fwrite($fileHandler,"TRUNCATE discussions;".PHP_EOL);
$testW = fwrite($fileHandler,"TRUNCATE posts;".PHP_EOL);
$testW = fwrite($fileHandler,"TRUNCATE users_discussions;".PHP_EOL);
}
$converted = 0;
$prev_id = -1;
while($message = $result->fetch_assoc())
{
// DISCUSSIONS TABLE
// We have a new topic, we create an entry in discussions, then we continue with entries in posts
if ($prev_id != $message["id_topic"])
{
// We try to detect and correct any subject which is not utf-8 encoded
$encoding = mb_detect_encoding($message["subject"],'UTF-8, ASCII, ISO-8859-1', true);
$subject = $exportDbConnection->real_escape_string(html_entity_decode($message["subject"]));
if ($encoding != 'UTF-8') $subject = $exportDbConnection->real_escape_string(html_entity_decode(iconv($encoding,'UTF-8',$message["subject"])));
//echo "New discussion, ID ".$message["id_topic"]." (".$message["subject"]." ($encoding) => $subject)"."<br>";
echo sprintf("Discussion ID %07d %s [Posts: %05d]<br>",$message["id_topic"],$subject,($message["num_replies"]+1));
$post_counter = 1;
$prev_id = $message["id_topic"];
$comments_counter = $message["num_replies"]+1;
// Partecipants Count
$auxQuery = $exportDbConnection->query("SELECT DISTINCT id_member FROM ".$table_prefix."messages WHERE id_topic='".$message["id_topic"]."';");
$participants_count = $auxQuery->num_rows;
$auxQuery->free_result();
// Last post date and time
$auxQuery = $exportDbConnection->query("SELECT from_unixtime(MAX(poster_time)) as last_time FROM ".$table_prefix."messages WHERE id_topic='".$message["id_topic"]."';");
$auxRes = $auxQuery->fetch_assoc();
$last_time = $auxRes["last_time"];
$auxQuery->free_result();
// NOTE: SMF was setting user_id to 0 for deleted/banned users. A NEW LOGIC SHALL BE DEFINED HERE (Define a "fake" user with user_id like -1?
$query = "INSERT INTO `discussions` (`id`,`title`,`comments_count`,`participants_count`,`number_index`,`start_time`,`start_user_id`,`start_post_id`,`last_time`,`last_user_id`,
`last_post_id`,`last_post_number`,`hide_time`,`hide_user_id`,`slug`,`is_approved`,`is_locked`,`is_sticky`
) VALUES (
'".$message["id_topic"]."','".$subject."','".$comments_counter."','".$participants_count."','".$message["num_replies"]."','".$message["poster_time"]."','".$message["id_member"]."','".$message["id_msg"]."','".$last_time."','".$message["id_member_updated"]."','".$message["id_last_msg"]."',DEFAULT,DEFAULT,DEFAULT,'".slugify($message["subject"])."','1','".$message["locked"]."','".$message["is_sticky"]."');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We now connect Tags with Discussions
$query = "INSERT IGNORE INTO discussions_tags (`discussion_id`,`tag_id`) VALUES ('".$message["id_topic"]."','".$message["id_board"]."');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We update discussion_count in table users
$query = "UPDATE users SET discussions_count=discussions_count+1 WHERE id='".$message["id_member"]."';";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
}
// posts table population
// We try to detect and correct any subject which is not utf-8 encoded
$encoding = mb_detect_encoding($message["body"],'UTF-8, ASCII, ISO-8859-1', true);
$body = $message["body"];
if ($encoding != 'UTF-8') $body = iconv($encoding,'UTF-8',$message["body"]);
// Single entries in post table now...
$query = "INSERT INTO `posts` (`id`,`discussion_id`,`number`,`time`,`user_id`,`type`,`content`,`edit_time`,`edit_user_id`,`hide_time`,`hide_user_id`,`ip_address`,`is_approved`
) VALUES (
'".$message["id_msg"]."','".$message["id_topic"]."','".$post_counter."','".$message["poster_time"]."','".$message["id_member"]."','comment','".newFormatText($exportDbConnection,$body)."',DEFAULT,DEFAULT,DEFAULT,DEFAULT,'".$message["poster_ip"]."',1);";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We add a record to users_discussions too
$query = "INSERT IGNORE INTO `users_discussions` (`user_id`,`discussion_id`) VALUES ('".$message["id_member"]."','".$message["id_topic"]."');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We update discussion_count in table users
$query = "UPDATE users SET comments_count=comments_count+1 WHERE id='".$message["id_member"]."';";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
$post_counter++;
$converted++;
// Every 100 converted posts, we sleep a few seconds 5 seconds
if (($converted % 100) == 0)
{
echo "Converted $converted of $totalPosts messages<br>";
echo "Elapsed time: ".(time() - $timestamp_start)." sec.<br>";
flush();
ob_flush();
sleep($server_interval);
}
}
$result->free_result();
}
else
echo "Topic/Messages export error.";
}
///////////////////////////////////////////////////////////////////////////////
// 4) ATTACHMENTS (IMAGES)
///////////////////////////////////////////////////////////////////////////////
if ($do_attachments_images)
{
echo "<H2>4) SMF ATTACHMENTS, IMAGES ONLY</H2>";
$result = $exportDbConnection->query("
SELECT
".$table_prefix."messages.id_member,
from_unixtime(".$table_prefix."messages.poster_time) as poster_time,
".$table_prefix."attachments.id_attach,
".$table_prefix."attachments.id_msg,
".$table_prefix."attachments.filename,
".$table_prefix."attachments.fileext,
".$table_prefix."attachments.file_hash,
".$table_prefix."attachments.size,
".$table_prefix."attachments.id_folder
FROM ".$table_prefix."attachments
INNER JOIN ".$table_prefix."messages
ON ".$table_prefix."attachments.id_msg=".$table_prefix."messages.id_msg
WHERE
".$table_prefix."attachments.attachment_type = 0
AND
(".$table_prefix."attachments.mime_type LIKE 'image%'
OR ".$table_prefix."attachments.fileext='jpeg'
OR ".$table_prefix."attachments.fileext='jpg'
OR ".$table_prefix."attachments.fileext='bmp'
OR ".$table_prefix."attachments.fileext='gif')
ORDER BY id_attach DESC
"
);
if (!$result) echo $exportDbConnection->error;
$totalAttachments = $result->num_rows;
if ($result)
{
if ($do_import)
{
$auxQuery = $importDbConnection->query("TRUNCATE flagrow_images;");
}
if ($do_dump)
{
$testW = fwrite($fileHandler,"TRUNCATE flagrow_images;".PHP_EOL);
}
// if images dir doesn't exists we create it
if (!file_exists(__DIR__ . '/images')) mkdir(__DIR__ . '/images');
echo "Found $totalAttachments attachments to export<br>";
$i = 0;
while($row = $result->fetch_assoc())
{
// We need to know whether the attachment has been saved with SMF1.x or SMF2.x
// The newer ones have file_hash
if (trim($row['file_hash']) == "")
{
$filename = getLegacyAttachmentFilename($row['filename'],$row['id_attach']);
} else {
$filename = $row['id_attach'] . "_" . $row['file_hash'];
}
$idx_src_dir = $row['id_folder'];
$src_dir = $attachments_dir[$idx_src_dir];
$src = $src_dir . '/' . $filename;
$dst = __DIR__.'/images/' . $row['id_member'] . "_" . md5($filename) . '.' . $row['fileext'];
$attachment = basename($dst);
$attachment_url = $flarum_website_url."/assets/images/$attachment";
// We make sure we have a unique filename
// while (file_exists($dst)) $dst = __DIR__.'/images/' . generateRandomString() . '.' . $row['fileext'];
// We try to copy the attachment file in images directory
$val = @copy($src,$dst);
if (!$val) continue;
$i++;
echo sprintf("%06d) id_attach:%06d id_msg:%06d id_member:%06d filename:%s\n<br>",$i,$row['id_attach'],$row['id_msg'],$row['id_member'],$row['filename']);
// We create the flagrow_images table entries
$query = "INSERT INTO `flagrow_images`
(`user_id`, `file_name`, `upload_method`, `created_at`, `file_url`, `file_size`) VALUES ('".$row['id_member']."', '".basename($dst)."', 'local', '".$row['poster_time']."', '".$attachment_url."', '".$row['size']."');";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
echo "$query<br>";
// We prepare the attachment link and we place it at the end of the message body
$text = '<p><IMG alt="image '.$attachment_url.'" src="'.$attachment_url.'"><s>![</s>image '.$attachment_url.'<e>]('.$attachment_url.')</e></IMG></p></r>';
// We make sure all <t> type posts are re-defined as <r>
$query = "UPDATE `posts` SET content=REPLACE(content, '<t>', '<r>') WHERE id='".$row['id_msg']."';";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// We make sure all </t> type posts are re-defined as </r>
$query = "UPDATE `posts` SET content=REPLACE(content,'".$exportDbConnection->real_escape_string("</t>")."','".$exportDbConnection->real_escape_string("</r>")."') WHERE id='".$row['id_msg']."';";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
// Finally we swap the last </r> in the content with the proper XML for the attachment.
$query = "UPDATE `posts` SET content=REPLACE(content,'".$exportDbConnection->real_escape_string("</r>")."','".$exportDbConnection->real_escape_string($text)."') WHERE id = '".$row['id_msg']."';";
if ($do_dump) $testW = fwrite($fileHandler,$query.PHP_EOL);
if ($do_import)
{
$auxQuery = $importDbConnection->query($query);
if (!$auxQuery) echo $importDbConnection->error . "<br>";
}
echo "<hr>";
}
$result->free_result();
echo "$i attachments of $totalAttachments exported<br>";
}
else
echo "Users export error.";
echo "<hr>";
}
//Clean-up
$exportDbConnection->close();
if ($do_import) $importDbConnection->close();
fclose($fileHandler);
?>
</tt>
</body>
</html>