2014-05-17 21:33:34 -04:00
# `dropdb bbpress`
# `createdb bbpress`
# `bundle exec rake db:migrate`
2015-03-16 13:18:20 -04:00
require 'mysql2'
2014-05-30 15:09:58 -04:00
require File . expand_path ( File . dirname ( __FILE__ ) + " /base.rb " )
2014-05-17 21:33:34 -04:00
2015-03-16 13:18:20 -04:00
BB_PRESS_DB = ENV [ 'BBPRESS_DB' ] || " bbpress "
2015-02-03 13:06:35 -05:00
DB_TABLE_PREFIX = " wp_ "
2014-05-17 21:33:34 -04:00
2014-05-30 15:09:58 -04:00
class ImportScripts :: Bbpress < ImportScripts :: Base
2014-05-17 21:33:34 -04:00
2014-05-30 15:09:58 -04:00
def initialize
super
2014-05-17 21:33:34 -04:00
2014-05-30 15:09:58 -04:00
@client = Mysql2 :: Client . new (
host : " localhost " ,
username : " root " ,
#password: "password",
database : BB_PRESS_DB
)
end
2014-05-17 21:33:34 -04:00
2015-02-03 13:06:35 -05:00
def table_name ( name )
DB_TABLE_PREFIX + name
end
2014-05-30 15:09:58 -04:00
def execute
users_results = @client . query ( "
2015-02-03 13:06:35 -05:00
SELECT id ,
2014-05-30 15:09:58 -04:00
user_login username ,
display_name name ,
user_url website ,
user_email email ,
user_registered created_at
2015-02-03 13:06:35 -05:00
FROM #{table_name 'users'}", cache_rows: false)
2014-05-30 15:09:58 -04:00
2015-03-20 15:54:42 -04:00
puts '' , " creating users "
2014-05-30 15:09:58 -04:00
create_users ( users_results ) do | u |
ActiveSupport :: HashWithIndifferentAccess . new ( u )
end
2014-05-17 21:33:34 -04:00
2015-03-20 15:54:42 -04:00
puts '' , '' , " creating categories "
2015-03-16 13:18:20 -04: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-30 15:09:58 -04:00
end
2014-05-17 21:33:34 -04:00
2014-05-30 15:09:58 -04:00
import_posts
end
2014-05-17 21:33:34 -04:00
2014-05-30 15:09:58 -04:00
def import_posts
puts '' , " creating topics and posts "
total_count = @client . query ( "
2015-02-03 13:06:35 -05:00
SELECT count ( * ) count
FROM #{table_name 'posts'}
WHERE post_status < > 'spam'
AND post_type IN ( 'topic' , 'reply' ) " ).first['count']
2014-05-30 15:09:58 -04:00
batch_size = 1000
batches ( batch_size ) do | offset |
results = @client . query ( "
2015-02-03 13:06:35 -05:00
SELECT id ,
2014-05-30 15:09:58 -04:00
post_author ,
post_date ,
post_content ,
post_title ,
post_type ,
post_parent
2015-02-03 13:06:35 -05: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-30 15:09:58 -04:00
break if results . size < 1
2015-09-21 19:48:42 -04:00
next if all_records_exist? :posts , results . map { | p | p [ " id " ] . to_i }
2014-05-30 15:09:58 -04:00
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 " ]
2015-03-20 15:54:42 -04:00
if mapped [ :raw ]
mapped [ :raw ] = mapped [ :raw ] . gsub ( " <pre><code> " , " ``` \n " ) . gsub ( " </code></pre> " , " \n ``` " )
end
2014-05-30 15:09:58 -04:00
mapped [ :created_at ] = post [ " post_date " ]
mapped [ :custom_fields ] = { import_id : post [ " id " ] }
if post [ " post_type " ] == " topic "
2015-03-12 16:15:02 -04:00
mapped [ :category ] = category_id_from_imported_category_id ( post [ " post_parent " ] )
2014-05-30 15:09:58 -04: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-17 21:33:34 -04:00
end
end
end
2014-05-30 15:09:58 -04:00
ImportScripts :: Bbpress . new . perform