Refining post/author/dump behavior

This commit is contained in:
Will Bradley 2014-03-05 21:27:57 -07:00
parent 60ce62ad1b
commit bd4cccd91a
5 changed files with 64 additions and 52 deletions

View File

@ -11,6 +11,10 @@ If your site (blog) structure uses new urls, the links WILL break! For example,
the popular WP blog url structure "YYYY-MM/slug", be warned that Refinery just uses "blog/slug". the popular WP blog url structure "YYYY-MM/slug", be warned that Refinery just uses "blog/slug".
So your inner site links will point to the old WP url. So your inner site links will point to the old WP url.
== TODO
- attachments
- categories
- tags
== Prerequisites == Prerequisites

View File

@ -6,6 +6,12 @@ module WordPressImport
@author_node = author_node @author_node = author_node
end end
def name
name = author_node.xpath("wp:author_display_name").text
name = author_node.xpath("wp:author_first_name").text + " " + author_node.xpath("wp:author_first_name").text if name.blank?
name
end
def login def login
author_node.xpath("wp:author_login").text author_node.xpath("wp:author_login").text
end end
@ -22,13 +28,16 @@ module WordPressImport
"WordPress::Author: #{login} <#{email}>" "WordPress::Author: #{login} <#{email}>"
end end
def to_refinery def to_rails
user = User.find_or_initialize_by_username_and_email(login, email) user = ::User.find_or_initialize_by_email(email)
user.wp_username = login
unless user.persisted? unless user.persisted?
user.name = name
user.password = 'password' user.password = 'password'
user.password_confirmation = 'password' user.password_confirmation = 'password'
user.save
end end
user.save
user user
end end
end end

View File

@ -9,6 +9,11 @@ module WordPressImport
unless File.file?(file_name) && File.readable?(file_name) unless File.file?(file_name) && File.readable?(file_name)
file = File.open(file_name) file = File.open(file_name)
if file.size >= 10485760 # 10MB
puts "WARNING: LibXML by default supports 10MB max file size. On some systems your file will be silently truncated; on others, an error will be raised. Consider splitting your file into smaller chunks, or double-checking the import results."
end
@doc = Nokogiri::XML(file) @doc = Nokogiri::XML(file)
end end

View File

@ -38,7 +38,15 @@ module WordPressImport
end end
def post_date def post_date
DateTime.parse node.xpath("wp:post_date").text Time.parse node.xpath("wp:post_date").text
end
def publish_date
Time.parse node.xpath("pubDate").text
end
def post_name
node.xpath("wp:post_name").text
end end
def post_id def post_id
@ -67,7 +75,7 @@ module WordPressImport
end end
#NEED: #NEED:
# dc:creator -> "user_id" # creator -> "user_id"
# wp:post_name -> "slug" # wp:post_name -> "slug"
# pubDate -> "published_at" # pubDate -> "published_at"
#OK: #OK:
@ -75,12 +83,11 @@ module WordPressImport
# content:encoded -> "body" # content:encoded -> "body"
# wp:post_date_gmt -> "created_at" # wp:post_date_gmt -> "created_at"
def to_refinery def to_rails
# :user_id => creator
page = ::Page.create!(:id => post_id, :title => title, page = ::Page.create!(:id => post_id, :title => title,
:created_at => post_date, :draft => draft?) :created_at => post_date, :slug => post_name,
:published_at => publish_date, :body => content_formatted)
page.parts.create(:title => 'Body', :body => content_formatted)
page
end end
private private
@ -92,7 +99,7 @@ module WordPressImport
text = ''.html_safe if text.nil? text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true) start_tag = tag('p', html_options, true)
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\r?\n/, "<br/>\n") # \r\n and \n -> line break
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.insert 0, start_tag text.insert 0, start_tag

View File

@ -29,53 +29,40 @@ module WordPressImport
end end
end end
def to_refinery def to_rails
user = ::User.find_by_username(creator) || ::User.first
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
unless user
begin user = ::User.find_by_wp_username(creator)
post = ::BlogPost.new :title => title, :body => content_formatted,
:draft => draft?, :published_at => post_date, :created_at => post_date,
:user_id => user.id, :tag_list => tag_list
post.save!
::BlogPost.transaction do if user.nil?
categories.each do |category| raise "User with wp_username #{creator} not found"
post.categories << category.to_refinery
end end
comments.each do |comment| post = ::Post.find_or_initialize_by(:id => post_id, :slug => post_name)
comment = comment.to_refinery
comment.post = post post.assign_attributes(
comment.save :user_id => user.id, :title => title,
:created_at => post_date,
:published_at => publish_date)
# :body => content_formatted taken care of by translation below
if post.translations.blank?
translation = post.translations.build
else
translation = post.translations.first
end end
end
rescue ActiveRecord::RecordInvalid translation.locale = "en"
# if the title has already been taken (WP allows duplicates here, translation.title = title
# refinery doesn't) append the post_id to it, making it unique translation.body = content_formatted
post.title = "#{title}-#{post_id}" translation.save
post.save post.save
end
post if post.errors.blank?
end return post.reload
else
def self.create_blog_page_if_necessary puts post.inspect
# refinerycms wants a page at /blog, so let's make sure there is one raise post.errors.full_messages.to_s
# 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
end end