forked from robrotheram/phpbb_to_flarum
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxenforo_to_flarum.php
137 lines (117 loc) · 3.63 KB
/
xenforo_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
<?php
/**
* Original script by robrotheram from discuss.flarum.org
* Modified by VIRUXE
* Modified by Reflic
* Modified by TidbitSoftware
* Modified by Jodie Struthers for XenForo.
*/
require_once 'xenforo_connection.php';
echo '<h1>XenForo 1.5 to Flarum</h1>';
$connection = new Database();
$exportDbConnection = $connection->connectExport();
$importDbConnection = $connection->connectImport();
$truncateTables = true;
if ($truncateTables) {
$tableNames = [
'discussions',
'discussion_tag',
'discussion_user',
'group_user',
'posts',
'tags',
'tag_user',
'users'
];
foreach ($tableNames as $tableName) {
$importDbConnection->query('TRUNCATE '.$connection->importDBPrefix.$tableName);
}
}
// Run migration scripts
require 'xfscripts/users.php';
require 'xfscripts/forums.php';
require 'xfscripts/threads.php';
// Convert user posted topics to user discussions?
echo '<h2>Step 5 - Update User Content Counts</h2>';
$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 discussion_user where user_id = '$userID' ");
$numTopics = $res->num_rows;
$res1 = $importDbConnection->query("select * from posts where user_id = '$userID' ");
$numPosts = $res1->num_rows;
$query = 'UPDATE '.$connection->importDBPrefix."users SET discussion_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 '<p><b>Success.</b> User post counts updated successfully.</p>';
} else {
echo 'Table is empty';
}
echo '<h2>Step 6 - Adding user_id# '.$connection->adminUserID.' to Flarum Admin Group</h2>';
$query = 'INSERT INTO '.$connection->importDBPrefix."group_user (user_id, group_id) VALUES( '$connection->adminUserID', 1)";
$res = $importDbConnection->query($query);
if ($res === false) {
echo 'Wrong SQL: '.$query.' Error: '.$importDbConnection->error." <br/>\n";
} else {
echo '<p><b>Success.</b> Admin user added to admin group.</p>';
}
// Close connections to the databases
$exportDbConnection->close();
$importDbConnection->close();
function slugify($text)
{
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
// Multibyte String Functions in case of emojis
$text = mb_strtolower($text);
$text = iconv('utf-8', 'utf-8//TRANSLIT', $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([
'\\',
"\0",
"\n",
"\r",
"'",
'"',
"\x1a"
], [
'\\\\',
'\\0',
'\\n',
'\\r',
"\\'",
'\\"',
'\\Z'
], $inp);
}
return $inp;
}
// Used to convert Categories to Tags
function stripBBCode($text_to_search)
{
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}