Created
January 11, 2009 19:12
-
-
Save jseifer/45760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mephisto to Wordpress converter | |
# | |
# Create your database credentials in mephisto.yml and wordpress.yml | |
# then run ruby converter.rb | |
# | |
require 'rubygems' | |
require 'activerecord' | |
require 'activesupport' | |
require 'yaml' | |
@blog_url = "YOUR_URL" # Without the 'http://' | |
module Mephisto | |
CREDENTIALS = YAML::load(File.open('mephisto.yml')) | |
ActiveRecord::Base.establish_connection(CREDENTIALS) | |
# Horrible shortcut alert! | |
%w(Content Tag Tagging).each do |tbl| | |
eval <<-STR | |
class #{tbl} < ActiveRecord::Base | |
self.abstract_class = true | |
establish_connection(CREDENTIALS) | |
end | |
STR | |
end | |
%w(Article Comment).each do |tbl| | |
eval <<-STR | |
class #{tbl} < Content | |
self.abstract_class = true | |
establish_connection(CREDENTIALS) | |
end | |
STR | |
end | |
end | |
module Wordpress | |
CREDENTIALS= YAML::load(File.open('wordpress.yml')) | |
ActiveRecord::Base.establish_connection(CREDENTIALS) | |
%w(WpTerm WpTermRelationships WpPost WpComment).each do |tbl| | |
eval <<-STR | |
class #{tbl} < ActiveRecord::Base | |
self.abstract_class = true | |
establish_connection(CREDENTIALS) | |
end | |
STR | |
end | |
# We have to override ActiveRecords plurilization of table names | |
# because wordpress doesn't follow with this table. | |
class WpTermTaxonomy < ActiveRecord::Base | |
self.abstract_class = true | |
establish_connection(CREDENTIALS) | |
set_table_name 'wp_term_taxonomy' | |
end | |
end | |
# This next section is shamlessly stole from Jason Gill at | |
# http://blog.gilluminate.com/2008/05/08/how-i-converted-mephisto-to-wordpress/ | |
# and adapted to work with the latest versions of Mephisto (Drax) and Wordpress (2.7) | |
## Get Mephisto's info | |
start_time = Time.now | |
puts 'querying mephisto articles' | |
@articles = Mephisto::Content.find(:all, :conditions=>"article_id IS NULL") | |
puts 'querying mephisto comments' | |
@comments = Mephisto::Content.find(:all, :conditions=>"article_id IS NOT NULL") | |
puts 'querying mephisto taggings' | |
@taggings = Mephisto::Tagging.find(:all) | |
puts 'querying mephisto tags' | |
@tags = Mephisto::Tag.find(:all) | |
puts 'processing terms' | |
Wordpress::WpTerm.delete_all | |
@old_tags = Hash.new | |
for tag in @tags | |
# I encountered an error with slugs where Mephisto allows distinct tags that | |
# are the same but with mixed cases. | |
slug = tag.name.downcase.gsub(' ','_') | |
if old_tag = Wordpress::WpTerm.find_by_slug(slug) | |
@old_tags[old_tag.id] = slug | |
else | |
@wp_term = Wordpress::WpTerm.new | |
@wp_term.term_id = tag.id | |
@wp_term.name = tag.name | |
@wp_term.slug = slug | |
@wp_term.term_group = 0 | |
@wp_term.save | |
end | |
end | |
puts 'processing term relationships' | |
Wordpress::WpTermRelationships.delete_all | |
for tagging in @taggings | |
@wp_tr = Wordpress::WpTermRelationships.new | |
if @old_tags.keys.include?(tagging.tag_id) | |
@wp_tr.term_taxonomy_id = @old_tags[tagging.tag_id] | |
else | |
@wp_tr.term_taxonomy_id = tagging.tag_id | |
end | |
@wp_tr.object_id = tagging.taggable_id | |
@wp_tr.save | |
end | |
puts 'processing term taxonomy' | |
Wordpress::WpTermTaxonomy.delete_all | |
for tag in @tags | |
c = Mephisto::Tagging.count(:all, :conditions=>"tag_id = #{tag.id}") | |
@wp_tt = Wordpress::WpTermTaxonomy.new | |
@wp_tt.term_taxonomy_id = tag.id | |
@wp_tt.term_id = tag.id | |
@wp_tt.taxonomy = "post_tag" | |
@wp_tt.parent = 0 | |
@wp_tt.count = c | |
@wp_tt.save | |
end | |
puts 'processing posts' | |
Wordpress::WpPost.delete_all | |
for article in @articles | |
c = Mephisto::Content.count(:all, :conditions=>"article_id = #{article.id}") | |
@wp_post = Wordpress::WpPost.new | |
@wp_post.ID = article.id | |
@wp_post.post_author = article.user_id | |
@wp_post.post_date = (article.published_at.nil? ? Time.now + 7.days : article.published_at-7.hours) | |
@wp_post.post_date_gmt = (article.published_at.nil? ? Time.now + 7.days : article.published_at) | |
@wp_post.post_content = article.body | |
@wp_post.post_title = article.title | |
@wp_post.post_category = 39 | |
@wp_post.post_status = article.published_at.nil? ? "open" : "publish" | |
@wp_post.ping_status = "closed" | |
@wp_post.post_name = article.permalink | |
@wp_post.post_modified = article.updated_at#-7.hours | |
@wp_post.post_modified_gmt = article.updated_at | |
@wp_post.post_parent = 0 | |
@wp_post.guid = article.published_at.strftime("http://#{@blog_url}/%Y/%m/%d/")+article.permalink rescue '' | |
@wp_post.menu_order = 0 | |
@wp_post.post_type = "post" | |
@wp_post.comment_count = c | |
@wp_post.save | |
end | |
puts 'processing comments' | |
Wordpress::WpComment.delete_all | |
for comment in @comments | |
@wp_com = Wordpress::WpComment.new | |
@wp_com.comment_ID = comment.id | |
@wp_com.comment_post_ID = comment.article_id | |
@wp_com.comment_author = comment.author | |
@wp_com.comment_author_email = comment.author_email | |
if comment.author_url == nil | |
comment.author_url = "" | |
end | |
@wp_com.comment_author_url = comment.author_url | |
@wp_com.comment_author_IP = comment.author_ip | |
@wp_com.comment_date = comment.published_at-7.hours | |
@wp_com.comment_date_gmt = comment.published_at | |
@wp_com.comment_content = comment.body | |
@wp_com.comment_karma = 0 | |
@wp_com.comment_approved = '1' | |
@wp_com.comment_parent = 0 | |
@wp_com.user_id = 0 | |
@wp_com.save | |
end | |
puts 'finished!' | |
end_time = Time.now | |
@lapsed = end_time-start_time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment