discourse-import_scripts/gossamer_forums.rb

296 lines
10 KiB
Ruby
Raw Normal View History

2024-06-17 09:32:40 +00:00
require 'mysql2'
require File.expand_path("../../../config/environment", __FILE__)
require_relative 'base'
class GossamerForumsImporter < ImportScripts::Base
def initialize
super
2024-06-17 09:33:54 +00:00
# Initialize MySQL client with connection details
2024-06-17 09:32:40 +00:00
@mysql_client = Mysql2::Client.new(
host: "slowtwitch.northend.network",
username: "admin",
password: "yxnh93Ybbz2Nm8#mp28zCVv",
database: "slowtwitch"
)
end
# Execute a query on the MySQL database
def execute_query(query)
@mysql_client.query(query, as: :hash)
end
2024-06-17 09:33:54 +00:00
# Sanitize username to comply with Discourse's rules
def sanitize_username(username, email, name)
original_username = username
# Replace unacceptable characters with underscores
sanitized = username.gsub(/[^a-zA-Z0-9._-]/, '_')
# Ensure the username is at least 3 characters long
sanitized = "#{sanitized}." if sanitized.length < 2
# Ensure the username is no more than 20 characters long
sanitized = sanitized[0, 20] if sanitized.length > 20
original_sanitized = sanitized
# Check for existing user with the same username
existing_user = User.find_by(username: sanitized)
if existing_user
2024-06-17 09:34:34 +00:00
# If email and name match, do not modify the username
# puts "existing_user.email: '#{existing_user.email}' vs. email: '#{email}'"
# puts "existing_user.name: '#{existing_user.name}' vs. email: '#{name}'"
if existing_user.email.downcase == email.downcase && existing_user.name == name
2024-06-17 09:33:54 +00:00
return sanitized
else
# Ensure the username is unique
counter = 1
while User.exists?(username: sanitized)
sanitized = "#{original_sanitized}_#{counter}"
sanitized = sanitized[0, 20] if sanitized.length > 20
counter += 1
end
end
end
# Print the original and sanitized usernames if they differ
if original_username != sanitized
puts "Sanitized username: '#{original_username}' --> '#{sanitized}'"
2024-06-17 09:34:34 +00:00
# else
# puts "UNsanitized username: '#{original_username}' --> '#{sanitized}'"
end
2024-06-17 09:33:54 +00:00
sanitized
end
# Sanitize email to replace restricted domains
def sanitize_email(email)
restricted_domains = ['mailinator.com', 'example.com'] # Add more restricted domains as needed
domain = email.split('@').last
if restricted_domains.include?(domain)
2024-06-17 09:34:34 +00:00
sanitized_email = email.gsub(domain, 'example.org') # Change to a permissible domain
2024-06-17 09:33:54 +00:00
puts "Sanitized email: '#{email}' --> '#{sanitized_email}'"
return sanitized_email
end
email
2024-06-17 09:33:28 +00:00
end
2024-06-17 09:32:40 +00:00
# Import users from gforum_User table
def import_users
puts "Importing users..."
users = []
execute_query("SELECT * FROM gforum_User").each do |row|
2024-06-17 09:34:34 +00:00
sanitized_username = sanitize_username(row['user_username'], row['user_email'], row['user_real_name'])
# sanitized_email = sanitize_email(row['user_email'])
2024-06-17 09:32:40 +00:00
users << {
id: row['user_id'],
2024-06-17 09:34:34 +00:00
username: sanitized_username,
2024-06-17 09:32:40 +00:00
email: row['user_email'],
created_at: Time.at(row['user_registered']),
updated_at: Time.at(row['user_last_seen']),
name: row['user_real_name'],
title: row['user_title'],
2024-06-17 09:34:34 +00:00
bio_raw: row['user_about'] || "",
2024-06-17 09:32:40 +00:00
website: row['user_homepage'],
2024-06-17 09:34:34 +00:00
location: row['user_location'],
custom_fields: {
'md5_password' => row['user_password'],
'original_gossamer_username' => row['user_username'],
'original_gossamer_id' => row['user_id']
}
2024-06-17 09:32:40 +00:00
}
end
# Create users in Discourse with the required block
create_users(users) do |user|
user
end
2024-06-17 09:34:34 +00:00
# Import user files after creating users
2024-06-17 09:32:40 +00:00
users.each do |user|
2024-06-17 09:34:34 +00:00
discourse_user = User.find_by(username: user[:username])
2024-06-17 09:33:28 +00:00
if discourse_user.nil?
2024-06-17 09:34:34 +00:00
puts "User #{user[:username]} not found in Discourse. Skipping file import."
2024-06-17 09:33:28 +00:00
next
end
2024-06-17 09:33:54 +00:00
2024-06-17 09:34:34 +00:00
# Update user profile bio_raw with user_about
discourse_user.user_profile.bio_raw = (discourse_user.user_profile.bio_raw || "") + "\n\n" + (user[:bio_raw] || "")
discourse_user.user_profile.save!
2024-06-17 09:32:40 +00:00
import_user_files(discourse_user)
end
end
# Import user files and append to user's bio
def import_user_files(user)
2024-06-17 09:33:28 +00:00
print "\rImporting files for user #{user.username}..."
2024-06-17 09:34:34 +00:00
original_gossamer_id = user.custom_fields['original_gossamer_id']
if original_gossamer_id.nil? || original_gossamer_id.empty?
puts "User #{user.username} does not have a valid original_gossamer_id. Skipping file import."
return
end
# puts "Original Gossamer ID for user #{user.username}: #{original_gossamer_id}"
execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{original_gossamer_id}").each do |file|
# execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColName = 'user_id' AND ForeignColKey = #{user.id}").each do |file|
# execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{user.custom_fields['original_gossamer_id']}").each do |file|
2024-06-17 09:32:40 +00:00
# Construct file URL
file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{file['File_Name']}"
2024-06-17 09:34:34 +00:00
puts "User #{user.username} User ID: #{user.id} original_gossamer_id: #{user.custom_fields['original_gossamer_id']} file_url: #{file_url}"
2024-06-17 09:32:40 +00:00
# Append image link to user's bio
2024-06-17 09:34:34 +00:00
user.user_profile.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
user.user_profile.save!
2024-06-17 09:32:40 +00:00
end
2024-06-17 09:34:34 +00:00
print "Importing files for user #{user.username}... Done.\n"
2024-06-17 09:32:40 +00:00
end
# Import categories from gforum_Category table
def import_categories
puts "Importing categories..."
execute_query("SELECT * FROM gforum_Category").each do |row|
2024-06-17 09:33:54 +00:00
# Use current time if created_at or updated_at is null
created_at = row['created_at'] ? Time.at(row['created_at']) : Time.now
updated_at = row['updated_at'] ? Time.at(row['updated_at']) : Time.now
2024-06-17 09:32:40 +00:00
create_category(
id: row['category_id'],
name: row['name'],
description: row['description'],
2024-06-17 09:33:54 +00:00
created_at: created_at,
updated_at: updated_at
2024-06-17 09:32:40 +00:00
)
end
end
# Import topics and posts from gforum_Post table
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']
# This is the root post, create a new topic
topic = create_topic(
id: row['post_id'],
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']
)
# Create the first post in the topic
create_post(
id: row['post_id'],
topic_id: row['post_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'])
)
else
# This is a reply post, add to the existing topic
create_post(
id: row['post_id'],
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']
)
end
end
end
# Import post attachments from gforum_PostAttachment table
def import_post_attachments(post_message, post_id)
# Query for attachments related to the post
attachments = execute_query("SELECT * FROM gforum_PostAttachment WHERE post_id_fk = #{post_id}")
attachments.each do |attachment|
# Append attachment link to the post message
post_message += "\n\n![#{attachment['postatt_filename']}](https://forum.slowtwitch.com/forum/?do=post_attachment;postatt_id=#{attachment['postatt_filename']})"
end
post_message
end
# Import personal messages (both inbox and sent messages)
def import_personal_messages
puts "Importing personal messages..."
import_inbox_messages
import_sent_messages
end
# Import inbox messages from gforum_Message table
def import_inbox_messages
puts "Importing inbox messages..."
execute_query("SELECT * FROM gforum_Message").each do |row|
# Create a private message topic in Discourse
topic = create_topic(
title: row['msg_subject'],
user_id: row['from_user_id_fk'],
archetype: Archetype.private_message,
created_at: Time.at(row['msg_time']),
updated_at: Time.at(row['msg_time'])
)
# Create the message as a post in the private topic
create_post(
topic_id: topic.id,
user_id: row['from_user_id_fk'],
raw: row['msg_body'],
created_at: Time.at(row['msg_time']),
updated_at: Time.at(row['msg_time'])
)
# Add recipient user to the private message topic
topic.add_user_by_id(row['to_user_id_fk'])
topic.save!
end
end
# Import sent messages from gforum_SentMessage table
def import_sent_messages
puts "Importing sent messages..."
execute_query("SELECT * FROM gforum_SentMessage").each do |row|
# Create a private message topic in Discourse
topic = create_topic(
title: row['msg_subject'],
user_id: row['from_user_id_fk'],
archetype: Archetype.private_message,
created_at: Time.at(row['msg_time']),
updated_at: Time.at(row['msg_time'])
)
# Create the message as a post in the private topic
create_post(
topic_id: topic.id,
user_id: row['from_user_id_fk'],
raw: row['msg_body'],
created_at: Time.at(row['msg_time']),
updated_at: Time.at(row['msg_time'])
)
# Add recipient user to the private message topic
topic.add_user_by_id(row['to_user_id_fk'])
topic.save!
end
end
# Perform the full import process
def perform_import
import_users
import_categories
import_topics_and_posts
import_personal_messages
end
end
# Create an instance of the importer and start the import process
importer = GossamerForumsImporter.new
importer.perform_import