-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.rb
executable file
·326 lines (272 loc) · 9.28 KB
/
convert.rb
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
require 'logger'
require 'yaml'
require 'optparse'
class Object
def try(name, *args)
method(name).call *args
end
end
class NilClass
def try(name, *args)
nil
end
end
def wp_table(name)
"#{$options[:prefix]}#{name}".to_sym
end
def yesno(prompt)
while 1
print "#{prompt} [yN] "
answer = STDIN.gets.strip
case answer
when /^y/i:
return true
when /^n/i, "":
return false
end
end
end
$options = {:prefix => "wp_", :ask => true, :overwrite => true}
opts = OptionParser.new do |opt|
opt.banner = "Usage: convert.rb [options] from-db [to-db]"
opt.separator ""
opt.on("-p", "--prefix PREFIX", "Set the WordPress table prefix", "Default: wp_") do |p|
$options[:prefix] = p
end
opt.separator ""
opt.on("--[no-]overwrite", "Overwrite existing entries") do |o|
$options[:overwrite] = o
$options[:ask] = false
end
opt.on("--ask-overwrite", "Ask before overwriting existing entries", "[This is the default]") do
$options[:ask] = true
end
opt.separator ""
opt.on_tail("-h", "--help", "Show this message") do
puts opt
puts
puts <<-EOF
from-db and to-db are URLs identifying a database.
Such a URL might look like mysql://username:password@host/blog
See the Sequel documentation for more information
EOF
exit
end
end
opts.parse!
if not (1..2).include?(ARGV.size)
STDERR.puts "Unexpected number of arguments"
STDERR.puts opts
exit 1
end
FROM = Sequel.open ARGV[0]
TO = ARGV.size > 1 ? Sequel.open(ARGV[1]) : FROM
class Sequel::Database
def last_insert_id
self["SELECT LAST_INSERT_ID()"].first.values.first
end
end
if not TO.table_exists?(wp_table(:terms))
STDERR.puts "Error: I can't find the wordpress tables. Perhaps your prefix is wrong?"
exit 1
end
puts "## Copying Categories"
categories = FROM[:categories].order(:position)
wp_terms = TO[wp_table(:terms)]
wp_term_taxonomy = TO[wp_table(:term_taxonomy)]
map_categories = {}
copy_term = proc do |name, slug, taxonomy|
puts "Copying #{name} (#{slug})"
term_id = wp_terms.filter(:slug => slug).first.try(:fetch, :term_id)
if term_id.nil?
wp_terms << {:name => name, :slug => slug}
term_id = TO.last_insert_id
end
term_taxonomy_id = wp_term_taxonomy.filter(:term_id => term_id, :taxonomy => taxonomy).first.try(:fetch, :term_taxonomy_id)
if term_taxonomy_id.nil?
wp_term_taxonomy << {:term_id => term_id, :taxonomy => taxonomy}
term_taxonomy_id = TO.last_insert_id
end
term_taxonomy_id
end
categories.all.each do |row|
name = row[:name]
slug = row[:permalink]
term_taxonomy_id = copy_term.call(name, slug, "category")
map_categories[row[:id]] = term_taxonomy_id
end
puts ""
puts "## Copying Tags"
tags = FROM[:tags]
map_tags = {}
tags.all.each do |row|
name = row[:display_name]
slug = row[:name]
term_taxonomy_id = copy_term.call(name, slug, "post_tag")
map_tags[row[:id]] = term_taxonomy_id
end
puts ""
puts "## Processing Text Filters"
text_filters = FROM[:text_filters]
map_text_filters = {}
MARKUP_MAP = {
"textile" => "textile2"
}.freeze
text_filters.each do |row|
markup = MARKUP_MAP.fetch(*[row[:markup]]*2)
filter = (YAML.load(row[:filters]).first || "none").to_s
puts "Found #{markup}, #{filter}"
map_text_filters[row[:id]] = [markup, filter]
end
puts ""
puts "## Copying Pages"
pages = FROM[:contents].filter(:type => "Page")
wp_posts = TO[wp_table(:posts)]
wp_postmeta = TO[wp_table(:postmeta)]
pages.all.each do |row|
title = row[:title]
body = row[:body]
created_at = row[:created_at]
updated_at = row[:updated_at]
# seems like Text-Control doesn't support per-page filters
# text_filter = map_text_filters.fetch(row[:text_filter_id],["markdown","smartypants"])
name = row[:name]
published = row[:published] == 1
post_status = published ? "publish" : "draft"
puts "Copying #{title}"
hash = {:post_author => 1, :post_date => created_at, :post_date_gmt => created_at + 4.hours,
:post_content => body, :post_title => title, :post_status => post_status,
:post_name => name, :post_modified => updated_at,
:post_modified_gmt => updated_at + 4.hours, :post_type => "page"}
existing = wp_posts.filter(:post_name => name, :post_type => "page")
if existing.count == 1
overwrite = $options[:ask] ? yesno("Post already exists, overwrite?") : $options[:overwrite]
if overwrite
wp_postmeta.filter(:post_id => existing.select(:ID)).delete
existing.update(hash)
post_id = existing.first[:ID]
else
puts "Skipping"
next
end
elsif existing.count > 1
STDERR.puts "Found more than 1 page with the same name"
STDERR.puts "Ids: #{existing.map(:ID).join(", ")}"
STDERR.puts "Aborting"
exit 1
else
wp_posts << hash
post_id = TO.last_insert_id
end
wp_postmeta << {:post_id => post_id, :meta_key => "_wp_page_template", :meta_value => "default"}
end
puts ""
puts "## Copying Articles"
articles = FROM[:contents].filter(:type => "Article")
articles_tags = FROM[:articles_tags]
categorizations = FROM[:categorizations]
wp_term_relationships = TO[wp_table(:term_relationships)]
map_articles = {}
make_relationship = proc do |object_id, term_taxonomy_id|
wp_term_relationships << {:object_id => object_id,
:term_taxonomy_id => term_taxonomy_id}
wp_term_taxonomy.filter(:term_taxonomy_id => term_taxonomy_id).update("count = count + 1")
end
articles.all.each do |row|
title = row[:title]
body = row[:body]
extended = row[:extended]
excerpt = row[:excerpt] || ""
published_at = row[:published_at]
updated_at = row[:updated_at]
permalink = row[:permalink]
guid = row[:guid]
text_filter = map_text_filters.fetch(row[:text_filter_id],["markdown","smartypants"])
published = row[:published] == 1
allow_pings = row[:allow_pings] == 1
allow_comments = row[:allow_comments] == 1
post_date_hash = published ? {:post_date => published_at, :post_date_gmt => published_at + 4.hours} : {}
post_content = extended.blank? ? body : "#{body}\n\n<!--more-->\n\n#{extended}"
post_status = published ? "publish" : "draft"
comment_status = allow_comments ? "open" : "closed"
ping_status = allow_pings ? "open" : "closed"
puts "Copying #{title}"
hash = {:post_author => 1, :post_content => post_content, :post_title => title,
:post_excerpt => excerpt, :post_status => post_status, :comment_status => comment_status,
:ping_status => ping_status, :post_name => permalink,
:post_modified => updated_at, :post_modified_gmt => updated_at + 4.hours,
:guid => guid, :post_type => "post"}.merge(post_date_hash)
existing = wp_posts.filter(:post_name => permalink, :post_type => "post")
if existing.count == 1
overwrite = $options[:ask] ? yesno("Article already exists, overwrite?") : $options[:overwrite]
if overwrite
wp_postmeta.filter(:post_id => existing.select(:ID)).delete
existing.update(hash)
post_id = existing.first[:ID]
else
puts "Skipping"
next
end
elsif existing.count > 1
STDERR.puts "Found more than 1 article with the same name"
STDERR.puts "Ids: #{existing.map(:ID).join(", ")}"
STDERR.puts "Aborting"
exit 1
else
wp_posts << hash
post_id = TO.last_insert_id
end
wp_postmeta << {:post_id => post_id, :meta_key => "_tc_post_format", :meta_value => text_filter[0]}
wp_postmeta << {:post_id => post_id, :meta_key => "_tc_post_encoding", :meta_value => text_filter[1]}
map_articles[row[:id]] = post_id
# set up categorizations
categorizations.filter(:article_id => row[:id]).all.each do |crow|
make_relationship.call post_id, map_categories[crow[:category_id]]
end
# set up tags
articles_tags.filter(:article_id => row[:id]).all.each do |trow|
make_relationship.call post_id, map_tags[trow[:tag_id]]
end
end
puts ""
puts "## Copying Comments"
comments = FROM[:feedback].filter(:type => "Comment")
wp_comments = TO[wp_table(:comments)]
comments.all.each_with_index do |row,idx|
author = row[:author]
body = row[:body]
created_at = row[:created_at]
user_id = row[:user_id] || 0
article_id = row[:article_id]
email = row[:email] || ""
url = row[:url] || ""
ip = row[:ip] || ""
post_id = map_articles[article_id]
puts "Copying comment #{idx}"
wp_comments << {:comment_post_ID => post_id, :comment_author => author,
:comment_author_email => email, :comment_author_url => url,
:comment_author_IP => ip, :comment_date => created_at,
:comment_date_gmt => created_at + 4.hours, :comment_content => body,
:user_id => user_id}
end
puts ""
puts "## Copying Trackbacks"
trackbacks = FROM[:feedback].filter(:type => "Trackback")
trackbacks.all.each_with_index do |row,idx|
title = row[:title]
excerpt = row[:excerpt]
created_at = row[:created_at]
article_id = row[:article_id]
url = row[:url]
ip = row[:ip]
blog_name = row[:blog_name]
post_id = map_articles[article_id]
puts "Copying trackback #{idx}"
wp_comments << {:comment_post_ID => post_id, :comment_author => blog_name,
:comment_author_url => url, :comment_author_IP => ip,
:comment_date => created_at, :comment_date_gmt => created_at + 4.hours,
:comment_content => excerpt, :comment_type => "trackback"}
end