diff --git a/app/controllers/xml_controller.rb b/app/controllers/xml_controller.rb index 7c0f023..e379a2a 100644 --- a/app/controllers/xml_controller.rb +++ b/app/controllers/xml_controller.rb @@ -4,11 +4,12 @@ class XmlController < ApplicationController NORMALIZED_FORMAT_FOR = {'atom' => 'atom', 'rss' => 'rss', 'atom10' => 'atom', 'atom03' => 'atom', 'rss20' => 'rss', - 'googlesitemap' => 'googlesitemap', 'rsd' => 'rsd' } + 'googlesitemap' => 'googlesitemap', 'rsd' => 'rsd', 'wxr' => 'wxr' } CONTENT_TYPE_FOR = { 'rss' => 'application/xml', 'atom' => 'application/atom+xml', - 'googlesitemap' => 'application/xml' } + 'googlesitemap' => 'application/xml', + 'wxr' => 'application/xml' } before_filter :adjust_format @@ -45,6 +46,7 @@ class XmlController < ApplicationController format.googlesitemap format.atom format.rss + format.wxr end end end @@ -79,7 +81,7 @@ class XmlController < ApplicationController if params[:format] params[:format] = NORMALIZED_FORMAT_FOR[params[:format]] else - params[:foramt] = 'rss' + params[:format] = 'rss' end return true end @@ -134,4 +136,10 @@ class XmlController < ApplicationController @items += Category.find_all_with_article_counters(1000) @items += Tag.find_all_with_article_counters(1000) end + + def prep_export + @categories = Category.find :all + @tags = Tag.find :all + fetch_items(:articles, 'created_at DESC', 10000) + end end diff --git a/app/views/xml/_export_article.wxr.builder b/app/views/xml/_export_article.wxr.builder new file mode 100644 index 0000000..3c62128 --- /dev/null +++ b/app/views/xml/_export_article.wxr.builder @@ -0,0 +1,64 @@ +xm.item do + xm.title item.title + xm.link item.permalink_url + xm.pubDate pub_date(item.published_at) + xm.dc :creator, item.user.login + for category in item.categories + xm.category category.name, :domain => "category", :nicename => category.name.downcase + end + for tag in item.tags + xm.category tag.display_name, :domain => "tag" + end + xm.guid "tag:#{request.host_with_port}:#{item.class}#{item.id}", "isPermaLink" => "false" + xm.description + xm.target! << '' + xm.excerpt :encoded + + # Wordpress specifics. + xm.wp :post_id, item.id + xm.wp :post_date, item.published_at.to_s(:db) + xm.wp :post_date_gmt, item.published_at.utc.to_s(:db) + xm.wp :comment_status, item.allow_comments ? 'open' : 'closed' + xm.wp :ping_status, item.allow_pings ? 'open' : 'closed' + xm.wp :post_name, item.permalink + xm.wp :status, item.published ? 'publish' : 'draft' + xm.wp :post_parent, 0 + xm.wp :menu_order, 0 + xm.wp :post_type, 'post' + xm.wp :post_password + xm.wp :postmeta do + xm.wp :meta_key, 'typo_id' + xm.wp :meta_value, item.id + end + item.comments.each do |comment| + xm.wp :comment do + xm.wp :comment_id, comment.id + xm.wp :comment_author, comment.author + xm.wp :comment_author_email, comment.email + xm.wp :comment_author_url, comment.url + xm.wp :comment_date, comment.published_at.to_s(:db) + xm.wp :comment_date_gmt, comment.published_at.utc.to_s(:db) + xm.target! << '' + xm.wp :comment_approved, comment.published ? 1 : 0 + xm.wp :comment_type + xm.wp :comment_parent, 0 + xm.wp :comment_user_id, 0 + end + end + + # RSS 2.0 only allows a single enclosure per item, so only include the first one here. + if not item.resources.empty? + resource = item.resources.first + xm.enclosure( + :url => item.blog.file_url(resource.filename), + :length => resource.size, + :type => resource.mime) + end + if item.allow_pings? + xm.trackback :ping, item.trackback_url + end +end diff --git a/app/views/xml/_export_comment.wxr.builder b/app/views/xml/_export_comment.wxr.builder new file mode 100644 index 0000000..98c1100 --- /dev/null +++ b/app/views/xml/_export_comment.wxr.builder @@ -0,0 +1,7 @@ +xm.item do + xm.title "\"#{item.article.title}\" by #{item.author}" + xm.description html(item) + xm.pubDate pub_date(item.created_at) + xm.guid "urn:uuid:#{item.guid}", "isPermaLink" => "false" + xm.link item.permalink_url +end diff --git a/app/views/xml/_export_trackback.wxr.builder b/app/views/xml/_export_trackback.wxr.builder new file mode 100644 index 0000000..cafb56e --- /dev/null +++ b/app/views/xml/_export_trackback.wxr.builder @@ -0,0 +1,7 @@ +xm.item do + xm.title "\"#{item.title}\" from #{item.blog_name} (#{item.article.title})" + xm.description item.excerpt + xm.pubDate pub_date(item.created_at) + xm.guid "urn:uuid:#{item.guid}", "isPermaLink" => "false" + xm.link item.url +end diff --git a/app/views/xml/feed.wxr.builder b/app/views/xml/feed.wxr.builder new file mode 100644 index 0000000..cc7a0ed --- /dev/null +++ b/app/views/xml/feed.wxr.builder @@ -0,0 +1,47 @@ +xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" +xml.instruct! 'xml-stylesheet', :type=>"text/css", :href => url_for("/stylesheets/rss.css") + +xml.rss "version" => "2.0", + "xmlns:dc" => "http://purl.org/dc/elements/1.1/", + "xmlns:trackback" => "http://madskills.com/public/xml/rss/module/trackback/", + "xmlns:excerpt" => "http://wordpress.org/excerpt/1.0", + "xmlns:content" => "http://purl.org/rss/1.0/modules/content/", + "xmlns:wp" => "http://wordpress.org/export/1.0/" do + xml.channel do + xml.title feed_title + xml.link @link + xml.language "en" + xml.ttl "40" + xml.description this_blog.blog_subtitle + xml.wp :wxr_version, "1.0" + xml.wp :base_site_url, @link + xml.wp :base_blog_url, @link + + @categories.each do |cat| + # XXX categories have to be on a single line. + xml.wp :category do + # NB: We specifically sidestep builder in order to avoid indentation + # and get everything on a single line. + xml.target! ""+ + "#{cat.name.downcase}"+ + ""+ + "#{cat.name}"+ + "\n" + end + end + + @tags.each do |tag| + # NB: We specifically sidestep builder in order to avoid indentation + # and get everything on a single line. + xml.target! << ""+ + "#{tag.name}"+ + ""+ + "\n" + end + + @items.each do |item| + render :partial => "export_#{item.class.name.to_s.downcase}", + :locals => {:item => item, :xm => xml} + end + end +end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index e9887d9..7a733dc 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -1,3 +1,3 @@ Mime::Type.register_alias "application/xml", :googlesitemap Mime::Type.register "application/rsd+xml", :rsd - +Mime::Type.register_alias "application/xml", :wxr