diff --git a/gossamer_forums.rb b/gossamer_forums.rb index cba9d16..04cdaff 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -18,6 +18,11 @@ class GossamerForumsImporter < ImportScripts::Base @mysql_client.query(query, as: :hash) end + # Sanitize usernames from non-alphanumeric chars + def sanitize_username(username) + username.gsub(/[^a-zA-Z0-9.\-]/, '_') + end + # Import users from gforum_User table def import_users puts "Importing users..." @@ -46,9 +51,27 @@ class GossamerForumsImporter < ImportScripts::Base # Update user passwords and import user files users.each do |user| - discourse_user = User.find_by(username: user[:username]) + # DSDS # Replace spaces with underscores in the username + # DSDS discourse_username = user[:username].gsub(' ', '_') + discourse_username = sanitize_username(user[:username]) + discourse_user = User.find_by(username: discourse_username) + + if discourse_user.nil? + # DSDS puts "User #{user[:username]} not found in Discourse. Skipping password update." + puts "User #{user[:username]} --> #{discourse_username} not found in Discourse. Skipping password update." + next + end + + if discourse_user.custom_fields.nil? + discourse_user.custom_fields = {} + end + + # Add the MD5 password as a custom field execute_query("SELECT * FROM gforum_User WHERE user_id = #{user[:id]}").each do |row| - discourse_user.update_columns(encrypted_password: row['user_password'], password_salt: nil) + # discourse_user.update_columns( + # discourse_user.update_columns(encrypted_password: row['user_password'], password_salt: nil) + discourse_user.custom_fields['md5_password'] = row['user_password'] + discourse_user.save_custom_fields end import_user_files(discourse_user) @@ -57,7 +80,8 @@ class GossamerForumsImporter < ImportScripts::Base # Import user files and append to user's bio def import_user_files(user) - puts "Importing files for user #{user.username}..." + # puts "Importing files for user #{user.username}..." + print "\rImporting files for user #{user.username}..." execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColName = 'user_id' AND ForeignColKey = #{user.id}").each do |file| # Construct file URL file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{file['File_Name']}" @@ -65,6 +89,7 @@ class GossamerForumsImporter < ImportScripts::Base user.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})" user.save! end + # print "\rImporting files for user #{user.username}... Done.\n" end # Import categories from gforum_Category table