This commit is contained in:
dsainty 2024-06-17 19:36:35 +10:00
parent 3bb197da4f
commit 286a7956ea

View File

@ -244,7 +244,7 @@ class GossamerForumsImporter < ImportScripts::Base
# Create category in Discourse # Create category in Discourse
category = create_category( category = create_category(
{ {
id: row['forum_id'], id: row['forum_id'] + 10,
name: category_name, name: category_name,
description: category_description, description: category_description,
created_at: row['forum_last'] ? Time.at(row['forum_last']) : Time.now, created_at: row['forum_last'] ? Time.at(row['forum_last']) : Time.now,
@ -264,20 +264,35 @@ class GossamerForumsImporter < ImportScripts::Base
puts "Importing categories... Done." puts "Importing categories... Done."
end 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 # Import topics and posts from Gossamer Forums to Discourse
def import_topics_and_posts def import_topics_and_posts
puts "Importing 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 # Skip if the topic already exists
unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id'])
# Create the topic # Create the topic
begin
topic = Topic.create!( topic = Topic.create!(
title: row['post_subject'], title: title,
user_id: row['user_id_fk'], user_id: row['user_id_fk'],
created_at: Time.at(row['post_time']), created_at: Time.at(row['post_time']),
updated_at: Time.at(row['post_latest_reply']), updated_at: Time.at(row['post_latest_reply']),
category_id: row['forum_id_fk'] category_id: row['forum_id_fk'] + 10
) )
topic.custom_fields['original_gossamer_id'] = row['post_id'] topic.custom_fields['original_gossamer_id'] = row['post_id']
topic.save! topic.save!
@ -292,26 +307,50 @@ def import_topics_and_posts
) )
post.custom_fields['original_gossamer_id'] = row['post_id'] post.custom_fields['original_gossamer_id'] = row['post_id']
post.save! post.save!
rescue ActiveRecord::RecordInvalid => e
puts "Error importing topic with post_id #{row['post_id']}: #{e.message}"
end end
end
else else
# Skip if the post already exists # Find the root topic for the post
unless PostCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) 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 # Create the post in the existing topic
begin
post = Post.create!( post = Post.create!(
topic_id: row['post_root_id'], topic_id: topic_id,
user_id: row['user_id_fk'], user_id: row['user_id_fk'],
raw: import_post_attachments(row['post_message'], row['post_id']), raw: import_post_attachments(row['post_message'], row['post_id']),
created_at: Time.at(row['post_time']), created_at: Time.at(row['post_time']),
updated_at: Time.at(row['post_latest_reply']), updated_at: Time.at(row['post_latest_reply']),
reply_to_post_number: row['post_father_id'] reply_to_post_number: reply_to_post_number
) )
post.custom_fields['original_gossamer_id'] = row['post_id'] post.custom_fields['original_gossamer_id'] = row['post_id']
post.save! 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
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 # Import attachments for a post
def import_post_attachments(post_message, post_id) def import_post_attachments(post_message, post_id)
# Fetch attachments related to the post # Fetch attachments related to the post
@ -397,6 +436,7 @@ end
# Main method to perform the import # Main method to perform the import
def perform_import def perform_import
RateLimiter.disable
puts "Starting Gossamer Forums import..." puts "Starting Gossamer Forums import..."
# import_users # import_users
import_categories import_categories