Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: d99fb943c9bec3f2820abe54761bf21fcf48ad39 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env ruby19
#
# A quick and dirty script to import posts, comments and tags from Blogsum into
# Wordpress
# Copyright (c) 2014, Olivier Mehani <shtrom@ssji.net>
# All rights reserved.
#
# $Id$
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 3. Neither the name of Olivier Mehani nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require 'rubygems'
require 'yaml'
require 'active_record'
require 'sqlite3'
require 'mysql'

## Databases to use

settings_file = './database.yml'
# The yaml file should look like that
# 
#  blogsum:
#    adapter: sqlite3
#     database: /PATH/TO/site.db
#  
#  wordpress:
#    adapter: mysql
#    encoding: latin1
#    username: wordpress
#    password:
#    database: blog
#    
exit unless File.exists?(settings_file)
$settings = YAML.load_file(settings_file)


## ActiveRecord models

# Models generated by RMRE [0], then manually extended to capture missed
# relationships
# [0] https://github.com/bosko/rmre

# Blogsum
class Article < ActiveRecord::Base
  establish_connection $settings["blogsum"]
  has_many :comments
end
class Comment < ActiveRecord::Base
  establish_connection $settings["blogsum"]
  belongs_to :article
end

# Wordpress
class WpComment < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :comment_ID
  has_many :wp_commentmeta
  belongs_to :wp_post
  belongs_to :wp_user
end
class WpCommentmetum < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :meta_id
  belongs_to :wp_comment, foreign_key: :comment_id
end
class WpPost < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :ID
  has_many :wp_postmeta, foreign_key: :post_id
  has_many :wp_comments, foreign_key: :comment_post_ID
  has_many :wp_term_relationships, foreign_key: :object_id
  has_many :wp_term_taxonomy, through: :wp_term_relationships
  has_many :wp_terms, through: :wp_term_taxonomy
  belongs_to :wp_user, foreign_key: :post_author
end
class WpPostmetum < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :meta_id
  belongs_to :wp_post
end
class WpTerm < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :term_id
  has_one :wp_term_taxonomy
end
class WpTermRelationship < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  belongs_to :wp_post
  belongs_to :wp_term_taxonomy, foreign_key: :term_taxonomy_id
end
class WpTermTaxonomy < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.table_name = 'wp_term_taxonomy'
  self.primary_key = :term_taxonomy_id
  belongs_to :wp_term, foreign_key: :term_id
  has_many :wp_term_relationships
  has_many :wp_posts, through: :wp_term_relationships
end
class WpUser < ActiveRecord::Base
  establish_connection $settings["wordpress"]
  self.primary_key = :ID
  has_many :wp_posts, foreign_key: :post_author
  has_many :wp_comments, foreign_key: :user_id
end


## Some sanity checks

# Blogsum
#puts Article.find(1).title
# I know this article has a comment
#puts Article.find(3).comment[0].comment

#Wordpress
#puts WpPost.find(1).post_title
#puts WpPost.find(1).wp_user.user_login
#puts WpPost.find(1).wp_comments[0].comment_content
#puts WpPost.find(1).wp_term_taxonomy
# I know this article has tags
#puts WpPost.find(7).wp_terms.map {|t| "#{t.name}, "}


## Do the import for real!

Article.all.each do |a|
  print "P #{a.title} #{a.date} #{a.author} #{a.uri} #{a.tags} #{a.body[0..19]}\n"
  u = WpUser.find_by_user_login(a.author) || WpUser.all[0] # Map unknown users to admin
  p = WpPost.find_by_post_title(a.title) || WpPost.create(
    post_author: u.id,
    post_date: a.date,
    post_date_gmt: a.date,
    post_content: a.body,
    post_title: a.title,
    post_excerpt: "",
    post_name: a.uri,
    to_ping: "",
    pinged: "",
    post_content_filtered: "",
  )
  a.tags.split(/\ *, */).each do |tg|
    t = WpTerm.find_by_name(tg)
    if t.nil?
      puts "New tag #{tg}"
      t = WpTerm.create(
	name: tg,
	slug: tg.downcase.gsub(/ /, "-"),
      )
      tt = WpTermTaxonomy.create(
	term_id: t.term_id,
	description: "",
	taxonomy: "post_tag",
      )
      t.save
      tt.save
    end
    begin
      tt = WpTermTaxonomy.find_by_term_id(t.term_id)
      tr = WpTermRelationship.create(
	term_taxonomy_id: tt.term_taxonomy_id,
	object_id: p.ID,
      )
      tr.save
    rescue
      # the relationship already exists
    end
  end

  a.comments.each do |cm|
    print "C #{cm.name} #{cm.email} #{cm.url} #{cm.date} #{cm.comment[0..19]}\n"
    cu = WpUser.find_by_user_email(cm.email)
    c = WpComment.find_by_comment_content(cm.comment) || WpComment.create(
      comment_author: cu ? cu.display_name : cm.name,
      comment_author_email: (cu ? cu.user_email: cm.email) || "",
      comment_author_url: (cu ? cu.user_url: cm.url) || "",
      comment_date: cm.date,
      comment_date_gmt: cm.date,
      comment_content: cm.comment,
    )
    if not cu.nil?
      c.wp_user = cu.id
    end
    p.wp_comments << c
    c.save
  end
  p.comment_count = p.wp_comments.length
  p.save
end