From e1c60163e483773dc6117e7438d578deb5cbb241 Mon Sep 17 00:00:00 2001
From: Marc Remolt
Date: Fri, 3 Jun 2011 18:50:27 +0200
Subject: [PATCH] Started on cms page import
* created rake tasks
* still needed: cleanups, more tests and docs
---
lib/tasks/wordpress.rake | 47 ++++++++++++++++++++++++++++++++++++++--
lib/wordpress/dump.rb | 7 ++++--
lib/wordpress/page.rb | 9 ++++----
lib/wordpress/post.rb | 19 ++++++++++++++++
4 files changed, 73 insertions(+), 9 deletions(-)
diff --git a/lib/tasks/wordpress.rake b/lib/tasks/wordpress.rake
index df7c7a4..f31f120 100644
--- a/lib/tasks/wordpress.rake
+++ b/lib/tasks/wordpress.rake
@@ -5,7 +5,8 @@ namespace :wordpress do
task :reset_blog do
Rake::Task["environment"].invoke
- %w(taggings tags blog_comments blog_categories blog_categories_blog_posts blog_posts).each do |table_name|
+ %w(taggings tags blog_comments blog_categories blog_categories_blog_posts
+ blog_posts).each do |table_name|
p "Truncating #{table_name} ..."
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
end
@@ -22,12 +23,13 @@ namespace :wordpress do
only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
dump.posts(only_published).each(&:to_refinery)
+ Refinery::WordPress::Post.create_blog_page_if_necessary
+
ENV["MODEL"] = 'BlogPost'
Rake::Task["friendly_id:redo_slugs"].invoke
ENV.delete("MODEL")
end
-
desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump"
task :reset_and_import_blog, :file_name do |task, params|
Rake::Task["environment"].invoke
@@ -35,4 +37,45 @@ namespace :wordpress do
Rake::Task["wordpress:import_blog"].invoke(params[:file_name])
end
+
+ desc "Reset the cms relevant tables for a clean import"
+ task :reset_pages do
+ Rake::Task["environment"].invoke
+
+ %w(page_part_translations page_translations page_parts pages).each do |table_name|
+ p "Truncating #{table_name} ..."
+ ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
+ end
+ end
+
+ desc "import cms data from a Refinery::WordPress XML dump"
+ task :import_pages, :file_name do |task, params|
+ Rake::Task["environment"].invoke
+ dump = Refinery::WordPress::Dump.new(params[:file_name])
+
+ only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
+ dump.pages(only_published).each(&:to_refinery)
+
+ # After all pages are persisted we can now create the parent - child
+ # relationships. This is necessary, as WordPress doesn't dump the pages in
+ # a correct order.
+ dump.pages(only_published).each do |dump_page|
+ page = ::Page.find(dump_page.post_id)
+ page.parent_id = dump_page.parent_id
+ page.save!
+ end
+
+ Refinery::WordPress::Post.create_blog_page_if_necessary
+
+ ENV["MODEL"] = 'Page'
+ Rake::Task["friendly_id:redo_slugs"].invoke
+ ENV.delete("MODEL")
+ end
+
+ desc "reset cms tables and then import cms data from a Refinery::WordPress XML dump"
+ task :reset_and_import_pages, :file_name do |task, params|
+ Rake::Task["environment"].invoke
+ Rake::Task["wordpress:reset_pages"].invoke
+ Rake::Task["wordpress:import_pages"].invoke(params[:file_name])
+ end
end
diff --git a/lib/wordpress/dump.rb b/lib/wordpress/dump.rb
index 67b9a58..0c31b6e 100644
--- a/lib/wordpress/dump.rb
+++ b/lib/wordpress/dump.rb
@@ -19,10 +19,13 @@ module Refinery
end
end
- def pages
- doc.xpath("//item[wp:post_type = 'page']").collect do |page|
+ def pages(only_published=false)
+ pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
Page.new(page)
end
+
+ pages = pages.select(&:published?) if only_published
+ pages
end
def posts(only_published=false)
diff --git a/lib/wordpress/page.rb b/lib/wordpress/page.rb
index cbb8b97..90406d3 100644
--- a/lib/wordpress/page.rb
+++ b/lib/wordpress/page.rb
@@ -36,7 +36,6 @@ module Refinery
# [ruby]p "Hello World"[/ruby]
# -> p "Hello world"
formatted.gsub!(/\[(\w+)\](.+?)\[\/\1\]/m, '\2
')
- #formatted.gsub!(/\[\/\w+\]/, '')
# remove all tags inside that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags
@@ -60,7 +59,8 @@ module Refinery
end
def parent_id
- node.xpath("wp:post_parent").text.to_i
+ dump_id = node.xpath("wp:post_parent").text.to_i
+ dump_id == 0 ? nil : dump_id
end
def status
@@ -80,8 +80,8 @@ module Refinery
end
def to_refinery
- page = ::Page.create!(:title => title, :created_at => post_date,
- :draft => draft?, :parent_id => parent_id)
+ page = ::Page.create!(:id => post_id, :title => title,
+ :created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted)
page
@@ -95,7 +95,6 @@ module Refinery
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "
\n\n#{start_tag}") # 2+ newline -> paragraph
- #text.gsub!(/([^\n]\n)(?=[^\n])/, '\1
') # 1 newline -> br
text.insert 0, start_tag
text.html_safe.safe_concat("")
diff --git a/lib/wordpress/post.rb b/lib/wordpress/post.rb
index 776554c..0e3e2d0 100644
--- a/lib/wordpress/post.rb
+++ b/lib/wordpress/post.rb
@@ -61,6 +61,25 @@ module Refinery
post
end
+
+ 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?(\/|\/.+?|)$"
+ )
+
+ ::Page.default_parts.each do |default_page_part|
+ page.parts.create(:title => default_page_part, :body => nil)
+ end
+ end
+ end
+
end
end
end