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".
So your inner site links will point to the old WP url.
== TODO
- attachments
- categories
- tags
== Prerequisites

View File

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

View File

@ -9,6 +9,11 @@ module WordPressImport
unless File.file?(file_name) && File.readable?(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)
end

View File

@ -38,7 +38,15 @@ module WordPressImport
end
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
def post_id
@ -67,7 +75,7 @@ module WordPressImport
end
#NEED:
# dc:creator -> "user_id"
# creator -> "user_id"
# wp:post_name -> "slug"
# pubDate -> "published_at"
#OK:
@ -75,12 +83,11 @@ module WordPressImport
# content:encoded -> "body"
# wp:post_date_gmt -> "created_at"
def to_refinery
def to_rails
# :user_id => creator
page = ::Page.create!(:id => post_id, :title => title,
:created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted)
page
:created_at => post_date, :slug => post_name,
:published_at => publish_date, :body => content_formatted)
end
private
@ -92,7 +99,7 @@ module WordPressImport
text = ''.html_safe if text.nil?
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.insert 0, start_tag

View File

@ -29,53 +29,40 @@ module WordPressImport
end
end
def to_refinery
user = ::User.find_by_username(creator) || ::User.first
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
unless user
def to_rails
begin
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!
user = ::User.find_by_wp_username(creator)
::BlogPost.transaction do
categories.each do |category|
post.categories << category.to_refinery
end
comments.each do |comment|
comment = comment.to_refinery
comment.post = post
comment.save
end
end
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
if user.nil?
raise "User with wp_username #{creator} not found"
end
post
end
post = ::Post.find_or_initialize_by(:id => post_id, :slug => post_name)
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?(\/|\/.+?|)$"
)
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
::Page.default_parts.each do |default_page_part|
page.parts.create(:title => default_page_part, :body => nil)
end
if post.translations.blank?
translation = post.translations.build
else
translation = post.translations.first
end
translation.locale = "en"
translation.title = title
translation.body = content_formatted
translation.save
post.save
if post.errors.blank?
return post.reload
else
puts post.inspect
raise post.errors.full_messages.to_s
end
end