Skip to content

Commit

Permalink
Merge remote branch 'core/blog-import-rss' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
cykod committed Oct 5, 2010
2 parents 249bf09 + 017dca7 commit 95ceae3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
19 changes: 16 additions & 3 deletions vendor/modules/blog/app/controllers/blog/manage_controller.rb
Expand Up @@ -252,13 +252,15 @@ def import
protected

class ImportOptions < HashModel
attributes :import_file_id => nil, :wordpress_export_file_id => nil, :wordpress_url => nil, :wordpress_username => nil, :wordpress_password => nil, :wordpress_import_settings => []
attributes :import_file_id => nil, :wordpress_export_file_id => nil, :wordpress_url => nil, :wordpress_username => nil, :wordpress_password => nil, :wordpress_import_settings => [], :rss_url => nil

domain_file_options :import_file_id, :wordpress_export_file_id

def validate
if self.import_file_id.blank? && self.wordpress_export_file_id.blank? && self.wordpress_url.blank?
if self.import_file_id.blank? && self.wordpress_export_file_id.blank? && self.wordpress_url.blank? && self.rss_url.blank?
self.errors.add_to_base 'Import settings not specified'
elsif ! self.rss_url.blank?
self.errors.add(:rss_url, 'is invalid') unless URI::regexp(%w(http https)).match(self.rss_url)
elsif self.import_file_id.blank? && self.wordpress_export_file_id.blank? && ! self.wordpress_url.blank?
self.errors.add(:wordpress_url, 'is invalid') unless URI::regexp(%w(http https)).match(self.wordpress_url)
self.errors.add(:wordpress_username, 'is missing') if self.wordpress_username.blank?
Expand All @@ -274,14 +276,25 @@ def wordpress_importer
@wordpress_importer
end

def rss_importer
@rss_importer ||= Blog::RssImporter.new
end

def import(blog, user)
if self.import_file
blog.import_file(self.import_file, user)
elsif ! self.rss_url.blank?
self.rss_importer.blog = blog
if self.rss_importer.import_feed(self.rss_url)
self.errors.add(:rss_url, 'failed to import feed') unless self.rss_importer.import
else
self.errors.add(:rss_url, 'is invalid')
end
elsif self.wordpress_export_file
self.wordpress_importer.blog = blog
self.wordpress_importer.import_file(self.wordpress_export_file)
self.errors.add(:wordpress_export_file_id, self.wordpress_importer.error) unless self.wordpress_importer.import
elsif self.wordpress_url
elsif ! self.wordpress_url.blank?
self.wordpress_importer.blog = blog
unless self.wordpress_importer.import_site(self.wordpress_url, self.wordpress_username, self.wordpress_password)
if self.wordpress_importer.error == 'Login failed'
Expand Down
105 changes: 105 additions & 0 deletions vendor/modules/blog/app/models/blog/rss_importer.rb
@@ -0,0 +1,105 @@
require 'nokogiri'

class Blog::RssImporter
attr_accessor :xml, :blog, :images, :folder, :error

include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper

def initialize
self.images = {}
end

def folder
@folder ||= DomainFile.push_folder self.blog.name
end

def import_feed(url)
begin
res = DomainFile.download url
self.xml = res.body.to_s
true
rescue
self.error = 'RSS feed download failed'
false
end
end

def import
doc = Nokogiri::XML self.xml
unless doc
self.error = 'RSS feed is invalid'
return false
end

channel = doc.css('channel').first
unless channel
self.error = 'RSS feed is invalid'
return false
end

channel.css('item').each do |item|
self.create_post item
end

true
end

def push_category(name)
return nil if name.downcase == 'uncategorized'
return nil if name.blank?
self.blog.blog_categories.find_by_name(name) || self.blog.blog_categories.create(:name => name)
end

def parse_body(body)
body.gsub!(/src=("|')([^\1]+?)\1/) do |match|
quote = $1
src = $2
file = nil
file = self.folder.add(src) if src =~ /^http/
if file
self.images[src] = file
"src=#{quote}#{file.editor_url}#{quote}"
else
match
end
end

self.images.each do |src, file|
body.gsub! src, file.editor_url
end

simple_format body
end

def create_post(item)
title = item.css('title').text
body = item.css('description').text
author = item.css('dc:creator').text
author = item.css('creator').text if author.blank?
author = item.css('author').text if author.blank?

pubDate = item.css('pubDate').text
pubDate = nil if pubDate.blank?
pubDate = pubDate ? Time.parse(pubDate) : Time.now
return if body.blank? || title.blank?

if author
author.gsub! /[^\s]+@[^\s]+/, '' # remove the email address
author.gsub! /[^ a-zA-Z0-9\-]/, '' # remove any non name characters
author.strip!
author = nil if author.blank?
end

post = self.blog.blog_posts.create :body => self.parse_body(body), :author => author, :title => title, :status => 'published', :published_at => pubDate, :disallow_comments => false, :created_at => pubDate, :updated_at => pubDate

return unless post.id

item.css('category').each do |category|
cat = self.push_category(category.text.strip)
Blog::BlogPostsCategory.create(:blog_post_id => post.id, :blog_category_id => cat.id) if cat
end

post
end
end
2 changes: 1 addition & 1 deletion vendor/modules/blog/app/models/blog/wordpress_importer.rb
Expand Up @@ -120,7 +120,7 @@ def create_post(categories, item={})

status = item['status'] == 'publish' ? 'published' : 'draft'
disallow_comments = item['comment_status'] == 'open' ? false : true
post = self.blog.blog_posts.create :body => self.parse_body(body), :author => item['creator'], :title => item['title'], :status => 'published', :published_at => Time.parse(item['pubDate']), :status => status, :disallow_comments => disallow_comments, :permalink => item['post_name'], :created_at => Time.parse(item['post_date_gmt']), :preview => self.parse_body(item['excerpt'])
post = self.blog.blog_posts.create :body => self.parse_body(body), :author => item['creator'], :title => item['title'], :published_at => Time.parse(item['pubDate']), :status => status, :disallow_comments => disallow_comments, :permalink => item['post_name'], :created_at => Time.parse(item['post_date_gmt']), :preview => self.parse_body(item['excerpt'])

return unless post.id

Expand Down
3 changes: 3 additions & 0 deletions vendor/modules/blog/app/views/blog/manage/import.rhtml
Expand Up @@ -5,6 +5,9 @@
<%= f.header 'Select a CSV file to import' %>
<%= f.filemanager_file :import_file_id, :description => "File must be a CSV with the following columns:\n Title, permalink, author,published date, preview, body, embed\nOnly title and body are required to have non-empty values but all columns must be present" %>
<%= f.header 'Import RSS Feed' %>
<%= f.text_field :rss_url, :label => 'URL', :size => 40 %>
<%= f.header 'Import WordPress Blog' %>
<%= f.filemanager_file :wordpress_export_file_id, :label => 'Export file', :description => "Log into your blog. Goto Tools > Export and click Download" %>
<%= f.custom_field '', :value => 'Or' %>
Expand Down

0 comments on commit 95ceae3

Please sign in to comment.