mybb: add permalink support

This commit is contained in:
Jay Pfaffman 2017-06-30 14:11:49 -07:00
parent 16d356ab4e
commit a7d89d2a8a
1 changed files with 89 additions and 7 deletions

View File

@ -1,22 +1,38 @@
require "mysql2"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
# Before running this script, paste these lines into your shell,
# then use arrow keys to edit the values
=begin
export DB_HOST="localhost"
export DB_NAME="mybb"
export DB_PW=""
export DB_USER="root"
export TABLE_PREFIX="mybb_"
export BASE="" #
=end
# Call it like this:
# RAILS_ENV=production ruby script/import_scripts/mybb.rb
class ImportScripts::MyBB < ImportScripts::Base
MYBB_DB = "mybb_db"
TABLE_PREFIX = "mybb_"
DB_HOST ||= ENV['DB_HOST'] || "localhost"
DB_NAME ||= ENV['DB_NAME'] || "mybb"
DB_PW ||= ENV['DB_PW'] || ""
DB_USER ||= ENV['DB_USER'] || "root"
TABLE_PREFIX ||= ENV['TABLE_PREFIX'] || "mybb_"
BATCH_SIZE = 1000
BASE = ""
QUIET = true
def initialize
super
@client = Mysql2::Client.new(
host: "localhost",
username: "root",
#password: "",
database: MYBB_DB
host: DB_HOST,
username: DB_USER,
password: DB_PW,
database: DB_NAME
)
end
@ -25,6 +41,7 @@ class ImportScripts::MyBB < ImportScripts::Base
import_categories
import_posts
import_private_messages
create_permalinks
suspend_users
end
@ -214,10 +231,75 @@ class ImportScripts::MyBB < ImportScripts::Base
s
end
def create_permalinks
puts '', 'Creating redirects...', ''
SiteSetting.permalink_normalizations= '/(\\w+)-(\\d+)[-.].*/\\1-\\2.html'
puts '', 'Users...', ''
total_users = User.count
start_time = Time.now
count = 0
User.find_each do |u|
ucf = u.custom_fields
count += 1
if ucf && ucf["import_id"] && ucf["import_username"]
Permalink.create(url: "#{BASE}/user-#{ucf['import_id']}.html", external_url: "/u/#{u.username}" ) rescue nil
end
print_status(count, total_users, start_time)
end
puts '', 'Categories...', ''
total_categories = Category.count
start_time = Time.now
count = 0
Category.find_each do |cat|
ccf = cat.custom_fields
count += 1
next unless id = ccf["import_id"]
unless QUIET
puts ("forum-#{id}.html --> /c/#{cat.id}")
end
Permalink.create( url: "#{BASE}/forum-#{id}.html", category_id: cat.id ) rescue nil
print_status(count, total_categories, start_time)
end
puts '', 'Topics...', ''
total_posts = Post.count
start_time = Time.now
count = 0
puts '', 'Posts...', ''
batches(BATCH_SIZE) do |offset|
results = mysql_query("
SELECT p.pid id,
p.tid topic_id
FROM #{TABLE_PREFIX}posts p,
#{TABLE_PREFIX}threads t
WHERE p.tid = t.tid
AND t.firstpost=p.pid
ORDER BY p.dateline
LIMIT #{BATCH_SIZE}
OFFSET #{offset};
")
break if results.size < 1
results.each do |post|
count += 1
if topic = topic_lookup_from_imported_post_id(post['id'])
id = post['topic_id']
Permalink.create( url: "#{BASE}/thread-#{id}.html", topic_id: topic[:topic_id] ) rescue nil
unless QUIET
puts ("#{BASE}/thread-#{id}.html --> http://localhost:3000/t/#{topic[:topic_id]}")
end
print_status(count, total_posts, start_time)
end
end
end
end
def mysql_query(sql)
@client.query(sql, cache_rows: false)
end
end
ImportScripts::MyBB.new.perform