Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 13
- Log:
Initial import of Typo 2.6.0 sources from a downloaded Tarball.
Typo is a Ruby On Rails based blog engine.
- Author:
- adh
- Date:
- Sat Jul 22 22:25:02 +0100 2006
- Size:
- 5927 Bytes
- Properties:
- Property svn:executable is set
1 | #!/usr/bin/env ruby |
2 | |
3 | # WordPress 1.5x converter for typo by Patrick Lenz <patrick@lenz.sh> |
4 | # |
5 | # MAKE BACKUPS OF EVERYTHING BEFORE RUNNING THIS SCRIPT! |
6 | # THIS SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND |
7 | |
8 | require File.dirname(__FILE__) + '/../../config/environment' |
9 | require 'optparse' |
10 | |
11 | class WPMigrate |
12 | attr_accessor :options |
13 | |
14 | def initialize |
15 | self.options = {} |
16 | self.parse_options |
17 | self.convert_categories |
18 | self.convert_entries |
19 | self.convert_prefs |
20 | end |
21 | |
22 | def convert_categories |
23 | wp_categories = ActiveRecord::Base.connection.select_all(%{ |
24 | SELECT cat_name AS name |
25 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_categories` |
26 | }) |
27 | |
28 | puts "Converting #{wp_categories.size} categories.." |
29 | |
30 | wp_categories.each do |cat| |
31 | Category.create(cat) unless Category.find_by_name(cat['name']) |
32 | end |
33 | end |
34 | |
35 | def convert_entries |
36 | wp_entries = ActiveRecord::Base.connection.select_all(%{ |
37 | SELECT |
38 | `#{self.options[:wp_prefix]}_posts`.ID, |
39 | (CASE comment_status WHEN 'closed' THEN '0' ELSE '1' END) AS allow_comments, |
40 | (CASE ping_status WHEN 'closed' THEN '0' ELSE '1' END) AS allow_pings, |
41 | post_title AS title, |
42 | post_content AS body, |
43 | post_excerpt AS excerpt, |
44 | post_date AS created_at, |
45 | post_modified AS updated_at, |
46 | (CASE LENGTH(user_nickname) WHEN '0' THEN user_login ELSE user_nickname END) AS author, |
47 | (CASE post_status WHEN 'publish' THEN '1' ELSE '0' END) AS published, |
48 | post_category |
49 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_posts`, `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_users` |
50 | WHERE `#{self.options[:wp_prefix]}_users`.ID = `#{self.options[:wp_prefix]}_posts`.post_author |
51 | }) |
52 | |
53 | puts "Converting #{wp_entries.size} entries.." |
54 | |
55 | wp_entries.each do |entry| |
56 | a = Article.new |
57 | a.attributes = entry.reject { |k,v| k =~ /^(ID|post_category)/ } |
58 | a.save |
59 | |
60 | # Assign primary category |
61 | unless entry['post_category'].to_i.zero? |
62 | puts "Assign primary category for entry #{entry['ID']}" |
63 | |
64 | ActiveRecord::Base.connection.select_all(%{ |
65 | SELECT cat_name |
66 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_categories` |
67 | WHERE cat_ID = '#{entry['post_category']}' |
68 | }).each do |c| |
69 | a.categories.push_with_attributes(Category.find_by_name(c['cat_name']), :is_primary => 1) rescue nil |
70 | end |
71 | end |
72 | |
73 | # Fetch category assignments |
74 | puts "Assign remaining categories for entry #{entry['ID']}" |
75 | ActiveRecord::Base.connection.select_all(%{ |
76 | SELECT cat_name |
77 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_categories`, `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_post2cat` |
78 | WHERE post_id = #{entry['ID']} |
79 | AND `#{self.options[:wp_prefix]}_post2cat`.category_id = `#{self.options[:wp_prefix]}_categories`.cat_ID |
80 | }).each do |c| |
81 | a.categories.push_with_attributes(Category.find_by_name(c['cat_name']), :is_primary => 0) |
82 | end |
83 | |
84 | # Fetch comments |
85 | ActiveRecord::Base.connection.select_all(%{ |
86 | SELECT |
87 | comment_author AS author, |
88 | comment_author_email AS email, |
89 | comment_author_url AS url, |
90 | comment_content AS body, |
91 | comment_date AS created_at, |
92 | comment_author_IP AS ip |
93 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_comments` |
94 | WHERE comment_post_ID = #{entry['ID']} |
95 | AND comment_type != 'trackback' |
96 | AND comment_approved = '1' |
97 | }).each do |c| |
98 | a.comments.create(c) |
99 | end |
100 | |
101 | # Fetch trackbacks |
102 | ActiveRecord::Base.connection.select_all(%{ |
103 | SELECT |
104 | comment_author AS blog_name, |
105 | comment_author_url AS url, |
106 | comment_content AS excerpt, |
107 | comment_date AS created_at, |
108 | comment_author_IP AS ip |
109 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_comments` |
110 | WHERE comment_post_ID = #{entry['ID']} |
111 | AND comment_type = 'trackback' |
112 | AND comment_approved = '1' |
113 | }).each do |c| |
114 | c['title'] = c['excerpt'].match("<(strong)>(.+?)</\\1>")[2] rescue c['blog_name'] |
115 | a.trackbacks.create(c) |
116 | end |
117 | |
118 | end |
119 | end |
120 | |
121 | def convert_prefs |
122 | puts "Converting prefs" |
123 | |
124 | ActiveRecord::Base.connection.select_all(%{ |
125 | SELECT |
126 | (CASE option_name |
127 | WHEN 'blogname' THEN 'blog_name' |
128 | WHEN 'default_comment_status' THEN 'default_allow_comments' |
129 | WHEN 'default_ping_status' THEN 'default_allow_pings' |
130 | END) AS name, |
131 | option_value AS value |
132 | FROM `#{self.options[:wp_db]}`.`#{self.options[:wp_prefix]}_options` |
133 | WHERE option_name IN ('blogname', 'default_comment_status', 'default_ping_status') |
134 | }).each do |pref| |
135 | if pref['name'] =~ /^default_allow/ |
136 | pref['value'] = (pref['value'] == "open" ? 1 : 0) |
137 | end |
138 | |
139 | begin |
140 | Setting.find_by_name(pref['name']).update_attribute("value", pref['value']) |
141 | rescue |
142 | Setting.create(pref) |
143 | end |
144 | end |
145 | end |
146 | |
147 | def parse_options |
148 | OptionParser.new do |opt| |
149 | opt.banner = "Usage: wordpress.rb [options]" |
150 | |
151 | opt.on('--db DBNAME', String, 'WordPress database name.') { |d| self.options[:wp_db] = d } |
152 | opt.on('--prefix PREFIX', String, 'WordPress table prefix (defaults to \'wp\').') { |d| self.options[:wp_prefix] = d } |
153 | |
154 | opt.on_tail('-h', '--help', 'Show this message.') do |
155 | puts opt |
156 | exit |
157 | end |
158 | |
159 | opt.parse!(ARGV) |
160 | end |
161 | |
162 | unless self.options.include?(:wp_db) |
163 | puts "See wordpress.rb --help for help." |
164 | exit |
165 | end |
166 | |
167 | unless self.options.include?(:wp_prefix) |
168 | self.options[:wp_prefix] = "wp" |
169 | end |
170 | end |
171 | end |
172 | |
173 | WPMigrate.new |