Report abuse

# A Simple ruby script to convert Mephisto to WordPress
# Tested with version 0.8 of Mephisto and 2.5.1 of WordPress
#
# Author: Caleb Jaffa
# Author: HaiMing Yin http://webwhere.cn
#
# Usage: update the hashes with the information to point to your mehpisto
# and wordpress databases and then run with ruby m_to_wp.rb

require 'rubygems'
require 'mysql'

start_time = Time.now

# Config options

mephisto_db = { 'host' => '127.0.0.1', 'user' => '', 'password' => '', 'database' => '' }
wordpress_db = { 'host' => '127.0.0.1', 'user' => '', 'password' => '', 'database' => '' }
domain = ""


mephisto = Mysql.real_connect(mephisto_db['host'], mephisto_db['user'], mephisto_db['password'], mephisto_db['database'])
wordpress = Mysql.real_connect(wordpress_db['host'], wordpress_db['user'], wordpress_db['password'], wordpress_db['database'])

puts "Terms"

mephisto.query("SELECT * FROM tags").each_hash do |tag|
  tag['name'] = Mysql.quote(tag['name'])
  wordpress.query("REPLACE INTO wp_terms(term_id, name, slug, term_group) VALUES(#{tag['id']}, '#{tag['name']}', '#{tag['name'].downcase.gsub(" ","_")}', 0)")  

  # term taxonomy
  c = mephisto.query("SELECT count(id) AS count FROM taggings WHERE tag_id = #{tag['id']}").fetch_hash['count']
  wordpress.query("REPLACE INTO wp_term_taxonomy(term_taxonomy_id, term_id, taxonomy, parent, count) VALUES(#{tag['id']}, #{tag['id']}, 'post_tag', 0, #{c})")
end

puts "Term Relationships"

mephisto.query("SELECT * FROM taggings").each_hash do |tagging|
  wordpress.query("REPLACE INTO wp_term_relationships(term_taxonomy_id, object_id) VALUES(#{tagging['tag_id']}, #{tagging['taggable_id']})")
end

puts "Sections"

sections = {}
mephisto.query("SELECT * FROM sections WHERE name <> 'Home'").each_hash do |section|
  section['name'] = Mysql.quote(section['name'])
  wordpress.query("REPLACE INTO wp_terms(name, slug, term_group) VALUES('#{section['name']}', '#{section['name'].downcase.gsub(" ","-")}', 0)")

  term_id = wordpress.insert_id
  sections[section["id"]] = term_id

  # term taxonomy
  wordpress.query("REPLACE INTO wp_term_taxonomy(term_taxonomy_id, term_id,
                     taxonomy, parent, count) 
                   VALUES(#{term_id}, #{term_id},
                     'category', 0, #{section['articles_count']})")
end

puts "Section Relationships"

mephisto.query("SELECT * FROM assigned_sections").each_hash do |as|
  next if sections[as['section_id']].nil?
  wordpress.query("REPLACE INTO wp_term_relationships(term_taxonomy_id, object_id) VALUES(#{sections[as['section_id']]}, #{as['article_id']})")
end

puts "Posts"

mephisto.query("SELECT * FROM contents WHERE article_id IS NULL").each_hash do |article|
  c = mephisto.query("SELECT count(id) AS count FROM contents WHERE article_id = #{article['id']}").fetch_hash['count']
  if article['published_at'] then
    guid = "http://#{domain}/"+article['published_at'][0..3]+"/"+article['published_at'][5..6]+"/"+article['published_at'][8..9]+"/"+article['permalink']
  else
    guid = "http://#{domain}/"+article['permalink']
  end
  article['body'] = Mysql.quote(article['body_html'])
  article['title'] = Mysql.quote(article['title'])
  wordpress.query("REPLACE INTO wp_posts(ID, post_author, post_date, post_date_gmt, post_content, post_title, post_category, post_status, ping_status, post_name, post_modified, post_modified_gmt, post_parent, guid, menu_order, post_type, comment_count) 
                              VALUES(#{article['id']}, 1, '#{article['published_at']}', '#{article['published_at']}', '#{article['body']}', '#{article['title']}', 39, 'publish', 'closed', '#{article['permalink']}', '#{article['updated_at']}', '#{article['updated_at']}', 0, '#{guid}', 0, 'post', #{c})")
end

puts "Comments"

mephisto.query("SELECT * FROM contents WHERE article_id IS NOT NULL").each_hash do |comment|
  comment['author_url'] ||= ""
  comment['author'] = Mysql.quote(comment['author'])
  comment['body'] = Mysql.quote(comment['body_html'])
  wordpress.query("REPLACE INTO wp_comments(comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_parent, user_id)
            VALUES(#{comment['id']}, #{comment['article_id']}, '#{comment['author']}', '#{comment['author_email']}', '#{comment['author_url']}', '#{comment['author_ip']}', '#{comment['published_at']}', '#{comment['published_at']}', '#{comment['body']}', 0, '1', 0, 0)")
end

end_time = Time.now

puts "Total elapsed time: #{end_time - start_time}"