close
#!/usr/local/bin/ruby
require "dbi"

require "parsedate"

db_plog_dsn = "dbi:Mysql:plogdb:localhost"
db_wp_dsn = "dbi:Mysql:wptest:localhost"
db_username = ARGV[0]
db_password = ARGV[1]

Category = Struct.new(:id, :name, :description)
Post = Struct.new(:id, :category, :title, :content, :createDateTime, :modifyDateTime, :status)

wp_cats = Hash.new(nil)
wp_posts = Hash.new(nil)
plog_cats = Hash.new(nil)
plog_posts = Hash.new(nil)

wp_conn = DBI.connect(db_wp_dsn, db_username, db_password)
plog_conn = DBI.connect(db_plog_dsn, db_username, db_password)

# initialize wp_cats
wp_conn.select_all("SELECT * FROM wp_categories") do |row|
  wp_cats[row["cat_name"]] = Category.new(row["cat_ID"], row["cat_name"], row["category_description"])
end

# initialize plog_cats
plog_conn.select_all("SELECT * FROM plog_articles_categories") do |row|
  plog_cats[row["name"]] = Category.new(row["id"], row["name"], row["description"])
end

# initialize wp_posts
wp_conn.select_all("SELECT wp_posts.ID id, wp_categories.cat_name catetory, wp_posts.post_date create_date_time, wp_posts.post_modified modify_date_time, wp_posts.post_title title, wp_posts.post_content content, wp_posts.post_status status FROM wp_post2cat JOIN wp_categories ON wp_post2cat.category_id=wp_categories.cat_ID JOIN wp_posts ON wp_post2cat.post_id=wp_posts.ID") do |row|
  wp_posts[row["title"]] = Post.new(
    row["id"], row["category"], row["title"],
    row["content"], row["create_date_time"],
    row["modify_date_time"], row["status"])
end

# initialize plog_posts
plog_conn.select_all("SELECT plog_articles.id id, plog_articles_categories.name category, plog_articles.`date` create_date_time, plog_articles.modification_date modify_date_time, plog_articles_text.normalized_topic title, plog_articles_text.`text` content, plog_articles.status status FROM plog_article_categories_link JOIN plog_articles_categories ON plog_article_categories_link.category_id=plog_articles_categories.id JOIN plog_articles ON plog_article_categories_link.article_id=plog_articles.id JOIN plog_articles_text ON plog_article_categories_link.article_id=plog_articles_text.article_id") do |row|
  plog_posts[row["title"]] = Post.new(
    row["id"], row["category"], row["title"],
    row["content"].gsub(/\[@more@\]/, ''),
    row["create_date_time"],
    row["modify_date_time"], row["status"])
end

# create category
plog_cats.each_key do |key|
  if false == wp_cats.has_key?(key)
    puts "Create category \"#{key}\""

    plog_cat = plog_cats[key]
    wp_conn.prepare("INSERT INTO wp_categories (cat_name, category_nicename, category_description) VALUES (?, ?, ?)") do |pstmt|
      pstmt.execute(plog_cat.name, plog_cat.name, plog_cat.description)
    end
    # put created category into hash
    id = wp_conn.select_one("SELECT last_insert_id()")
    wp_cat = Category.new(id, plog_cat.name, plog_cat.description)
    wp_cats[wp_cat.name] = wp_cat
  end
end

# create post
(plog_posts.keys - wp_posts.keys).each do |post_title|
  plog_post = plog_posts[post_title]
  puts "Import #{plog_post.title}"
  createGmtDateTime = Time.local(*ParseDate.parsedate(plog_post.createDateTime)).gmtime.strftime("%Y-%m-%d %H:%M:%S")
  modifyDateTime = plog_post.modifyDateTime
  if modifyDateTime == "0000-00-00 00:00:00"
    modifyDateTime = plog_post.createDateTime
  end
  modifyGmtDateTime = plog_post.modifyDateTime
  if modifyGmtDateTime == "0000-00-00 00:00:00"

    modifyGmtDateTime = createGmtDateTime 
  else
    modifyGmtDateTime = Time.local(*ParseDate.parsedate(modifyGmtDateTime)).gmtime.strftime("%Y-%m-%d %H:%M:%S")
  end
  status = plog_post.status
  if status  == 1
    status = "publish"
  else
    status = "draft"
  end
  wp_conn.prepare("INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, post_modified, post_modified_gmt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)") do |pstmt|
    pstmt.execute(1, plog_post.createDateTime, createGmtDateTime, plog_post.content, plog_post.title, status, modifyDateTime, modifyGmtDateTime)
  end
  post_id = wp_conn.select_one("SELECT last_insert_id()")
  wp_conn.prepare("INSERT INTO wp_post2cat (post_id, category_id) VALUES (?, ?)") do |pstmt|
    pstmt.execute(post_id, wp_cats[plog_post.category].id)
  end
end

wp_conn.disconnect
plog_conn.disconnect
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Forth 的頭像
    Forth

    不就是個blog

    Forth 發表在 痞客邦 留言(0) 人氣()