2011-06-02 12:41:31 +00:00
|
|
|
module Refinery
|
|
|
|
module WordPress
|
|
|
|
class Post < Page
|
|
|
|
def tags
|
2011-06-02 14:43:14 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
node.xpath(path).collect do |tag_node|
|
2011-06-02 12:41:31 +00:00
|
|
|
Tag.new(tag_node.text)
|
|
|
|
end
|
2011-06-01 19:11:57 +00:00
|
|
|
end
|
|
|
|
|
2011-06-02 12:41:31 +00:00
|
|
|
def tag_list
|
|
|
|
tags.collect(&:name).join(',')
|
2011-06-01 19:11:57 +00:00
|
|
|
end
|
|
|
|
|
2011-06-02 12:41:31 +00:00
|
|
|
def categories
|
|
|
|
node.xpath("category[@domain='category']").collect do |cat|
|
|
|
|
Category.new(cat.text)
|
|
|
|
end
|
2011-06-01 19:11:57 +00:00
|
|
|
end
|
|
|
|
|
2011-06-02 12:41:31 +00:00
|
|
|
def comments
|
|
|
|
node.xpath("wp:comment").collect do |comment_node|
|
|
|
|
Comment.new(comment_node)
|
|
|
|
end
|
|
|
|
end
|
2011-06-01 19:11:57 +00:00
|
|
|
|
2011-06-02 12:41:31 +00:00
|
|
|
def to_refinery
|
2011-06-02 14:43:14 +00:00
|
|
|
user = ::User.find_by_username(creator) || ::User.first
|
2011-06-02 12:41:31 +00:00
|
|
|
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
|
|
|
|
unless user
|
2011-06-02 21:26:18 +00:00
|
|
|
|
|
|
|
begin
|
2011-06-03 09:35:11 +00:00
|
|
|
post = ::BlogPost.new :title => title, :body => content_formatted,
|
|
|
|
:draft => draft?, :published_at => post_date, :created_at => post_date,
|
|
|
|
:author => user, :tag_list => tag_list
|
|
|
|
post.save!
|
2011-06-03 09:02:25 +00:00
|
|
|
|
2011-06-03 09:35:11 +00:00
|
|
|
::BlogPost.transaction do
|
|
|
|
categories.each do |category|
|
|
|
|
post.categories << category.to_refinery
|
|
|
|
end
|
2011-06-02 23:05:16 +00:00
|
|
|
|
2011-06-03 09:35:11 +00:00
|
|
|
comments.each do |comment|
|
|
|
|
comment = comment.to_refinery
|
|
|
|
comment.post = post
|
|
|
|
comment.save
|
2011-06-02 23:05:16 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-03 09:02:25 +00:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
# if the title has already been taken (WP allows duplicates here,
|
|
|
|
# refinery doesn't) append the post_id to it, making it unique
|
|
|
|
post.title = "#{title}-#{post_id}"
|
|
|
|
post.save
|
2011-06-02 21:26:18 +00:00
|
|
|
end
|
2011-06-01 19:11:57 +00:00
|
|
|
|
2011-06-02 12:41:31 +00:00
|
|
|
post
|
|
|
|
end
|
2011-06-03 16:50:27 +00:00
|
|
|
|
|
|
|
def self.create_blog_page_if_necessary
|
|
|
|
# refinerycms wants a page at /blog, so let's make sure there is one
|
|
|
|
# taken from the original db seeds from refinery-blog
|
|
|
|
unless ::Page.where("link_url = ?", '/blog').exists?
|
|
|
|
page = ::Page.create(
|
|
|
|
:title => "Blog",
|
|
|
|
:link_url => "/blog",
|
|
|
|
:deletable => false,
|
|
|
|
:position => ((::Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
|
|
|
|
:menu_match => "^/blogs?(\/|\/.+?|)$"
|
|
|
|
)
|
|
|
|
|
|
|
|
::Page.default_parts.each do |default_page_part|
|
|
|
|
page.parts.create(:title => default_page_part, :body => nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-01 19:11:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|