<commit_before>namespace :db do
  desc "Imports PunBB content"
  task :import => :environment do
    require 'tzinfo'
    puts "This task will import from a PunBB database defined as 'import' in your database.yml file."
    puts "It will import into your 'development' database unless you specify otherwise (e.g. 'rake db:import RAILS_ENV=production')."
    puts "Please make sure the receiving database is empty before continuing."
    puts "Tested with PunBB version 1.2.15."
    puts "Type '1' to continue, or '2' to quit."
    confirmation = STDIN.gets.chomp
    exit unless confirmation == "1"
    system "rake db:schema:load"
    ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV])
    #
    # ABOUT
    #
    # This will import a PunBB database into the El Dorado structure. 
    # Be careful to keep your text encoding consistent. Most databases will be in latin1.
    # This whole shebang is only tested with PunBB 1.2.15.
    # The user with id 2 (the "guest" user is 1) will be set as the only "admin" user.
    # Any topics, posts, etc made by a user not in the database will be assigned to the "guest" user.
    # 
    # importing: pun_bans, pun_categories, pun_config, pun_forums, pun_posts, pun_ranks, pun_topics, pun_users, pun_subscriptions
    # ignoring: pun_censoring, pun_forum_perms, pun_groups, pun_online, pun_reports, pun_search_cache, pun_search_matches, pun_search_words
    # 
    # STORE RAILS DATABASE CONNECTION INFO
    #
    eldorado = ActiveRecord::Base.configurations
    prefix = eldorado['import']['prefix']
    puts 'Starting import...'
    #
    # CONFIG
    #
    # ignoring: almost everything
    # importing: o_cur_version, o_board_title, o_board_desc, o_server_timezone
    #
    puts 'Importing configuration...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT conf_name, conf_value FROM #{prefix}config LIMIT 4")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    @item = Option.new
    @index = 0
    for i in @items 
      @index += 1
      @item.site_title = i[1] if @index == 2 # o_board_title
      @item.site_tagline = i[1] if @index == 3 # o_board_desc
      TZ = i[1].to_i if @index == 4 # o_server_timezone
    end
    TZ = '+' + TZ.to_s if TZ == TZ.abs # add a plus sign if this is a positive number
    TZ = '' if TZ == '+0' # clear timezone if it's 0, will end up being GMT
    TzTime.zone = TZInfo::Timezone.get("Etc/GMT#{TZ.to_s}")
    @item.footer_left = ''
    @item.footer_right = 'Powered by El Dorado | <a href="http://almosteffortless.com">&aelig;</a>'
    @item.newest_user = 'Newest User'
    @item.admin_rank = 'Administrator'
    @item.save!
    #
    # USERS
    #
    # ignoring the following fields from PunBB: 
    # num_posts (will be updated during import), 
    # group_id, title, realname, url, jabber, icq, msn, aim, yahoo, location, use_avatar, 
    # disp_topics, disp_posts, email_setting, save_pass, notify_with_post, show_smilies, 
    # show_img, show_img_sig, show_avatars, show_sig, language, style, last_post, 
    # registration_ip, admin_note, activate_string, activate_key, birthday
    #
    # not setting the following for El Dorado:
    # admin, bio, avatar_id
    #
    puts 'Importing users...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT id, username, password, email, signature, registered, last_visit, timezone FROM #{prefix}users")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    for i in @items
      @item = User.new
      @item.id = i[0] # id
      @item.login = i[1] # username
      @item.password_hash = i[2] # password 
      @item.email = i[3] # email 
      @item.signature = i[4] # signature
      @item.created_at = TzTime.at(Time.at(i[5].to_i)) # registered 
        @item.password_hash = User.encrypt(rand.to_s) if @item.login == 'Guest' # random password for Guest user
        @item.admin = true if @item.id == 2 # make first non-guest user into admin
      @item.time_zone = ('Etc/GMT' + i[7].to_s) unless i[7] == 0 # timezone
        @item.time_zone = 'Etc/GMT' if @item.time_zone == 'Etc/GMT0' # fix for users with no zone
      @item.save!
      # manually fix timestamp issues raised by controller actions
        @item.profile_updated_at = @item.created_at 
        @item.online_at = TzTime.at(Time.at(i[6].to_i)) # last_visit 
        @item.save!
      puts "Importing user: #{@item.id}"
    end
    #
    # BANS
    #
    puts 'Importing bans...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT id, username, ip, email, message, expire FROM #{prefix}bans")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    for i in @items
      @item = Ban.new
      @item.id = i[0] # id
        @temp = User.find_by_login(i[1]) # username
        @temp = User.find_by_id(1) if @temp.nil? # assign to guest account if user isn't found
      @item.user_id = @temp.id # get user_id instead of username      
      @item.ip = i[2] # ip
      @item.email = i[3] # email
      @item.message = i[4] # message
      @item.expires_at = TzTime.at(Time.at(i[5].to_i)) unless i[5].nil? # expire
      @item.save!
      puts "Importing ban: #{@item.id}"
    end
    #
    # RANKS
    #<commit_msg>i think the timestamps are working<commit_after>namespace :db do
  desc "Imports PunBB content"
  task :import => :environment do
    require 'tzinfo'
    puts "This task will import from a PunBB database defined as 'import' in your database.yml file."
    puts "It will import into your 'development' database unless you specify otherwise (e.g. 'rake db:import RAILS_ENV=production')."
    puts "Please make sure the receiving database is empty before continuing."
    puts "Tested with PunBB version 1.2.15."
    puts "Type '1' to continue, or '2' to quit."
    confirmation = STDIN.gets.chomp
    exit unless confirmation == "1"
    system "rake db:schema:load"
    ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV])
    #
    # ABOUT
    #
    # This will import a PunBB database into the El Dorado structure. 
    # Be careful to keep your text encoding consistent. Most databases will be in latin1.
    # This whole shebang is only tested with PunBB 1.2.15.
    # Times may not be 100% accurate do to timezone and daylight savings issues. 
    # The user with id 2 (the "guest" user is 1) will be set as the only "admin" user.
    # Any topics, posts, etc made by a user not in the database will be assigned to the "guest" user.
    # 
    # importing: pun_bans, pun_categories, pun_config, pun_forums, pun_posts, pun_ranks, pun_topics, pun_users, pun_subscriptions
    # ignoring: pun_censoring, pun_forum_perms, pun_groups, pun_online, pun_reports, pun_search_cache, pun_search_matches, pun_search_words
    # 
    # STORE RAILS DATABASE CONNECTION INFO
    #
    eldorado = ActiveRecord::Base.configurations
    prefix = eldorado['import']['prefix']
    puts 'Starting import...'
    #
    # CONFIG
    #
    # ignoring: almost everything
    # importing: o_cur_version, o_board_title, o_board_desc, o_server_timezone
    #
    puts 'Importing configuration...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT conf_name, conf_value FROM #{prefix}config LIMIT 4")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    @item = Option.new
    @index = 0
    for i in @items 
      @index += 1
      @item.site_title = i[1] if @index == 2 # o_board_title
      @item.site_tagline = i[1] if @index == 3 # o_board_desc
      TZ = i[1].to_i if @index == 4 # o_server_timezone
    end
    TZ = '+' + TZ.to_s if TZ == TZ.abs # add a plus sign if this is a positive number
    TZ = '' if TZ == '+0' # clear timezone if it's 0, will end up being GMT
    TzTime.zone = TZInfo::Timezone.get("Etc/GMT#{TZ.to_s}")
    @item.footer_left = ''
    @item.footer_right = 'Powered by El Dorado | <a href="http://almosteffortless.com">&aelig;</a>'
    @item.newest_user = 'Newest User'
    @item.admin_rank = 'Administrator'
    @item.save!
    #
    # USERS
    #
    # ignoring the following fields from PunBB: 
    # num_posts (will be updated during import), 
    # group_id, title, realname, url, jabber, icq, msn, aim, yahoo, location, use_avatar, 
    # disp_topics, disp_posts, email_setting, save_pass, notify_with_post, show_smilies, 
    # show_img, show_img_sig, show_avatars, show_sig, timezone, language, style, last_post, 
    # registration_ip, admin_note, activate_string, activate_key, birthday
    #
    # not setting the following for El Dorado:
    # admin, bio, avatar_id
    #
    puts 'Importing users...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT id, username, password, email, signature, registered, last_visit FROM #{prefix}users")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    for i in @items
      @item = User.new
      @item.id = i[0] # id
      @item.login = i[1] # username
      @item.password_hash = i[2] # password 
      @item.email = i[3] # email 
      @item.signature = i[4] # signature
      @item.created_at = TzTime.at(Time.at(i[5].to_i)) # registered 
        @item.password_hash = <PASSWORD>) if @item.login == 'Guest' # random password for Guest user
        @item.admin = true if @item.id == 2 # make first non-guest user into admin
      @item.save!
      # manually fix timestamp issues raised by controller actions
        @item.profile_updated_at = @item.created_at 
        @item.online_at = TzTime.at(Time.at(i[6].to_i)) # last_visit 
        @item.save!
      puts "Importing user: #{@item.id}"
    end
    #
    # BANS
    #
    puts 'Importing bans...'
    ActiveRecord::Base.establish_connection(eldorado['import'])
    @items = ActiveRecord::Base.connection.execute("SELECT id, username, ip, email, message, expire FROM #{prefix}bans")
    ActiveRecord::Base.establish_connection(eldorado[RAILS_ENV])
    for i in @items
      @item = Ban.new
      @item.id = i[0] # id
        @temp = User.find_by_login(i[1]) # username
        @temp = User.find_by_id(1) if @temp.nil? # assign to guest account if user isn't found
      @item.user_id = @temp.id # get user_id instead of username      
      @item.ip = i[2] # ip
      @item.email = i[3] # email
      @item.message = i[4] # message
      @item.expires_at = TzTime.at(Time.at(i[5].to_i)) unless i[5].nil? # expire
      @item.save!
      puts "Importing ban: #{@item.id}"
    end
    #
    # RANKS
    #
{'fraction_non_alphanumeric': 0.11591099525183875, 'fraction_numerical': 0.008286006889488875, 'mean_word_length': 3.202660406885759, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '205246', 'n_tokens_mistral': 3636, 'n_tokens_neox': 3519, 'n_words': 1000}