From 5d96f3a33627ec14f0bac804fc2d70c47612d363 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Tue, 11 Mar 2014 17:57:28 -0700 Subject: [PATCH] Updating with more rake tweaks and some post customizations --- README.rdoc | 13 +++++-------- lib/tasks/wordpress.rake | 21 +++++++------------- lib/wordpress/author.rb | 3 +++ lib/wordpress/page.rb | 4 ++++ lib/wordpress/post.rb | 41 ++++++++++++++++------------------------ 5 files changed, 35 insertions(+), 47 deletions(-) diff --git a/README.rdoc b/README.rdoc index cd1c2bb..989d1a0 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 diff --git a/lib/tasks/wordpress.rake b/lib/tasks/wordpress.rake index 6976945..271198a 100644 --- a/lib/tasks/wordpress.rake +++ b/lib/tasks/wordpress.rake @@ -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 diff --git a/lib/wordpress/author.rb b/lib/wordpress/author.rb index c88a2a6..13b5dd7 100644 --- a/lib/wordpress/author.rb +++ b/lib/wordpress/author.rb @@ -38,6 +38,9 @@ module WordPressImport user.password_confirmation = 'password' end user.save + + puts "User #{login} imported." + user end end diff --git a/lib/wordpress/page.rb b/lib/wordpress/page.rb index 32f24aa..55c6661 100644 --- a/lib/wordpress/page.rb +++ b/lib/wordpress/page.rb @@ -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 diff --git a/lib/wordpress/post.rb b/lib/wordpress/post.rb index f751a5f..83dc7ba 100644 --- a/lib/wordpress/post.rb +++ b/lib/wordpress/post.rb @@ -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 - - # 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 + :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 + :category_list => categories.collect(&:name) | tags.collect(&:name) + }} + }) if post.errors.blank? + puts "Post #{post_name} imported." return post.reload else puts post.inspect