# PunbbToBeast class PunbbToBeast < ImportDatabase def initialize @src_prefix = 'punbb' @map = {} @tables = {} @tables[:users] = { :src => 'users', :conditions => "group_id<>32000 AND username<>'Guest'", :model => User, :attributes => { :login => :username, :password_hash => :password, :email => :email, :created_at => lambda {|record| Time.at(record["registered"].to_i).utc}, :last_login_at => lambda {|record| Time.at(record["last_visit"].to_i).utc}, :admin => 0, :posts_count => :num_posts, :last_seen_at => lambda {|record| Time.at(record["last_visit"].to_i).utc}, :activated => 1 }, } @tables[:forums] = { :src => 'forums', :model => Forum, :attributes => { :name => :forum_name, :description => :forum_desc, :topics_count => :num_topics, :posts_count => :num_posts, :position => :disp_position, :description_html => nil } } @tables[:topics] = { :src => 'topics', :model => Topic, :attributes => { :forum_id => lambda {|record| mapped_from(:forums, record['forum_id'])}, :user_id => lambda {|record| User.find_by_login(record['poster']).id}, :title => :subject, :created_at => lambda {|record| Time.at(record["posted"].to_i).utc}, :updated_at => lambda {|record| Time.at(record["posted"].to_i).utc}, :hits => :num_views, :sticky => :sticky, :posts_count => :num_replies, :replied_at => lambda {|record| Time.at(record["last_post"].to_i).utc}, :locked => :closed, :replied_by => lambda {|record| User.find_by_login(record['last_poster']).id rescue User.find_by_login(record['poster']).id}, :last_post_id => nil # add later, we dont know id because posts are not imported yet }, } @tables[:posts] = { :src => 'posts', :model => Post, :attributes => { :user_id => lambda {|record| mapped_from(:users, record['poster_id'])}, :topic_id => lambda {|record| mapped_from(:topics, record['topic_id'])}, :body => :message, :created_at => lambda {|record| Time.at(record["posted"].to_i).utc}, :updated_at => nil, :forum_id => lambda {|record| Topic.find(mapped_from(:topics, record['topic_id'])).forum_id}, :body_html => nil } } end def import(options = {}) record_timestamps = ActiveRecord::Base.record_timestamps ActiveRecord::Base.record_timestamps = false import_table(:users, options) import_table(:forums, options) import_table(:topics, options) import_table(:posts, options) #topic#last_post_id Post.find_by_sql("SELECT topic_id, id FROM posts GROUP BY topic_id ORDER BY created_at DESC").each do |r| Topic.find(r['topic_id']).update_attribute(:last_post_id, r['id']) end # monitorships convert a:2:{s:10:"username1";i:27324;s:6:"username2";i:24922;} Moderatorship.destroy_all sql = "SELECT * FROM #{@src_prefix}#{@tables[:forums][:src]} WHERE moderators<>''" begin ActiveRecord::Base.connection.select_all(sql).each do |src_record| src_record["moderators"].scan(/i\:(\d+);/) do |match| Moderatorship.create!( :forum_id => mapped_from(:forums, src_record['id']), :user_id => mapped_from(:users, match[0]) ) end end rescue logger.warn " #{$!}" end ActiveRecord::Base.record_timestamps = record_timestamps end end