#!/usr/bin/env ruby # Script for importing posts from the mikrocontroller.net forum require 'time' require 'rubygems' require 'config/environment' require 'mysql' class Post def validate true end end class String def to_utf8 Iconv.iconv('utf-8', 'iso-8859-1', self)[0] end end Post.delete_all Topic.delete_all Forum.delete_all TopicRead.delete_all TopicSubscription.delete_all Post.indexing_disabled = true dbh = Mysql.connect(nil, ARGV[0], ARGV[1], 'oldforum') res = dbh.query 'SELECT * FROM forums' res.each_hash do |of| puts "Importing forum ##{of['id']}" f = Forum.new f.name = of['name'].to_utf8 f.description = of['description'].to_utf8 f.save res = dbh.query "SELECT posts.*, bodies.content FROM posts LEFT JOIN bodies ON posts.id = bodies.id WHERE posts.id = posts.topic_id AND forum_id=#{of['id']} ORDER BY posts.id DESC LIMIT 100" res.each_hash do |ot| begin puts "Importing thread ##{ot['id']}" puts "Importing post ##{ot['id']}" t = Post.new t.guest_name = ot['authorname'].to_utf8 t.guest_email = ot['authormail'].to_utf8 t.subject = ot['subject'].to_utf8 t.text = Wraptools.wrap_ff(ot['content'], 78).to_utf8 t.created_at = Time.parse(ot['created_at']) f.add_post(t) rescue => e puts e.inspect next end res = dbh.query "SELECT posts.*, bodies.content FROM posts LEFT JOIN bodies ON posts.id = bodies.id WHERE posts.id <> posts.topic_id AND topic_id=#{ot['id']}" res.each_hash do |op| begin puts "Importing post ##{op['id']}" p = Post.new p.guest_name = op['authorname'].to_utf8 p.guest_email = op['authormail'].to_utf8 p.subject = op['subject'].to_utf8 p.text = Wraptools.wrap_ff(op['content'], 78).to_utf8 p.created_at = Time.parse(op['created_at']) t.add_reply(p) rescue => e puts e.inspect end end end end dbh.close Post.rebuild_index