Resource import working
* added rake tasks
This commit is contained in:
@@ -19,7 +19,6 @@ namespace :wordpress do
|
||||
dump = Refinery::WordPress::Dump.new(params[:file_name])
|
||||
|
||||
dump.authors.each(&:to_refinery)
|
||||
attachments = dump.attachments.each(&:to_refinery)
|
||||
|
||||
only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
|
||||
dump.posts(only_published).each(&:to_refinery)
|
||||
@@ -29,12 +28,6 @@ namespace :wordpress do
|
||||
ENV["MODEL"] = 'BlogPost'
|
||||
Rake::Task["friendly_id:redo_slugs"].invoke
|
||||
ENV.delete("MODEL")
|
||||
|
||||
# parse all created BlogPosts bodys and replace the old wordpress image uls
|
||||
# with the newly created ones
|
||||
attachments.each do |attachment|
|
||||
attachment.replace_image_url_in_blog_posts
|
||||
end
|
||||
end
|
||||
|
||||
desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump"
|
||||
@@ -55,7 +48,7 @@ namespace :wordpress do
|
||||
end
|
||||
end
|
||||
|
||||
desc "import cms data from a Refinery::WordPress XML dump"
|
||||
desc "import cms data from a WordPress XML dump"
|
||||
task :import_pages, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
dump = Refinery::WordPress::Dump.new(params[:file_name])
|
||||
@@ -79,10 +72,50 @@ namespace :wordpress do
|
||||
ENV.delete("MODEL")
|
||||
end
|
||||
|
||||
desc "reset cms tables and then import cms data from a Refinery::WordPress XML dump"
|
||||
desc "reset cms tables and then import cms data from a 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
|
||||
|
||||
|
||||
desc "Reset the media relevant tables for a clean import"
|
||||
task :reset_media do
|
||||
Rake::Task["environment"].invoke
|
||||
|
||||
%w(images resources).each do |table_name|
|
||||
p "Truncating #{table_name} ..."
|
||||
ActiveRecord::Base.connection.execute "DELETE FROM #{table_name}"
|
||||
end
|
||||
end
|
||||
|
||||
desc "import media data (images and files) from a WordPress XML dump and replace target URLs in pages and posts"
|
||||
task :import_and_replace_media, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
dump = Refinery::WordPress::Dump.new(params[:file_name])
|
||||
|
||||
attachments = dump.attachments.each(&:to_refinery)
|
||||
|
||||
# parse all created BlogPost and Page bodys and replace the old wordpress media uls
|
||||
# with the newly created ones
|
||||
attachments.each do |attachment|
|
||||
attachment.replace_url
|
||||
end
|
||||
end
|
||||
|
||||
desc "reset media tables and then import media data from a WordPress XML dump"
|
||||
task :reset_import_and_replace_media, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
Rake::Task["wordpress:reset_media"].invoke
|
||||
Rake::Task["wordpress:import_and_replace_media"].invoke(params[:file_name])
|
||||
end
|
||||
|
||||
desc "reset and import all data (see the other tasks)"
|
||||
task :full_import, :file_name do |task, params|
|
||||
Rake::Task["environment"].invoke
|
||||
Rake::Task["wordpress:reset_and_import_blog"].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])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ module Refinery
|
||||
class Attachment
|
||||
attr_reader :node
|
||||
attr_reader :refinery_image
|
||||
attr_reader :refinery_resource
|
||||
|
||||
def initialize(node)
|
||||
@node = node
|
||||
@@ -28,6 +29,14 @@ module Refinery
|
||||
node.xpath("wp:attachment_url").text
|
||||
end
|
||||
|
||||
def url_pattern
|
||||
url_parts = url.split('.')
|
||||
extension = url_parts.pop
|
||||
url_without_extension = url_parts.join('.')
|
||||
|
||||
/#{url_without_extension}(-\d+x\d+)?\.#{extension}/
|
||||
end
|
||||
|
||||
def image?
|
||||
url.match /\.(png|jpg|jpeg|gif)$/
|
||||
end
|
||||
@@ -36,21 +45,15 @@ module Refinery
|
||||
if image?
|
||||
to_image
|
||||
else
|
||||
to_file
|
||||
to_resource
|
||||
end
|
||||
end
|
||||
|
||||
def replace_image_url_in_blog_posts
|
||||
::BlogPost.all.each do |post|
|
||||
if post.body.include? url
|
||||
url_parts = url.split('.')
|
||||
extension = url_parts.pop
|
||||
url_without_extension = url_parts.join('.')
|
||||
pattern = /#{url_without_extension}(-\d+x\d+)?\.#{extension}/
|
||||
|
||||
post.body = post.body.gsub(pattern, refinery_image.image.url)
|
||||
post.save!
|
||||
end
|
||||
def replace_url
|
||||
if image?
|
||||
replace_image_url
|
||||
else
|
||||
replace_resource_url
|
||||
end
|
||||
end
|
||||
|
||||
@@ -66,8 +69,60 @@ module Refinery
|
||||
image
|
||||
end
|
||||
|
||||
def to_file
|
||||
raise "to_file is not implemented yet, sorry!"
|
||||
def to_resource
|
||||
resource = ::Resource.new
|
||||
resource.created_at = post_date
|
||||
resource.file_url = url
|
||||
resource.save!
|
||||
|
||||
@refinery_resource = resource
|
||||
resource
|
||||
end
|
||||
|
||||
def replace_image_url
|
||||
replace_image_url_in_blog_posts
|
||||
replace_image_url_in_pages
|
||||
end
|
||||
|
||||
def replace_resource_url
|
||||
replace_resource_url_in_blog_posts
|
||||
replace_resource_url_in_pages
|
||||
end
|
||||
|
||||
def replace_image_url_in_blog_posts
|
||||
replace_url_in_blog_posts(refinery_image.image.url)
|
||||
end
|
||||
|
||||
def replace_image_url_in_pages
|
||||
replace_url_in_pages(refinery_image.image.url)
|
||||
end
|
||||
|
||||
def replace_resource_url_in_blog_posts
|
||||
replace_url_in_blog_posts(refinery_resource.file.url)
|
||||
end
|
||||
|
||||
def replace_resource_url_in_pages
|
||||
replace_url_in_pages(refinery_resource.file.url)
|
||||
end
|
||||
|
||||
def replace_url_in_blog_posts(new_url)
|
||||
::BlogPost.all.each do |post|
|
||||
if (! post.body.empty?) && post.body.include?(url)
|
||||
post.body = post.body.gsub(url_pattern, new_url)
|
||||
post.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def replace_url_in_pages(new_url)
|
||||
::Page.all.each do |page|
|
||||
page.parts.each do |part|
|
||||
if (! part.body.to_s.blank?) && part.body.include?(url)
|
||||
part.body = part.body.gsub(url_pattern, new_url)
|
||||
part.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user