2015-03-16 13:18:20 -04:00
|
|
|
require 'mysql2'
|
2014-05-30 15:09:58 -04:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2016-09-26 18:03:42 -04:00
|
|
|
# Before running this script, paste these lines into your shell,
|
|
|
|
# then use arrow keys to edit the values
|
|
|
|
=begin
|
2017-05-25 07:15:50 -04:00
|
|
|
export BBPRESS_HOST="localhost"
|
2016-09-26 18:03:42 -04:00
|
|
|
export BBPRESS_USER="root"
|
|
|
|
export BBPRESS_DB="bbpress"
|
|
|
|
export BBPRESS_PW=""
|
|
|
|
=end
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
class ImportScripts::Bbpress < ImportScripts::Base
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2017-05-25 07:15:50 -04:00
|
|
|
BB_PRESS_HOST ||= ENV['BBPRESS_HOST'] || "localhost"
|
2016-03-07 12:21:09 -05:00
|
|
|
BB_PRESS_DB ||= ENV['BBPRESS_DB'] || "bbpress"
|
|
|
|
BATCH_SIZE ||= 1000
|
2016-09-26 18:03:42 -04:00
|
|
|
BB_PRESS_PW ||= ENV['BBPRESS_PW'] || ""
|
|
|
|
BB_PRESS_USER ||= ENV['BBPRESS_USER'] || "root"
|
2017-01-10 05:37:13 -05:00
|
|
|
BB_PRESS_PREFIX ||= ENV['BBPRESS_PREFIX'] || "wp_"
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
def initialize
|
|
|
|
super
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2017-11-07 11:50:43 -05:00
|
|
|
@he = HTMLEntities.new
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
@client = Mysql2::Client.new(
|
2017-05-25 07:15:50 -04:00
|
|
|
host: BB_PRESS_HOST,
|
2016-09-26 18:03:42 -04:00
|
|
|
username: BB_PRESS_USER,
|
2016-03-07 12:21:09 -05:00
|
|
|
database: BB_PRESS_DB,
|
2016-09-26 18:03:42 -04:00
|
|
|
password: BB_PRESS_PW,
|
2014-05-30 15:09:58 -04:00
|
|
|
)
|
|
|
|
end
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
def execute
|
|
|
|
import_users
|
2017-05-25 07:15:50 -04:00
|
|
|
import_anonymous_users
|
2016-03-07 12:21:09 -05:00
|
|
|
import_categories
|
|
|
|
import_topics_and_posts
|
2017-08-21 16:39:48 -04:00
|
|
|
import_private_messages
|
2017-11-07 11:50:43 -05:00
|
|
|
create_permalinks
|
2015-02-03 13:06:35 -05:00
|
|
|
end
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
def import_users
|
|
|
|
puts "", "importing users..."
|
|
|
|
|
|
|
|
last_user_id = -1
|
2017-11-07 11:50:43 -05:00
|
|
|
total_users = bbpress_query(<<-SQL
|
|
|
|
SELECT COUNT(DISTINCT(u.id)) AS cnt
|
2017-11-07 14:20:46 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}users u
|
|
|
|
LEFT JOIN #{BB_PRESS_PREFIX}posts p ON p.post_author = u.id
|
|
|
|
WHERE p.post_type IN ('forum', 'reply', 'topic')
|
2017-11-07 11:50:43 -05:00
|
|
|
AND user_email LIKE '%@%'
|
|
|
|
SQL
|
|
|
|
).first["cnt"]
|
2016-03-07 12:21:09 -05:00
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
users = bbpress_query(<<-SQL
|
2017-11-07 11:50:43 -05:00
|
|
|
SELECT u.id, user_nicename, display_name, user_email, user_registered, user_url, user_pass
|
|
|
|
FROM #{BB_PRESS_PREFIX}users u
|
2017-11-07 14:20:46 -05:00
|
|
|
LEFT JOIN #{BB_PRESS_PREFIX}posts p ON p.post_author = u.id
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE user_email LIKE '%@%'
|
2017-11-07 11:50:43 -05:00
|
|
|
AND p.post_type IN ('forum', 'reply', 'topic')
|
|
|
|
AND u.id > #{last_user_id}
|
|
|
|
GROUP BY u.id
|
|
|
|
ORDER BY u.id
|
2016-03-07 12:21:09 -05:00
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
SQL
|
|
|
|
).to_a
|
|
|
|
|
|
|
|
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 = {}
|
|
|
|
bbpress_query(<<-SQL
|
|
|
|
SELECT user_id, meta_value description
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}usermeta
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE user_id IN (#{user_ids_sql})
|
|
|
|
AND meta_key = 'description'
|
|
|
|
SQL
|
|
|
|
).each { |um| users_description[um["user_id"]] = um["description"] }
|
|
|
|
|
|
|
|
users_last_activity = {}
|
|
|
|
bbpress_query(<<-SQL
|
|
|
|
SELECT user_id, meta_value last_activity
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}usermeta
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE user_id IN (#{user_ids_sql})
|
|
|
|
AND meta_key = 'last_activity'
|
|
|
|
SQL
|
|
|
|
).each { |um| users_last_activity[um["user_id"]] = um["last_activity"] }
|
|
|
|
|
|
|
|
create_users(users, total: total_users, offset: offset) do |u|
|
|
|
|
{
|
|
|
|
id: u["id"].to_i,
|
|
|
|
username: u["user_nicename"],
|
2017-11-07 11:50:43 -05:00
|
|
|
password: u["user_pass"],
|
2016-03-07 12:21:09 -05:00
|
|
|
email: u["user_email"].downcase,
|
2017-01-11 15:55:25 -05:00
|
|
|
name: u["display_name"].presence || u['user_nicename'],
|
2016-03-07 12:21:09 -05:00
|
|
|
created_at: u["user_registered"],
|
|
|
|
website: u["user_url"],
|
|
|
|
bio_raw: users_description[u["id"]],
|
|
|
|
last_seen_at: users_last_activity[u["id"]],
|
|
|
|
}
|
|
|
|
end
|
2014-05-30 15:09:58 -04:00
|
|
|
end
|
2016-03-07 12:21:09 -05:00
|
|
|
end
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2017-05-25 07:15:50 -04:00
|
|
|
def import_anonymous_users
|
|
|
|
puts "", "importing anonymous users..."
|
|
|
|
|
|
|
|
anon_posts = Hash.new
|
|
|
|
anon_names = Hash.new
|
|
|
|
emails = Array.new
|
|
|
|
|
|
|
|
# gather anonymous users via postmeta table
|
|
|
|
bbpress_query(<<-SQL
|
|
|
|
SELECT post_id, meta_key, meta_value
|
|
|
|
FROM #{BB_PRESS_PREFIX}postmeta
|
|
|
|
WHERE meta_key LIKE '_bbp_anonymous%'
|
|
|
|
SQL
|
|
|
|
).each do |pm|
|
|
|
|
anon_posts[pm['post_id']] = Hash.new if not anon_posts[pm['post_id']]
|
|
|
|
|
|
|
|
if pm['meta_key'] == '_bbp_anonymous_email'
|
|
|
|
anon_posts[pm['post_id']]['email'] = pm['meta_value']
|
|
|
|
end
|
|
|
|
if pm['meta_key'] == '_bbp_anonymous_name'
|
|
|
|
anon_posts[pm['post_id']]['name'] = pm['meta_value']
|
|
|
|
end
|
|
|
|
if pm['meta_key'] == '_bbp_anonymous_website'
|
|
|
|
anon_posts[pm['post_id']]['website'] = pm['meta_value']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# gather every existent username
|
2017-07-27 21:20:09 -04:00
|
|
|
anon_posts.each do |id, post|
|
2017-05-25 07:15:50 -04:00
|
|
|
anon_names[post['name']] = Hash.new if not anon_names[post['name']]
|
|
|
|
# overwriting email address, one user can only use one email address
|
|
|
|
anon_names[post['name']]['email'] = post['email']
|
|
|
|
anon_names[post['name']]['website'] = post['website'] if post['website'] != ''
|
|
|
|
end
|
|
|
|
|
|
|
|
# make sure every user name has a unique email address
|
2017-07-27 21:20:09 -04:00
|
|
|
anon_names.each do |k, name|
|
2017-05-25 07:15:50 -04:00
|
|
|
if not emails.include? name['email']
|
|
|
|
emails.push ( name['email'])
|
|
|
|
else
|
|
|
|
name['email'] = "anonymous_#{SecureRandom.hex}@no-email.invalid"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
create_users(anon_names) do |k, n|
|
|
|
|
{
|
|
|
|
id: k,
|
|
|
|
email: n["email"].downcase,
|
|
|
|
name: k,
|
|
|
|
website: n["website"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
def import_categories
|
|
|
|
puts "", "importing categories..."
|
2015-03-20 15:54:42 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
categories = bbpress_query(<<-SQL
|
|
|
|
SELECT id, post_name, post_parent
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}posts
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE post_type = 'forum'
|
|
|
|
AND LENGTH(COALESCE(post_name, '')) > 0
|
|
|
|
ORDER BY post_parent, id
|
|
|
|
SQL
|
|
|
|
)
|
2015-03-20 15:54:42 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
create_categories(categories) do |c|
|
|
|
|
category = { id: c['id'], name: c['post_name'] }
|
|
|
|
if (parent_id = c['post_parent'].to_i) > 0
|
|
|
|
category[:parent_category_id] = category_id_from_imported_category_id(parent_id)
|
2015-03-16 13:18:20 -04:00
|
|
|
end
|
2016-03-07 12:21:09 -05:00
|
|
|
category
|
2014-05-30 15:09:58 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-17 21:33:34 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
def import_topics_and_posts
|
|
|
|
puts "", "importing topics and posts..."
|
2014-05-30 15:09:58 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
last_post_id = -1
|
|
|
|
total_posts = bbpress_query(<<-SQL
|
|
|
|
SELECT COUNT(*) count
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}posts
|
2015-02-03 13:06:35 -05:00
|
|
|
WHERE post_status <> 'spam'
|
2016-03-07 12:21:09 -05:00
|
|
|
AND post_type IN ('topic', 'reply')
|
|
|
|
SQL
|
|
|
|
).first["count"]
|
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
posts = bbpress_query(<<-SQL
|
|
|
|
SELECT id,
|
|
|
|
post_author,
|
|
|
|
post_date,
|
|
|
|
post_content,
|
|
|
|
post_title,
|
|
|
|
post_type,
|
|
|
|
post_parent
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}posts
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE post_status <> 'spam'
|
|
|
|
AND post_type IN ('topic', 'reply')
|
|
|
|
AND id > #{last_post_id}
|
|
|
|
ORDER BY id
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
SQL
|
|
|
|
).to_a
|
|
|
|
|
|
|
|
break if posts.empty?
|
|
|
|
|
|
|
|
last_post_id = posts[-1]["id"].to_i
|
|
|
|
post_ids = posts.map { |p| p["id"].to_i }
|
|
|
|
|
|
|
|
next if all_records_exist?(:posts, post_ids)
|
|
|
|
|
|
|
|
post_ids_sql = post_ids.join(",")
|
|
|
|
|
|
|
|
posts_likes = {}
|
|
|
|
bbpress_query(<<-SQL
|
|
|
|
SELECT post_id, meta_value likes
|
2017-01-10 05:37:13 -05:00
|
|
|
FROM #{BB_PRESS_PREFIX}postmeta
|
2016-03-07 12:21:09 -05:00
|
|
|
WHERE post_id IN (#{post_ids_sql})
|
|
|
|
AND meta_key = 'Likes'
|
|
|
|
SQL
|
|
|
|
).each { |pm| posts_likes[pm["post_id"]] = pm["likes"].to_i }
|
|
|
|
|
2017-05-25 07:15:50 -04:00
|
|
|
anon_names = {}
|
|
|
|
bbpress_query(<<-SQL
|
|
|
|
SELECT post_id, meta_value
|
|
|
|
FROM #{BB_PRESS_PREFIX}postmeta
|
|
|
|
WHERE post_id IN (#{post_ids_sql})
|
|
|
|
AND meta_key = '_bbp_anonymous_name'
|
|
|
|
SQL
|
|
|
|
).each { |pm| anon_names[pm["post_id"]] = pm["meta_value"] }
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
create_posts(posts, total: total_posts, offset: offset) do |p|
|
2014-05-30 15:09:58 -04:00
|
|
|
skip = false
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
post = {
|
|
|
|
id: p["id"],
|
2017-05-25 07:15:50 -04:00
|
|
|
user_id: user_id_from_imported_user_id(p["post_author"]) || find_user_by_import_id(p["post_author"]).try(:id) ||
|
|
|
|
user_id_from_imported_user_id(anon_names[p['id']]) || find_user_by_import_id(anon_names[p['id']]).try(:id) || -1,
|
2016-03-07 12:21:09 -05:00
|
|
|
raw: p["post_content"],
|
|
|
|
created_at: p["post_date"],
|
|
|
|
like_count: posts_likes[p["id"]],
|
|
|
|
}
|
|
|
|
|
|
|
|
if post[:raw].present?
|
2017-11-07 11:50:43 -05:00
|
|
|
post[:raw].gsub!(/\<pre\>\<code(=[a-z]*)?\>(.*?)\<\/code\>\<\/pre\>/im) { "```\n#{@he.decode($2)}\n```" }
|
2015-03-20 15:54:42 -04:00
|
|
|
end
|
2014-05-30 15:09:58 -04:00
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
if p["post_type"] == "topic"
|
|
|
|
post[:category] = category_id_from_imported_category_id(p["post_parent"])
|
|
|
|
post[:title] = CGI.unescapeHTML(p["post_title"])
|
2014-05-30 15:09:58 -04:00
|
|
|
else
|
2016-03-07 12:21:09 -05:00
|
|
|
if parent = topic_lookup_from_imported_post_id(p["post_parent"])
|
|
|
|
post[:topic_id] = parent[:topic_id]
|
|
|
|
post[:reply_to_post_number] = parent[:post_number] if parent[:post_number] > 1
|
2014-05-30 15:09:58 -04:00
|
|
|
else
|
2016-03-07 12:21:09 -05:00
|
|
|
puts "Skipping #{p["id"]}: #{p["post_content"][0..40]}"
|
2014-05-30 15:09:58 -04:00
|
|
|
skip = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
skip ? nil : post
|
2014-05-30 15:09:58 -04:00
|
|
|
end
|
2014-05-17 21:33:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-07 11:50:43 -05:00
|
|
|
def create_permalinks
|
|
|
|
puts "", "creating permalinks..."
|
|
|
|
|
|
|
|
last_topic_id = -1
|
|
|
|
total_topics = bbpress_query(<<-SQL
|
|
|
|
SELECT COUNT(*) count
|
|
|
|
FROM #{BB_PRESS_PREFIX}posts
|
|
|
|
WHERE post_status <> 'spam'
|
|
|
|
AND post_type IN ('topic')
|
|
|
|
SQL
|
|
|
|
).first["count"]
|
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
topics = bbpress_query(<<-SQL
|
|
|
|
SELECT id,
|
|
|
|
guid
|
|
|
|
FROM #{BB_PRESS_PREFIX}posts
|
|
|
|
WHERE post_status <> 'spam'
|
|
|
|
AND post_type IN ('topic')
|
|
|
|
AND id > #{last_topic_id}
|
|
|
|
ORDER BY id
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
SQL
|
|
|
|
).to_a
|
|
|
|
break if topics.empty?
|
|
|
|
|
|
|
|
topics.each do |t|
|
|
|
|
topic = topic_lookup_from_imported_post_id(t['id'])
|
2017-11-07 14:20:46 -05:00
|
|
|
Permalink.create(url: URI.parse(t['guid']).path.chomp('/'), topic_id: topic[:topic_id]) rescue nil
|
2017-11-07 11:50:43 -05:00
|
|
|
end
|
|
|
|
last_topic_id = topics[-1]["id"].to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-21 16:39:48 -04:00
|
|
|
def import_private_messages
|
|
|
|
puts "", "importing private messages..."
|
|
|
|
|
|
|
|
last_post_id = -1
|
|
|
|
total_posts = bbpress_query("SELECT COUNT(*) count FROM #{BB_PRESS_PREFIX}bp_messages_messages").first["count"]
|
|
|
|
|
|
|
|
threads = {}
|
|
|
|
|
|
|
|
total_count = bbpress_query("SELECT COUNT(*) count FROM #{BB_PRESS_PREFIX}bp_messages_recipients").first["count"]
|
|
|
|
current_count = 0
|
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
rows = bbpress_query(<<-SQL
|
|
|
|
SELECT thread_id, user_id
|
|
|
|
FROM #{BB_PRESS_PREFIX}bp_messages_recipients
|
|
|
|
ORDER BY id
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
OFFSET #{offset}
|
|
|
|
SQL
|
|
|
|
).to_a
|
|
|
|
|
|
|
|
break if rows.empty?
|
|
|
|
|
|
|
|
rows.each do |row|
|
|
|
|
current_count += 1
|
|
|
|
print_status(current_count, total_count, get_start_time('private_messages'))
|
|
|
|
|
|
|
|
threads[row['thread_id']] ||= {
|
|
|
|
target_user_ids: [],
|
|
|
|
imported_topic_id: nil
|
|
|
|
}
|
|
|
|
user_id = user_id_from_imported_user_id(row['user_id'])
|
|
|
|
if user_id && !threads[row['thread_id']][:target_user_ids].include?(user_id)
|
|
|
|
threads[row['thread_id']][:target_user_ids] << user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
batches(BATCH_SIZE) do |offset|
|
|
|
|
posts = bbpress_query(<<-SQL
|
|
|
|
SELECT id,
|
|
|
|
thread_id,
|
|
|
|
date_sent,
|
|
|
|
sender_id,
|
|
|
|
subject,
|
|
|
|
message
|
|
|
|
FROM wp_bp_messages_messages
|
|
|
|
WHERE id > #{last_post_id}
|
|
|
|
ORDER BY thread_id, date_sent
|
|
|
|
LIMIT #{BATCH_SIZE}
|
|
|
|
SQL
|
|
|
|
).to_a
|
|
|
|
|
|
|
|
break if posts.empty?
|
|
|
|
|
|
|
|
last_post_id = posts[-1]["id"].to_i
|
|
|
|
|
|
|
|
create_posts(posts, total: total_posts, offset: offset) do |post|
|
|
|
|
if tcf = TopicCustomField.where(name: 'bb_thread_id', value: post['thread_id']).first
|
|
|
|
{
|
|
|
|
id: "pm#{post['id']}",
|
|
|
|
topic_id: threads[post['thread_id']][:imported_topic_id],
|
|
|
|
user_id: user_id_from_imported_user_id(post['sender_id']) || find_user_by_import_id(post['sender_id'])&.id || -1,
|
|
|
|
raw: post['message'],
|
|
|
|
created_at: post['date_sent'],
|
|
|
|
}
|
|
|
|
else
|
|
|
|
# First post of the thread
|
|
|
|
{
|
|
|
|
id: "pm#{post['id']}",
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
user_id: user_id_from_imported_user_id(post['sender_id']) || find_user_by_import_id(post['sender_id'])&.id || -1,
|
|
|
|
title: post['subject'],
|
|
|
|
raw: post['message'],
|
|
|
|
created_at: post['date_sent'],
|
|
|
|
target_usernames: User.where(id: threads[post['thread_id']][:target_user_ids]).pluck(:username),
|
|
|
|
post_create_action: proc do |new_post|
|
|
|
|
if topic = new_post.topic
|
|
|
|
threads[post['thread_id']][:imported_topic_id] = topic.id
|
|
|
|
TopicCustomField.create(topic_id: topic.id, name: 'bb_thread_id', value: post['thread_id'])
|
|
|
|
else
|
|
|
|
puts "Error in post_create_action! Can't find topic!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 12:21:09 -05:00
|
|
|
def bbpress_query(sql)
|
|
|
|
@client.query(sql, cache_rows: false)
|
|
|
|
end
|
|
|
|
|
2014-05-17 21:33:34 -04:00
|
|
|
end
|
|
|
|
|
2014-05-30 15:09:58 -04:00
|
|
|
ImportScripts::Bbpress.new.perform
|