forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplepress.rb
More file actions
249 lines (205 loc) · 7.19 KB
/
Copy pathsimplepress.rb
File metadata and controls
249 lines (205 loc) · 7.19 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# frozen_string_literal: true
require "mysql2"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
class ImportScripts::SimplePress < ImportScripts::Base
SIMPLE_PRESS_DB = ENV["SIMPLEPRESS_DB"] || "simplepress"
TABLE_PREFIX = "wp_sf"
BATCH_SIZE = 1000
def initialize
super
@client = Mysql2::Client.new(host: "localhost", username: "root", database: SIMPLE_PRESS_DB)
SiteSetting.max_username_length = 50
end
def execute
import_users
import_categories
import_topics
import_posts
end
def import_users
puts "", "importing users..."
last_user_id = -1
total_users =
mysql_query("SELECT COUNT(*) count FROM wp_users WHERE user_email LIKE '%@%'").first["count"]
batches(BATCH_SIZE) do |offset|
users = mysql_query(<<-SQL).to_a
SELECT ID id, user_nicename, display_name, user_email, user_registered, user_url
FROM wp_users
WHERE user_email LIKE '%@%'
AND id > #{last_user_id}
ORDER BY id
LIMIT #{BATCH_SIZE}
SQL
break if users.empty?
last_user_id = users[-1]["id"]
user_ids = users.map { |u| u["id"].to_i }
next if all_records_exist?(:users, user_ids)
user_ids_sql = user_ids.join(",")
users_description = {}
mysql_query(<<-SQL).each { |um| users_description[um["user_id"]] = um["description"] }
SELECT user_id, meta_value description
FROM wp_usermeta
WHERE user_id IN (#{user_ids_sql})
AND meta_key = 'description'
SQL
create_users(users, total: total_users, offset: offset) do |u|
{
id: u["id"].to_i,
username: u["user_nicename"],
email: u["user_email"].downcase,
name: u["display_name"],
created_at: u["user_registered"],
website: u["user_url"],
bio_raw: users_description[u["id"]],
}
end
end
end
def import_categories
puts "", "importing categories..."
categories = mysql_query(<<-SQL)
SELECT forum_id, forum_name, forum_seq, forum_desc, parent
FROM #{TABLE_PREFIX}forums
ORDER BY forum_id
SQL
create_categories(categories) do |c|
category = {
id: c["forum_id"],
name: CGI.unescapeHTML(c["forum_name"]),
description: CGI.unescapeHTML(c["forum_desc"]),
position: c["forum_seq"],
}
if (parent_id = c["parent"].to_i) > 0
category[:parent_category_id] = category_id_from_imported_category_id(parent_id)
end
category
end
end
def import_topics
puts "", "creating topics"
total_count =
mysql_query("SELECT COUNT(*) count FROM #{TABLE_PREFIX}posts WHERE post_index = 1").first[
"count"
]
batches(BATCH_SIZE) do |offset|
results =
mysql_query(
"
SELECT p.post_id id,
p.topic_id topic_id,
t.forum_id category_id,
t.topic_name title,
t.topic_opened views,
t.topic_pinned pinned,
p.user_id user_id,
p.post_content raw,
p.post_date post_time
FROM #{TABLE_PREFIX}posts p,
#{TABLE_PREFIX}topics t
WHERE p.topic_id = t.topic_id
AND p.post_index = 1
ORDER BY p.post_id
LIMIT #{BATCH_SIZE}
OFFSET #{offset};
",
)
break if results.size < 1
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
create_posts(results, total: total_count, offset: offset) do |m|
created_at = Time.zone.at(m["post_time"])
{
id: m["id"],
user_id: user_id_from_imported_user_id(m["user_id"]) || -1,
raw: process_simplepress_post(m["raw"], m["id"]),
created_at: created_at,
category: category_id_from_imported_category_id(m["category_id"]),
title: CGI.unescapeHTML(m["title"]),
views: m["views"],
pinned_at: m["pinned"] == 1 ? created_at : nil,
}
end
end
end
def import_posts
puts "", "creating posts"
topic_first_post_id = {}
mysql_query(
"
SELECT t.topic_id, p.post_id
FROM #{TABLE_PREFIX}topics t
JOIN #{TABLE_PREFIX}posts p ON p.topic_id = t.topic_id
WHERE p.post_index = 1
",
).each { |r| topic_first_post_id[r["topic_id"]] = r["post_id"] }
total_count =
mysql_query("SELECT count(*) count FROM #{TABLE_PREFIX}posts WHERE post_index <> 1").first[
"count"
]
batches(BATCH_SIZE) do |offset|
results =
mysql_query(
"
SELECT p.post_id id,
p.topic_id topic_id,
p.user_id user_id,
p.post_content raw,
p.post_date post_time
FROM #{TABLE_PREFIX}posts p,
#{TABLE_PREFIX}topics t
WHERE p.topic_id = t.topic_id
AND p.post_index <> 1
ORDER BY p.post_id
LIMIT #{BATCH_SIZE}
OFFSET #{offset};
",
)
break if results.size < 1
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
create_posts(results, total: total_count, offset: offset) do |m|
if parent = topic_lookup_from_imported_post_id(topic_first_post_id[m["topic_id"]])
{
id: m["id"],
user_id: user_id_from_imported_user_id(m["user_id"]) || -1,
topic_id: parent[:topic_id],
raw: process_simplepress_post(m["raw"], m["id"]),
created_at: Time.zone.at(m["post_time"]),
}
else
puts "Parent post #{m["topic_id"]} doesn't exist. Skipping #{m["id"]}"
nil
end
end
end
end
def process_simplepress_post(raw, import_id)
s = raw.dup
# fix invalid byte sequence in UTF-8 (ArgumentError)
s.force_encoding("UTF-8") unless s.valid_encoding?
# convert the quote line
s.gsub!(/\[quote='([^']+)'.*?pid='(\d+).*?\]/) do
"[quote=\"#{convert_username($1, import_id)}, " +
post_id_to_post_num_and_topic($2, import_id) + '"]'
end
# :) is encoded as <!-- s:) --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" /><!-- s:) -->
s.gsub!(/<!-- s(\S+) -->(?:.*)<!-- s(?:\S+) -->/, '\1')
# Some links look like this: <!-- m --><a class="postlink" href="http://www.onegameamonth.com">http://www.onegameamonth.com</a><!-- m -->
s.gsub!(%r{<!-- \w --><a(?:.+)href="(\S+)"(?:.*)>(.+)</a><!-- \w -->}, '[\2](\1)')
# Many phpbb bbcode tags have a hash attached to them. Examples:
# [url=https://google.com:1qh1i7ky]click here[/url:1qh1i7ky]
# [quote="cybereality":b0wtlzex]Some text.[/quote:b0wtlzex]
s.gsub!(/:(?:\w{8})\]/, "]")
# Remove mybb video tags.
s.gsub!(%r{(^\[video=.*?\])|(\[/video\]$)}, "")
s = CGI.unescapeHTML(s)
# phpBB shortens link text like this, which breaks our markdown processing:
# [http://answers.yahoo.com/question/index ... 223AAkkPli](http://answers.yahoo.com/question/index?qid=20070920134223AAkkPli)
#
# Work around it for now:
s.gsub!(%r{\[http(s)?://(www\.)?}, "[")
s
end
def mysql_query(sql)
@client.query(sql, cache_rows: false)
end
end
ImportScripts::SimplePress.new.perform