#!/usr/bin/env ruby BLOG_ID = 'default' USER_ID = 'scox' module FmtHelper def FmtHelper.date(date) "#{date.year}-#{date.month}-#{date.day} #{date.hour}:#{date.min}:#{date.sec}" end def FmtHelper.content(content) content.gsub(/(.*)<\/code>/m) { |m| m.gsub("\n", '
') }.gsub("\n", '') end def FmtHelper.words(content) words = content.gsub(/<\/?\w+\/?>/, ' ').gsub(/[^\w]/, ' ').split(' ') words.reject! { |w| w.size <= 1 } words.map! { |w| w.downcase } words.join(' ') end def FmtHelper.tags(tags) #"a:1:{s:3:\"tag\";a:3:{i:0;s:3:\"aaa\";i:1;s:3:\"bar\";i:2;s:12:\"one time pad\";}}" #"a:1:{s:3:"tag"; a:3:{i:0;s:4:"DBUS" ;i:1;s:3:"HAL";i:2;s:4:"Ruby";}}" return 'a:0:{}' if tags.empty? taglist = "a:1:{s:3:\\\"tag\\\";a:#{tags.size}:{" tags.each_with_index do |tag, index| taglist += "i:#{index};s:#{tag.name.size}:\\\"#{tag.name}\\\";" end taglist += "}}" taglist end end ## CATEGORIES def dump_categories puts "[category cat_id,blog_id,cat_title,cat_url,cat_desc,cat_position]" Section.find(:all).each do |section| name = section.name.downcase id = section.id.to_i next if id == 1 puts "\"#{id - 1}\",\"#{BLOG_ID}\",\"#{name}\",\"#{name}\",\"\",\"#{id}\"" end end ## POSTS def dump_posts puts "[post post_id,blog_id,user_id,cat_id,post_dt,post_tz,post_creadt,post_upddt,post_password,post_type,post_format,post_url,post_lang,post_title,post_excerpt,post_excerpt_xhtml,post_content,post_content_xhtml,post_notes,post_words,post_status,post_selected,post_open_comment,post_open_tb,nb_comment,nb_trackback,post_meta]" posts = Hash.new post_id = 0 Article.find(:all).each do |article| post_id = post_id + 1 posts[post_id] = article blog_id = BLOG_ID user_id = USER_ID cat_id = article.sections.reject { |s| s.id == 1 }.first.id.to_i - 1 post_dt = FmtHelper.date(article.created_at) post_tz = "UTC" post_creadt = post_dt post_upddt = FmtHelper.date(article.updated_at) post_password = "" post_type = "post" post_format = "xhtml" post_url = article.full_permalink post_lang = 'en' post_title = article.title post_excerpt = FmtHelper.content(article.excerpt_html) post_excerpt_xhtml = FmtHelper.content(article.excerpt_html) post_content = FmtHelper.content(article.body_html) post_content_xhtml = FmtHelper.content(article.body_html) post_notes = '' post_words = FmtHelper.words(article.body_html) post_status = article.published? ? 1 : -2 post_selected = '0' post_open_comment = '1' post_open_tb = '1' nb_comment = article.comments_count nb_trackback = '0' post_meta = FmtHelper.tags(article.tags) puts "\"#{post_id}\",\"#{blog_id}\",\"#{user_id}\",\"#{cat_id}\",\"#{post_dt}\",\"#{post_tz}\",\"#{post_creadt}\",\"#{post_upddt}\",\"#{post_password}\",\"#{post_type}\",\"#{post_format}\",\"#{post_url}\",\"#{post_lang}\",\"#{post_title}\",\"#{post_excerpt}\",\"#{post_excerpt_xhtml}\",\"#{post_content}\",\"#{post_content_xhtml}\",\"#{post_notes}\",\"#{post_words}\",\"#{post_status}\",\"#{post_selected}\",\"#{post_open_comment}\",\"#{post_open_tb}\",\"#{nb_comment}\",\"#{nb_trackback}\",\"#{post_meta}\"" end posts end ## Comments def dump_comments(posts) puts "[comment comment_id,post_id,comment_dt,comment_tz,comment_upddt,comment_author,comment_email,comment_site,comment_content,comment_words,comment_ip,comment_status,comment_spam_status,comment_spam_filter,comment_trackback]" comment_id = 0 posts.each do |post_id, article| article.comments.each do |comment| comment_id = comment_id + 1 comment_dt = FmtHelper.date(comment.published_at) comment_tz = 'UTC' comment_upddt = comment_dt comment_author = comment.author comment_email = comment.author_email comment_site = comment.author_url comment_content = FmtHelper.content(comment.body_html) comment_words = FmtHelper.words(comment.body_html) comment_ip = comment.author_ip comment_status = '1' comment_spam_status = '0' comment_spam_filter = '' comment_trackback = '0' puts "\"#{comment_id}\",\"#{post_id}\",\"#{comment_dt}\",\"#{comment_tz}\",\"#{comment_upddt}\",\"#{comment_author}\",\"#{comment_email}\",\"#{comment_site}\",\"#{comment_content}\",\"#{comment_words}\",\"#{comment_ip}\",\"#{comment_status}\",\"#{comment_spam_status}\",\"#{comment_spam_filter}\",\"#{comment_trackback}\"" end end end if __FILE__ == $0 ENV['RAILS_ENV'] ||= 'production' require File.dirname(__FILE__) + '/../config/environment' puts "///DOTCLEAR|2.0-beta7|single" puts dump_categories puts posts = dump_posts puts dump_comments(posts) end