lfa-wordpress-import/lib/wordpress/post.rb

68 lines
1.7 KiB
Ruby
Raw Normal View History

2014-03-04 22:47:53 +00:00
module WordPressImport
class Post < Page
def tags
# xml dump has "post_tag" for wordpress 3.1 and "tag" for 3.0
path = if node.xpath("category[@domain='post_tag']").count > 0
"category[@domain='post_tag']"
else
"category[@domain='tag']"
end
2014-03-04 22:47:53 +00:00
node.xpath(path).collect do |tag_node|
Tag.new(tag_node.text)
end
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
def tag_list
tags.collect(&:name).join(',')
end
def categories
node.xpath("category[@domain='category']").collect do |cat|
Category.new(cat.text)
end
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
def comments
node.xpath("wp:comment").collect do |comment_node|
Comment.new(comment_node)
end
2014-03-04 22:47:53 +00:00
end
# blog_slug is used to identify which blog this import is from
def to_rails(blog_slug)
2014-03-06 04:27:57 +00:00
user = ::User.find_by_wp_username(creator)
2014-03-06 04:27:57 +00:00
if user.nil?
raise "User with wp_username #{creator} not found"
end
post = ::Post.create({
:wp_post_id => post_id, :slug => post_name,
2014-03-06 04:27:57 +00:00
:user_id => user.id, :title => title,
:created_at => post_date,
:published_at => publish_date,
:wp_link => link,
:wp_blog => blog_slug,
:translations_attributes => { "0" => {
:locale => "en",
:title => title,
:body => content_formatted,
# merge the translation's category list with the wordpress post's
:category_list => categories.collect(&:name) | tags.collect(&:name)
}}
})
2014-03-06 04:27:57 +00:00
if post.errors.blank?
puts "Post #{post_name} imported."
2014-03-06 04:27:57 +00:00
return post.reload
else
puts post.inspect
raise post.errors.full_messages.to_s
end
end
2014-03-04 22:47:53 +00:00
end
end