Updating with more rake tweaks and some post customizations

This commit is contained in:
Will Bradley 2014-03-11 17:57:28 -07:00
parent 39914c83c9
commit 5d96f3a336
5 changed files with 35 additions and 47 deletions

View File

@ -1,8 +1,10 @@
= Wordpress-import
Fork of Marc Remolt's Refinerycms-wordpress-import ( https://github.com/mremolt/refinerycms-wordpress-import )
This little project is an importer for WordPress XML dumps into Rails.
This little project is an importer for WordPress XML dumps into Rails
It's been somewhat customized for one particular project; you probably want to fork this and modify it to fit your app's schema.
It's a fork of Marc Remolt's Refinerycms-wordpress-import ( https://github.com/mremolt/refinerycms-wordpress-import )
You can find the source code on github: https://github.com/zyphlar/wordpress-import
@ -11,11 +13,6 @@ 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
- tags
- attachments
- what about wordpress suffixes like -300x290 ?
== Prerequisites
TODO
@ -29,7 +26,7 @@ Just add the gem to your projects Gemfile:
Or if you want to stay on the bleeding edge:
gem 'wordpress-import', :git => 'git://github.com/zyphlarz/wordpress-import.git'
gem 'wordpress-import', :git => 'git://github.com/zyphlar/wordpress-import.git'
and run

View File

@ -13,9 +13,9 @@ namespace :wordpress do
end
desc "import blog data from a WordPressImport XML dump"
task :import_blog, :file_name do |task, params|
task :import_blog, :file_name, :blog_slug do |task, params|
Rake::Task["environment"].invoke
p "Loading XML from #{params[:file_name]} ..."
p "Loading XML from #{params[:file_name]} (using blog #{params[:blog_slug]}) ..."
dump = WordPressImport::Dump.new(params[:file_name])
p "Importing #{dump.authors.count} authors ..."
@ -31,14 +31,14 @@ namespace :wordpress do
p "(export ONLY_PUBLISHED=true to import only published posts)"
end
dump.posts(only_published).each(&:to_rails)
dump.posts(only_published).each{|p| p.to_rails(params[:blog_slug]) }
end
desc "reset blog tables and then import blog data from a WordPressImport XML dump"
task :reset_and_import_blog, :file_name do |task, params|
task :reset_and_import_blog, :file_name, :blog_slug do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:reset_blog"].invoke
Rake::Task["wordpress:import_blog"].invoke(params[:file_name])
Rake::Task["wordpress:import_blog"].invoke(params[:file_name], params[:blog_slug])
end
@ -129,19 +129,12 @@ namespace :wordpress do
end
desc "reset and import all data (see the other tasks)"
task :full_import, :file_name do |task, params|
task :full_import, :file_name, :blog_slug do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:reset_and_import_blog"].invoke(params[:file_name])
Rake::Task["wordpress:reset_and_import_blog"].invoke(params[:file_name],params[:blog_slug])
#Rake::Task["wordpress:reset_and_import_pages"].invoke(params[:file_name])
#Rake::Task["wordpress:reset_import_and_replace_media"].invoke(params[:file_name])
Rake::Task["wordpress:import_and_replace_media"].invoke(params[:file_name])
end
desc "Local First master import (no resets)"
task :lfa_import, :file_name do |task, params|
Rake::Task["environment"].invoke
Rake::Task["wordpress:import_blog"].invoke(params[:file_name])
Rake::Task["wordpress:import_and_replace_media"].invoke(params[:file_name])
end
end

View File

@ -38,6 +38,9 @@ module WordPressImport
user.password_confirmation = 'password'
end
user.save
puts "User #{login} imported."
user
end
end

View File

@ -13,6 +13,10 @@ module WordPressImport
"WordPress::Page(#{post_id}): #{title}"
end
def link
node.xpath("link").text
end
def title
node.xpath("title").text
end

View File

@ -29,7 +29,8 @@ module WordPressImport
end
end
def to_rails
# blog_slug is used to identify which blog this import is from
def to_rails(blog_slug)
user = ::User.find_by_wp_username(creator)
@ -37,34 +38,24 @@ module WordPressImport
raise "User with wp_username #{creator} not found"
end
post = ::Post.find_or_initialize_by(:id => post_id, :slug => post_name)
post.assign_attributes(
post = ::Post.create({
:wp_post_id => post_id, :slug => post_name,
: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
translation.locale = "en"
translation.title = title
translation.body = content_formatted
: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
translation.category_list |= categories.collect(&:name)
# and tags
translation.category_list |= tags.collect(&:name)
translation.save
post.save
:category_list => categories.collect(&:name) | tags.collect(&:name)
}}
})
if post.errors.blank?
puts "Post #{post_name} imported."
return post.reload
else
puts post.inspect