/
ipboard.rb
1089 lines (977 loc) · 39.2 KB
/
ipboard.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# frozen_string_literal: true
require "mysql2"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
require "htmlentities"
begin
require "reverse_markdown" # https://github.com/jqr/php-serialize
rescue LoadError
puts
puts "reverse_markdown not found."
puts "Add to Gemfile, like this: "
puts
puts "echo gem \\'reverse_markdown\\' >> Gemfile"
puts "bundle install"
exit
end
# 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="ipboard"
export DB_PW="ipboard"
export DB_USER="ipboard"
export TABLE_PREFIX="ipb_"
export IMPORT_AFTER="1970-01-01"
export UPLOADS="http://example.com/uploads"
export URL="http://example.com/"
export AVATARS_DIR="/imports/avatars/"
export USERDIR="user"
=end
class ImportScripts::IpboardSQL < ImportScripts::Base
DB_HOST ||= ENV["DB_HOST"] || "localhost"
DB_NAME ||= ENV["DB_NAME"] || "ipboard"
DB_PW ||= ENV["DB_PW"] || "ipboard"
DB_USER ||= ENV["DB_USER"] || "ipboard"
TABLE_PREFIX ||= ENV["TABLE_PREFIX"] || "ipb_"
IMPORT_AFTER ||= ENV["IMPORT_AFTER"] || "1970-01-01"
UPLOADS ||= ENV["UPLOADS"] || "http://UPLOADS+LOCATION+IS+NOT+SET/uploads"
USERDIR ||= ENV["USERDIR"] || "user"
URL ||= ENV["URL"] || "https://forum.example.com"
AVATARS_DIR ||= ENV["AVATARS_DIR"] || "/home/pfaffman/data/example.com/avatars/"
BATCH_SIZE = 1000
ID_FIRST = true
QUIET = true
DEBUG = false
GALLERY_CAT_ID = 1_234_567
GALLERY_CAT_NAME = "galeria"
EMO_DIR ||= ENV["EMO_DIR"] || "default"
OLD_FORMAT = false
if OLD_FORMAT
MEMBERS_TABLE = "#{TABLE_PREFIX}core_members"
FORUMS_TABLE = "#{TABLE_PREFIX}forums_forums"
POSTS_TABLE = "#{TABLE_PREFIX}forums_posts"
TOPICS_TABLE = "#{TABLE_PREFIX}forums_topics"
else
MEMBERS_TABLE = "#{TABLE_PREFIX}members"
FORUMS_TABLE = "#{TABLE_PREFIX}forums"
POSTS_TABLE = "#{TABLE_PREFIX}posts"
TOPICS_TABLE = "#{TABLE_PREFIX}topics"
GROUPS_TABLE = "#{TABLE_PREFIX}groups"
PROFILE_TABLE = "#{TABLE_PREFIX}profile_portal"
ATTACHMENT_TABLE = "#{TABLE_PREFIX}attachments"
end
# TODO: replace ipb_ with TABLE_PREFIX
#################
# Site settings #
#################
# don't send any emails
SiteSetting.disable_emails = "non-staff"
# don't send digests (so you can enable email without users noticing)
SiteSetting.disable_digest_emails = true
# keep site and users private
SiteSetting.login_required = true
SiteSetting.hide_user_profiles_from_public = true
# if site is made available, don't let it get indexed
SiteSetting.allow_index_in_robots_txt = false
# don't notify users when images in their posts get downloaded
SiteSetting.disable_system_edit_notifications = true
# SiteSetting.force_hostname='forum.dev1dev.com'
SiteSetting.title = "IPB Import"
if ID_FIRST
# TODO figure this out
puts "WARNING: permalink_normalizations not set!!!"
sleep 1
#raw = "[ORIGINAL POST](#{URL}/topic/#{id}-#{slug})\n\n" + raw
#SiteSetting.permalink_normalizations='/topic/(.*t)\?.*/\1'
else
# remove stuff after a "?" and work for urls that end in .html
SiteSetting.permalink_normalizations = '/(.*t)[?.].*/\1'
#raw = "[ORIGINAL POST](#{URL}/#{slug}-#{id}t)\n\n" + raw
end
def initialize
print_warning("Importing data after #{IMPORT_AFTER}") if IMPORT_AFTER > "1970-01-01"
super
@htmlentities = HTMLEntities.new
begin
@client =
Mysql2::Client.new(host: DB_HOST, username: DB_USER, password: DB_PW, database: DB_NAME)
rescue Exception => e
puts "=" * 50
puts e.message
puts <<~TEXT
Cannot log in to database.
Hostname: #{DB_HOST}
Username: #{DB_USER}
Password: #{DB_PW}
database: #{DB_NAME}
You should set these variables:
export DB_HOST="localhost"
export DB_NAME="ipboard"
export DB_PW="ipboard"
export DB_USER="ipboard"
export TABLE_PREFIX="ipb_"
export IMPORT_AFTER="1970-01-01"
export URL="http://example.com"
export UPLOADS=
export USERDIR="user"
Exiting.
TEXT
exit
end
end
def execute
import_users
import_categories
import_topics
import_posts
import_private_messages
# not supported import_image_categories
# NOT SUPPORTED import_gallery_topics
update_tl0
create_permalinks
end
def import_users
puts "", "creating users"
total_count =
mysql_query(
"SELECT count(*) count FROM #{MEMBERS_TABLE}
WHERE last_activity > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'));",
).first[
"count"
]
batches(BATCH_SIZE) do |offset|
#notes: no location, url,
results =
mysql_query(
"
SELECT member_id id,
name username,
member_group_id usergroup,
email,
pp_thumb_photo avatar_url,
# pp_main_photo avatar_url,
# avatar_location avatar_url,
# TODO consider joining ibf_profile_portal.avatar_location and avatar_type
FROM_UNIXTIME(joined) created_at,
FROM_UNIXTIME(last_activity) last_seen_at,
ip_address registration_ip_address,
member_banned banned,
bday_year, bday_month, bday_day,
g_title member_type,
last_visit last_seen_at
FROM #{MEMBERS_TABLE}, #{PROFILE_TABLE}, #{GROUPS_TABLE}
WHERE last_activity > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
AND member_id=pp_member_id
AND member_group_id = g_id
order by member_id ASC
LIMIT #{BATCH_SIZE}
OFFSET #{offset};",
)
break if results.size < 1
next if all_records_exist? :users, results.map { |u| u["id"].to_i }
create_users(results, total: total_count, offset: offset) do |user|
next if user["email"].blank?
next if user["username"].blank?
next if @lookup.user_id_from_imported_user_id(user["id"])
birthday =
begin
Date.parse("#{user["bday_year"]}-#{user["bday_month"]}-#{user["bday_day"]}")
rescue StandardError
nil
end
# TODO: what about timezones?
next if user["id"] == 0
{
id: user["id"],
email: user["email"],
username: user["username"],
avatar_url: user["avatar_url"],
title: user["member_type"],
created_at: user["created_at"] == nil ? 0 : Time.zone.at(user["created_at"]),
# bio_raw: user['bio_raw'],
registration_ip_address: user["registration_ip_address"],
# birthday: birthday,
last_seen_at: user["last_seen_at"] == nil ? 0 : Time.zone.at(user["last_seen_at"]),
admin: /^Admin/.match(user["member_type"]) ? true : false,
moderator: /^MOD/.match(user["member_type"]) ? true : false,
post_create_action:
proc do |newuser|
if user["avatar_url"] && user["avatar_url"].length > 0
photo_path = AVATARS_DIR + user["avatar_url"]
if File.exist?(photo_path)
begin
upload = create_upload(newuser.id, photo_path, File.basename(photo_path))
if upload && upload.persisted?
newuser.import_mode = false
newuser.create_user_avatar
newuser.import_mode = true
newuser.user_avatar.update(custom_upload_id: upload.id)
newuser.update(uploaded_avatar_id: upload.id)
else
puts "Error: Upload did not persist for #{photo_path}!"
end
rescue SystemCallError => err
puts "Could not import avatar #{photo_path}: #{err.message}"
end
else
puts "avatar file not found at #{photo_path}"
end
end
suspend_user(newuser) if user["banned"] != 0
end,
}
end
end
end
def suspend_user(user)
user.suspended_at = Time.now
user.suspended_till = 200.years.from_now
ban_reason = "Account deactivated by administrator"
user_option = user.user_option
user_option.email_digests = false
user_option.email_level = UserOption.email_level_types[:never]
user_option.email_messages_level = UserOption.email_level_types[:never]
user_option.save!
if user.save
StaffActionLogger.new(Discourse.system_user).log_user_suspend(user, ban_reason)
else
puts "Failed to suspend user #{user.username}. #{user.errors.try(:full_messages).try(:inspect)}"
end
end
def file_full_path(relpath)
File.join JSON_FILES_DIR, relpath.split("?").first
end
def import_image_categories
puts "", "importing image categories..."
categories =
mysql_query(
"
SELECT category_id id,
category_name_seo name,
category_parent_id as parent_id
FROM #{TABLE_PREFIX}gallery_categories
ORDER BY id ASC
",
).to_a
category_names =
mysql_query(
"
SELECT DISTINCT word_key, word_default title
FROM #{TABLE_PREFIX}core_sys_lang_words where word_app='gallery'
AND word_key REGEXP 'gallery_category_[0-9]+$'
ORDER BY word_key ASC
",
).to_a
cat_map = {}
puts "Creating gallery_cat_map"
category_names.each do |name|
title = name["title"]
word_key = name["word_key"]
puts "Processing #{word_key}: #{title}"
id = word_key.gsub("gallery_category_", "")
next if cat_map[id]
cat_map[id] = cat_map.has_value?(title) ? title + " " + id : title
puts "#{id} => #{cat_map[id]}"
end
params = { id: GALLERY_CAT_ID, name: GALLERY_CAT_NAME }
create_category(params, params[:id])
create_categories(categories) do |category|
id = (category["id"]).to_s
name = CGI.unescapeHTML(cat_map[id])
{
id: id + "gal",
name: name,
parent_category_id: @lookup.category_id_from_imported_category_id(GALLERY_CAT_ID),
color: random_category_color,
}
end
end
def import_categories
puts "", "importing categories..."
categories =
mysql_query(
"
SELECT id,
name name,
parent_id as parent_id
FROM #{FORUMS_TABLE}
ORDER BY parent_id ASC
",
).to_a
top_level_categories = categories.select { |c| c["parent.id"] == -1 }
create_categories(top_level_categories) do |category|
id = category["id"].to_s
name = category["name"]
{ id: id, name: name }
end
children_categories = categories.select { |c| c["parent.id"] != -1 }
create_categories(children_categories) do |category|
id = category["id"].to_s
name = category["name"]
{
id: id,
name: name,
parent_category_id: @lookup.category_id_from_imported_category_id(category["parent_id"]),
color: random_category_color,
}
end
end
def import_topics
puts "", "importing topics..."
total_count =
mysql_query(
"SELECT count(*) count FROM #{POSTS_TABLE}
WHERE post_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
AND new_topic=1;",
).first[
"count"
]
batches(BATCH_SIZE) do |offset|
discussions = mysql_query(<<-SQL)
SELECT #{TOPICS_TABLE}.tid tid,
#{TOPICS_TABLE}.forum_id category,
#{POSTS_TABLE}.pid pid,
#{TOPICS_TABLE}.title title,
#{TOPICS_TABLE}.pinned pinned,
#{POSTS_TABLE}.post raw,
#{TOPICS_TABLE}.title_seo as slug,
FROM_UNIXTIME(#{POSTS_TABLE}.post_date) created_at,
#{POSTS_TABLE}.author_id user_id
FROM #{POSTS_TABLE}, #{TOPICS_TABLE}
WHERE #{POSTS_TABLE}.topic_id = #{TOPICS_TABLE}.tid
AND #{POSTS_TABLE}.new_topic = 1
AND #{POSTS_TABLE}.post_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
ORDER BY #{POSTS_TABLE}.post_date ASC
LIMIT #{BATCH_SIZE}
OFFSET #{offset}
SQL
break if discussions.size < 1
next if all_records_exist? :posts, discussions.map { |t| "discussion#" + t["tid"].to_s }
create_posts(discussions, total: total_count, offset: offset) do |discussion|
slug = discussion["slug"]
id = discussion["tid"]
raw = clean_up(discussion["raw"])
{
id: "discussion#" + discussion["tid"].to_s,
user_id:
user_id_from_imported_user_id(discussion["user_id"]) || Discourse::SYSTEM_USER_ID,
title: CGI.unescapeHTML(discussion["title"]),
category: category_id_from_imported_category_id(discussion["category"].to_s),
raw: raw,
pinned_at: discussion["pinned"].to_i == 1 ? Time.zone.at(discussion["created_at"]) : nil,
created_at: Time.zone.at(discussion["created_at"]),
}
end
end
end
def array_from_members_string(invited_members = "a:3:{i:0;i:22629;i:1;i:21837;i:2;i:22234;}")
out = []
count_regex = /a:(\d)+:/
count = count_regex.match(invited_members)[1]
rest = invited_members.sub(count_regex, "")
i_regex = /i:\d+;i:(\d+);/
while m = i_regex.match(rest)
i = m[1]
rest.sub!(i_regex, "")
puts "i: #{i}, #{rest}"
out += [i.to_i]
end
out
end
def import_private_messages
puts "", "importing private messages..."
topic_count =
mysql_query("SELECT COUNT(msg_id) count FROM #{TABLE_PREFIX}message_posts").first["count"]
last_private_message_topic_id = -1
batches(BATCH_SIZE) do |offset|
private_messages = mysql_query(<<-SQL)
SELECT msg_id pmtextid,
msg_topic_id topic_id,
msg_author_id fromuserid,
mt_title title,
msg_post message,
mt_invited_members touserarray,
mt_to_member_id to_user_id,
msg_is_first_post first_post,
msg_date dateline
FROM #{TABLE_PREFIX}message_topics, #{TABLE_PREFIX}message_posts
WHERE msg_topic_id = mt_id
AND msg_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
ORDER BY msg_topic_id, msg_id
LIMIT #{BATCH_SIZE}
OFFSET #{offset}
SQL
puts "Processing #{private_messages.count} messages"
break if private_messages.count < 1
puts "Processing . . . "
private_messages =
private_messages.reject { |pm| @lookup.post_already_imported?("pm-#{pm["pmtextid"]}") }
title_username_of_pm_first_post = {}
create_posts(private_messages, total: topic_count, offset: offset) do |m|
skip = false
mapped = {}
mapped[:id] = "pm-#{m["pmtextid"]}"
mapped[:user_id] = user_id_from_imported_user_id(m["fromuserid"]) ||
Discourse::SYSTEM_USER_ID
mapped[:raw] = begin
clean_up(m["message"])
rescue StandardError
nil
end
mapped[:created_at] = Time.zone.at(m["dateline"])
title = @htmlentities.decode(m["title"]).strip[0...255]
topic_id = nil
next if mapped[:raw].blank?
# users who are part of this private message.
target_usernames = []
target_userids = []
begin
to_user_array = [m["to_user_id"]] + array_from_members_string(m["touserarray"])
rescue StandardError
puts "#{m["pmtextid"]} -- #{m["touserarray"]}"
skip = true
end
begin
to_user_array.each do |to_user|
user_id = user_id_from_imported_user_id(to_user)
username = User.find_by(id: user_id).try(:username)
target_userids << user_id || Discourse::SYSTEM_USER_ID
target_usernames << username if username
if user_id
puts "Found user: #{to_user} -- #{user_id} -- #{username}"
else
puts "Can't find user: #{to_user}"
end
end
rescue StandardError
puts "skipping pm-#{m["pmtextid"]} `to_user_array` is broken -- #{to_user_array.inspect}"
skip = true
end
participants = target_userids
participants << mapped[:user_id]
begin
participants.sort!
rescue StandardError
puts "one of the participant's id is nil -- #{participants.inspect}"
end
if last_private_message_topic_id != m["topic_id"]
last_private_message_topic_id = m["topic_id"]
unless QUIET
puts "New message: #{m["topic_id"]}: #{title} from #{m["fromuserid"]} (#{mapped[:user_id]})"
end
# topic post message
topic_id = m["topic_id"]
mapped[:title] = title
mapped[:archetype] = Archetype.private_message
mapped[:target_usernames] = target_usernames.join(",")
if mapped[:target_usernames].size < 1 # pm with yourself?
# skip = true
mapped[:target_usernames] = "system"
puts "pm-#{m["pmtextid"]} has no target (#{m["touserarray"]})"
end
else # reply
topic_id = topic_lookup_from_imported_post_id("pm-#{topic_id}")
skip = true if !topic_id
mapped[:topic_id] = topic_id
unless QUIET
puts "Reply message #{topic_id}: #{m["topic_id"]}: from #{m["fromuserid"]} (#{mapped[:user_id]})"
end
end
# puts "#{target_usernames} -- #{mapped[:target_usernames]}"
# puts "Adding #{mapped}"
skip ? nil : mapped
# puts "#{'-'*50}> added"
end
end
end
def import_gallery_topics
# pfaffman: I'm not clear whether this is an IPBoard thing or from some other system
puts "", "importing gallery albums..."
gallery_count = 0
total_count =
mysql_query(
"SELECT count(*) count FROM #{TABLE_PREFIX}gallery_images
;",
).first[
"count"
]
# NOTE: for imports with huge numbers of galleries, this needs to use limits
batches(BATCH_SIZE) do |offset|
# galleries = mysql_query(<<-SQL
# SELECT #{TABLE_PREFIX}gallery_albums.album_id tid,
# #{TABLE_PREFIX}gallery_albums.album_category_id category,
# #{TABLE_PREFIX}gallery_albums.album_owner_id user_id,
# #{TABLE_PREFIX}gallery_albums.album_name title,
# #{TABLE_PREFIX}gallery_albums.album_description raw,
# #{TABLE_PREFIX}gallery_albums.album_type,
# FROM_UNIXTIME(#{TABLE_PREFIX}gallery_albums.album_last_img_date) created_at
# FROM #{TABLE_PREFIX}gallery_albums
# ORDER BY #{TABLE_PREFIX}gallery_albums.album_id ASC
# SQL
# )
images = mysql_query(<<-SQL)
SELECT #{TABLE_PREFIX}gallery_albums.album_id tid,
#{TABLE_PREFIX}gallery_albums.album_category_id category,
#{TABLE_PREFIX}gallery_albums.album_owner_id user_id,
#{TABLE_PREFIX}gallery_albums.album_name title,
#{TABLE_PREFIX}gallery_albums.album_description raw,
#{TABLE_PREFIX}gallery_albums.album_type,
#{TABLE_PREFIX}gallery_images.image_caption caption,
#{TABLE_PREFIX}gallery_images.image_description description,
#{TABLE_PREFIX}gallery_images.image_masked_file_name masked,
#{TABLE_PREFIX}gallery_images.image_id image_id,
#{TABLE_PREFIX}gallery_images.image_medium_file_name medium,
#{TABLE_PREFIX}gallery_images.image_original_file_name orig,
FROM_UNIXTIME(#{TABLE_PREFIX}gallery_albums.album_last_img_date) created_at,
#{TABLE_PREFIX}gallery_images.image_file_name filename
FROM #{TABLE_PREFIX}gallery_albums, #{TABLE_PREFIX}gallery_images
WHERE #{TABLE_PREFIX}gallery_images.image_album_id=#{TABLE_PREFIX}gallery_albums.album_id
ORDER BY #{TABLE_PREFIX}gallery_albums.album_id, image_date DESC
LIMIT #{BATCH_SIZE}
OFFSET #{offset};
SQL
break if images.size < 1
if all_records_exist? :posts,
images.map { |t| "gallery#" + t["tid"].to_s + t["image_id"].to_s }
next
end
last_id = images.first["tid"]
raw = "Gallery ID: #{last_id}\n" + clean_up(images.first["raw"])
raw += "#{clean_up(images.first["description"])}\n"
last_gallery = images.first.dup
create_posts(images, total: total_count, offset: offset) do |gallery|
id = gallery["tid"].to_i
#puts "ID: #{id}, last_id: #{last_id}, image: #{gallery['image_id']}"
if id == last_id
raw += "### #{gallery["caption"]}\n"
raw += "#{UPLOADS}/#{gallery["orig"]}\n"
last_gallery = gallery.dup
next
else
insert_raw = raw.dup
last_id = gallery["tid"]
if DEBUG
raw = "Gallery ID: #{last_id}\n" + clean_up(gallery["raw"])
raw +=
"Cat: #{last_gallery["category"]} - #{category_id_from_imported_category_id(last_gallery["category"].to_s + "gal")}"
end
raw += "#{clean_up(images.first["description"])}\n"
raw += "### #{gallery["caption"]}\n"
raw += "User #{gallery["user_id"]}, image_id: #{gallery["image_id"]}\n" if DEBUG
raw += "#{UPLOADS}/#{gallery["orig"]}\n"
gallery_count += 1
unless QUIET
puts "#{gallery_count}--Cat: #{last_gallery["category"]} ==> #{category_id_from_imported_category_id(last_gallery["category"].to_s + "gal")}"
end
{
id: "gallery#" + last_gallery["tid"].to_s + last_gallery["image_id"].to_s,
user_id:
user_id_from_imported_user_id(last_gallery["user_id"]) || Discourse::SYSTEM_USER_ID,
title: CGI.unescapeHTML(last_gallery["title"]),
category: category_id_from_imported_category_id(last_gallery["category"].to_s + "gal"),
raw: insert_raw,
}
end
end
end
end
# TODO: use this to figure out to pin posts
def map_first_post(row, mapped)
mapped[:category] = @lookup.category_id_from_imported_category_id(row[:forum_id])
mapped[:title] = CGI.unescapeHTML(row[:topic_title]).strip[0...255]
mapped[:pinned_at] = mapped[:created_at] unless row[:topic_type] == Constants::POST_NORMAL
mapped[:pinned_globally] = row[:topic_type] == Constants::POST_GLOBAL
mapped[:post_create_action] = proc do |post|
@permalink_importer.create_for_topic(post.topic, row[:topic_id])
end
mapped
end
def import_comments
puts "", "importing gallery comments..."
total_count =
mysql_query("SELECT count(*) count FROM #{TABLE_PREFIX}gallery_comments;").first["count"]
batches(BATCH_SIZE) do |offset|
comments = mysql_query(<<-SQL)
SELECT #{TABLE_PREFIX}gallery_comments.tid tid,
#{TABLE_PREFIX}gallery_topics.forum_id category,
#{TABLE_PREFIX}gallery_posts.pid pid,
#{TABLE_PREFIX}gallery_topics.title title,
#{TABLE_PREFIX}gallery_posts.post raw,
FROM_UNIXTIME(#{TABLE_PREFIX}gallery_posts.post_date) created_at,
#{TABLE_PREFIX}gallery_posts.author_id user_id
FROM #{TABLE_PREFIX}gallery_posts, #{TABLE_PREFIX}gallery_topics
WHERE #{TABLE_PREFIX}gallery_posts.topic_id = #{TABLE_PREFIX}gallery_topics.tid
AND #{TABLE_PREFIX}gallery_posts.new_topic = 0
AND #{TABLE_PREFIX}gallery_posts.post_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
ORDER BY #{TABLE_PREFIX}gallery_posts.post_date ASC
LIMIT #{BATCH_SIZE}
OFFSET #{offset}
SQL
break if comments.size < 1
next if all_records_exist? :posts, comments.map { |comment| "comment#" + comment["pid"].to_s }
create_posts(comments, total: total_count, offset: offset) do |comment|
next unless t = topic_lookup_from_imported_post_id("discussion#" + comment["tid"].to_s)
next if comment["raw"].blank?
{
id: "comment#" + comment["pid"].to_s,
user_id: user_id_from_imported_user_id(comment["user_id"]) || Discourse::SYSTEM_USER_ID,
topic_id: t[:topic_id],
raw: clean_up(comment["raw"]),
created_at: Time.zone.at(comment["created_at"]),
}
end
end
end
def import_posts
puts "", "importing posts..."
total_count =
mysql_query(
"SELECT count(*) count FROM #{POSTS_TABLE}
WHERE post_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
AND new_topic=0;",
).first[
"count"
]
batches(BATCH_SIZE) do |offset|
comments = mysql_query(<<-SQL)
SELECT #{TOPICS_TABLE}.tid tid,
#{TOPICS_TABLE}.forum_id category,
#{POSTS_TABLE}.pid pid,
#{TOPICS_TABLE}.title title,
#{POSTS_TABLE}.post raw,
FROM_UNIXTIME(#{POSTS_TABLE}.post_date) created_at,
#{POSTS_TABLE}.author_id user_id
FROM #{POSTS_TABLE}, #{TOPICS_TABLE}
WHERE #{POSTS_TABLE}.topic_id = #{TOPICS_TABLE}.tid
AND #{POSTS_TABLE}.new_topic = 0
AND #{POSTS_TABLE}.post_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
ORDER BY #{POSTS_TABLE}.post_date ASC
LIMIT #{BATCH_SIZE}
OFFSET #{offset}
SQL
break if comments.size < 1
next if all_records_exist? :posts, comments.map { |comment| "comment#" + comment["pid"].to_s }
create_posts(comments, total: total_count, offset: offset) do |comment|
next unless t = topic_lookup_from_imported_post_id("discussion#" + comment["tid"].to_s)
next if comment["raw"].blank?
{
id: "comment#" + comment["pid"].to_s,
user_id: user_id_from_imported_user_id(comment["user_id"]) || Discourse::SYSTEM_USER_ID,
topic_id: t[:topic_id],
raw: clean_up(comment["raw"]),
created_at: Time.zone.at(comment["created_at"]),
}
end
end
end
def nokogiri_fix_blockquotes(raw)
# this makes proper quotes with user/topic/post references.
# I'm not clear if it is for just some bizarre imported data, or it might ever be useful
# It should be integrated into the Nokogiri section of clean_up, though.
@doc = Nokogiri.XML("<html>" + raw + "</html>")
# handle <blockquote>s with links to original post
@doc
.css("blockquote[class=ipsQuote]")
.each do |b|
# puts "\n#{'#'*50}\n#{b}\n\nCONTENT: #{b['data-ipsquote-contentid']}"
# b.options = Nokogiri::XML::ParseOptions::STRICT
imported_post_id = b["data-ipsquote-contentcommentid"].to_s
content_type = b["data-ipsquote-contenttype"].to_s
content_class = b["data-ipsquote-contentclass"].to_s
content_id = b["data-ipsquote-contentid"].to_s || b["data-cid"].to_s
topic_lookup = topic_lookup_from_imported_post_id("comment#" + imported_post_id)
post_lookup = topic_lookup_from_imported_post_id("discussion#" + content_id)
post = topic_lookup ? topic_lookup[:post_number] : nil
topic = topic_lookup ? topic_lookup[:topic_id] : nil
post ||= post_lookup ? post_lookup[:post_number] : nil
topic ||= post_lookup ? post_lookup[:topic_id] : nil
# TODO: consider: <blockquote class="ipsStyle_spoiler" data-ipsspoiler="">
# consider: <pre class="ipsCode prettyprint">
# TODO make sure it's the imported username
# TODO: do _s still get \-escaped?
ips_username = b["data-ipsquote-username"] || b["data-author"]
username = ips_username
new_text = ""
if DEBUG
# new_text += "post: #{imported_post_id} --> #{post_lookup} --> |#{post}|<br>\n"
# new_text += "topic: #{content_id} --> #{topic_lookup} --> |#{topic}|<br>\n"
# new_text += "user: #{ips_username} --> |#{username}|<br>\n"
# new_text += "class: #{content_class}<br>\n"
# new_text += "type: #{content_type}<br>\n"
if content_class.length > 0 && content_class != "forums_Topic"
new_text += "UNEXPECTED CONTENT CLASS! #{content_class}<br>\n"
end
if content_type.length > 0 && content_type != "forums"
new_text += "UNEXPECTED CONTENT TYPE! #{content_type}<br>\n"
end
# puts "#{'-'*20} and NOWWWWW!!!! \n #{new_text}"
end
if post && topic && username
quote = "\n[quote=\"#{username}, post:#{post}, topic: #{topic}\"]\n\n"
else
if username && username.length > 1
quote = "\n[quote=\"#{username}\"]\n\n"
else
quote = "\n[quote]\n"
end
# new_doc = Nokogiri::XML("<div>#{new_text}</div>")
end
puts "QUOTE: #{quote}"
sleep 1
b.content = quote + b.content + "\n[/quote]\n"
b.name = "div"
end
raw = @doc.to_html
end
def clean_up(raw)
return "" if raw.blank?
raw.gsub!(/<#EMO_DIR#>/, EMO_DIR)
# TODO what about uploads?
# raw.gsub!(/<fileStore.core_Attachment>/,UPLOADS)
raw.gsub!(/<br>/, "\n\n")
raw.gsub!(%r{<br />}, "\n\n")
raw.gsub!(%r{<p> </p>}, "\n\n")
raw.gsub!(/\[hr\]/, "\n***\n")
raw.gsub!(/'/, "'")
raw.gsub!(%r{\[url="(.+?)"\]http.+?\[/url\]}, "\\1\n")
raw.gsub!(%r{\[media\](.+?)\[/media\]}, "\n\\1\n\n")
raw.gsub!(%r{\[php\](.+?)\[/php\]}m) do |m|
"\n\n```php\n\n" + @htmlentities.decode($1.gsub(/\n\n/, "\n")) + "\n\n```\n\n"
end
raw.gsub!(%r{\[code\](.+?)\[/code\]}m) do |m|
"\n\n```\n\n" + @htmlentities.decode($1.gsub(/\n\n/, "\n")) + "\n\n```\n\n"
end
raw.gsub!(%r{\[list\](.+?)\[/list\]}m) { |m| "\n" + $1.gsub(/\[\*\]/, "\n- ") + "\n\n" }
raw.gsub!(/\[quote\]/, "\n[quote]\n")
raw.gsub!(%r{\[/quote\]}, "\n[/quote]\n")
raw.gsub!(/date=\'(.+?)\'/, "")
raw.gsub!(/timestamp=\'(.+?)\' /, "")
quote_regex = /\[quote name=\'(.+?)\'\s+post=\'(\d+?)\'\s*\]/
while quote = quote_regex.match(raw)
# get IPB post number and find Discourse post and topic number
unless QUIET
puts "----------------------------------------\nName: #{quote[1]}, post: #{quote[2]}"
end
imported_post_id = quote[2].to_s
topic_lookup = topic_lookup_from_imported_post_id("comment#" + imported_post_id)
post_lookup = topic_lookup_from_imported_post_id("discussion#" + imported_post_id)
puts "topic_lookup: #{topic_lookup}, post: #{post_lookup}" unless QUIET
post_num = topic_lookup ? topic_lookup[:post_number] : nil
topic_num = topic_lookup ? topic_lookup[:topic_id] : nil
post_num ||= post_lookup ? post_lookup[:post_number] : nil
topic_num ||= post_lookup ? post_lookup[:topic_id] : nil
# Fix or leave bogus username?
username = find_user_by_import_id(quote[1]) || quote[1]
puts "username: #{username}, post_id: #{post_num}, topic_id: #{topic_num}" unless QUIET
puts "Before fixing a quote: #{raw}\n**************************************** " unless QUIET
post_string = post_num ? ", post:#{post_num}" : ""
topic_string = topic_num ? ", topic:#{topic_num}" : ""
raw.gsub!(quote_regex, "\n[quote=\"#{username}#{post_string}#{topic_string}\"]\n\n")
puts "AFTER!!!!!!!!!!!!1: #{raw}" unless QUIET
sleep 1
raw
end
attach_regex = /\[attachment=(\d+?):.+\]/
while attach = attach_regex.match(raw)
attach_id = attach[1]
attachments =
mysql_query(
"SELECT attach_location as loc,
attach_file as filename
FROM #{ATTACHMENT_TABLE}
WHERE attach_id=#{attach_id}",
)
if attachments.count < 1
puts "Attachment #{attach_id} not found."
attach_string = "Attachment #{attach_id} not found."
else
attach_url = "#{UPLOADS}/#{attachments.first["loc"].gsub(" ", "%20")}"
if attachments.first["filename"].match(/(png|jpg|jpeg|gif)$/)
# images are rendered as a link that contains the image
attach_string =
"#{attach_id}\n\n[![#{attachments.first["filename"]}](#{attach_url})](#{attach_url})\n"
else
# other attachments are simple download links
attach_string = "#{attach_id}\n\n[#{attachments.first["filename"]}](#{attach_url})\n"
end
end
raw.sub!(attach_regex, attach_string)
end
raw
end
def random_category_color
colors = SiteSetting.category_colors.split("|")
colors[rand(colors.count)]
end
def old_clean_up(raw)
# This was for a forum that appeared to have lots of customization's.
# IT did a good job of handling quotes and whatnot, but I don't know
# what version if IPBoard it was for.
return "" if raw.blank?
raw.gsub!(/<___base_url___>/, URL)
raw.gsub!(/<fileStore.core_Emoticons>/, UPLOADS)
raw.gsub!(/<fileStore.core_Attachment>/, UPLOADS)
raw.gsub!(/<br>/, "\n")
@doc = Nokogiri.XML("<html>" + raw + "</html>")
# handle <blockquote>s with links to original post
@doc
.css("blockquote[class=ipsQuote]")
.each do |b|
imported_post_id = b["data-ipsquote-contentcommentid"].to_s
content_type = b["data-ipsquote-contenttype"].to_s
content_class = b["data-ipsquote-contentclass"].to_s
content_id = b["data-ipsquote-contentid"].to_s || b["data-cid"].to_s
topic_lookup = topic_lookup_from_imported_post_id("comment#" + imported_post_id)
post_lookup = topic_lookup_from_imported_post_id("discussion#" + content_id)
post = topic_lookup ? topic_lookup[:post_number] : nil
topic = topic_lookup ? topic_lookup[:topic_id] : nil
post ||= post_lookup ? post_lookup[:post_number] : nil
topic ||= post_lookup ? post_lookup[:topic_id] : nil
# TODO: consider: <blockquote class="ipsStyle_spoiler" data-ipsspoiler="">
# consider: <pre class="ipsCode prettyprint">
ips_username = b["data-ipsquote-username"] || b["data-author"]
username = ips_username
new_text = ""
if DEBUG
if content_class.length > 0 && content_class != "forums_Topic"
new_text += "UNEXPECTED CONTENT CLASS! #{content_class}<br>\n"
end
if content_type.length > 0 && content_type != "forums"
new_text += "UNEXPECTED CONTENT TYPE! #{content_type}<br>\n"
end
end
if post && topic && username
quote = "[quote=\"#{username}, post:#{post}, topic: #{topic}\"]\n\n"
else
if username && username.length > 1
quote = "[quote=\"#{username}\"]\n\n"
else
quote = "[quote]\n"
end
end
b.content = quote + b.content + "\n[/quote]\n"
b.name = "div"
end
@doc.css("object param embed").each { |embed| embed.replace("\n#{embed["src"]}\n") }
# handle <iframe data-embedcontent>s with links to original post
# no examples in recent import
@doc
.css("iframe[data-embedcontent]")
.each do |d|
d.to_s.match(/\-([0-9]+)t/)
imported_post_id = $1
if imported_post_id
puts "Searching for #{imported_post_id}" unless QUIET
topic_lookup = topic_lookup_from_imported_post_id("discussion#" + imported_post_id)
topic = topic_lookup ? topic_lookup[:topic_id] : nil
if topic
url = URL + "/t/#{topic}"
d.to_s.match(/comment=([0-9]+)&/)
content_id = $1 || "-1"
if content_id
post_lookup = topic_lookup_from_imported_post_id("comment#" + content_id)
post = topic_lookup ? topic_lookup[:post_number] : 1
url += "/#{post}"
end
d.content = url
end
end
d.name = "div"
end
@doc.css("div[class=ipsQuote_citation]").each { |d| d.remove }
raw = @doc.to_html
# let ReverseMarkdown handle the rest
raw = ReverseMarkdown.convert raw
# remove tabs at start of line to avoid everything being a <pre>
raw = raw.gsub(/^\t+/, "")
# un \-escape _s in usernames in [quote]s
raw.gsub!(/^\[quote=.+?_.*$/) do |match|
match = match.gsub('\_', "_")
match
end
raw
end
def staff_guardian
@_staff_guardian ||= Guardian.new(Discourse.system_user)