<commit_before>class StaticProgramExport

  EXPORT_PATH = Rails.root.join("tmp", "static_export").to_s

  # Export a static html version of the conference program.
  #
  # @param conference [Conference] conference to export
  # @param locale [String] export conference in supported locale
  # @param destination [String] export into this directory
  def initialize(conference, locale = 'en', destination = EXPORT_PATH)
    @conference = conference
    @locale = locale
    @destination = destination || EXPORT_PATH
  end

  # create a tarball from the conference export directory
  def create_tarball
    out_file = filename(@conference, @locale)
    if File.exist? out_file
      File.unlink out_file
    end
    system( 'tar', *['-cpz', '-f', out_file.to_s, '-C', @destination, @conference.acronym].flatten )
    out_file.to_s
  end

  # export the conference to disk
  #
  # only run by rake task, cannot run in the same thread as rails
  def run_export
    fail "No conference found!" if @conference.nil?

    @asset_paths = []
    @base_directory = File.join(@destination, @conference.acronym)
    @base_url = get_base_url
    @original_schedule_public = @conference.schedule_public

    @session = ActionDispatch::Integration::Session.new(Frab::Application)
    @session.host = Settings.host
    @session.https! if Settings['protocol'] == "https"
    ActiveRecord::Base.transaction do
      unlock_schedule unless @original_schedule_public

      setup_directories
      download_pages
      copy_stripped_assets
      create_index_page

      lock_schedule unless @original_schedule_public
    end
  end

  private

  def get_base_url
    if @conference.program_export_base_url.present?
      base_url = URI.parse(@conference.program_export_base_url).path
      base_url += '/' unless base_url.end_with?('/')
      base_url
    else 
      "/"
    end
  end

  def filename(conference, locale = 'en')
    File.join(@destination, "#{@conference.acronym}-#{@locale}.tar.gz")
  end

  def setup_directories
    FileUtils.rm_r(@base_directory, secure: true) if File.exist? @base_directory
    FileUtils.mkdir_p(@base_directory)
  end

  def download_pages
    paths = get_query_paths
    path_prefix = "/#{@conference.acronym}/public"
    unless @locale.nil?
      path_prefix = "/#{@locale}" + path_prefix
    end
    paths.each { |p| save_response("#{path_prefix}/#{p[:source]}", p[:target]) }
  end

  def copy_stripped_assets
    @asset_paths.uniq.each do |asset_path|
      original_path = File.join(Rails.root, "public", URI.unescape(asset_path))
      if File.exist? original_path
        new_path = File.join(@base_directory, URI.unescape(asset_path))
        FileUtils.mkdir_p(File.dirname(new_path))
        FileUtils.cp(original_path, new_path)
      else
        STDERR.puts '?? We might be missing "%s"' % original_path
      end
    end
  end

  def create_index_page
    schedule_file = File.join(@base_directory, 'schedule.html')
    if File.exist? schedule_file
      FileUtils.cp(schedule_file, File.join(@base_directory, 'index.html'))
    end
  end

  def get_query_paths
    paths = [
        {source: "schedule", target: "schedule.html"},
        {source: "events", target: "events.html"},
        {source: "speakers", target: "speakers.html"},
        {source: "speakers.json", target: "speakers.json"},
        {source: "schedule/style.css", target: "style.css"},
        {source: "schedule.ics", target: "schedule.ics"},
        {source: "schedule.xcal", target: "schedule.xcal"},
        {source: "schedule.json", target: "schedule.json"},
        {source: "schedule.xml", target: "schedule.xml"},
    ]

    day_index = 0
    @conference.days.each do |day|
      paths << {source: "schedule/#{day_index}", target: "schedule/#{day_index}.html"}
      paths << {source: "schedule/#{day_index}.pdf", target: "schedule/#{day_index}.pdf"}
      day_index += 1
    end

    @conference.events.public.confirmed.scheduled.each do |event|
      paths << {source: "events/#{event.id}", target: "events/#{event.id}.html"}
      paths << {source: "events/#{event.id}.ics", target: "events/#{event.id}.ics"}
    end
    Person.publicly_speaking_at(@conference).confirmed(@conference).each do |speaker|
      paths << {source: "speakers/#{speaker.id}", target: "speakers/#{speaker.id}.html"}<commit_msg>put new event json into static export too<commit_after>class StaticProgramExport

  EXPORT_PATH = Rails.root.join("tmp", "static_export").to_s

  # Export a static html version of the conference program.
  #
  # @param conference [Conference] conference to export
  # @param locale [String] export conference in supported locale
  # @param destination [String] export into this directory
  def initialize(conference, locale = 'en', destination = EXPORT_PATH)
    @conference = conference
    @locale = locale
    @destination = destination || EXPORT_PATH
  end

  # create a tarball from the conference export directory
  def create_tarball
    out_file = filename(@conference, @locale)
    if File.exist? out_file
      File.unlink out_file
    end
    system( 'tar', *['-cpz', '-f', out_file.to_s, '-C', @destination, @conference.acronym].flatten )
    out_file.to_s
  end

  # export the conference to disk
  #
  # only run by rake task, cannot run in the same thread as rails
  def run_export
    fail "No conference found!" if @conference.nil?

    @asset_paths = []
    @base_directory = File.join(@destination, @conference.acronym)
    @base_url = get_base_url
    @original_schedule_public = @conference.schedule_public

    @session = ActionDispatch::Integration::Session.new(Frab::Application)
    @session.host = Settings.host
    @session.https! if Settings['protocol'] == "https"
    ActiveRecord::Base.transaction do
      unlock_schedule unless @original_schedule_public

      setup_directories
      download_pages
      copy_stripped_assets
      create_index_page

      lock_schedule unless @original_schedule_public
    end
  end

  private

  def get_base_url
    if @conference.program_export_base_url.present?
      base_url = URI.parse(@conference.program_export_base_url).path
      base_url += '/' unless base_url.end_with?('/')
      base_url
    else 
      "/"
    end
  end

  def filename(conference, locale = 'en')
    File.join(@destination, "#{@conference.acronym}-#{@locale}.tar.gz")
  end

  def setup_directories
    FileUtils.rm_r(@base_directory, secure: true) if File.exist? @base_directory
    FileUtils.mkdir_p(@base_directory)
  end

  def download_pages
    paths = get_query_paths
    path_prefix = "/#{@conference.acronym}/public"
    unless @locale.nil?
      path_prefix = "/#{@locale}" + path_prefix
    end
    paths.each { |p| save_response("#{path_prefix}/#{p[:source]}", p[:target]) }
  end

  def copy_stripped_assets
    @asset_paths.uniq.each do |asset_path|
      original_path = File.join(Rails.root, "public", URI.unescape(asset_path))
      if File.exist? original_path
        new_path = File.join(@base_directory, URI.unescape(asset_path))
        FileUtils.mkdir_p(File.dirname(new_path))
        FileUtils.cp(original_path, new_path)
      else
        STDERR.puts '?? We might be missing "%s"' % original_path
      end
    end
  end

  def create_index_page
    schedule_file = File.join(@base_directory, 'schedule.html')
    if File.exist? schedule_file
      FileUtils.cp(schedule_file, File.join(@base_directory, 'index.html'))
    end
  end

  def get_query_paths
    paths = [
        {source: "schedule", target: "schedule.html"},
        {source: "events", target: "events.html"},
        {source: "speakers", target: "speakers.html"},
        {source: "events.json", target: "events.json"},
        {source: "speakers.json", target: "speakers.json"},
        {source: "schedule/style.css", target: "style.css"},
        {source: "schedule.ics", target: "schedule.ics"},
        {source: "schedule.xcal", target: "schedule.xcal"},
        {source: "schedule.json", target: "schedule.json"},
        {source: "schedule.xml", target: "schedule.xml"},
    ]

    day_index = 0
    @conference.days.each do |day|
      paths << {source: "schedule/#{day_index}", target: "schedule/#{day_index}.html"}
      paths << {source: "schedule/#{day_index}.pdf", target: "schedule/#{day_index}.pdf"}
      day_index += 1
    end

    @conference.events.public.confirmed.scheduled.each do |event|
      paths << {source: "events/#{event.id}", target: "events/#{event.id}.html"}
      paths << {source: "events/#{event.id}.ics", target: "events/#{event.id}.ics"}
    end
    Person.publicly_speaking_at(@conference).confirmed(@conference).each do |speaker|
      paths << {source: "speakers/#{speaker.id}", target: "speakers/#{speaker.id}.html"}
{'fraction_non_alphanumeric': 0.13139188259573492, 'fraction_numerical': 0.00045861041045631735, 'mean_word_length': 3.5103412616339194, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6787479', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2718, 'n_words': 618}