From 53591ade001f5108d5eb32da6429d388eddcb273 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 13 Mar 2015 21:24:11 +0100 Subject: [PATCH] Adds a few enhancements to the base importer - Allows importing globally pinned topics - Doesn't restore the original value of a SiteSetting anymore if an importer changed its value - Allows all file extensions during the import - Adds the ability to import bookmarks --- lib/post_creator.rb | 1 + lib/topic_creator.rb | 1 + script/import_scripts/base.rb | 47 ++++++++++++++++++++++++++++++++--- script/import_scripts/smf2.rb | 4 --- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 3c1d60279f6..4bc5e7d8f0e 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -43,6 +43,7 @@ class PostCreator # meta_data - Topic meta data hash # created_at - Topic creation time (optional) # pinned_at - Topic pinned time (optional) + # pinned_globally - Is the topic pinned globally (optional) # def initialize(user, opts) # TODO: we should reload user in case it is tainted, should take in a user_id as opposed to user diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 179e8d23332..40b6aee9dc9 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -87,6 +87,7 @@ class TopicCreator topic_params[:created_at] = Time.zone.parse(@opts[:created_at].to_s) if @opts[:created_at].present? topic_params[:pinned_at] = Time.zone.parse(@opts[:pinned_at].to_s) if @opts[:pinned_at].present? + topic_params[:pinned_globally] = @opts[:pinned_globally] if @opts[:pinned_globally].present? topic_params end diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index 190e0309d93..a3120edd24e 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -32,6 +32,7 @@ class ImportScripts::Base @categories_lookup = {} @existing_posts = {} @topic_lookup = {} + @site_settings_during_import @old_site_settings = {} @start_time = Time.now @@ -94,17 +95,18 @@ class ImportScripts::Base end def change_site_settings - new_settings = { + @site_settings_during_import = { email_domains_blacklist: '', min_topic_title_length: 1, min_post_length: 1, min_private_message_post_length: 1, min_private_message_title_length: 1, allow_duplicate_topic_titles: true, - disable_emails: true + disable_emails: true, + authorized_extensions: '*' } - new_settings.each do |key, value| + @site_settings_during_import.each do |key, value| @old_site_settings[key] = SiteSetting.send(key) SiteSetting.set(key, value) end @@ -114,7 +116,8 @@ class ImportScripts::Base def reset_site_settings @old_site_settings.each do |key, value| - SiteSetting.set(key, value) + current_value = SiteSetting.send(key) + SiteSetting.set(key, value) unless current_value != @site_settings_during_import[key] end RateLimiter.enable @@ -469,6 +472,42 @@ class ImportScripts::Base tmp.unlink rescue nil end + # Iterate through a list of bookmark records to be imported. + # Takes a collection, and yields to the block for each element. + # Block should return a hash with the attributes for the bookmark. + # Required fields are :user_id and :post_id, where both ids are + # the values in the original datasource. + def create_bookmarks(results, opts={}) + bookmarks_created = 0 + bookmarks_skipped = 0 + total = opts[:total] || results.size + + user = User.new + post = Post.new + + results.each do |result| + params = yield(result) + + # only the IDs are needed, so this should be enough + user.id = user_id_from_imported_user_id(params[:user_id]) + post.id = post_id_from_imported_post_id(params[:post_id]) + + if user.id.nil? || post.id.nil? + bookmarks_skipped += 1 + puts "Skipping bookmark for user id #{params[:user_id]} and post id #{params[:post_id]}" + else + begin + PostAction.act(user, post, PostActionType.types[:bookmark]) + bookmarks_created += 1 + rescue PostAction::AlreadyActed + bookmarks_skipped += 1 + end + + print_status bookmarks_created + bookmarks_skipped + (opts[:offset] || 0), total + end + end + end + def close_inactive_topics(opts={}) num_days = opts[:days] || 30 puts '', "Closing topics that have been inactive for more than #{num_days} days." diff --git a/script/import_scripts/smf2.rb b/script/import_scripts/smf2.rb index 3b03914f86d..5daa29a4cd1 100644 --- a/script/import_scripts/smf2.rb +++ b/script/import_scripts/smf2.rb @@ -61,15 +61,11 @@ class ImportScripts::Smf2 < ImportScripts::Base end def execute - authorized_extensions = SiteSetting.authorized_extensions - SiteSetting.authorized_extensions = "*" import_groups import_users import_categories import_posts postprocess_posts - ensure - SiteSetting.authorized_extensions = authorized_extensions end def import_groups