DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Typo Export To Wordpress WXR (script Version)

07.18.2007
| 7106 views |
  • submit to reddit
        I tried to use this method here: http://snippets.dzone.com/posts/show/3264 however my typo blog was too slow and it kept timing out (which is why we're switching to wordpress anyway...).  So I stole the code and hacked it up a bit so it could be run as a script.

Put this in $RAILS_ROOT/script/wp_export.rb, and run it like:
RAILS_ENV=production ./script/wp_export.rb > out.wxr

Import it with the "Import Wordpress" tool in your wordpress blog's admin interface.

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'environment'

require 'application'

# we need a controller to generate html output
class FakeController < ApplicationController
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers::TagHelper
end
fake_controller = FakeController.new

articles = Article.find(:all)

xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss 'version' => "2.0",
        'xmlns:content' => "http://purl.org/rss/1.0/modules/content/",
        'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/",
        'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
        'xmlns:wp' => "http://wordpress.org/export/1.0/" do
  xml.channel do
    xml.title "The Robot Co-op"
    xml.link "http://www.robotcoop.com"
    xml.language "en-us"
    xml.ttl "40"
    xml.description "Robot coop blog"

    articles.each do |a|
        xml.item do
          xml.title a.title
          xml.content(:encoded) { |x| x << a.html(fake_controller, :all) }
          xml.pubDate a.published_at.rfc2822
          xml.guid "urn:uuid:{a.guid}", "isPermaLink" => "false"
          author = a.user.name rescue a.author
          xml.author author
          xml.dc :creator, author
          for category in a.categories
            xml.category category.name
          end
          for tag in a.tags
            xml.category tag.display_name
          end
          xml.wp :post_id, a.id
          xml.wp :post_date, a.published_at.strftime("%Y-%m-%d %H:%M:%S")
          xml.wp :comment_status, 'closed'
          xml.wp :ping_status, 'closed'
          xml.wp :post_name, a.permalink
          xml.wp :status, 'publish'
          xml.wp :post_parent, '0'
          xml.wp :post_type, 'post'
          for comment in a.comments
            xml.wp(:comment) do
              xml.wp :comment_id, comment.id
              xml.wp :comment_author, comment.author
              xml.wp :comment_author_email, comment.email
              xml.wp :comment_author_url, comment.url
              xml.wp :comment_author_IP, comment.ip
              xml.wp :comment_date, comment.published_at.strftime("%Y-%m-%d %H:%M:%S")
              xml.wp(:comment_content) { |x| x << comment.body }
              xml.wp :comment_approved, '1'
              xml.wp :comment_parent, '0'
            end
          end
       end
    end
  end
end

    

Comments

Snippets Manager replied on Tue, 2011/08/16 - 6:22pm

I needed to remove: require 'environment' require 'application' and replace it with: require 'config/environment' After that - bam!, awesome work. Thanks!

Antonio Cangiano replied on Mon, 2007/10/15 - 9:21pm

@indiehead: chmod +x ./script/wp_export.rb export RAILS_ENV=production ./script/wp_export.rb > out.wxr

John Griffiths replied on Mon, 2007/07/23 - 7:25am

tried running this and keep getting a permission denied. RAILS_ENV=production ./script/wp_export.rb > out.wxr any ideas?