diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 460b40a..cf557e0 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,6 +1,8 @@ require 'mysql2' require 'open-uri' require 'net/http' +require 'tempfile' +require 'sqlite3' require File.expand_path("../../../config/environment", __FILE__) require_relative 'base' @@ -8,13 +10,40 @@ require_relative 'base' class GossamerForumsImporter < ImportScripts::Base def initialize super - # Initialize MySQL client to connect to Gossamer Forums database - @mysql_client = Mysql2::Client.new( - host: "slowtwitch.northend.network", - username: "admin", - password: "yxnh93Ybbz2Nm8#mp28zCVv", - database: "slowtwitch" - ) + begin + # Initialize MySQL client to connect to Gossamer Forums database + @mysql_client = Mysql2::Client.new( + host: "slowtwitch.northend.network", + username: "admin", + password: "yxnh93Ybbz2Nm8#mp28zCVv", + database: "slowtwitch" + ) + rescue Mysql2::Error => e + puts "Error connecting to MySQL: #{e.message}" + exit 1 + end + +# # Create a mapping of old Gossamer user IDs to new Discourse user IDs +# @user_id_map = {} + initialize_sqlite_user_id_db + end + + def initialize_sqlite_user_id_db + @db = SQLite3::Database.new 'user_id_map.db' + @db.execute <<-SQL + CREATE TABLE IF NOT EXISTS user_id_map ( + old_user_id INTEGER PRIMARY KEY, + new_user_id INTEGER + ); + SQL + end + + def insert_user_id_mapping(old_user_id, new_user_id) + @db.execute "INSERT OR REPLACE INTO user_id_map (old_user_id, new_user_id) VALUES (?, ?)", old_user_id, new_user_id + end + + def fetch_user_id_mapping(old_user_id) + @db.get_first_value "SELECT new_user_id FROM user_id_map WHERE old_user_id = ?", old_user_id end # Execute an SQL query on the Gossamer Forums database @@ -122,10 +151,11 @@ class GossamerForumsImporter < ImportScripts::Base # Create or update users in Discourse create_users(users) do |user| +# insert_user_id_mapping(user[:id], user.id) user end - # Append user bio and import user files + # For each user, add user ID mapping to @user_id_map now that we know what the Discourse user ID is, ... and append user bio and import user files users.each do |user| discourse_username = sanitize_username(user[:username], user[:email], user[:name]) discourse_user = User.find_by(username: discourse_username) @@ -135,8 +165,15 @@ class GossamerForumsImporter < ImportScripts::Base next end - # Append bio if it exists, otherwise set it to empty string to avoid nil errors +# # Store the user ID mapping +# @user_id_map[user[:id]] = discourse_user.id + insert_user_id_mapping(user[:id], discourse_user.id) + + # Ensure user profile exists and bio_raw is a string + discourse_user.user_profile ||= UserProfile.new(user_id: discourse_user.id) discourse_user.user_profile.bio_raw ||= "" + + # Append bio if it exists, otherwise set it to empty string to avoid nil errors if discourse_user.user_profile.bio_raw.empty? discourse_user.user_profile.bio_raw = user[:bio_raw] else @@ -151,7 +188,7 @@ class GossamerForumsImporter < ImportScripts::Base discourse_user.user_profile.save! # Import user files - import_user_files(discourse_user) + # import_user_files(discourse_user) end end @@ -278,7 +315,12 @@ def import_topics_and_posts # Execute the query to get all posts ordered by post_id execute_query("SELECT * FROM gforum_Post ORDER BY post_id").each do |row| - puts "post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk/category_id #{row['forum_id_fk']}" + puts "post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk/category_id #{row['forum_id_fk']}" + discourse_user_id = fetch_user_id_mapping(row['user_id_fk']) +# discourse_user_id = @user_id_map[row['user_id_fk']] + + next unless discourse_user_id + if row['post_root_id'] == 0 # Ensure the title is valid title = ensure_valid_title(row['post_subject']) @@ -289,7 +331,7 @@ def import_topics_and_posts begin topic = Topic.create!( title: title, - user_id: row['user_id_fk'], + user_id: discourse_user_id, created_at: Time.at(row['post_time']), updated_at: Time.at(row['post_latest_reply']), category_id: row['forum_id_fk'] + 10 @@ -300,7 +342,7 @@ def import_topics_and_posts # Create the initial post in the topic post = Post.create!( topic_id: topic.id, - user_id: row['user_id_fk'], + user_id: discourse_user_id, raw: import_post_attachments(row['post_message'], row['post_id']), created_at: Time.at(row['post_time']), updated_at: Time.at(row['post_latest_reply']) @@ -327,7 +369,7 @@ def import_topics_and_posts begin post = Post.create!( topic_id: topic_id, - user_id: row['user_id_fk'], + user_id: discourse_user_id, raw: import_post_attachments(row['post_message'], row['post_id']), created_at: Time.at(row['post_time']), updated_at: Time.at(row['post_latest_reply']), @@ -377,9 +419,10 @@ def import_inbox_messages # Skip if the message already exists unless TopicCustomField.exists?(name: 'original_gossamer_msg_id', value: row['msg_id']) # Create a private message topic in Discourse + discourse_user_id = @user_id_map[row['from_user_id_fk']] topic = Topic.create!( title: row['msg_subject'], - user_id: row['from_user_id_fk'], + user_id: discourse_user_id, archetype: Archetype.private_message, created_at: Time.at(row['msg_time']), updated_at: Time.at(row['msg_time']) @@ -390,7 +433,7 @@ def import_inbox_messages # Create the message as a post in the private topic Post.create!( topic_id: topic.id, - user_id: row['from_user_id_fk'], + user_id: discourse_user_id, raw: row['msg_body'], created_at: Time.at(row['msg_time']), updated_at: Time.at(row['msg_time']) @@ -409,9 +452,10 @@ def import_sent_messages # Skip if the message already exists unless TopicCustomField.exists?(name: 'original_gossamer_sent_msg_id', value: row['msg_id']) # Create a private message topic in Discourse + discourse_user_id = @user_id_map[row['from_user_id_fk']] topic = Topic.create!( title: row['msg_subject'], - user_id: row['from_user_id_fk'], + user_id: discourse_user_id, archetype: Archetype.private_message, created_at: Time.at(row['msg_time']), updated_at: Time.at(row['msg_time']) @@ -422,7 +466,7 @@ def import_sent_messages # Create the message as a post in the private topic Post.create!( topic_id: topic.id, - user_id: row['from_user_id_fk'], + user_id: discourse_user_id, raw: row['msg_body'], created_at: Time.at(row['msg_time']), updated_at: Time.at(row['msg_time']) @@ -438,10 +482,10 @@ end def perform_import RateLimiter.disable puts "Starting Gossamer Forums import..." -# import_users + import_users import_categories - import_topics_and_posts - import_personal_messages + # import_topics_and_posts + # import_personal_messages puts "Gossamer Forums import complete!" end end