From c06bc8142dc6ed12ec0f0c9f324c660979a4abd0 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 6 Mar 2014 18:20:09 -0700 Subject: [PATCH] Fixing occurrance count and error reporting in attachments --- lib/tasks/wordpress.rake | 18 +++++++++++++++--- lib/wordpress/attachment.rb | 26 +++++++++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/tasks/wordpress.rake b/lib/tasks/wordpress.rake index f544c72..6976945 100644 --- a/lib/tasks/wordpress.rake +++ b/lib/tasks/wordpress.rake @@ -99,14 +99,26 @@ namespace :wordpress do Rake::Task["environment"].invoke dump = WordPressImport::Dump.new(params[:file_name]) - p "Importing #{dump.attachments.each_slice(25).first.count} attachments ..." - attachments = dump.attachments.each_slice(25).first.each(&:to_rails) - p "Errors were encountered: #{$ATTACHMENT_EXCEPTIONS.inspect}" unless $ATTACHMENT_EXCEPTIONS.blank? + p "Importing #{dump.attachments.each_slice(200).first.count} attachments ..." + attachments = dump.attachments.each_slice(200).first.each(&:to_rails) + unless $ATTACHMENT_EXCEPTIONS.blank? + p "----------------------------------------------------------" + p "ERRORS WERE ENCOUNTERED IMPORTING ATTACHMENTS:" + $ATTACHMENT_EXCEPTIONS.each{|exception| puts exception} + p "----------------------------------------------------------" + end # parse all created Post and Page bodys and replace the old wordpress media urls # with the newly created ones p "Replacing attachment URLs found in posts/pages ..." attachments.each(&:replace_url) + + unless $REPLACEMENT_EXCEPTIONS.blank? + p "----------------------------------------------------------" + p "ERRORS WERE ENCOUNTERED REPLACING ATTACHMENTS:" + $REPLACEMENT_EXCEPTIONS.each{|exception| puts exception} + p "----------------------------------------------------------" + end end desc "reset media tables and then import media data from a WordPress XML dump" diff --git a/lib/wordpress/attachment.rb b/lib/wordpress/attachment.rb index 690abb0..76f4b11 100644 --- a/lib/wordpress/attachment.rb +++ b/lib/wordpress/attachment.rb @@ -47,7 +47,7 @@ module WordPressImport else to_file end - rescue Exception => ex + rescue StandardError => ex message = "ERROR saving attachment #{url} -- #{ex.message}" p message $ATTACHMENT_EXCEPTIONS = [] if $ATTACHMENT_EXCEPTIONS.blank? @@ -57,13 +57,21 @@ module WordPressImport end def replace_url - @occurrance_count = 0 - if image? - replace_image_url - else - replace_resource_url + begin + @occurrance_count = 0 + if image? + replace_image_url + else + replace_resource_url + end + p "Replaced #{@occurrance_count} occurrances of #{url}" + rescue StandardError => ex + message = "ERROR replacing URL #{url} -- #{ex.message}" + p message + $REPLACEMENT_EXCEPTIONS = [] if $REPLACEMENT_EXCEPTIONS.blank? + $REPLACEMENT_EXCEPTIONS << message + return nil end - p "Replaced #{@occurrance_count} occurrances of #{url}" end private @@ -146,7 +154,7 @@ module WordPressImport def replace_url_in_blog_posts(new_url) ::Post.all.each do |post| if (! post.body.empty?) && post.body.include?(url) - @occurrance_count++ + @occurrance_count += 1 post.body = post.body.gsub(url_pattern, new_url) post.save! end @@ -158,7 +166,7 @@ module WordPressImport page.translations.each do |translation| translation.parts.each do |part| if (! part.content.to_s.blank?) && part.content.include?(url) - @occurrance_count++ + @occurrance_count += 1 part.content = part.content.gsub(url_pattern, new_url) part.save! end