Conversion in a gem finished
* specs are working again * added railitie * added missing migration (acts-as-taggable) * cleanups
This commit is contained in:
7
lib/refinerycms-wordpress-import.rb
Normal file
7
lib/refinerycms-wordpress-import.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Refinery
|
||||
module WordPress
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
require 'wordpress'
|
||||
@@ -1,8 +1,7 @@
|
||||
require 'refinerycms'
|
||||
require 'refinerycms-blog'
|
||||
|
||||
module Refinery
|
||||
module WordPress
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
require 'wordpress'
|
||||
|
||||
@@ -3,56 +3,36 @@ require 'wordpress'
|
||||
|
||||
namespace :wordpress do
|
||||
desc "Reset the blog relevant tables for a clean import"
|
||||
task :reset do
|
||||
task :reset_blog do
|
||||
Rake::Task["environment"].invoke
|
||||
|
||||
%w(blog_categories blog_posts).each do |table_name|
|
||||
%w(taggings tags blog_comments blog_categories blog_posts).each do |table_name|
|
||||
p "Truncating #{table_name} ..."
|
||||
ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{table_name}"
|
||||
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
|
||||
end
|
||||
end
|
||||
|
||||
desc "Import data from a WordPress XML dump"
|
||||
|
||||
desc "import blog data from a Refinery::WordPress XML dump"
|
||||
task :import_blog, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
dump = Refinery::WordPress::Dump.new(params[:file_name])
|
||||
|
||||
dump.authors.each(&:to_refinery)
|
||||
dump.posts.each(&:to_refinery)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
desc "Import data from a Refinery::WordPress XML dump"
|
||||
task :import, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
|
||||
file_name = File.absolute_path(params[:file_name])
|
||||
unless File.file?(file_name) && File.readable?(file_name)
|
||||
raise "Given file '#{file_name}' no file or not readable."
|
||||
end
|
||||
|
||||
file = File.open(file_name)
|
||||
doc = Nokogiri::XML(file)
|
||||
file.close
|
||||
|
||||
p "Importing blog categories ..."
|
||||
doc.xpath("//wp:category/wp:cat_name").each do |category|
|
||||
BlogCategory.exists?(:title => category.text) || BlogCategory.create!(:title => category.text)
|
||||
end
|
||||
|
||||
doc.xpath("//wp:tag/wp:tag_slug").each do |tag|
|
||||
p tag.text
|
||||
end
|
||||
|
||||
doc.xpath("//item[wp:post_type = 'page']").each do |post|
|
||||
title = post.xpath("title").text
|
||||
body = post.xpath("content:encoded").text
|
||||
author = post.xpath("dc:creator").text
|
||||
published_at = DateTime.parse(post.xpath("wp:post_date").text)
|
||||
|
||||
tags = post.xpath("category[@domain='tag'][not(@nicename)]").collect {|tag| tag.text }
|
||||
tag_list = tags.join(', ')
|
||||
|
||||
categories = post.xpath("category[not(@*)]").collect {|cat| cat.text }
|
||||
|
||||
|
||||
p '*' * 100
|
||||
p title
|
||||
p author
|
||||
p published_at
|
||||
p tag_list
|
||||
p categories
|
||||
end
|
||||
end
|
||||
|
||||
desc "New import (testing)"
|
||||
@@ -64,7 +44,7 @@ namespace :wordpress do
|
||||
raise "Given file '#{file_name}' no file or not readable."
|
||||
end
|
||||
|
||||
dump = WordPress::Dump.new(file_name)
|
||||
dump = Refinery::WordPress::Dump.new(file_name)
|
||||
p dump.authors
|
||||
p dump.pages
|
||||
dump.posts.each do |post|
|
||||
@@ -77,7 +57,7 @@ namespace :wordpress do
|
||||
end
|
||||
|
||||
|
||||
desc "Import data from a WordPress XML dump into a clean database (reset first)"
|
||||
desc "Import data from a Refinery::WordPress XML dump into a clean database (reset first)"
|
||||
task :import_clean, :file_name do |task, params|
|
||||
Rake::Task["wordpress:reset"].invoke
|
||||
Rake::Task["wordpress:import"].invoke(params[:file_name])
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
module WordPress
|
||||
|
||||
module Refinery
|
||||
module WordPress
|
||||
end
|
||||
end
|
||||
|
||||
require 'nokogiri'
|
||||
require 'wordpress/author'
|
||||
require 'wordpress/tag'
|
||||
require 'wordpress/category'
|
||||
@@ -9,3 +11,5 @@ require 'wordpress/page'
|
||||
require 'wordpress/post'
|
||||
require 'wordpress/comment'
|
||||
require 'wordpress/dump'
|
||||
|
||||
require "wordpress/railtie"
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
module WordPress
|
||||
class Author
|
||||
attr_reader :author_node
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Author
|
||||
attr_reader :author_node
|
||||
|
||||
def initialize(author_node)
|
||||
@author_node = author_node
|
||||
end
|
||||
|
||||
def login
|
||||
author_node.xpath("wp:author_login").text
|
||||
end
|
||||
|
||||
def email
|
||||
author_node.xpath("wp:author_email").text
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
login == other.login
|
||||
end
|
||||
|
||||
def inspect
|
||||
"WordPress::Author: #{login} <#{email}>"
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
user = User.find_or_initialize_by_username_and_email(login, email)
|
||||
unless user.persisted?
|
||||
user.password = 'password'
|
||||
user.password_confirmation = 'password'
|
||||
user.save
|
||||
def initialize(author_node)
|
||||
@author_node = author_node
|
||||
end
|
||||
|
||||
def login
|
||||
author_node.xpath("wp:author_login").text
|
||||
end
|
||||
|
||||
def email
|
||||
author_node.xpath("wp:author_email").text
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
login == other.login
|
||||
end
|
||||
|
||||
def inspect
|
||||
"WordPress::Author: #{login} <#{email}>"
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
user = User.find_or_initialize_by_username_and_email(login, email)
|
||||
unless user.persisted?
|
||||
user.password = 'password'
|
||||
user.password_confirmation = 'password'
|
||||
user.save
|
||||
end
|
||||
user
|
||||
end
|
||||
user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
module WordPress
|
||||
class Category
|
||||
attr_accessor :name
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Category
|
||||
attr_accessor :name
|
||||
|
||||
def initialize(text)
|
||||
@name = text
|
||||
end
|
||||
def initialize(text)
|
||||
@name = text
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
name == other.name
|
||||
end
|
||||
def ==(other)
|
||||
name == other.name
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
BlogCategory.find_or_create_by_title(name)
|
||||
def to_refinery
|
||||
BlogCategory.find_or_create_by_title(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,46 +1,48 @@
|
||||
module WordPress
|
||||
class Comment
|
||||
attr_reader :node
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Comment
|
||||
attr_reader :node
|
||||
|
||||
def initialize(node)
|
||||
@node = node
|
||||
end
|
||||
def initialize(node)
|
||||
@node = node
|
||||
end
|
||||
|
||||
def author
|
||||
node.xpath('wp:comment_author').text
|
||||
end
|
||||
def author
|
||||
node.xpath('wp:comment_author').text
|
||||
end
|
||||
|
||||
def email
|
||||
node.xpath('wp:comment_author_email').text
|
||||
end
|
||||
def email
|
||||
node.xpath('wp:comment_author_email').text
|
||||
end
|
||||
|
||||
def url
|
||||
node.xpath('wp:comment_author_url').text
|
||||
end
|
||||
def url
|
||||
node.xpath('wp:comment_author_url').text
|
||||
end
|
||||
|
||||
def date
|
||||
DateTime.parse node.xpath("wp:comment_date").text
|
||||
end
|
||||
def date
|
||||
DateTime.parse node.xpath("wp:comment_date").text
|
||||
end
|
||||
|
||||
def content
|
||||
node.xpath('wp:comment_content').text
|
||||
end
|
||||
def content
|
||||
node.xpath('wp:comment_content').text
|
||||
end
|
||||
|
||||
def approved?
|
||||
node.xpath('wp:comment_approved').text.to_i == 1
|
||||
end
|
||||
def approved?
|
||||
node.xpath('wp:comment_approved').text.to_i == 1
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
(email == other.email) && (date == other.date) && (content == other.content)
|
||||
end
|
||||
def ==(other)
|
||||
(email == other.email) && (date == other.date) && (content == other.content)
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
comment = BlogComment.new :name => author, :email => email
|
||||
def to_refinery
|
||||
comment = BlogComment.new :name => author, :email => email
|
||||
|
||||
comment.body = content
|
||||
comment.created_at = date
|
||||
comment.state = approved? ? 'approved' : 'rejected'
|
||||
comment
|
||||
comment.body = content
|
||||
comment.created_at = date
|
||||
comment.state = approved? ? 'approved' : 'rejected'
|
||||
comment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,41 +1,46 @@
|
||||
require 'nokogiri'
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Dump
|
||||
attr_reader :doc
|
||||
|
||||
module WordPress
|
||||
class Dump
|
||||
attr_reader :doc
|
||||
def initialize(file_name)
|
||||
file_name = File.absolute_path(file_name)
|
||||
|
||||
def initialize(file_name)
|
||||
file = File.open(file_name)
|
||||
@doc = Nokogiri::XML(file)
|
||||
end
|
||||
|
||||
def authors
|
||||
doc.xpath("//wp:author").collect do |author|
|
||||
WordPress::Author.new(author)
|
||||
raise "Given file '#{file_name}' no file or not readable." \
|
||||
unless File.file?(file_name) && File.readable?(file_name)
|
||||
|
||||
file = File.open(file_name)
|
||||
@doc = Nokogiri::XML(file)
|
||||
end
|
||||
end
|
||||
|
||||
def pages
|
||||
doc.xpath("//item[wp:post_type = 'page']").collect do |page|
|
||||
WordPress::Page.new(page)
|
||||
def authors
|
||||
doc.xpath("//wp:author").collect do |author|
|
||||
Author.new(author)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def posts
|
||||
doc.xpath("//item[wp:post_type = 'post']").collect do |post|
|
||||
WordPress::Post.new(post)
|
||||
def pages
|
||||
doc.xpath("//item[wp:post_type = 'page']").collect do |page|
|
||||
Page.new(page)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def tags
|
||||
doc.xpath("//wp:tag/wp:tag_slug").collect do |tag|
|
||||
WordPress::Tag.new(tag.text)
|
||||
def posts
|
||||
doc.xpath("//item[wp:post_type = 'post']").collect do |post|
|
||||
Post.new(post)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def categories
|
||||
doc.xpath("//wp:category/wp:cat_name").collect do |category|
|
||||
WordPress::Category.new(category.text)
|
||||
def tags
|
||||
doc.xpath("//wp:tag/wp:tag_slug").collect do |tag|
|
||||
Tag.new(tag.text)
|
||||
end
|
||||
end
|
||||
|
||||
def categories
|
||||
doc.xpath("//wp:category/wp:cat_name").collect do |category|
|
||||
Category.new(category.text)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,57 +1,59 @@
|
||||
module WordPress
|
||||
class Page
|
||||
attr_reader :node
|
||||
|
||||
def initialize(node)
|
||||
@node = node
|
||||
end
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Page
|
||||
attr_reader :node
|
||||
|
||||
def inspect
|
||||
"WordPress::Page(#{post_id}): #{title}"
|
||||
end
|
||||
def initialize(node)
|
||||
@node = node
|
||||
end
|
||||
|
||||
def title
|
||||
node.xpath("title").text
|
||||
end
|
||||
def inspect
|
||||
"WordPress::Page(#{post_id}): #{title}"
|
||||
end
|
||||
|
||||
def content
|
||||
node.xpath("content:encoded").text
|
||||
end
|
||||
def title
|
||||
node.xpath("title").text
|
||||
end
|
||||
|
||||
def creator
|
||||
node.xpath("dc:creator").text
|
||||
end
|
||||
def content
|
||||
node.xpath("content:encoded").text
|
||||
end
|
||||
|
||||
def post_date
|
||||
DateTime.parse node.xpath("wp:post_date").text
|
||||
end
|
||||
def creator
|
||||
node.xpath("dc:creator").text
|
||||
end
|
||||
|
||||
def post_id
|
||||
node.xpath("wp:post_id").text.to_i
|
||||
end
|
||||
def post_date
|
||||
DateTime.parse node.xpath("wp:post_date").text
|
||||
end
|
||||
|
||||
def parent_id
|
||||
node.xpath("wp:post_parent").text.to_i
|
||||
end
|
||||
def post_id
|
||||
node.xpath("wp:post_id").text.to_i
|
||||
end
|
||||
|
||||
def status
|
||||
node.xpath("wp:status").text
|
||||
end
|
||||
def parent_id
|
||||
node.xpath("wp:post_parent").text.to_i
|
||||
end
|
||||
|
||||
def draft?
|
||||
status != 'publish'
|
||||
end
|
||||
def status
|
||||
node.xpath("wp:status").text
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
post_id == other.post_id
|
||||
end
|
||||
def draft?
|
||||
status != 'publish'
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
page = ::Page.create!(:title => title, :created_at => post_date,
|
||||
:draft => draft?, :parent_id => parent_id)
|
||||
def ==(other)
|
||||
post_id == other.post_id
|
||||
end
|
||||
|
||||
page.parts.create(:title => 'Body', :body => content)
|
||||
page
|
||||
def to_refinery
|
||||
page = ::Page.create!(:title => title, :created_at => post_date,
|
||||
:draft => draft?, :parent_id => parent_id)
|
||||
|
||||
page.parts.create(:title => 'Body', :body => content)
|
||||
page
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,49 +1,54 @@
|
||||
module WordPress
|
||||
class Post < Page
|
||||
def tags
|
||||
node.xpath("category[@domain='post_tag']").collect do |tag_node|
|
||||
WordPress::Tag.new(tag_node.text)
|
||||
end
|
||||
end
|
||||
|
||||
def tag_list
|
||||
tags.collect(&:name).join(',')
|
||||
end
|
||||
|
||||
def categories
|
||||
node.xpath("category[@domain='category']").collect do |cat|
|
||||
WordPress::Category.new(cat.text)
|
||||
end
|
||||
end
|
||||
|
||||
def comments
|
||||
node.xpath("wp:comment").collect do |comment_node|
|
||||
WordPress::Comment.new(comment_node)
|
||||
end
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
user = User.find_by_username creator
|
||||
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
|
||||
unless user
|
||||
|
||||
post = BlogPost.create! :title => title, :body => content, :draft => draft?,
|
||||
:published_at => post_date, :created_at => post_date, :author => user,
|
||||
:tag_list => tag_list
|
||||
|
||||
BlogPost.transaction do
|
||||
categories.each do |category|
|
||||
post.categories << category.to_refinery
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Post < Page
|
||||
def tags
|
||||
node.xpath("category[@domain='post_tag']").collect do |tag_node|
|
||||
Tag.new(tag_node.text)
|
||||
end
|
||||
end
|
||||
|
||||
def tag_list
|
||||
tags.collect(&:name).join(',')
|
||||
end
|
||||
|
||||
def categories
|
||||
node.xpath("category[@domain='category']").collect do |cat|
|
||||
Category.new(cat.text)
|
||||
end
|
||||
end
|
||||
|
||||
def comments
|
||||
node.xpath("wp:comment").collect do |comment_node|
|
||||
Comment.new(comment_node)
|
||||
end
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
user = ::User.find_by_username creator
|
||||
raise "Referenced User doesn't exist! Make sure the authors are imported first." \
|
||||
unless user
|
||||
|
||||
post = BlogPost.new
|
||||
|
||||
comments.each do |comment|
|
||||
comment = comment.to_refinery
|
||||
comment.post = post
|
||||
comment.save
|
||||
end
|
||||
end
|
||||
|
||||
post
|
||||
post = ::BlogPost.create! :title => title, :body => content, :draft => draft?,
|
||||
:published_at => post_date, :created_at => post_date, :author => user,
|
||||
:tag_list => tag_list
|
||||
|
||||
::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
|
||||
|
||||
post
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
10
lib/wordpress/railtie.rb
Normal file
10
lib/wordpress/railtie.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Railtie < Rails::Railtie
|
||||
rake_tasks do
|
||||
load "tasks/wordpress.rake"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
module WordPress
|
||||
class Tag
|
||||
attr_accessor :name
|
||||
module Refinery
|
||||
module WordPress
|
||||
class Tag
|
||||
attr_accessor :name
|
||||
|
||||
def initialize(text)
|
||||
@name = text
|
||||
end
|
||||
def initialize(text)
|
||||
@name = text
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
name == other.name
|
||||
end
|
||||
def ==(other)
|
||||
name == other.name
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
::ActsAsTaggableOn::Tag.find_or_create_by_name(name)
|
||||
end
|
||||
|
||||
def to_refinery
|
||||
ActsAsTaggableOn::Tag.find_or_create_by_name(name)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user