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
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