jseifer (owner)

Revisions

gist: 45760 Download_button fork
public
Public Clone URL: git://gist.github.com/45760.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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