mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 19:02:40 +08:00
DEV: Apply syntax_tree formatting to script/*
This commit is contained in:
@ -7,19 +7,19 @@ require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
||||
# Call it like this:
|
||||
# RAILS_ENV=production bundle exec ruby script/import_scripts/punbb.rb
|
||||
class ImportScripts::PunBB < ImportScripts::Base
|
||||
|
||||
PUNBB_DB = "punbb_db"
|
||||
BATCH_SIZE = 1000
|
||||
|
||||
def initialize
|
||||
super
|
||||
|
||||
@client = Mysql2::Client.new(
|
||||
host: "localhost",
|
||||
username: "root",
|
||||
password: "pa$$word",
|
||||
database: PUNBB_DB
|
||||
)
|
||||
@client =
|
||||
Mysql2::Client.new(
|
||||
host: "localhost",
|
||||
username: "root",
|
||||
password: "pa$$word",
|
||||
database: PUNBB_DB,
|
||||
)
|
||||
end
|
||||
|
||||
def execute
|
||||
@ -30,36 +30,41 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
end
|
||||
|
||||
def import_users
|
||||
puts '', "creating users"
|
||||
puts "", "creating users"
|
||||
|
||||
total_count = mysql_query("SELECT count(*) count FROM users;").first['count']
|
||||
total_count = mysql_query("SELECT count(*) count FROM users;").first["count"]
|
||||
|
||||
batches(BATCH_SIZE) do |offset|
|
||||
results = mysql_query(
|
||||
"SELECT id, username, realname name, url website, email email, registered created_at,
|
||||
results =
|
||||
mysql_query(
|
||||
"SELECT id, username, realname name, url website, email email, registered created_at,
|
||||
registration_ip registration_ip_address, last_visit last_visit_time, last_email_sent last_emailed_at,
|
||||
last_email_sent last_emailed_at, location, group_id
|
||||
FROM users
|
||||
LIMIT #{BATCH_SIZE}
|
||||
OFFSET #{offset};")
|
||||
OFFSET #{offset};",
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :users, results.map { |u| u["id"].to_i }
|
||||
|
||||
create_users(results, total: total_count, offset: offset) do |user|
|
||||
{ id: user['id'],
|
||||
email: user['email'],
|
||||
username: user['username'],
|
||||
name: user['name'],
|
||||
created_at: Time.zone.at(user['created_at']),
|
||||
website: user['website'],
|
||||
registration_ip_address: user['registration_ip_address'],
|
||||
last_seen_at: Time.zone.at(user['last_visit_time']),
|
||||
last_emailed_at: user['last_emailed_at'] == nil ? 0 : Time.zone.at(user['last_emailed_at']),
|
||||
location: user['location'],
|
||||
moderator: user['group_id'] == 4,
|
||||
admin: user['group_id'] == 1 }
|
||||
{
|
||||
id: user["id"],
|
||||
email: user["email"],
|
||||
username: user["username"],
|
||||
name: user["name"],
|
||||
created_at: Time.zone.at(user["created_at"]),
|
||||
website: user["website"],
|
||||
registration_ip_address: user["registration_ip_address"],
|
||||
last_seen_at: Time.zone.at(user["last_visit_time"]),
|
||||
last_emailed_at:
|
||||
user["last_emailed_at"] == nil ? 0 : Time.zone.at(user["last_emailed_at"]),
|
||||
location: user["location"],
|
||||
moderator: user["group_id"] == 4,
|
||||
admin: user["group_id"] == 1,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -67,33 +72,34 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
def import_categories
|
||||
puts "", "importing top level categories..."
|
||||
|
||||
categories = mysql_query("
|
||||
categories =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT id, cat_name name, disp_position position
|
||||
FROM categories
|
||||
ORDER BY id ASC
|
||||
").to_a
|
||||
",
|
||||
).to_a
|
||||
|
||||
create_categories(categories) do |category|
|
||||
{
|
||||
id: category["id"],
|
||||
name: category["name"]
|
||||
}
|
||||
end
|
||||
create_categories(categories) { |category| { id: category["id"], name: category["name"] } }
|
||||
|
||||
puts "", "importing children categories..."
|
||||
|
||||
children_categories = mysql_query("
|
||||
children_categories =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT id, forum_name name, forum_desc description, disp_position position, cat_id parent_category_id
|
||||
FROM forums
|
||||
ORDER BY id
|
||||
").to_a
|
||||
",
|
||||
).to_a
|
||||
|
||||
create_categories(children_categories) do |category|
|
||||
{
|
||||
id: "child##{category['id']}",
|
||||
id: "child##{category["id"]}",
|
||||
name: category["name"],
|
||||
description: category["description"],
|
||||
parent_category_id: category_id_from_imported_category_id(category["parent_category_id"])
|
||||
parent_category_id: category_id_from_imported_category_id(category["parent_category_id"]),
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -104,7 +110,9 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
total_count = mysql_query("SELECT count(*) count from posts").first["count"]
|
||||
|
||||
batches(BATCH_SIZE) do |offset|
|
||||
results = mysql_query("
|
||||
results =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT p.id id,
|
||||
t.id topic_id,
|
||||
t.forum_id category_id,
|
||||
@ -119,29 +127,30 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
ORDER BY p.posted
|
||||
LIMIT #{BATCH_SIZE}
|
||||
OFFSET #{offset};
|
||||
").to_a
|
||||
",
|
||||
).to_a
|
||||
|
||||
break if results.size < 1
|
||||
next if all_records_exist? :posts, results.map { |m| m['id'].to_i }
|
||||
next if all_records_exist? :posts, results.map { |m| m["id"].to_i }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |m|
|
||||
skip = false
|
||||
mapped = {}
|
||||
|
||||
mapped[:id] = m['id']
|
||||
mapped[:user_id] = user_id_from_imported_user_id(m['user_id']) || -1
|
||||
mapped[:raw] = process_punbb_post(m['raw'], m['id'])
|
||||
mapped[:created_at] = Time.zone.at(m['created_at'])
|
||||
mapped[:id] = m["id"]
|
||||
mapped[:user_id] = user_id_from_imported_user_id(m["user_id"]) || -1
|
||||
mapped[:raw] = process_punbb_post(m["raw"], m["id"])
|
||||
mapped[:created_at] = Time.zone.at(m["created_at"])
|
||||
|
||||
if m['id'] == m['first_post_id']
|
||||
mapped[:category] = category_id_from_imported_category_id("child##{m['category_id']}")
|
||||
mapped[:title] = CGI.unescapeHTML(m['title'])
|
||||
if m["id"] == m["first_post_id"]
|
||||
mapped[:category] = category_id_from_imported_category_id("child##{m["category_id"]}")
|
||||
mapped[:title] = CGI.unescapeHTML(m["title"])
|
||||
else
|
||||
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
|
||||
parent = topic_lookup_from_imported_post_id(m["first_post_id"])
|
||||
if parent
|
||||
mapped[:topic_id] = parent[:topic_id]
|
||||
else
|
||||
puts "Parent post #{m['first_post_id']} doesn't exist. Skipping #{m["id"]}: #{m["title"][0..40]}"
|
||||
puts "Parent post #{m["first_post_id"]} doesn't exist. Skipping #{m["id"]}: #{m["title"][0..40]}"
|
||||
skip = true
|
||||
end
|
||||
end
|
||||
@ -152,16 +161,16 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
end
|
||||
|
||||
def suspend_users
|
||||
puts '', "updating banned users"
|
||||
puts "", "updating banned users"
|
||||
|
||||
banned = 0
|
||||
failed = 0
|
||||
total = mysql_query("SELECT count(*) count FROM bans").first['count']
|
||||
total = mysql_query("SELECT count(*) count FROM bans").first["count"]
|
||||
|
||||
system_user = Discourse.system_user
|
||||
|
||||
mysql_query("SELECT username, email FROM bans").each do |b|
|
||||
user = User.find_by_email(b['email'])
|
||||
user = User.find_by_email(b["email"])
|
||||
if user
|
||||
user.suspended_at = Time.now
|
||||
user.suspended_till = 200.years.from_now
|
||||
@ -174,7 +183,7 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
failed += 1
|
||||
end
|
||||
else
|
||||
puts "Not found: #{b['email']}"
|
||||
puts "Not found: #{b["email"]}"
|
||||
failed += 1
|
||||
end
|
||||
|
||||
@ -189,15 +198,15 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
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!(/<!-- \w --><a(?:.+)href="(\S+)"(?:.*)>(.+)<\/a><!-- \w -->/, '[\2](\1)')
|
||||
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})\]/, ']')
|
||||
s.gsub!(/:(?:\w{8})\]/, "]")
|
||||
|
||||
# Remove mybb video tags.
|
||||
s.gsub!(/(^\[video=.*?\])|(\[\/video\]$)/, '')
|
||||
s.gsub!(%r{(^\[video=.*?\])|(\[/video\]$)}, "")
|
||||
|
||||
s = CGI.unescapeHTML(s)
|
||||
|
||||
@ -205,7 +214,7 @@ class ImportScripts::PunBB < ImportScripts::Base
|
||||
# [http://answers.yahoo.com/question/index ... 223AAkkPli](http://answers.yahoo.com/question/index?qid=20070920134223AAkkPli)
|
||||
#
|
||||
# Work around it for now:
|
||||
s.gsub!(/\[http(s)?:\/\/(www\.)?/, '[')
|
||||
s.gsub!(%r{\[http(s)?://(www\.)?}, "[")
|
||||
|
||||
s
|
||||
end
|
||||
|
Reference in New Issue
Block a user