From a7d89d2a8ab963a342afef0d8fbdb0b29eeba551 Mon Sep 17 00:00:00 2001 From: Jay Pfaffman Date: Fri, 30 Jun 2017 14:11:49 -0700 Subject: [PATCH 1/2] mybb: add permalink support --- script/import_scripts/mybb.rb | 96 ++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/script/import_scripts/mybb.rb b/script/import_scripts/mybb.rb index c6d104e28ff..c61fb130305 100644 --- a/script/import_scripts/mybb.rb +++ b/script/import_scripts/mybb.rb @@ -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 - From 1c9cab09086a88da26f249032dc8dc3bc2cd28c8 Mon Sep 17 00:00:00 2001 From: Jay Pfaffman Date: Fri, 30 Jun 2017 16:28:14 -0700 Subject: [PATCH 2/2] mybb: disable outgoing mail --- script/import_scripts/mybb.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/script/import_scripts/mybb.rb b/script/import_scripts/mybb.rb index c61fb130305..8bf101c39ab 100644 --- a/script/import_scripts/mybb.rb +++ b/script/import_scripts/mybb.rb @@ -37,6 +37,7 @@ class ImportScripts::MyBB < ImportScripts::Base end def execute + SiteSetting.disable_emails=true import_users import_categories import_posts