# frozen_string_literal: true # getsatisfaction importer # # pre-req: You will either get an Excel or a bunch of CSV files. Be sure to rename them all so that # # - users.csv is the users table export # - replies.csv is the reply table export # - topics.csv is the topics table export # - categories.csv is the categories table export # - topics_categories.csv is the mapping between the topics and categories table # # Make sure that the CSV files use UTF-8 encoding, have consistent line endings and use comma as column separator. # That's usually the case when you export Excel sheets as CSV. # When you get MalformedCSVError during the import, try converting the line endings of the CSV into the Unix format. # Mixed line endings in CSV files can create weird errors! # # You need to call fix_quotes_in_csv() for CSV files that use \" to escape quotes within quoted fields. # The import script expects quotes to be escaped with "". # # It's likely that some posts in replies.csv aren't in the correct order. Currently the import script doesn't handle # that correctly and will import the replies in the wrong order. # You should run `rake posts:reorder_posts` after the import. require 'csv' require 'set' require File.expand_path(File.dirname(__FILE__) + "/base.rb") require 'reverse_markdown' # gem 'reverse_markdown' # Call it like this: # RAILS_ENV=production bundle exec ruby script/import_scripts/getsatisfaction.rb DIRNAME class ImportScripts::GetSatisfaction < ImportScripts::Base IMPORT_ARCHIVED_TOPICS = false # The script classifies each topic as private when at least one associated category # in "topics_categories.csv" is unknown (not included i "categories.csv"). IMPORT_PRIVATE_TOPICS = false # Should the creation of permalinks be skipped? Make sure you configure OLD_DOMAIN if you CREATE_PERMALINKS = true # Replace "http://community.example.com/" with the URL of your community for permalinks OLD_DOMAIN = "http://community.example.com/" BATCH_SIZE = 1000 def initialize(path) @path = path super() @bbcode_to_md = true @topic_slug = {} @topic_categories = {} @skipped_topics = Set.new end def execute # TODO Remove the call to fix_quotes_in_csv() if your replies.csv uses the double quotes (""). # That's usually the case when you exported the file from Excel. fix_quotes_in_csv("replies") import_users import_categories import_topics import_posts create_permalinks if CREATE_PERMALINKS end def csv_filename(table_name, use_fixed: true) if use_fixed filename = File.join(@path, "#{table_name}_fixed.csv") return filename if File.exist?(filename) end File.join(@path, "#{table_name}.csv") end def fix_quotes_in_csv(*table_names) puts "", "fixing CSV files" table_names.each do |table_name| source_filename = csv_filename(table_name, use_fixed: false) target_filename = csv_filename("#{table_name}_fixed", use_fixed: false) previous_line = nil File.open(target_filename, "w") do |file| File.open(source_filename).each_line do |line| line.gsub!(/(?" if raw.blank? raw = raw.dup # hoist code hoisted = {} raw.gsub!(/(
\s*)?(.*?)<\/code>(\s*<\/pre>)?/mi) do
      code = $2
      hoist = SecureRandom.hex
      # tidy code, wow, this is impressively crazy
      code.gsub!(/  (\s*)/, "\n\\1")
      code.gsub!(/^\s*\n$/, "\n")
      code.gsub!(/\n+/m, "\n")
      code.strip!
      hoisted[hoist] = code
      hoist
    end

    # impressive seems to be using tripple space as a 

unless hoisted # in this case double space works best ... so odd raw.gsub!(" ", "\n\n") hoisted.each do |hoist, code| raw.gsub!(hoist, "\n```\n#{code}\n```\n") end raw = CGI.unescapeHTML(raw) raw = ReverseMarkdown.convert(raw) raw end def create_permalinks puts '', 'Creating Permalinks...', '' Topic.listable_topics.find_each do |topic| tcf = topic.first_post.custom_fields if tcf && tcf["import_id"] if slug = @topic_slug[tcf["import_id"]] slug.gsub!(OLD_DOMAIN, "") Permalink.find_or_create_by(url: slug, topic_id: topic.id) if slug.present? end end end end end unless ARGV[0] && Dir.exist?(ARGV[0]) puts "", "Usage:", "", "bundle exec ruby script/import_scripts/getsatisfaction.rb DIRNAME", "" exit 1 end ImportScripts::GetSatisfaction.new(ARGV[0]).perform