From 119746aaa05824bbddc6513149125161a9455fda Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Oct 2016 20:00:39 +0530 Subject: [PATCH 1/5] new: FluxBB import script file added --- script/import_scripts/fluxbb.rb | 216 ++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 script/import_scripts/fluxbb.rb diff --git a/script/import_scripts/fluxbb.rb b/script/import_scripts/fluxbb.rb new file mode 100644 index 00000000000..27d0682a6f6 --- /dev/null +++ b/script/import_scripts/fluxbb.rb @@ -0,0 +1,216 @@ +require "mysql2" + +require File.expand_path(File.dirname(__FILE__) + "/base.rb") + +# Call it like this: +# RAILS_ENV=production bundle exec ruby script/import_scripts/fluxbb.rb +class ImportScripts::FluxBB < ImportScripts::Base + + FLUXBB_DB = "fluxbb_db" + BATCH_SIZE = 1000 + + def initialize + super + + @client = Mysql2::Client.new( + host: "localhost", + username: "root", + password: "pa$$word", + database: FLUXBB_DB + ) + end + + def execute + import_users + import_categories + import_posts + suspend_users + end + + def import_users + puts '', "creating users" + + total_count = mysql_query("SELECT count(*) count FROM users;").first['count'] + + batches(BATCH_SIZE) do |offset| + results = mysql_query( + "SELECT id, username, realname name, url website, email email, registered created_at, + registration_ip registration_ip_address, last_visit last_visit_time, + last_email_sent last_emailed_at, location, group_id + FROM users + LIMIT #{BATCH_SIZE} + OFFSET #{offset};") + + break if results.size < 1 + + next if all_records_exist? :users, results.map {|u| u["id"].to_i} + + create_users(results, total: total_count, offset: offset) do |user| + { id: user['id'], + email: user['email'], + username: user['username'], + name: user['name'], + created_at: Time.zone.at(user['created_at']), + website: user['website'], + registration_ip_address: user['registration_ip_address'], + last_seen_at: Time.zone.at(user['last_visit_time']), + last_emailed_at: user['last_emailed_at'] == nil ? 0 : Time.zone.at(user['last_emailed_at']), + location: user['location'], + moderator: user['group_id'] == 2, + admin: user['group_id'] == 1 } + end + end + end + + def import_categories + puts "", "importing top level categories..." + + categories = mysql_query(" + SELECT id, cat_name name, disp_position position + FROM categories + ORDER BY id ASC + ").to_a + + create_categories(categories) do |category| + { + id: category["id"], + name: category["name"] + } + end + + puts "", "importing children categories..." + + children_categories = mysql_query(" + SELECT id, forum_name name, forum_desc description, disp_position position, cat_id parent_category_id + FROM forums + ORDER BY id + ").to_a + + create_categories(children_categories) do |category| + { + id: "child##{category['id']}", + name: category["name"], + description: category["description"], + parent_category_id: category_id_from_imported_category_id(category["parent_category_id"]) + } + end + end + + def import_posts + puts "", "creating topics and posts" + + total_count = mysql_query("SELECT count(*) count from posts").first["count"] + + batches(BATCH_SIZE) do |offset| + results = mysql_query(" + SELECT p.id id, + t.id topic_id, + t.forum_id category_id, + t.subject title, + t.first_post_id first_post_id, + p.poster_id user_id, + p.message raw, + p.posted created_at + FROM posts p, + topics t + WHERE p.topic_id = t.id + ORDER BY p.posted + LIMIT #{BATCH_SIZE} + OFFSET #{offset}; + ").to_a + + break if results.size < 1 + next if all_records_exist? :posts, results.map {|m| m['id'].to_i} + + create_posts(results, total: total_count, offset: offset) do |m| + skip = false + mapped = {} + + mapped[:id] = m['id'] + mapped[:user_id] = user_id_from_imported_user_id(m['user_id']) || -1 + mapped[:raw] = process_fluxbb_post(m['raw'], m['id']) + mapped[:created_at] = Time.zone.at(m['created_at']) + + if m['id'] == m['first_post_id'] + mapped[:category] = category_id_from_imported_category_id("child##{m['category_id']}") + 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 suspend_users + puts '', "updating banned users" + + banned = 0 + failed = 0 + total = mysql_query("SELECT count(*) count FROM bans").first['count'] + + system_user = Discourse.system_user + + mysql_query("SELECT username, email FROM bans").each do |b| + user = User.find_by_email(b['email']) + if user + user.suspended_at = Time.now + user.suspended_till = 200.years.from_now + + if user.save + StaffActionLogger.new(system_user).log_user_suspend(user, "banned during initial import") + banned += 1 + else + puts "Failed to suspend user #{user.username}. #{user.errors.try(:full_messages).try(:inspect)}" + failed += 1 + end + else + puts "Not found: #{b['email']}" + failed += 1 + end + + print_status banned + failed, total + end + end + + def process_fluxbb_post(raw, import_id) + s = raw.dup + + # :) is encoded as :) + s.gsub!(/(?:.*)/, '\1') + + # Some links look like this: http://www.onegameamonth.com + s.gsub!(/(.+)<\/a>/, '[\2](\1)') + + # Many 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})\]/, ']') + + # Remove video tags. + s.gsub!(/(^\[video=.*?\])|(\[\/video\]$)/, '') + + s = CGI.unescapeHTML(s) + + # 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!(/\[http(s)?:\/\/(www\.)?/, '[') + + s + end + + def mysql_query(sql) + @client.query(sql, cache_rows: false) + end +end + +ImportScripts::FluxBB.new.perform From 7bf75f8828d62088ace9a2e7a7630d4927854ab1 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Oct 2016 20:09:40 +0530 Subject: [PATCH 2/5] fluxbb.rb: move configuration to ENV --- script/import_scripts/fluxbb.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/script/import_scripts/fluxbb.rb b/script/import_scripts/fluxbb.rb index 27d0682a6f6..505750dc5a6 100644 --- a/script/import_scripts/fluxbb.rb +++ b/script/import_scripts/fluxbb.rb @@ -2,20 +2,30 @@ require "mysql2" require File.expand_path(File.dirname(__FILE__) + "/base.rb") +# Before running this script, paste these lines into your shell, +# then use arrow keys to edit the values +=begin +export FLUXBB_USER="root" +export FLUXBB_DB="fluxbb" +export FLUXBB_PW="" +=end + # Call it like this: # RAILS_ENV=production bundle exec ruby script/import_scripts/fluxbb.rb class ImportScripts::FluxBB < ImportScripts::Base - FLUXBB_DB = "fluxbb_db" - BATCH_SIZE = 1000 + FLUXBB_DB ||= ENV['FLUXBB_DB'] || "fluxbb" + BATCH_SIZE ||= 1000 + FLUXBB_PW ||= ENV['FLUXBB_PW'] || "" + FLUXBB_USER ||= ENV['FLUXBB_USER'] || "root" def initialize super @client = Mysql2::Client.new( host: "localhost", - username: "root", - password: "pa$$word", + username: FLUXBB_USER, + password: FLUXBB_PW, database: FLUXBB_DB ) end From 470529d4c8fd3edc806eed6ef9fe09128823db18 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Oct 2016 21:08:40 +0530 Subject: [PATCH 3/5] fluxbb.rb: importing groups & members --- script/import_scripts/fluxbb.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/script/import_scripts/fluxbb.rb b/script/import_scripts/fluxbb.rb index 505750dc5a6..79dc081d494 100644 --- a/script/import_scripts/fluxbb.rb +++ b/script/import_scripts/fluxbb.rb @@ -31,12 +31,27 @@ class ImportScripts::FluxBB < ImportScripts::Base end def execute + import_groups import_users import_categories import_posts suspend_users end + def import_groups + puts '', "creating groups" + + results = mysql_query( + "SELECT g_id id, g_title name, g_user_title title + FROM groups") + + create_groups(results) do |group| + { id: group['id'], + name: group['name'], + title: group['title'] } + end + end + def import_users puts '', "creating users" @@ -69,6 +84,17 @@ class ImportScripts::FluxBB < ImportScripts::Base moderator: user['group_id'] == 2, admin: user['group_id'] == 1 } end + + create_group_members(results) do |user| + if user.group_id + user_id = user_id_from_imported_user_id(user.id) + group_id = group_id_from_imported_group_id(user.group_id) + + if user_id && group_id + GroupUser.find_or_create_by(user_id: user_id, group_id: group_id) + end + end + end end end From ae8088ab990143f75719e3a84543ec2925abb903 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 7 Oct 2016 10:29:56 +0530 Subject: [PATCH 4/5] fluxbb.rb: optional table prefix env variable added --- script/import_scripts/fluxbb.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/script/import_scripts/fluxbb.rb b/script/import_scripts/fluxbb.rb index 79dc081d494..7238370eb6f 100644 --- a/script/import_scripts/fluxbb.rb +++ b/script/import_scripts/fluxbb.rb @@ -5,25 +5,29 @@ require File.expand_path(File.dirname(__FILE__) + "/base.rb") # Before running this script, paste these lines into your shell, # then use arrow keys to edit the values =begin -export FLUXBB_USER="root" +export FLUXBB_HOST="localhost" export FLUXBB_DB="fluxbb" +export FLUXBB_USER="root" export FLUXBB_PW="" +export FLUXBB_PREFIX="" =end # Call it like this: # RAILS_ENV=production bundle exec ruby script/import_scripts/fluxbb.rb class ImportScripts::FluxBB < ImportScripts::Base + FLUXBB_HOST ||= ENV['FLUXBB_HOST'] || "localhost" FLUXBB_DB ||= ENV['FLUXBB_DB'] || "fluxbb" BATCH_SIZE ||= 1000 - FLUXBB_PW ||= ENV['FLUXBB_PW'] || "" FLUXBB_USER ||= ENV['FLUXBB_USER'] || "root" + FLUXBB_PW ||= ENV['FLUXBB_PW'] || "" + FLUXBB_PREFIX ||= ENV['FLUXBB_PREFIX'] || "" def initialize super @client = Mysql2::Client.new( - host: "localhost", + host: FLUXBB_HOST, username: FLUXBB_USER, password: FLUXBB_PW, database: FLUXBB_DB @@ -43,7 +47,7 @@ class ImportScripts::FluxBB < ImportScripts::Base results = mysql_query( "SELECT g_id id, g_title name, g_user_title title - FROM groups") + FROM #{FLUXBB_PREFIX}groups") create_groups(results) do |group| { id: group['id'], @@ -62,7 +66,7 @@ class ImportScripts::FluxBB < ImportScripts::Base "SELECT id, username, realname name, url website, email email, registered created_at, registration_ip registration_ip_address, last_visit last_visit_time, last_email_sent last_emailed_at, location, group_id - FROM users + FROM #{FLUXBB_PREFIX}users LIMIT #{BATCH_SIZE} OFFSET #{offset};") @@ -103,7 +107,7 @@ class ImportScripts::FluxBB < ImportScripts::Base categories = mysql_query(" SELECT id, cat_name name, disp_position position - FROM categories + FROM #{FLUXBB_PREFIX}categories ORDER BY id ASC ").to_a @@ -118,7 +122,7 @@ class ImportScripts::FluxBB < ImportScripts::Base children_categories = mysql_query(" SELECT id, forum_name name, forum_desc description, disp_position position, cat_id parent_category_id - FROM forums + FROM #{FLUXBB_PREFIX}forums ORDER BY id ").to_a @@ -135,7 +139,7 @@ class ImportScripts::FluxBB < ImportScripts::Base def import_posts puts "", "creating topics and posts" - total_count = mysql_query("SELECT count(*) count from posts").first["count"] + total_count = mysql_query("SELECT count(*) count from #{FLUXBB_PREFIX}posts").first["count"] batches(BATCH_SIZE) do |offset| results = mysql_query(" @@ -147,8 +151,8 @@ class ImportScripts::FluxBB < ImportScripts::Base p.poster_id user_id, p.message raw, p.posted created_at - FROM posts p, - topics t + FROM #{FLUXBB_PREFIX}posts p, + #{FLUXBB_PREFIX}topics t WHERE p.topic_id = t.id ORDER BY p.posted LIMIT #{BATCH_SIZE} @@ -190,11 +194,11 @@ class ImportScripts::FluxBB < ImportScripts::Base banned = 0 failed = 0 - total = mysql_query("SELECT count(*) count FROM bans").first['count'] + total = mysql_query("SELECT count(*) count FROM #{FLUXBB_PREFIX}bans").first['count'] system_user = Discourse.system_user - mysql_query("SELECT username, email FROM bans").each do |b| + mysql_query("SELECT username, email FROM #{FLUXBB_PREFIX}bans").each do |b| user = User.find_by_email(b['email']) if user user.suspended_at = Time.now From 743ddf50a90da128360d4a1538cc0b9deaeea19d Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 7 Oct 2016 13:56:10 +0530 Subject: [PATCH 5/5] fluxbb.rb: skip admin & mod groups --- script/import_scripts/fluxbb.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/script/import_scripts/fluxbb.rb b/script/import_scripts/fluxbb.rb index 7238370eb6f..d754c5db0c7 100644 --- a/script/import_scripts/fluxbb.rb +++ b/script/import_scripts/fluxbb.rb @@ -49,7 +49,9 @@ class ImportScripts::FluxBB < ImportScripts::Base "SELECT g_id id, g_title name, g_user_title title FROM #{FLUXBB_PREFIX}groups") - create_groups(results) do |group| + customgroups = results.select { |group| group['id'] > 2 } + + create_groups(customgroups) do |group| { id: group['id'], name: group['name'], title: group['title'] } @@ -89,10 +91,12 @@ class ImportScripts::FluxBB < ImportScripts::Base admin: user['group_id'] == 1 } end - create_group_members(results) do |user| - if user.group_id - user_id = user_id_from_imported_user_id(user.id) - group_id = group_id_from_imported_group_id(user.group_id) + groupusers = results.select{ |user| user['group_id'] > 2 } + + groupusers.each do |user| + if user['group_id'] + user_id = user_id_from_imported_user_id(user['id']) + group_id = group_id_from_imported_group_id(user['group_id']) if user_id && group_id GroupUser.find_or_create_by(user_id: user_id, group_id: group_id)