2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
require "mysql2"
|
2015-03-15 13:31:08 +08:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
|
|
|
|
2017-07-01 05:11:49 +08:00
|
|
|
# Before running this script, paste these lines into your shell,
|
|
|
|
# then use arrow keys to edit the values
|
|
|
|
=begin
|
|
|
|
export DB_HOST="localhost"
|
|
|
|
export DB_NAME="mybb"
|
|
|
|
export DB_PW=""
|
|
|
|
export DB_USER="root"
|
|
|
|
export TABLE_PREFIX="mybb_"
|
2022-07-25 21:22:25 +08:00
|
|
|
export UPLOADS_DIR="/data/youruploads"
|
2017-07-01 05:11:49 +08:00
|
|
|
export BASE="" #
|
|
|
|
=end
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
# Call it like this:
|
2015-09-22 20:26:57 +08:00
|
|
|
# RAILS_ENV=production ruby script/import_scripts/mybb.rb
|
2014-09-30 02:37:35 +08:00
|
|
|
class ImportScripts::MyBB < ImportScripts::Base
|
2024-11-06 06:27:49 +08:00
|
|
|
DB_HOST = ENV["DB_HOST"] || "localhost"
|
|
|
|
DB_NAME = ENV["DB_NAME"] || "mybb"
|
|
|
|
DB_PW = ENV["DB_PW"] || ""
|
|
|
|
DB_USER = ENV["DB_USER"] || "root"
|
|
|
|
TABLE_PREFIX = ENV["TABLE_PREFIX"] || "mybb_"
|
|
|
|
UPLOADS_DIR = ENV["UPLOADS"] || "/data/limelightgaming/uploads"
|
2014-09-30 02:37:35 +08:00
|
|
|
BATCH_SIZE = 1000
|
2017-07-01 05:11:49 +08:00
|
|
|
BASE = ""
|
|
|
|
QUIET = true
|
2021-07-22 15:55:02 +08:00
|
|
|
IMPORT_DELETED_POSTS = false
|
2014-09-30 02:37:35 +08:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
|
|
|
|
@client =
|
2017-07-01 05:11:49 +08:00
|
|
|
Mysql2::Client.new(host: DB_HOST, username: DB_USER, password: DB_PW, database: DB_NAME)
|
2014-09-30 02:37:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-06-07 12:14:35 +08:00
|
|
|
SiteSetting.disable_emails = "non-staff"
|
2014-09-30 02:37:35 +08:00
|
|
|
import_users
|
|
|
|
import_categories
|
|
|
|
import_posts
|
|
|
|
import_private_messages
|
2017-07-01 05:11:49 +08:00
|
|
|
create_permalinks
|
2014-09-30 02:37:35 +08:00
|
|
|
suspend_users
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_users
|
|
|
|
puts "", "creating users"
|
|
|
|
|
|
|
|
total_count =
|
|
|
|
mysql_query(
|
|
|
|
"SELECT count(*) count
|
2015-03-15 13:31:08 +08:00
|
|
|
FROM #{TABLE_PREFIX}users u
|
|
|
|
JOIN #{TABLE_PREFIX}usergroups g ON g.gid = u.usergroup
|
2014-09-30 02:37:35 +08:00
|
|
|
WHERE g.title != 'Banned';",
|
|
|
|
).first[
|
|
|
|
"count"
|
|
|
|
]
|
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
results =
|
|
|
|
mysql_query(
|
2022-07-25 21:22:25 +08:00
|
|
|
"SELECT uid id, email email, username, regdate, g.title `group`, avatar
|
2015-03-15 13:31:08 +08:00
|
|
|
FROM #{TABLE_PREFIX}users u
|
|
|
|
JOIN #{TABLE_PREFIX}usergroups g ON g.gid = u.usergroup
|
2014-09-30 02:37:35 +08:00
|
|
|
WHERE g.title != 'Banned'
|
|
|
|
ORDER BY u.uid ASC
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
OFFSET #{offset};",
|
|
|
|
)
|
|
|
|
|
|
|
|
break if results.size < 1
|
|
|
|
|
2015-09-29 14:52:35 +08:00
|
|
|
next if all_records_exist? :users, results.map { |u| u["id"].to_i }
|
2015-09-22 07:48:42 +08:00
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
create_users(results, total: total_count, offset: offset) do |user|
|
2022-07-25 21:22:25 +08:00
|
|
|
avatar_url = user["avatar"].match(/^http/) ? user["avatar"].gsub(/\?.*/, "") : nil
|
2014-09-30 02:37:35 +08:00
|
|
|
{
|
|
|
|
id: user["id"],
|
|
|
|
email: user["email"],
|
|
|
|
username: user["username"],
|
|
|
|
created_at: Time.zone.at(user["regdate"]),
|
|
|
|
moderator: user["group"] == "Super Moderators",
|
2022-07-25 21:22:25 +08:00
|
|
|
admin: user["group"] == "Administrators",
|
|
|
|
avatar_url: avatar_url,
|
|
|
|
post_create_action:
|
|
|
|
proc do |newuser|
|
|
|
|
if !user["avatar"].blank?
|
|
|
|
avatar = user["avatar"].gsub(/\?.*/, "")
|
|
|
|
if avatar.match(/^http.*/)
|
|
|
|
UserAvatar.import_url_for_user(avatar, newuser)
|
|
|
|
else
|
|
|
|
filename = File.join(UPLOADS_DIR, avatar)
|
2023-12-07 06:25:00 +08:00
|
|
|
@uploader.create_avatar(newuser, filename) if File.exist?(filename)
|
2023-01-07 19:53:14 +08:00
|
|
|
end
|
2022-07-25 21:22:25 +08:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
2014-09-30 02:37:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_categories
|
|
|
|
results =
|
|
|
|
mysql_query(
|
|
|
|
"
|
|
|
|
SELECT fid id, pid parent_id, left(name, 50) name, description
|
2015-03-15 13:31:08 +08:00
|
|
|
FROM #{TABLE_PREFIX}forums
|
2014-09-30 02:37:35 +08:00
|
|
|
ORDER BY pid ASC, fid ASC
|
|
|
|
",
|
|
|
|
)
|
|
|
|
|
|
|
|
create_categories(results) do |row|
|
|
|
|
h = {
|
|
|
|
id: row["id"],
|
|
|
|
name: CGI.unescapeHTML(row["name"]),
|
|
|
|
description: CGI.unescapeHTML(row["description"]),
|
|
|
|
}
|
|
|
|
if row["parent_id"].to_i > 0
|
2015-03-13 04:15:02 +08:00
|
|
|
h[:parent_category_id] = category_id_from_imported_category_id(row["parent_id"])
|
2014-09-30 02:37:35 +08:00
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_posts
|
|
|
|
puts "", "creating topics and posts"
|
|
|
|
|
2015-03-15 13:31:08 +08:00
|
|
|
total_count = mysql_query("SELECT count(*) count from #{TABLE_PREFIX}posts").first["count"]
|
2014-09-30 02:37:35 +08:00
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
results =
|
|
|
|
mysql_query(
|
|
|
|
"
|
|
|
|
SELECT p.pid id,
|
|
|
|
p.tid topic_id,
|
|
|
|
t.fid category_id,
|
|
|
|
t.subject title,
|
|
|
|
t.firstpost first_post_id,
|
|
|
|
p.uid user_id,
|
|
|
|
p.message raw,
|
|
|
|
p.dateline post_time
|
2015-03-15 13:31:08 +08:00
|
|
|
FROM #{TABLE_PREFIX}posts p,
|
|
|
|
#{TABLE_PREFIX}threads t
|
2014-09-30 02:37:35 +08:00
|
|
|
WHERE p.tid = t.tid
|
2021-07-22 15:55:02 +08:00
|
|
|
#{"AND (p.visible = 1 AND t.visible = 1)" unless IMPORT_DELETED_POSTS}
|
2014-11-14 21:28:09 +08:00
|
|
|
ORDER BY p.dateline
|
2014-09-30 02:37:35 +08:00
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
OFFSET #{offset};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
|
|
|
|
break if results.size < 1
|
|
|
|
|
2015-09-22 07:48:42 +08:00
|
|
|
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
create_posts(results, total: total_count, offset: offset) do |m|
|
|
|
|
skip = false
|
|
|
|
mapped = {}
|
|
|
|
|
2014-11-14 21:41:34 +08:00
|
|
|
# If you have imported a phpbb forum to mybb previously there might
|
2015-03-15 13:31:08 +08:00
|
|
|
# be a problem with #{TABLE_PREFIX}threads.firstpost. If these ids are wrong
|
2014-11-14 21:41:34 +08:00
|
|
|
# the thread cannot be imported to discourse as the topic post is
|
|
|
|
# missing. This query retrieves the first_post_id manually. As it
|
|
|
|
# will decrease the performance it is commented out by default.
|
|
|
|
# m['first_post_id'] = mysql_query("
|
|
|
|
# SELECT p.pid id,
|
2015-03-15 13:31:08 +08:00
|
|
|
# FROM #{TABLE_PREFIX}posts p,
|
|
|
|
# #{TABLE_PREFIX}threads t
|
2014-11-14 21:41:34 +08:00
|
|
|
# WHERE p.tid = #{m['topic_id']} AND t.tid = #{m['topic_id']}
|
|
|
|
# ORDER BY p.dateline
|
|
|
|
# LIMIT 1
|
|
|
|
# ").first['id']
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
mapped[:id] = m["id"]
|
|
|
|
mapped[:user_id] = user_id_from_imported_user_id(m["user_id"]) || -1
|
|
|
|
mapped[:raw] = process_mybb_post(m["raw"], m["id"])
|
|
|
|
mapped[:created_at] = Time.zone.at(m["post_time"])
|
|
|
|
|
|
|
|
if m["id"] == m["first_post_id"]
|
2015-03-13 04:15:02 +08:00
|
|
|
mapped[:category] = category_id_from_imported_category_id(m["category_id"])
|
2014-09-30 02:37:35 +08:00
|
|
|
mapped[:title] = CGI.unescapeHTML(m["title"])
|
|
|
|
else
|
|
|
|
parent = topic_lookup_from_imported_post_id(m["first_post_id"])
|
|
|
|
if parent
|
|
|
|
mapped[:topic_id] = parent[:topic_id]
|
|
|
|
else
|
|
|
|
puts "Parent post #{m["first_post_id"]} doesn't exist. Skipping #{m["id"]}: #{m["title"][0..40]}"
|
|
|
|
skip = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
skip ? nil : mapped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_private_messages
|
|
|
|
puts "", "private messages are not implemented"
|
|
|
|
end
|
|
|
|
|
|
|
|
def suspend_users
|
|
|
|
puts "", "banned users are not implemented"
|
|
|
|
end
|
|
|
|
|
2015-09-23 13:31:42 +08:00
|
|
|
# Discourse usernames don't allow spaces
|
2015-09-22 20:26:57 +08:00
|
|
|
def convert_username(username, post_id)
|
|
|
|
count = 0
|
2015-10-15 08:44:27 +08:00
|
|
|
username.gsub!(/\s+/) do |a|
|
|
|
|
count += 1
|
2015-09-22 20:26:57 +08:00
|
|
|
"_"
|
|
|
|
end
|
2015-09-23 13:31:42 +08:00
|
|
|
# Warn on MyBB bug that places post text in the quote line - http://community.mybb.com/thread-180526.html
|
2015-09-22 20:26:57 +08:00
|
|
|
puts "Warning: probably incorrect quote in post #{post_id}" if count > 5
|
2019-11-15 04:10:51 +08:00
|
|
|
username
|
2015-09-22 20:26:57 +08:00
|
|
|
end
|
|
|
|
|
2015-09-23 13:31:42 +08:00
|
|
|
# Take an original post id and return the migrated topic id and post number for it
|
2015-09-22 20:26:57 +08:00
|
|
|
def post_id_to_post_num_and_topic(quoted_post_id, post_id)
|
|
|
|
quoted_post_id_from_imported = post_id_from_imported_post_id(quoted_post_id.to_i)
|
|
|
|
if quoted_post_id_from_imported
|
|
|
|
begin
|
|
|
|
post = Post.find(quoted_post_id_from_imported)
|
2019-12-10 08:48:27 +08:00
|
|
|
"post:#{post.post_number}, topic:#{post.topic_id}"
|
2015-09-22 20:26:57 +08:00
|
|
|
rescue StandardError
|
|
|
|
puts "Could not find migrated post #{quoted_post_id_from_imported} quoted by original post #{post_id} as #{quoted_post_id}"
|
2019-12-10 08:48:27 +08:00
|
|
|
""
|
2015-09-22 20:26:57 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "Original post #{post_id} quotes nonexistent post #{quoted_post_id}"
|
2019-12-10 08:48:27 +08:00
|
|
|
""
|
2015-09-22 20:26:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
def process_mybb_post(raw, import_id)
|
|
|
|
s = raw.dup
|
|
|
|
|
2015-09-22 20:26:57 +08:00
|
|
|
# convert the quote line
|
|
|
|
s.gsub!(/\[quote='([^']+)'.*?pid='(\d+).*?\]/) do
|
|
|
|
"[quote=\"#{convert_username($1, import_id)}, " +
|
|
|
|
post_id_to_post_num_and_topic($2, import_id) + '"]'
|
2023-01-07 19:53:14 +08:00
|
|
|
end
|
2015-09-22 20:26:57 +08:00
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
# :) is encoded as <!-- s:) --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /><!-- s:) -->
|
|
|
|
s.gsub!(/<!-- s(\S+) -->(?:.*)<!-- s(?:\S+) -->/, '\1')
|
|
|
|
|
|
|
|
# Some links look like this: <!-- m --><a class="postlink" href="http://www.onegameamonth.com">http://www.onegameamonth.com</a><!-- m -->
|
|
|
|
s.gsub!(%r{<!-- \w --><a(?:.+)href="(\S+)"(?:.*)>(.+)</a><!-- \w -->}, '[\2](\1)')
|
|
|
|
|
|
|
|
# Many phpbb bbcode tags have a hash attached to them. Examples:
|
|
|
|
# [url=https://google.com:1qh1i7ky]click here[/url:1qh1i7ky]
|
|
|
|
# [quote="cybereality":b0wtlzex]Some text.[/quote:b0wtlzex]
|
|
|
|
s.gsub!(/:(?:\w{8})\]/, "]")
|
|
|
|
|
2014-11-14 21:26:13 +08:00
|
|
|
# Remove mybb video tags.
|
|
|
|
s.gsub!(%r{(^\[video=.*?\])|(\[/video\]$)}, "")
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
s = CGI.unescapeHTML(s)
|
|
|
|
|
|
|
|
# phpBB shortens link text like this, which breaks our markdown processing:
|
|
|
|
# [http://answers.yahoo.com/question/index ... 223AAkkPli](http://answers.yahoo.com/question/index?qid=20070920134223AAkkPli)
|
|
|
|
#
|
|
|
|
# Work around it for now:
|
|
|
|
s.gsub!(%r{\[http(s)?://(www\.)?}, "[")
|
|
|
|
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2017-07-01 05:11:49 +08:00
|
|
|
def create_permalinks
|
|
|
|
puts "", "Creating redirects...", ""
|
|
|
|
|
|
|
|
SiteSetting.permalink_normalizations = '/(\\w+)-(\\d+)[-.].*/\\1-\\2.html'
|
|
|
|
puts "", "Users...", ""
|
|
|
|
total_users = User.count
|
|
|
|
start_time = Time.now
|
|
|
|
count = 0
|
|
|
|
User.find_each do |u|
|
|
|
|
ucf = u.custom_fields
|
|
|
|
count += 1
|
|
|
|
if ucf && ucf["import_id"] && ucf["import_username"]
|
2023-01-07 19:53:14 +08:00
|
|
|
begin
|
2017-07-01 05:11:49 +08:00
|
|
|
Permalink.create(
|
|
|
|
url: "#{BASE}/user-#{ucf["import_id"]}.html",
|
|
|
|
external_url: "/u/#{u.username}",
|
|
|
|
)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-07 19:53:14 +08:00
|
|
|
end
|
2017-07-01 05:11:49 +08:00
|
|
|
end
|
|
|
|
print_status(count, total_users, start_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Categories...", ""
|
|
|
|
total_categories = Category.count
|
|
|
|
start_time = Time.now
|
|
|
|
count = 0
|
|
|
|
Category.find_each do |cat|
|
|
|
|
ccf = cat.custom_fields
|
|
|
|
count += 1
|
|
|
|
next unless id = ccf["import_id"]
|
2023-12-07 06:25:00 +08:00
|
|
|
puts("forum-#{id}.html --> /c/#{cat.id}") unless QUIET
|
2023-01-07 19:53:14 +08:00
|
|
|
begin
|
2017-07-01 05:11:49 +08:00
|
|
|
Permalink.create(url: "#{BASE}/forum-#{id}.html", category_id: cat.id)
|
2015-09-23 13:31:42 +08:00
|
|
|
rescue StandardError
|
2023-01-07 19:53:14 +08:00
|
|
|
nil
|
2017-07-01 05:11:49 +08:00
|
|
|
end
|
|
|
|
print_status(count, total_categories, start_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Topics...", ""
|
|
|
|
total_posts = Post.count
|
|
|
|
start_time = Time.now
|
|
|
|
count = 0
|
|
|
|
puts "", "Posts...", ""
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
results =
|
|
|
|
mysql_query(
|
|
|
|
"
|
|
|
|
SELECT p.pid id,
|
|
|
|
p.tid topic_id
|
|
|
|
FROM #{TABLE_PREFIX}posts p,
|
|
|
|
#{TABLE_PREFIX}threads t
|
|
|
|
WHERE p.tid = t.tid
|
|
|
|
AND t.firstpost=p.pid
|
|
|
|
ORDER BY p.dateline
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
OFFSET #{offset};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
break if results.size < 1
|
|
|
|
results.each do |post|
|
|
|
|
count += 1
|
|
|
|
if topic = topic_lookup_from_imported_post_id(post["id"])
|
|
|
|
id = post["topic_id"]
|
2023-01-07 19:53:14 +08:00
|
|
|
begin
|
2017-07-01 05:11:49 +08:00
|
|
|
Permalink.create(url: "#{BASE}/thread-#{id}.html", topic_id: topic[:topic_id])
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-07 19:53:14 +08:00
|
|
|
end
|
2017-07-01 05:11:49 +08:00
|
|
|
unless QUIET
|
2023-12-07 06:25:00 +08:00
|
|
|
puts("#{BASE}/thread-#{id}.html --> http://localhost:3000/t/#{topic[:topic_id]}")
|
2017-07-01 05:11:49 +08:00
|
|
|
end
|
|
|
|
print_status(count, total_posts, start_time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-30 02:37:35 +08:00
|
|
|
def mysql_query(sql)
|
|
|
|
@client.query(sql, cache_rows: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ImportScripts::MyBB.new.perform
|