2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
require "mysql2"
|
2015-03-18 15:30:42 -04:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
2014-05-30 15:09:58 -04:00
|
|
|
|
|
|
|
class ImportScripts::Kunena < ImportScripts::Base
|
|
|
|
KUNENA_DB = "kunena"
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
|
|
|
|
@users = {}
|
|
|
|
|
|
|
|
@client =
|
|
|
|
Mysql2::Client.new(
|
|
|
|
host: "localhost",
|
|
|
|
username: "root",
|
|
|
|
#password: "password",
|
|
|
|
database: KUNENA_DB,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
parse_users
|
|
|
|
|
2014-07-04 16:05:15 -04:00
|
|
|
puts "creating users"
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
create_users(@users) do |id, user|
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
email: user[:email],
|
|
|
|
username: user[:username],
|
|
|
|
created_at: user[:created_at],
|
|
|
|
bio_raw: user[:bio],
|
|
|
|
moderator: user[:moderator] ? true : false,
|
2014-07-04 16:05:15 -04:00
|
|
|
admin: user[:admin] ? true : false,
|
2014-05-30 15:09:58 -04:00
|
|
|
suspended_at: user[:suspended] ? Time.zone.now : nil,
|
|
|
|
suspended_till: user[:suspended] ? 100.years.from_now : nil,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-08-29 14:55:04 -04:00
|
|
|
@users = nil
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
create_categories(
|
|
|
|
@client.query(
|
|
|
|
"SELECT id, parent, name, description, ordering FROM jos_kunena_categories ORDER BY parent, id;",
|
2023-01-07 06:53:14 -05:00
|
|
|
),
|
2014-05-30 15:09:58 -04:00
|
|
|
) do |c|
|
|
|
|
h = {
|
|
|
|
id: c["id"],
|
|
|
|
name: c["name"],
|
|
|
|
description: c["description"],
|
|
|
|
position: c["ordering"].to_i,
|
|
|
|
}
|
|
|
|
if c["parent"].to_i > 0
|
2015-03-12 16:15:02 -04:00
|
|
|
h[:parent_category_id] = category_id_from_imported_category_id(c["parent"])
|
2014-05-30 15:09:58 -04:00
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
import_posts
|
|
|
|
|
|
|
|
begin
|
|
|
|
create_admin(email: "neil.lalonde@discourse.org", username: UserNameSuggester.suggest("neil"))
|
|
|
|
rescue => e
|
|
|
|
puts "", "Failed to create admin user"
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_users
|
|
|
|
# Need to merge data from joomla with kunena
|
|
|
|
|
2014-08-29 14:55:04 -04:00
|
|
|
puts "fetching Joomla users data from mysql"
|
|
|
|
results =
|
|
|
|
@client.query("SELECT id, username, email, registerDate FROM jos_users;", cache_rows: false)
|
|
|
|
results.each do |u|
|
2024-05-27 06:27:13 -04:00
|
|
|
next if u["id"].to_i <= (0) || u["username"].blank? || u["email"].blank?
|
2014-08-29 14:55:04 -04:00
|
|
|
username = u["username"].gsub(" ", "_").gsub(/[^A-Za-z0-9_]/, "")[0, User.username_length.end]
|
2014-05-30 15:09:58 -04:00
|
|
|
if username.length < User.username_length.first
|
|
|
|
username = username * User.username_length.first
|
|
|
|
end
|
2014-08-29 14:55:04 -04:00
|
|
|
@users[u["id"].to_i] = {
|
|
|
|
id: u["id"].to_i,
|
|
|
|
username: username,
|
|
|
|
email: u["email"],
|
|
|
|
created_at: u["registerDate"],
|
|
|
|
}
|
2014-05-30 15:09:58 -04:00
|
|
|
end
|
|
|
|
|
2014-08-29 14:55:04 -04:00
|
|
|
puts "fetching Kunena user data from mysql"
|
2014-05-30 15:09:58 -04:00
|
|
|
results =
|
|
|
|
@client.query(
|
|
|
|
"SELECT userid, signature, moderator, banned FROM jos_kunena_users;",
|
|
|
|
cache_rows: false,
|
|
|
|
)
|
|
|
|
results.each do |u|
|
2023-02-16 04:40:11 -05:00
|
|
|
next if u["userid"].to_i <= 0
|
2014-05-30 15:09:58 -04:00
|
|
|
user = @users[u["userid"].to_i]
|
|
|
|
if user
|
|
|
|
user[:bio] = u["signature"]
|
|
|
|
user[:moderator] = (u["moderator"].to_i == 1)
|
|
|
|
user[:suspended] = u["banned"].present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_posts
|
|
|
|
puts "", "creating topics and posts"
|
|
|
|
|
|
|
|
total_count = @client.query("SELECT COUNT(*) count FROM jos_kunena_messages m;").first["count"]
|
|
|
|
|
|
|
|
batch_size = 1000
|
|
|
|
|
|
|
|
batches(batch_size) do |offset|
|
|
|
|
results =
|
|
|
|
@client.query(
|
|
|
|
"
|
|
|
|
SELECT m.id id,
|
|
|
|
m.thread thread,
|
|
|
|
m.parent parent,
|
|
|
|
m.catid catid,
|
|
|
|
m.userid userid,
|
|
|
|
m.subject subject,
|
|
|
|
m.time time,
|
|
|
|
t.message message
|
|
|
|
FROM jos_kunena_messages m,
|
|
|
|
jos_kunena_messages_text t
|
|
|
|
WHERE m.id = t.mesid
|
|
|
|
ORDER BY m.id
|
|
|
|
LIMIT #{batch_size}
|
|
|
|
OFFSET #{offset};
|
|
|
|
",
|
|
|
|
cache_rows: false,
|
|
|
|
)
|
|
|
|
|
|
|
|
break if results.size < 1
|
|
|
|
|
2015-09-21 19:48:42 -04:00
|
|
|
next if all_records_exist? :posts, results.map { |p| p["id"].to_i }
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
create_posts(results, total: total_count, offset: offset) do |m|
|
|
|
|
skip = false
|
|
|
|
mapped = {}
|
|
|
|
|
|
|
|
mapped[:id] = m["id"]
|
2014-07-04 16:05:15 -04:00
|
|
|
mapped[:user_id] = user_id_from_imported_user_id(m["userid"]) || -1
|
2014-05-30 15:09:58 -04:00
|
|
|
mapped[:raw] = m["message"]
|
|
|
|
mapped[:created_at] = Time.zone.at(m["time"])
|
|
|
|
|
|
|
|
if m["id"] == m["thread"]
|
2015-03-12 16:15:02 -04:00
|
|
|
mapped[:category] = category_id_from_imported_category_id(m["catid"])
|
2014-05-30 15:09:58 -04:00
|
|
|
mapped[:title] = m["subject"]
|
|
|
|
else
|
|
|
|
parent = topic_lookup_from_imported_post_id(m["parent"])
|
|
|
|
if parent
|
|
|
|
mapped[:topic_id] = parent[:topic_id]
|
|
|
|
mapped[:reply_to_post_number] = parent[:post_number] if parent[:post_number] > 1
|
|
|
|
else
|
|
|
|
puts "Parent post #{m["parent"]} doesn't exist. Skipping #{m["id"]}: #{m["subject"][0..40]}"
|
|
|
|
skip = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
skip ? nil : mapped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ImportScripts::Kunena.new.perform
|