From Wordpress to Obtvse, rake task

Obtvse is a minimalist blog, made simple and built on Rails. I like minimalism, so I wanted to test and do something with it. I worked in a rake task to just migrate my actual posts to this new platform.

To make it work there's only a few steps needed.

In your Gemfile you need to put...


gem "mysql"

Then add an environment to your config/database.yml


wordpress:
  adapter: mysql
  encoding: utf8
  database: obtvse_example
  username: my_user
  password: my_pass
  host: localhost

Finally you could call it from the command line...


rake wordpress:migrate_wordpress[source_db, destiny_db]

Where sourcedb is your Wordpress db in the database.yml file, and destinydb is the Obtvse one.

The code is not a big deal, just a few lines. I know It could be a better implementation, but is my first rake task, I would like some feedback. :-)


# -*- coding:utf-8 -*-                                                                                                                              
# Usage rake wordpress:migrate_wordpress[:src, :dst]
# You will need gem "mysql" in your Gemfile
namespace :wordpress do
    task :migrate_wordpress, [:src, :dst] => :environment do |cmd, args|
        args.with_defaults(:src => "wordpress", :dst => "development")
        config = Rails.configuration.database_configuration
        src_db = config[args[:src]]
        dst_db = config[args[:dst]]

        conn = ActiveRecord::Base.establish_connection src_db
        posts = conn.connection.execute "SELECT * FROM wp_posts where post_status='draft' or post_status='publish'"

        conn = ActiveRecord::Base.establish_connection dst_db

        posts.each do |post|
            title = post[5].force_encoding 'utf-8'
            content = post[4].force_encoding 'utf-8'
            created_at = post[2]
            updated_at = post[14]
            draft = post[7]

            if draft.end_with? "draft"
                draft_flag = true
            elsif draft.end_with? "publish"
                draft_flag = false
            end

            p = Post.create! :title => title, :content => content, :slug => Post.acts_as_url(:title, :url_attribute => :slug),
                             :created_at => created_at, :updated_at => updated_at, :draft => draft_flag
        end
    end
end

More in my Obtvse fork. Obtvse Demo.

Javier Aguirre

Read more posts by this author.

comments powered by Disqus