2014-05-18 09:33:34 +08:00
# `dropdb bbpress`
# `createdb bbpress`
# `bundle exec rake db:migrate`
2015-03-17 01:18:20 +08:00
require 'mysql2'
2014-05-31 03:09:58 +08:00
require File . expand_path ( File . dirname ( __FILE__ ) + " /base.rb " )
2014-05-18 09:33:34 +08:00
2015-03-17 01:18:20 +08:00
BB_PRESS_DB = ENV [ 'BBPRESS_DB' ] || " bbpress "
2015-02-04 02:06:35 +08:00
DB_TABLE_PREFIX = " wp_ "
2014-05-18 09:33:34 +08:00
2014-05-31 03:09:58 +08:00
class ImportScripts :: Bbpress < ImportScripts :: Base
2014-05-18 09:33:34 +08:00
2014-05-31 03:09:58 +08:00
def initialize
super
2014-05-18 09:33:34 +08:00
2014-05-31 03:09:58 +08:00
@client = Mysql2 :: Client . new (
host : " localhost " ,
username : " root " ,
#password: "password",
database : BB_PRESS_DB
)
end
2014-05-18 09:33:34 +08:00
2015-02-04 02:06:35 +08:00
def table_name ( name )
DB_TABLE_PREFIX + name
end
2014-05-31 03:09:58 +08:00
def execute
users_results = @client . query ( "
2015-02-04 02:06:35 +08:00
SELECT id ,
2014-05-31 03:09:58 +08:00
user_login username ,
display_name name ,
user_url website ,
user_email email ,
user_registered created_at
2015-02-04 02:06:35 +08:00
FROM #{table_name 'users'}", cache_rows: false)
2014-05-31 03:09:58 +08:00
create_users ( users_results ) do | u |
ActiveSupport :: HashWithIndifferentAccess . new ( u )
end
2014-05-18 09:33:34 +08:00
2015-03-17 01:18:20 +08:00
create_categories ( @client . query ( " SELECT id, post_name, post_parent from #{ table_name 'posts' } WHERE post_type = 'forum' AND post_name != '' ORDER BY post_parent " ) ) do | c |
result = { id : c [ 'id' ] , name : c [ 'post_name' ] }
parent_id = c [ 'post_parent' ] . to_i
if parent_id > 0
result [ :parent_category_id ] = category_id_from_imported_category_id ( parent_id )
end
result
2014-05-31 03:09:58 +08:00
end
2014-05-18 09:33:34 +08:00
2014-05-31 03:09:58 +08:00
import_posts
end
2014-05-18 09:33:34 +08:00
2014-05-31 03:09:58 +08:00
def import_posts
puts '' , " creating topics and posts "
total_count = @client . query ( "
2015-02-04 02:06:35 +08:00
SELECT count ( * ) count
FROM #{table_name 'posts'}
WHERE post_status < > 'spam'
AND post_type IN ( 'topic' , 'reply' ) " ).first['count']
2014-05-31 03:09:58 +08:00
batch_size = 1000
batches ( batch_size ) do | offset |
results = @client . query ( "
2015-02-04 02:06:35 +08:00
SELECT id ,
2014-05-31 03:09:58 +08:00
post_author ,
post_date ,
post_content ,
post_title ,
post_type ,
post_parent
2015-02-04 02:06:35 +08:00
FROM #{table_name 'posts'}
WHERE post_status < > 'spam'
AND post_type IN ( 'topic' , 'reply' )
ORDER BY id
LIMIT #{batch_size}
OFFSET #{offset}", cache_rows: false)
2014-05-31 03:09:58 +08:00
break if results . size < 1
create_posts ( results , total : total_count , offset : offset ) do | post |
skip = false
mapped = { }
mapped [ :id ] = post [ " id " ]
mapped [ :user_id ] = user_id_from_imported_user_id ( post [ " post_author " ] ) || find_user_by_import_id ( post [ " post_author " ] ) . try ( :id ) || - 1
mapped [ :raw ] = post [ " post_content " ]
mapped [ :created_at ] = post [ " post_date " ]
mapped [ :custom_fields ] = { import_id : post [ " id " ] }
if post [ " post_type " ] == " topic "
2015-03-13 04:15:02 +08:00
mapped [ :category ] = category_id_from_imported_category_id ( post [ " post_parent " ] )
2014-05-31 03:09:58 +08:00
mapped [ :title ] = CGI . unescapeHTML post [ " post_title " ]
else
parent = topic_lookup_from_imported_post_id ( post [ " post_parent " ] )
if parent
mapped [ :topic_id ] = parent [ :topic_id ]
mapped [ :reply_to_post_number ] = parent [ :post_number ] if parent [ :post_number ] > 1
else
puts " Skipping #{ post [ " id " ] } : #{ post [ " post_content " ] [ 0 .. 40 ] } "
skip = true
end
end
skip ? nil : mapped
end
2014-05-18 09:33:34 +08:00
end
end
end
2014-05-31 03:09:58 +08:00
ImportScripts :: Bbpress . new . perform