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

77 lines
1.8 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
2014-03-06 04:27:57 +00:00
def to_rails
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
2014-03-06 04:27:57 +00:00
post = ::Post.find_or_initialize_by(:id => post_id, :slug => post_name)
post.assign_attributes(
:user_id => user.id, :title => title,
:created_at => post_date,
:published_at => publish_date)
# :body => content_formatted taken care of by translation below
2014-03-04 22:47:53 +00:00
2014-03-06 04:27:57 +00:00
if post.translations.blank?
translation = post.translations.build
else
translation = post.translations.first
end
translation.locale = "en"
translation.title = title
translation.body = content_formatted
2014-03-11 22:17:46 +00:00
# merge the translation's category list with the wordpress post's
translation.category_list |= categories.collect(&:name)
# and tags
translation.category_list |= tags.collect(&:name)
2014-03-06 04:27:57 +00:00
translation.save
post.save
2014-03-06 04:27:57 +00:00
if post.errors.blank?
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