2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
module ImportExport
|
|
|
|
class BaseExporter
|
2023-12-06 06:18:34 -06:00
|
|
|
attr_reader :export_data
|
2017-10-26 15:29:36 -05:00
|
|
|
|
|
|
|
CATEGORY_ATTRS = %i[
|
|
|
|
id
|
|
|
|
name
|
|
|
|
color
|
|
|
|
created_at
|
|
|
|
user_id
|
|
|
|
slug
|
|
|
|
description
|
|
|
|
text_color
|
2020-05-06 21:17:15 -05:00
|
|
|
auto_close_hours
|
|
|
|
position
|
|
|
|
parent_category_id
|
|
|
|
auto_close_based_on_last_post
|
2019-11-18 00:58:35 -06:00
|
|
|
topic_template
|
|
|
|
all_topics_wiki
|
|
|
|
permissions_params
|
|
|
|
]
|
2023-01-09 06:10:19 -06:00
|
|
|
|
2020-04-17 16:59:54 -05:00
|
|
|
GROUP_ATTRS = %i[
|
|
|
|
id
|
|
|
|
name
|
|
|
|
created_at
|
|
|
|
automatic_membership_email_domains
|
|
|
|
primary_group
|
|
|
|
title
|
|
|
|
grant_trust_level
|
|
|
|
incoming_email
|
|
|
|
bio_raw
|
|
|
|
allow_membership_requests
|
|
|
|
full_name
|
|
|
|
default_notification_level
|
|
|
|
visibility_level
|
|
|
|
public_exit
|
|
|
|
public_admission
|
|
|
|
membership_request_template
|
|
|
|
messageable_level
|
|
|
|
mentionable_level
|
|
|
|
members_visibility_level
|
|
|
|
publish_read_state
|
|
|
|
]
|
2023-01-09 06:10:19 -06:00
|
|
|
|
2021-10-22 12:02:56 -05:00
|
|
|
USER_ATTRS = %i[
|
|
|
|
id
|
|
|
|
email
|
|
|
|
username
|
|
|
|
name
|
|
|
|
created_at
|
|
|
|
trust_level
|
|
|
|
active
|
|
|
|
last_emailed_at
|
|
|
|
custom_fields
|
|
|
|
]
|
2023-01-09 06:10:19 -06:00
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
TOPIC_ATTRS = %i[id title created_at views category_id closed archived archetype]
|
2023-01-09 06:10:19 -06:00
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
POST_ATTRS = %i[
|
|
|
|
id
|
|
|
|
user_id
|
|
|
|
post_number
|
|
|
|
raw
|
|
|
|
created_at
|
|
|
|
reply_to_post_number
|
|
|
|
hidden
|
|
|
|
hidden_reason_id
|
|
|
|
wiki
|
|
|
|
]
|
|
|
|
|
|
|
|
def categories
|
|
|
|
@categories ||= Category.all.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_categories
|
|
|
|
data = []
|
|
|
|
|
|
|
|
categories.each do |cat|
|
2019-05-06 20:27:05 -05:00
|
|
|
data << CATEGORY_ATTRS.inject({}) do |h, a|
|
|
|
|
h[a] = cat.public_send(a)
|
|
|
|
h
|
2023-01-09 06:10:19 -06:00
|
|
|
end
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_categories!
|
|
|
|
@export_data[:categories] = export_categories
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2020-04-17 16:59:54 -05:00
|
|
|
def export_groups(group_names)
|
|
|
|
data = []
|
|
|
|
groups = Group.all
|
|
|
|
groups = groups.where(name: group_names) if group_names.present?
|
|
|
|
|
|
|
|
groups.find_each do |group|
|
|
|
|
attrs =
|
|
|
|
GROUP_ATTRS.inject({}) do |h, a|
|
|
|
|
h[a] = group.public_send(a)
|
|
|
|
h
|
2023-01-09 06:10:19 -06:00
|
|
|
end
|
2020-04-17 16:59:54 -05:00
|
|
|
attrs[:user_ids] = group.users.pluck(:id)
|
|
|
|
data << attrs
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_groups!
|
|
|
|
@export_data[:groups] = export_groups([])
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
def export_category_groups
|
|
|
|
groups = []
|
|
|
|
group_names = []
|
|
|
|
auto_group_names = Group::AUTO_GROUPS.keys.map(&:to_s)
|
|
|
|
|
|
|
|
@export_data[:categories].each do |c|
|
|
|
|
c[:permissions_params].each do |group_name, _|
|
2024-05-27 05:27:13 -05:00
|
|
|
group_names << group_name if auto_group_names.exclude?(group_name.to_s)
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
group_names.uniq!
|
|
|
|
return [] if group_names.empty?
|
|
|
|
|
2020-04-17 16:59:54 -05:00
|
|
|
export_groups(group_names)
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def export_category_groups!
|
|
|
|
@export_data[:groups] = export_category_groups
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_group_users
|
|
|
|
user_ids = []
|
|
|
|
|
|
|
|
@export_data[:groups].each { |g| user_ids += g[:user_ids] }
|
|
|
|
|
|
|
|
user_ids.uniq!
|
2018-01-16 11:51:37 -06:00
|
|
|
return User.none if user_ids.empty?
|
2017-10-26 15:29:36 -05:00
|
|
|
|
|
|
|
users = User.where(id: user_ids)
|
2018-01-16 11:51:37 -06:00
|
|
|
export_users(users)
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def export_group_users!
|
|
|
|
@export_data[:users] = export_group_users
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_topics
|
|
|
|
data = []
|
|
|
|
|
|
|
|
@topics.each do |topic|
|
|
|
|
puts topic.title
|
|
|
|
|
2019-05-06 20:27:05 -05:00
|
|
|
topic_data =
|
|
|
|
TOPIC_ATTRS.inject({}) do |h, a|
|
|
|
|
h[a] = topic.public_send(a)
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
topic_data[:posts] = []
|
|
|
|
|
|
|
|
topic.ordered_posts.find_each do |post|
|
2019-05-06 20:27:05 -05:00
|
|
|
attributes =
|
|
|
|
POST_ATTRS.inject({}) do |h, a|
|
|
|
|
h[a] = post.public_send(a)
|
|
|
|
h
|
|
|
|
end
|
2018-09-03 21:16:21 -05:00
|
|
|
|
|
|
|
attributes[:raw] = attributes[:raw].gsub(
|
|
|
|
'src="/uploads',
|
|
|
|
"src=\"#{Discourse.base_url_no_prefix}/uploads",
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_data[:posts] << attributes
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
data << topic_data
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_topics!
|
|
|
|
@export_data[:topics] = export_topics
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_topic_users
|
|
|
|
return if @export_data[:topics].blank?
|
|
|
|
topic_ids = @export_data[:topics].pluck(:id)
|
|
|
|
|
2018-01-16 11:51:37 -06:00
|
|
|
users = User.joins(:posts).where("posts.topic_id IN (?)", topic_ids).distinct
|
2017-10-26 15:29:36 -05:00
|
|
|
|
2018-01-16 11:51:37 -06:00
|
|
|
export_users(users)
|
2017-10-26 15:29:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def export_topic_users!
|
|
|
|
@export_data[:users] = export_topic_users
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_users(users)
|
|
|
|
data = []
|
|
|
|
|
2018-01-16 11:51:37 -06:00
|
|
|
users.find_each do |u|
|
|
|
|
next if u.id == Discourse::SYSTEM_USER_ID
|
2019-05-06 20:27:05 -05:00
|
|
|
|
|
|
|
x =
|
|
|
|
USER_ATTRS.inject({}) do |h, a|
|
|
|
|
h[a] = u.public_send(a)
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
x.merge(
|
|
|
|
bio_raw: u.user_profile.bio_raw,
|
|
|
|
website: u.user_profile.website,
|
|
|
|
location: u.user_profile.location,
|
|
|
|
)
|
|
|
|
data << x
|
|
|
|
end
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2022-10-05 14:22:16 -05:00
|
|
|
def export_translation_overrides
|
|
|
|
@export_data[:translation_overrides] = TranslationOverride.all.select(
|
|
|
|
:locale,
|
|
|
|
:translation_key,
|
|
|
|
:value,
|
|
|
|
)
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-10-26 15:29:36 -05:00
|
|
|
def default_filename_prefix
|
|
|
|
raise "Overwrite me!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_to_file(filename = nil)
|
|
|
|
output_basename =
|
|
|
|
filename ||
|
|
|
|
File.join("#{default_filename_prefix}-#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.json")
|
|
|
|
File.open(output_basename, "w:UTF-8") { |f| f.write(@export_data.to_json) }
|
|
|
|
puts "Export saved to #{output_basename}"
|
|
|
|
output_basename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|