From 286a7956ea660aa8a33ccb653d2a5b1a39b0be52 Mon Sep 17 00:00:00 2001 From: dsainty Date: Mon, 17 Jun 2024 19:36:35 +1000 Subject: [PATCH] 20240604 --- gossamer_forums.rb | 108 +++++++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index c11339f..460b40a 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -244,7 +244,7 @@ class GossamerForumsImporter < ImportScripts::Base # Create category in Discourse category = create_category( { - id: row['forum_id'], + id: row['forum_id'] + 10, name: category_name, description: category_description, created_at: row['forum_last'] ? Time.at(row['forum_last']) : Time.now, @@ -264,54 +264,93 @@ class GossamerForumsImporter < ImportScripts::Base puts "Importing categories... Done." end +# Helper function to ensure title meets the minimum length requirement +def ensure_valid_title(title, min_length = 5) + if title.length < min_length + title += "." * (min_length - title.length) # Append dots to make it longer + end + title +end + # Import topics and posts from Gossamer Forums to Discourse def import_topics_and_posts puts "Importing topics and posts..." - execute_query("SELECT * FROM gforum_Post ORDER BY post_root_id, post_time").each do |row| - if row['post_id'] == row['post_root_id'] + + # 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']}" + if row['post_root_id'] == 0 + # Ensure the title is valid + title = ensure_valid_title(row['post_subject']) + # Skip if the topic already exists unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) # Create the topic - topic = Topic.create!( - title: row['post_subject'], - user_id: row['user_id_fk'], - created_at: Time.at(row['post_time']), - updated_at: Time.at(row['post_latest_reply']), - category_id: row['forum_id_fk'] - ) - topic.custom_fields['original_gossamer_id'] = row['post_id'] - topic.save! + begin + topic = Topic.create!( + title: title, + user_id: row['user_id_fk'], + created_at: Time.at(row['post_time']), + updated_at: Time.at(row['post_latest_reply']), + category_id: row['forum_id_fk'] + 10 + ) + topic.custom_fields['original_gossamer_id'] = row['post_id'] + topic.save! - # Create the initial post in the topic - post = Post.create!( - topic_id: topic.id, - user_id: row['user_id_fk'], - 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']) - ) - post.custom_fields['original_gossamer_id'] = row['post_id'] - post.save! + # Create the initial post in the topic + post = Post.create!( + topic_id: topic.id, + user_id: row['user_id_fk'], + 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']) + ) + post.custom_fields['original_gossamer_id'] = row['post_id'] + post.save! + rescue ActiveRecord::RecordInvalid => e + puts "Error importing topic with post_id #{row['post_id']}: #{e.message}" + end end + else - # Skip if the post already exists - unless PostCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) + # Find the root topic for the post + root_topic_field = TopicCustomField.find_by(name: 'original_gossamer_id', value: row['post_root_id']) + + if root_topic_field + topic_id = root_topic_field.topic_id + + # Find the parent post for the reply + parent_post_field = PostCustomField.find_by(name: 'original_gossamer_id', value: row['post_father_id']) + reply_to_post_number = parent_post_field ? Post.find(parent_post_field.post_id).post_number : nil + # Create the post in the existing topic - post = Post.create!( - topic_id: row['post_root_id'], - user_id: row['user_id_fk'], - 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']), - reply_to_post_number: row['post_father_id'] - ) - post.custom_fields['original_gossamer_id'] = row['post_id'] - post.save! + begin + post = Post.create!( + topic_id: topic_id, + user_id: row['user_id_fk'], + 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']), + reply_to_post_number: reply_to_post_number + ) + post.custom_fields['original_gossamer_id'] = row['post_id'] + post.save! + rescue ActiveRecord::RecordInvalid => e + puts "Error importing post with post_id #{row['post_id']}: #{e.message}" + end + else + puts "Warning: Root topic not found for post_id #{row['post_id']} with post_root_id #{row['post_root_id']}" end end end end +# Import topics and posts from Gossamer Forums to Discourse +# def import_topics_and_posts +# puts "Importing topics and posts..." +# execute_query("SELECT * FROM gforum_Post ORDER BY post_root_id, post_time").each do |row| +# puts "post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject #{row['post_subject']}" + # Import attachments for a post def import_post_attachments(post_message, post_id) # Fetch attachments related to the post @@ -397,6 +436,7 @@ end # Main method to perform the import def perform_import + RateLimiter.disable puts "Starting Gossamer Forums import..." # import_users import_categories