mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
DEV: Apply syntax_tree formatting to script/*
This commit is contained in:
@ -1,22 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'mysql2'
|
||||
require "mysql2"
|
||||
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
||||
|
||||
class ImportScripts::Elgg < ImportScripts::Base
|
||||
|
||||
BATCH_SIZE ||= 1000
|
||||
|
||||
def initialize
|
||||
super
|
||||
|
||||
@client = Mysql2::Client.new(
|
||||
host: "127.0.0.1",
|
||||
port: "3306",
|
||||
username: "",
|
||||
database: "",
|
||||
password: ""
|
||||
)
|
||||
@client =
|
||||
Mysql2::Client.new(host: "127.0.0.1", port: "3306", username: "", database: "", password: "")
|
||||
|
||||
SiteSetting.max_username_length = 50
|
||||
end
|
||||
@ -31,7 +25,7 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
def create_avatar(user, guid)
|
||||
puts "#{@path}"
|
||||
# Put your avatar at the root of discourse in this folder:
|
||||
path_prefix = 'import/data/www/'
|
||||
path_prefix = "import/data/www/"
|
||||
# https://github.com/Elgg/Elgg/blob/2fc9c1910a9169bbe4010026c61d8e41a5b56239/engine/classes/ElggDiskFilestore.php#L24
|
||||
# const BUCKET_SIZE = 5000;
|
||||
bucket_size = 5000
|
||||
@ -40,13 +34,11 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
bucket_id = [guid / bucket_size * bucket_size, 1].max
|
||||
|
||||
avatar_path = File.join(path_prefix, bucket_id.to_s, "/#{guid}/profile/#{guid}master.jpg")
|
||||
if File.exist?(avatar_path)
|
||||
@uploader.create_avatar(user, avatar_path)
|
||||
end
|
||||
@uploader.create_avatar(user, avatar_path) if File.exist?(avatar_path)
|
||||
end
|
||||
|
||||
def grant_admin(user, is_admin)
|
||||
if is_admin == 'yes'
|
||||
if is_admin == "yes"
|
||||
puts "", "#{user.username} is granted admin!"
|
||||
user.grant_admin!
|
||||
end
|
||||
@ -56,10 +48,11 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
puts "", "importing users..."
|
||||
|
||||
last_user_id = -1
|
||||
total_users = mysql_query("select count(*) from elgg_users_entity where banned='no'").first["count"]
|
||||
total_users =
|
||||
mysql_query("select count(*) from elgg_users_entity where banned='no'").first["count"]
|
||||
|
||||
batches(BATCH_SIZE) do |offset|
|
||||
users = mysql_query(<<-SQL
|
||||
users = mysql_query(<<-SQL).to_a
|
||||
select eue.guid, eue.username, eue.name, eue.email, eue.admin,
|
||||
max(case when ems1.string='cae_structure' then ems2.string end)cae_structure,
|
||||
max(case when ems1.string='location' then ems2.string end)location,
|
||||
@ -76,7 +69,6 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
group by eue.guid
|
||||
LIMIT #{BATCH_SIZE}
|
||||
SQL
|
||||
).to_a
|
||||
|
||||
break if users.empty?
|
||||
|
||||
@ -97,11 +89,12 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
name: u["name"],
|
||||
website: u["website"],
|
||||
bio_raw: u["briefdescription"].to_s + " " + u["cae_structure"].to_s,
|
||||
post_create_action: proc do |user|
|
||||
create_avatar(user, u["guid"])
|
||||
#add_user_to_group(user, u["cae_structure"])
|
||||
grant_admin(user, u["admin"])
|
||||
end
|
||||
post_create_action:
|
||||
proc do |user|
|
||||
create_avatar(user, u["guid"])
|
||||
#add_user_to_group(user, u["cae_structure"])
|
||||
grant_admin(user, u["admin"])
|
||||
end,
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -115,9 +108,9 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
|
||||
create_categories(categories) do |c|
|
||||
{
|
||||
id: c['guid'],
|
||||
name: CGI.unescapeHTML(c['name']),
|
||||
description: CGI.unescapeHTML(c['description'])
|
||||
id: c["guid"],
|
||||
name: CGI.unescapeHTML(c["name"]),
|
||||
description: CGI.unescapeHTML(c["description"]),
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -125,10 +118,13 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
def import_topics
|
||||
puts "", "creating topics"
|
||||
|
||||
total_count = mysql_query("select count(*) count from elgg_entities where subtype = 32;").first["count"]
|
||||
total_count =
|
||||
mysql_query("select count(*) count from elgg_entities where subtype = 32;").first["count"]
|
||||
|
||||
batches(BATCH_SIZE) do |offset|
|
||||
results = mysql_query("
|
||||
results =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT
|
||||
ee.guid id,
|
||||
owner_guid user_id,
|
||||
@ -143,30 +139,35 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
ORDER BY ee.guid
|
||||
LIMIT #{BATCH_SIZE}
|
||||
OFFSET #{offset};
|
||||
")
|
||||
",
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |m| m['id'].to_i }
|
||||
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |m|
|
||||
{
|
||||
id: m['id'],
|
||||
user_id: user_id_from_imported_user_id(m['user_id']) || -1,
|
||||
raw: CGI.unescapeHTML(m['raw']),
|
||||
created_at: Time.zone.at(m['created_at']),
|
||||
category: category_id_from_imported_category_id(m['category_id']),
|
||||
title: CGI.unescapeHTML(m['title']),
|
||||
post_create_action: proc do |post|
|
||||
tag_names = mysql_query("
|
||||
id: m["id"],
|
||||
user_id: user_id_from_imported_user_id(m["user_id"]) || -1,
|
||||
raw: CGI.unescapeHTML(m["raw"]),
|
||||
created_at: Time.zone.at(m["created_at"]),
|
||||
category: category_id_from_imported_category_id(m["category_id"]),
|
||||
title: CGI.unescapeHTML(m["title"]),
|
||||
post_create_action:
|
||||
proc do |post|
|
||||
tag_names =
|
||||
mysql_query(
|
||||
"
|
||||
select ms.string
|
||||
from elgg_metadata md
|
||||
join elgg_metastrings ms on md.value_id = ms.id
|
||||
where name_id = 43
|
||||
and entity_guid = #{m['id']};
|
||||
").map { |tag| tag['string'] }
|
||||
DiscourseTagging.tag_topic_by_names(post.topic, staff_guardian, tag_names)
|
||||
end
|
||||
and entity_guid = #{m["id"]};
|
||||
",
|
||||
).map { |tag| tag["string"] }
|
||||
DiscourseTagging.tag_topic_by_names(post.topic, staff_guardian, tag_names)
|
||||
end,
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -179,10 +180,13 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
def import_posts
|
||||
puts "", "creating posts"
|
||||
|
||||
total_count = mysql_query("SELECT count(*) count FROM elgg_entities WHERE subtype = 42").first["count"]
|
||||
total_count =
|
||||
mysql_query("SELECT count(*) count FROM elgg_entities WHERE subtype = 42").first["count"]
|
||||
|
||||
batches(BATCH_SIZE) do |offset|
|
||||
results = mysql_query("
|
||||
results =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT
|
||||
ee.guid id,
|
||||
container_guid topic_id,
|
||||
@ -195,19 +199,20 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
ORDER BY ee.guid
|
||||
LIMIT #{BATCH_SIZE}
|
||||
OFFSET #{offset};
|
||||
")
|
||||
",
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |m| m['id'].to_i }
|
||||
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |m|
|
||||
{
|
||||
id: m['id'],
|
||||
user_id: user_id_from_imported_user_id(m['user_id']) || -1,
|
||||
topic_id: topic_lookup_from_imported_post_id(m['topic_id'])[:topic_id],
|
||||
raw: CGI.unescapeHTML(m['raw']),
|
||||
created_at: Time.zone.at(m['created_at']),
|
||||
id: m["id"],
|
||||
user_id: user_id_from_imported_user_id(m["user_id"]) || -1,
|
||||
topic_id: topic_lookup_from_imported_post_id(m["topic_id"])[:topic_id],
|
||||
raw: CGI.unescapeHTML(m["raw"]),
|
||||
created_at: Time.zone.at(m["created_at"]),
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -216,7 +221,6 @@ class ImportScripts::Elgg < ImportScripts::Base
|
||||
def mysql_query(sql)
|
||||
@client.query(sql, cache_rows: false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
ImportScripts::Elgg.new.perform
|
||||
|
Reference in New Issue
Block a user