Improve the base importer

- FEATURE: Print the time it took for the import to finish
- FEATURE: Allow importing of unactivated users
- FIX: Remove unused variables
- FIX: Accessing an undefined variable is not a good idea
This commit is contained in:
Gerhard Schlager 2015-01-23 22:44:00 +01:00
parent 256519dddf
commit 72755970c6
1 changed files with 12 additions and 12 deletions

View File

@ -22,7 +22,7 @@ class ImportScripts::Base
include ActionView::Helpers::NumberHelper
def initialize
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
require_relative '../../config/environment'
preload_i18n
@bbcode_to_md = true if ARGV.include?('bbcode-to-md')
@ -34,6 +34,7 @@ class ImportScripts::Base
@existing_posts = {}
@topic_lookup = {}
@old_site_settings = {}
@start_time = Time.now
puts "loading existing groups..."
GroupCustomField.where(name: 'import_id').pluck(:group_id, :value).each do |group_id, import_id|
@ -85,7 +86,8 @@ class ImportScripts::Base
update_topic_count_replies
reset_topic_counters
puts "", "Done"
elapsed = Time.now - @start_time
puts '', "Done (#{elapsed.to_s} seconds)"
ensure
reset_site_settings
@ -226,10 +228,8 @@ class ImportScripts::Base
# user in the original datasource. The given id will not be used to
# create the Discourse user record.
def create_users(results, opts={})
num_users_before = User.count
users_created = 0
users_skipped = 0
progress = 0
total = opts[:total] || results.size
results.each do |result|
@ -280,16 +280,16 @@ class ImportScripts::Base
opts[:name] = User.suggest_name(opts[:email]) unless opts[:name]
if opts[:username].blank? ||
opts[:username].length < User.username_length.begin ||
opts[:username].length > User.username_length.end ||
opts[:username] =~ /[^A-Za-z0-9_]/ ||
opts[:username][0] =~ /[^A-Za-z0-9]/ ||
!User.username_available?(opts[:username])
opts[:username].length < User.username_length.begin ||
opts[:username].length > User.username_length.end ||
opts[:username] =~ /[^A-Za-z0-9_]/ ||
opts[:username][0] =~ /[^A-Za-z0-9]/ ||
!User.username_available?(opts[:username])
opts[:username] = UserNameSuggester.suggest(opts[:username] || opts[:name] || opts[:email])
end
opts[:email] = opts[:email].downcase
opts[:trust_level] = TrustLevel[1] unless opts[:trust_level]
opts[:active] = true
opts[:active] = opts.fetch(:active, true)
opts[:import_mode] = true
u = User.new(opts)
@ -470,8 +470,8 @@ class ImportScripts::Base
end
def close_inactive_topics(opts={})
puts "", "Closing topics that have been inactive for more than #{num_days} days."
num_days = opts[:days] || 30
puts '', "Closing topics that have been inactive for more than #{num_days} days."
query = Topic.where('last_posted_at < ?', num_days.days.ago).where(closed: false)
total_count = query.count
@ -523,7 +523,7 @@ class ImportScripts::Base
end
def reset_topic_counters
puts "", "reseting topic counters"
puts "", "resetting topic counters"
total_count = Topic.count
progress_count = 0