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 = 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 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". 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
- tags
- attachments
- what about wordpress suffixes like -300x290 ?
== Prerequisites == Prerequisites
TODO TODO
@ -29,7 +26,7 @@ Just add the gem to your projects Gemfile:
Or if you want to stay on the bleeding edge: 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 and run

View File

@ -13,9 +13,9 @@ namespace :wordpress do
end end
desc "import blog data from a WordPressImport XML dump" 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 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]) dump = WordPressImport::Dump.new(params[:file_name])
p "Importing #{dump.authors.count} authors ..." p "Importing #{dump.authors.count} authors ..."
@ -31,14 +31,14 @@ namespace :wordpress do
p "(export ONLY_PUBLISHED=true to import only published posts)" p "(export ONLY_PUBLISHED=true to import only published posts)"
end end
dump.posts(only_published).each(&:to_rails) dump.posts(only_published).each{|p| p.to_rails(params[:blog_slug]) }
end end
desc "reset blog tables and then import blog data from a WordPressImport XML dump" 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["environment"].invoke
Rake::Task["wordpress:reset_blog"].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 end
@ -129,19 +129,12 @@ namespace :wordpress do
end end
desc "reset and import all data (see the other tasks)" 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["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_and_import_pages"].invoke(params[:file_name])
#Rake::Task["wordpress:reset_import_and_replace_media"].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]) Rake::Task["wordpress:import_and_replace_media"].invoke(params[:file_name])
end 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 end

View File

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

View File

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

View File

@ -29,7 +29,8 @@ module WordPressImport
end end
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) user = ::User.find_by_wp_username(creator)
@ -37,34 +38,24 @@ module WordPressImport
raise "User with wp_username #{creator} not found" raise "User with wp_username #{creator} not found"
end end
post = ::Post.find_or_initialize_by(:id => post_id, :slug => post_name) post = ::Post.create({
:wp_post_id => post_id, :slug => post_name,
post.assign_attributes(
:user_id => user.id, :title => title, :user_id => user.id, :title => title,
:created_at => post_date, :created_at => post_date,
:published_at => publish_date) :published_at => publish_date,
# :body => content_formatted taken care of by translation below :wp_link => link,
:wp_blog => blog_slug,
if post.translations.blank? :translations_attributes => { "0" => {
translation = post.translations.build :locale => "en",
else :title => title,
translation = post.translations.first :body => content_formatted,
end # merge the translation's category list with the wordpress post's
:category_list => categories.collect(&:name) | tags.collect(&:name)
translation.locale = "en" }}
translation.title = title })
translation.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
if post.errors.blank? if post.errors.blank?
puts "Post #{post_name} imported."
return post.reload return post.reload
else else
puts post.inspect puts post.inspect