Roller 1.0RC1 converter for typo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#!/usr/bin/env ruby

# Roller 1.0RC1 converter for typo by Aitor Garcia <aitor.garcia@gmail.com>
# greatly based on the MovableType 3.x converter by Patrick Lenz <patrick@lenz.sh>
#
# MAKE BACKUPS OF EVERYTHING BEFORE RUNNING THIS SCRIPT!
# THIS SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND

require  File.dirname(__FILE__)  + '/,,/,,/config/environment'
require 'optparse'

class RollerMigrate
  attr_accessor :options

  def initialize
    self.options = {}
    self.parse_options
    self.convert_categories
    self.convert_entries
    self.convert_prefs
  end

  def convert_categories
    roller_categories = ActiveRecord::Base.connection.select_all(%{
      SELECT name
      FROM `#{self.options[:roller_db]}`.weblogcategory
      WHERE websiteid = '#{self.options[:blog_id]}'
    })

    puts "Converting #{roller_categories.size} categories.."

    roller_categories.each do |cat|
      Category.create(cat) unless Category.find_by_name(cat['name'])
    end
  end

  def convert_entries

    roller_entries = ActiveRecord::Base.connection.select_all(%{
      SELECT
        weblogentry.id AS id,
        weblogentry.allowcomments AS allow_comments,
        title,
        text AS body,
        pubtime AS created_at,
        updatetime AS updated_at,
        1 AS published,
        fullname AS author       
      FROM `#{self.options[:roller_db]}`.weblogentry, `#{self.options[:roller_db]}`.website, `#{self.options[:roller_db]}`.rolleruser
      WHERE weblogentry.websiteid = '#{self.options[:blog_id]}'
      AND website.id = weblogentry.websiteid
      AND rolleruser.id = website.userid
    })

    puts "Converting #{roller_entries.size} entries.."

    roller_entries.each do |entry|
      a = Article.new
      a.attributes = entry.reject { |k,v| k =~ /^(ID|post_category)/ }
      a.text_filter = 'none'
      a.save
      # Fetch category assignments
      ActiveRecord::Base.connection.select_all(%{
        SELECT weblogcategory.name AS label
        FROM `#{self.options[:roller_db]}`.weblogcategory, `#{self.options[:roller_db]}`.weblogentry
        WHERE weblogentry.id = '#{entry['id']}'
        AND weblogcategory.id = weblogentry.categoryid
      }).each do |c|
        a.categories.push(Category.find_by_name(c['label']))
      end

      # Fetch comments
      ActiveRecord::Base.connection.select_all(%{
        SELECT
          name AS author,
          email,
          url,
          content AS body,
          posttime AS created_at,
          posttime AS updated_at         
        FROM `#{self.options[:roller_db]}`.comment
        WHERE entryid = '#{entry['id']}'
      }).each do |c|
        a.comments.create(c)
      end
    end
  end

  def convert_prefs
    puts "Converting prefs"

    ActiveRecord::Base.connection.select_one(%{
      SELECT
        name AS blog_name,
        description AS blog_subtitle,
        allowcomments AS default_allow_comments
      FROM `#{self.options[:roller_db]}`.website
      WHERE id = '#{self.options[:blog_id]}'
    }).each do |pref_name, pref_value|
      begin
        Setting.find_by_name(pref_name).update_attribute("value", pref_value)
      rescue
        Setting.create({'name' => pref_name, 'value' => pref_value})
      end
    end

  end

  def parse_options
    OptionParser.new do |opt|
      opt.banner = "Usage: rollerRC1.rb [options]"

      opt.on('--blog-id BLOGID', String, 'Blog ID to import from.') { |i| self.options[:blog_id] = i }
      opt.on('--db DBNAME', String, 'Roller database name.') { |d| self.options[:roller_db] = d }

      opt.on_tail('-h', '--help', 'Show this message.') do
        puts opt
        exit
      end

      opt.parse!(ARGV)
    end

    unless self.options.include?(:blog_id) and self.options.include?(:roller_db)
      puts "See rollerRC1.rb --help for help."
      exit

    end
  end

end
RollerMigrate.new