DEV: Apply syntax_tree formatting to script/*
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'mysql2'
|
||||
require "mysql2"
|
||||
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
||||
|
||||
class ImportScripts::SimplePress < ImportScripts::Base
|
||||
|
||||
SIMPLE_PRESS_DB ||= ENV['SIMPLEPRESS_DB'] || "simplepress"
|
||||
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,
|
||||
)
|
||||
@client = Mysql2::Client.new(host: "localhost", username: "root", database: SIMPLE_PRESS_DB)
|
||||
|
||||
SiteSetting.max_username_length = 50
|
||||
end
|
||||
@@ -32,10 +27,11 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
puts "", "importing users..."
|
||||
|
||||
last_user_id = -1
|
||||
total_users = mysql_query("SELECT COUNT(*) count FROM wp_users WHERE user_email LIKE '%@%'").first["count"]
|
||||
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
|
||||
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 '%@%'
|
||||
@@ -43,7 +39,6 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
ORDER BY id
|
||||
LIMIT #{BATCH_SIZE}
|
||||
SQL
|
||||
).to_a
|
||||
|
||||
break if users.empty?
|
||||
|
||||
@@ -55,13 +50,12 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
user_ids_sql = user_ids.join(",")
|
||||
|
||||
users_description = {}
|
||||
mysql_query(<<-SQL
|
||||
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
|
||||
).each { |um| users_description[um["user_id"]] = um["description"] }
|
||||
|
||||
create_users(users, total: total_users, offset: offset) do |u|
|
||||
{
|
||||
@@ -71,7 +65,7 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
name: u["display_name"],
|
||||
created_at: u["user_registered"],
|
||||
website: u["user_url"],
|
||||
bio_raw: users_description[u["id"]]
|
||||
bio_raw: users_description[u["id"]],
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -80,16 +74,20 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
def import_categories
|
||||
puts "", "importing categories..."
|
||||
|
||||
categories = mysql_query(<<-SQL
|
||||
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 = {
|
||||
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
|
||||
@@ -99,10 +97,15 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
def import_topics
|
||||
puts "", "creating topics"
|
||||
|
||||
total_count = mysql_query("SELECT COUNT(*) count FROM #{TABLE_PREFIX}posts WHERE post_index = 1").first["count"]
|
||||
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("
|
||||
results =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT p.post_id id,
|
||||
p.topic_id topic_id,
|
||||
t.forum_id category_id,
|
||||
@@ -119,23 +122,24 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
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 }
|
||||
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'])
|
||||
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']),
|
||||
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,
|
||||
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
|
||||
@@ -146,17 +150,24 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
|
||||
topic_first_post_id = {}
|
||||
|
||||
mysql_query("
|
||||
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"] }
|
||||
",
|
||||
).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"]
|
||||
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("
|
||||
results =
|
||||
mysql_query(
|
||||
"
|
||||
SELECT p.post_id id,
|
||||
p.topic_id topic_id,
|
||||
p.user_id user_id,
|
||||
@@ -169,23 +180,24 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
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 }
|
||||
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']])
|
||||
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,
|
||||
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']),
|
||||
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"]}"
|
||||
puts "Parent post #{m["topic_id"]} doesn't exist. Skipping #{m["id"]}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
@@ -196,28 +208,27 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
s = raw.dup
|
||||
|
||||
# fix invalid byte sequence in UTF-8 (ArgumentError)
|
||||
unless s.valid_encoding?
|
||||
s.force_encoding("UTF-8")
|
||||
end
|
||||
s.force_encoding("UTF-8") unless s.valid_encoding?
|
||||
|
||||
# convert the quote line
|
||||
s.gsub!(/\[quote='([^']+)'.*?pid='(\d+).*?\]/) {
|
||||
"[quote=\"#{convert_username($1, import_id)}, " + post_id_to_post_num_and_topic($2, import_id) + '"]'
|
||||
}
|
||||
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!(/<!-- \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)
|
||||
|
||||
@@ -225,7 +236,7 @@ class ImportScripts::SimplePress < 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
|
||||
@@ -233,7 +244,6 @@ class ImportScripts::SimplePress < ImportScripts::Base
|
||||
def mysql_query(sql)
|
||||
@client.query(sql, cache_rows: false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
ImportScripts::SimplePress.new.perform
|
||||
|
||||
Reference in New Issue
Block a user