Started on cms page import

* created rake tasks
* still needed: cleanups, more tests and docs
This commit is contained in:
Marc Remolt 2011-06-03 18:50:27 +02:00
parent dacd503d8f
commit e1c60163e4
4 changed files with 73 additions and 9 deletions

View File

@ -5,7 +5,8 @@ namespace :wordpress do
task :reset_blog do task :reset_blog do
Rake::Task["environment"].invoke 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} ..." p "Truncating #{table_name} ..."
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}" ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
end end
@ -22,12 +23,13 @@ namespace :wordpress do
only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
dump.posts(only_published).each(&:to_refinery) dump.posts(only_published).each(&:to_refinery)
Refinery::WordPress::Post.create_blog_page_if_necessary
ENV["MODEL"] = 'BlogPost' ENV["MODEL"] = 'BlogPost'
Rake::Task["friendly_id:redo_slugs"].invoke Rake::Task["friendly_id:redo_slugs"].invoke
ENV.delete("MODEL") ENV.delete("MODEL")
end end
desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump" 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| task :reset_and_import_blog, :file_name do |task, params|
Rake::Task["environment"].invoke Rake::Task["environment"].invoke
@ -35,4 +37,45 @@ namespace :wordpress do
Rake::Task["wordpress:import_blog"].invoke(params[:file_name]) Rake::Task["wordpress:import_blog"].invoke(params[:file_name])
end 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 end

View File

@ -19,10 +19,13 @@ module Refinery
end end
end end
def pages def pages(only_published=false)
doc.xpath("//item[wp:post_type = 'page']").collect do |page| pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
Page.new(page) Page.new(page)
end end
pages = pages.select(&:published?) if only_published
pages
end end
def posts(only_published=false) def posts(only_published=false)

View File

@ -36,7 +36,6 @@ module Refinery
# [ruby]p "Hello World"[/ruby] # [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre> # -> <pre class="brush: ruby">p "Hello world"</pre>
formatted.gsub!(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>') formatted.gsub!(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
#formatted.gsub!(/\[\/\w+\]/, '</pre>')
# remove all tags inside <pre> that simple_format created # remove all tags inside <pre> that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags # TODO: replace simple_format with a method, that ignores pre-tags
@ -60,7 +59,8 @@ module Refinery
end end
def parent_id 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 end
def status def status
@ -80,8 +80,8 @@ module Refinery
end end
def to_refinery def to_refinery
page = ::Page.create!(:title => title, :created_at => post_date, page = ::Page.create!(:id => post_id, :title => title,
:draft => draft?, :parent_id => parent_id) :created_at => post_date, :draft => draft?)
page.parts.create(:title => 'Body', :body => content_formatted) page.parts.create(:title => 'Body', :body => content_formatted)
page page
@ -95,7 +95,6 @@ module Refinery
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
#text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
text.insert 0, start_tag text.insert 0, start_tag
text.html_safe.safe_concat("</p>") text.html_safe.safe_concat("</p>")

View File

@ -61,6 +61,25 @@ module Refinery
post post
end 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 end
end end