-
Notifications
You must be signed in to change notification settings - Fork 8
/
slash2toslash2.2
executable file
·1527 lines (1209 loc) · 37.5 KB
/
slash2toslash2.2
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
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2004 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
use strict;
use File::Basename;
use Getopt::Std;
use DBIx::Password;
use Digest::MD5 'md5_hex';
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my(%opts, %forced_plugins, %sacred_tables);
my($del_users, $poll_uid, $process_menus);
my(%topics, %discussions, %polls, %comments, %skip_polls, %poll_data);
my(%vars, %story_authors);
# Remember to doublecheck these match usage()!
usage('Options used incorrectly') unless getopts('IMhvu:U:P:X:', \%opts);
usage() if $opts{'h'};
version() if $opts{'v'};
usage('Need v2 virtual user') unless $opts{'U'};
usage('Need new virtual user') unless $opts{'u'};
$process_menus++ if $opts{'M'};
# Determine plugins to force conversion for.
for (split /,/, ($opts{P} || '')) {
$forced_plugins{$_} = 1;
}
# Determine tables to be left alone.
for (split /,/, ($opts{X} || '')) {
$sacred_tables{$_} = 1;
}
####################################
# disclaimer
{
# We don't ask this if we are in incremental-mode.
last if $opts{I};
my $answer = ask(<<'EOT');
Slash 2.0.0 (Bender) to Slash 2.2.0 (Fry) Conversion Utility
By running this I realize that there is no warranty, expressed or implied.
Any data loss as a result of running this program is my responsibility.
I have read the documentation for this program, I understand it, and I
have taken the necessary precautions and done the required preparation.
[yes/No]:
EOT
exit unless $answer eq 'yes';
}
####################################
# setup
my $dbh_old = DBIx::Password->connect($opts{'U'});
die "Can't open connection to old (v2) database!" unless $dbh_old;
my $dbh_new = DBIx::Password->connect($opts{'u'});
die "Can't open connection to new database!" unless $dbh_new;
END {
$dbh_old->disconnect if $dbh_old;
$dbh_new->disconnect if $dbh_new;
}
my @user_tables = qw(
users users_comments users_info users_index users_prefs users_param
);
# Run ONLY for plugins that were installed on both sites.
my %plugins = (
Journal => {
tables => [qw(
journals journals_text journal_friends journal_themes
)],
},
Search => undef,
);
# Run ONLY for plugins that were installed on both sites.
my(%plugin_cleanup) = (
Journal => sub {
$dbh_new->do(<<EOT);
UPDATE journals SET tid=$vars{journal_default_topic} WHERE tid=0
EOT
},
);
# Run for plugins that are INSTALLED on the NEW site.
my(%plugin_fixup) = (
Messages => sub {
my $m_list = $dbh_old->selectcol_arrayref(<<EOT);
SELECT uid FROM users_info WHERE maillist=1
EOT
for (@{$m_list}) {
$dbh_new->do(<<EOT)
INSERT INTO users_param (uid, name, value) VALUES ($_, 'messagecodes_1', 1)
EOT
}
},
);
my (%conversions) = (
# Straight copy tables.
'metamodlog' => undef,
'menus' => undef,
'tzcodes' => undef,
'users_comments' => undef,
'users_info' => undef,
'users_param' => undef,
'users_prefs' => undef,
'vars' => undef,
# This is solely for fixup, we have NON NULL columns that were once
# NULL.
'sections' => sub {
my($data) = @_;
delete $data->{artcount} if !defined($data->{artcount});
delete $data->{isolate} if !defined($data->{isolate});
delete $data->{issue} if !defined($data->{issue});
return $data;
},
# Plugin tables.
'journals' => sub {
my($data) = @_;
$data->{description} ||= '';
$data->{discussion} = $data->{comment_header};
delete $data->{comment_header};
return $data;
},
'journals_text' => sub {
my($data) = @_;
$data->{article} ||= '';
return $data;
},
'journal_friends' => undef,
'journal_themes' => undef,
# Processed tables.
'blocks' => sub {
my($data) = @_;
$data->{seclev} ||= 0;
$data->{portal} ||= 0;
$data->{retrieve} ||= 0;
$data->{title} ||= '';
# If it's not an allowed enum, ditch it.
delete $data->{type}
if $data->{type} !~ /(static|color|portald)/;
return $data;
},
'content_filters' => sub {
my($data) = @_;
delete $data->{maximum_length};
# Since this used to be "comment_filters" and then evolved into
# "comtent_filters", we should be specific about what we
# are filtering. The default is for 'comments' as The Beast
# didn't have a concept of much more than that.
$data->{form} = 'comments';
return $data;
},
'topics' => sub {
my($data) = @_;
$data->{name} = $data->{tid};
delete $data->{tid};
return $data;
},
'discussions' => sub {
# Convert discussion primary key into a sequence.
my($data, $story_hash) = @_;
if (! $data->{url}) {
printf <<EOT, ($data->{sid}) ? "($data->{sid})":'';
Discussion "$data->{title}" %s had null URL; skipping...
EOT
return;
}
if (! exists $story_hash->{$data->{sid}} &&
! exists $polls{$data->{sid}})
{
print <<EOT;
Discussion '$data->{sid}' is not a story or poll; skipping...
EOT
return;
}
# Program should accept a default topic for discussions.
$data->{'topic'} =
(defined($story_hash->{$data->{sid}}{tid})) ?
$topics{$story_hash->{$data->{sid}}{tid}} :
$vars{defaulttopic};
# Admins must disable stories via their own criterion.
$data->{type} = 'open';
$data->{'ALLOWED_FIELDS'} = [qw(
id sid title url topic ts type uid commentcount
)];
return $data;
},
'users' => sub {
my($data, $maillist) = @_;
# Handle author flag conversion.
$data->{author} = 1 if exists $story_authors{lc($data->{nickname})};
return $data;
},
'users_index' => sub {
my($data) = @_;
if ($data->{exaid}) {
my @auth_uids;
for (split /,/, $data->{exaid}) {
push @auth_uids, $story_authors{$_}
if $story_authors{$_};
}
$data->{exaid} = join ',', @auth_uids;
}
return $data;
},
'stories' => sub {
my($data, $hitlist, $storylist) = @_;
# We need to COPY the data to the story list if writestatus
# allows.
my $datacopy;
while (my($key, $value) = each %{$data}) {
$datacopy->{$key} = $value;
}
delete $data->{aid};
$data->{tid} = $topics{$data->{tid}} || $vars{defaulttopic};
# Dammit, this relationship becomes CIRCULAR if looked at from
# an incremental perspective! So for now we must load this info
# somewhere else.
#
#$data->{discussion} = $discussions{$data->{sid}};
$data->{hits} = $hitlist->{$data->{sid}}{hits} || 0;
# We only care about accurate representation of writestats when
# we do a full import.
if (!$opts{I}) {
my $ws = $data->{writestatus};
$data->{writestatus} = 0;
$data->{writestatus} = 'delete' if $ws==5 || $ws==10;
$data->{writestatus} = 'dirty' if $ws==1;
$data->{writestatus} ||= 'ok';
} else {
$data->{writestatus} = 'dirty';
}
push @{$storylist}, $datacopy if $storylist;
# Use to properly assign topics to story-based discussions.
#$story_topics{$data->{sid}} = $data->{tid};
my %newfields;
map { $newfields{$_} = $dbh_new->quote($data->{$_}) }
qw[sid introtext bodytext relatedtext];
$dbh_new->do(<<EOT);
REPLACE INTO story_text (sid, introtext, bodytext, relatedtext) VALUES
($newfields{sid},
$newfields{introtext},
$newfields{bodytext},
$newfields{relatedtext})
EOT
delete $data->{introtext};
delete $data->{bodytext};
delete $data->{relatedtext};
# extratext just goes awaaaayyy.
delete $data->{extratext};
return $data;
},
'pollquestions' => sub {
my($data, $poll_data) = @_;
$data->{'discussion'} = $discussions{$data->{qid}};
$data->{'topic'} = $vars{defaulttopic};
$data->{'uid'} = $poll_uid;
# QID is now SID and the new QID is now a serial key.
$data->{'sid'} = $data->{'qid'};
$poll_data->{$data->{qid}} = $data if $poll_data;
delete $data->{qid};
return $data;
},
'pollanswers' => sub {
my($data) = @_;
return if exists $skip_polls{$data->{qid}};
# Note AID here does means "ANSWER ID", not "author id".
my $oldpoll = $data->{qid};
$data->{qid} = $polls{$oldpoll};
if (! $data->{qid}) {
print <<EOT;
Skipping answers for deleted poll '$oldpoll'
EOT
$skip_polls{$oldpoll}++;
return;
}
return $data;
},
'pollvoters' => sub {
my($data) = @_;
$data->{qid} = $polls{$data->{qid}};
return $data;
},
'moderatorlog' => sub {
my($data) = @_;
my $sid = $data->{sid};
$data->{sid} = $discussions{$data->{sid}};
# We do not import records for comments without a discussion.
return if !$data->{sid};
my $oldcid = $data->{cid};
$data->{cid} = $comments{$sid}->{$oldcid};
return $data if $data->{cid};
print <<EOT if !$opts{I};
Warning: NULL CID found for '$sid' #$oldcid - record not imported.
EOT
return;
},
'submissions' => sub {
my($data) = @_;
$data->{tid} = $topics{$data->{tid}} if $data->{tid};
$data->{tid} ||= $vars{defaulttopic};
$data->{$_} ||= '' for qw(comment name email note story);
return $data;
},
);
# List the conditions necessary for incremental updates for all
# updatable tables.
#
# Users have the same condition, so we encapsulate it here.
my $usercond = {
cond => 'uid > %ld',
field => 'uid',
type => 'int',
};
# Each condition is an sprintf format string.
my (%conditions) = (
'abusers' => {
cond => 'abuser_id > %ld',
field => 'abuser_id',
type => 'int',
},
'accesslog' => {
cond => 'id > %ld',
field => 'id',
type => 'int',
},
'comments' => {
cond => 'date > %s',
field => 'date',
type => 'date',
},
'discussions' => {
cond => 'ts > %s',
field => 'ts',
type => 'date',
},
#Ignoring hitters
'metamodlog' => {
cond => 'id > %ld',
field => 'id',
type => 'int',
},
'moderatorlog' => {
cond => 'id > %ld',
field => 'id',
type => 'int',
},
'pollquestions' => {
cond => 'date > %s',
field => 'date',
type => 'date',
},
#Stories will need special treatment.
'submissions' => {
cond => 'time > %s',
field => 'time',
type => 'date',
},
'users' => $usercond,
'users_comments' => $usercond,
'users_index' => $usercond,
'users_info' => $usercond,
'users_prefs' => $usercond,
);
####################################
# main body
{
my %tmpvars;
load_vars($dbh_old, \%tmpvars);
# Populate aid->uid author resolver.
get_authors();
if ($opts{I}) {
incremental();
# We're done.
exit 0;
}
####################################
# Conversion Questions
$del_users = ask(<<'EOT') eq 'yes';
Should we delete all rows from the existing users tables? If not, you should
take steps to insure that no user collisions will result as the importer
will otherwise attempt to merge the user databases, and a collision will
cause a fatal error. [yes/No]:
EOT
if ($del_users) {
for (@user_tables) {
$dbh_new->do("DELETE FROM $_");
}
}
$poll_uid = '';
while ($poll_uid !~ /^\d+$/) {
$poll_uid = ask(<<EOT);
What User ID (UID) should be used as owner of your converted polls?
[Default = UID #$tmpvars{anonymous_coward_uid}->{value}]:
EOT
$poll_uid = $tmpvars{anonymous_coward_uid}->{value}
if !length($poll_uid);
print "Invalid UID!\n\n" if $poll_uid !~ /^\d+$/;
}
convert();
}
sub incremental {
# Load vars from existing 2.2 site.
load_vars($dbh_new);
# Reload topic data for the rest of the show.
reload_keys('topics', \%topics, 'tid', 'alttext');
# Stories.
#
# Determine list of authors, before story processing.
update_stories();
# Polls.
update('pollquestions', \%poll_data);
reload_keys('pollquestions', \%polls, 'qid', 'date');
update_pollresults(\%poll_data);
%skip_polls=();
# If you need to be SURE of consistent comment records, a full import is
# your best bet, although incrementals should be "good enough".
copy_comments();
%polls = (); %poll_data = ();
# And the rest of the rabble.
update('moderatorlog', 'id');
%discussions=(); %comments=();
for my $table (@user_tables, qw(submissions metamodlog)) {
update($table);
}
%topics=();
users_keys();
}
sub merge_vars {
my (%oldvars, %newvars);
local $" = '|';
print "Processing vars\n";
load_vars($dbh_new, \%newvars);
load_vars($dbh_old, \%oldvars);
# These vars are not replaced in the new setup.
my @install_vars = qw(
logdir
basedir
datadir
siteid
);
my $search = "^(@install_vars)\$";
for (keys %oldvars) {
next if /$search/;
$newvars{$_} = $oldvars{$_};
}
$dbh_new->do('DELETE FROM vars');
for my $key (sort keys %newvars) {
# Remember to set the global vars on merge!
$vars{$key} = $newvars{$key}->{value};
map { $newvars{$key}->{$_} =
$dbh_new->quote($newvars{$key}->{$_})
} qw(name value description);
$dbh_new->do(<<EOT);
INSERT INTO vars VALUES(
$newvars{$key}->{name},
$newvars{$key}->{value},
$newvars{$key}->{description}
)
EOT
}
}
# This doesn't yet support incremental mode.
sub convert_plugins {
my($new_plugins, $old_plugins);
$new_plugins = $dbh_new->selectcol_arrayref(
'SELECT value FROM site_info WHERE name="plugin"'
);
$old_plugins = $dbh_old->selectcol_arrayref(
'SELECT value FROM site_info WHERE name="plugin"'
);
my $printed = 0;
my $alit;
{
local $" = '|';
$alit = "(@{$old_plugins})";
}
for my $pi (@{$new_plugins}) {
# Handle plugin fixup.
if ($plugin_fixup{$pi}) {
print "\nExecuting '$pi' plugin fixup.\n\n";
&{$plugin_fixup{$pi}}();
}
# Handle plugin conversion and cleanup.
if ($forced_plugins{$pi} || $pi =~ /^$alit$/) {
if (! $printed) {
print "\nConverting PLUGINS...\n";
$printed++;
}
if ($plugins{$pi} && ! $plugins{$pi}{tables}) {
print <<EOT;
Skipping conversion: No conversion tableset for PLUGIN '$pi'!
EOT
next;
}
print "\n=== $pi ===\n";
for (@{$plugins{$pi}{tables}}) {
duplicate($_, 1);
}
if (exists $plugin_cleanup{$pi} &&
(ref $plugin_cleanup{$pi} eq 'CODE'))
{
print "Plugin Cleanup.\n";
&{$plugin_cleanup{$pi}}();
}
}
}
}
sub load_vars {
my($db, $varhash) = @_;
my $sth = $db->prepare('SELECT * FROM vars');
$sth->execute;
while (my $rec = $sth->fetchrow_hashref) {
# If VARHASH is passed, we create a hash of hashrefs based on
# var name.
if ($varhash) {
$varhash->{$rec->{name}} = $rec;
next;
}
# Otherwise we populate the global vars hash.
$vars{$rec->{name}} = $rec->{value};
}
}
# Technically, this will work on any table with a non date/int key that
# we want to update. Something to think about for the future.
sub update_stories {
my(@stories, %hitlist);
# There should be a DESTQUERY "DELETE FROM stories..." to remove stories
# within the last two weeks. Then these stories (remember to grab SIDs!)
# should be imported/updated and/or marked dirty for refreshing.
my $datecond = <<EOT;
date_format(date_sub(now(), interval 15 day), '%Y-%m-%d 00:00')
EOT
# Load the hits for processing.
load_storystuff(\%hitlist);
# Remove stale rows.
my($deleted_stories, $deleted_discussions) = (0, 0);
$deleted_stories =
$dbh_new->do("DELETE FROM stories WHERE time>=$datecond")
if $opts{I};
# Update stories.
my $sql = 'SELECT * FROM stories';
$sql .= " WHERE time >= $datecond" if $opts{I};
my $sth_s = $dbh_old->prepare($sql);
$sth_s->execute;
die "SQL: $sql\n" if $dbh_new->errstr;
printf "Processing stories\n";
do_handle('stories', $sth_s, !$opts{I}, \%hitlist, \@stories);
# Now go back and handle discussion updates.
print "Reprocessing discussions and hitparade...\n";
# Now what we NEED to do here is flatten @stories into a temporary
# hash keyed on SID so when discussions gets processed it doesn't take
# forever to get the associated record.
my %story_hash;
for (@stories) {
$story_hash{$_->{sid}} = $_;
}
my $cond = '';
if ($opts{I}) {
$cond = "WHERE ts >= $datecond";
$deleted_discussions =
$dbh_new->do("DELETE FROM discussions $cond");
}
$sql = <<EOT;
SELECT * FROM discussions $cond
EOT
$sth_s = $dbh_old->prepare($sql);
$sth_s->execute;
die "SQL: $sql\n" if $dbh_old->errstr;
# Need a list of the Discussion SIDs that are updated, here.
printf "Processing discussions\n";
do_handle('discussions', $sth_s, 0, \%story_hash);
reload_keys('discussions', \%discussions, 'sid', 'sid', 'id');
# Update stories with the proper discussion ID.
for my $story (@stories) {
# If an ARCHIVED STORY has a non-existant discussion ID, then
# we must create one.
if (! $discussions{$story->{sid}}) {
$sql = <<EOT;
INSERT INTO discussions (sid, title, url, topic, ts, flags)
VALUES (
@{[$dbh_new->quote($story->{sid})]},
@{[$dbh_new->quote($story->{title})]},
'$vars{rootdir}/article.pl?sid=$story->{sid}',
@{[$topics{$story->{tid}} || $vars{defaulttopic}]},
'$story->{time}',
'dirty'
)
EOT
$dbh_new->do($sql);
die "SQL: $sql\n" if $dbh_new->errstr;
$discussions{$story->{sid}} = getLastInsertID();
print <<EOT;
+ Inserted missing story data "$story->{title}"
($story->{sid}) as #$discussions{$story->{sid}}
EOT
}
# Make SURE we update stories....damned circular relationships!
$dbh_new->do(<<EOT);
UPDATE stories SET discussion = $discussions{$story->{sid}}
WHERE sid=@{[$dbh_new->quote($story->{sid})]}
EOT
}
print <<EOT if $deleted_discussions + $deleted_stories;
Deleted $deleted_stories stories, $deleted_discussions discussions
EOT
print <<EOT;
Imported @{[scalar @stories]} stories and associated records.
EOT
}
sub update_pollresults {
my ($poll_data, $del) = @_;
$del ||= 0;
my $sth_s;
return if !keys %{$poll_data};
printf "Updating results from %d new polls...\n",
scalar keys %{$poll_data};
for (keys %{$poll_data}) {
$sth_s = $dbh_old->prepare(<<EOT);
SELECT * FROM pollanswers WHERE qid=@{[$dbh_old->quote($_)]}
EOT
$sth_s->execute;
do_handle('pollanswers', $sth_s, $del);
$sth_s = $dbh_old->prepare(<<EOT);
SELECT * FROM pollvoters WHERE qid=@{[$dbh_old->quote($_)]}
EOT
$sth_s->execute;
do_handle('pollvoters', $sth_s, $del);
# Create discussion for existing poll if it doesn't already
# exist.
if (! $discussions{$_}) {
my $poll_url = "$vars{rootdir}/pollBooth.pl?qid=$_";
my $insert_q = <<EOT;
INSERT INTO discussions (title, url, topic, ts, flags)
VALUES (
$poll_data{$_}->{question},
@{[$dbh_new->quote($poll_url)]},
$vars{defaulttopic},
$poll_data{$_}->{date},
'dirty'
)
EOT
$dbh_new->do($insert_q);
die "SQL: $insert_q\n" if $dbh_new->errstr;
$discussions{$_} = getLastInsertID();
# Now have to update the poll!
$dbh_new->do(<<EOT);
UPDATE pollquestions SET discussion = $discussions{$_} WHERE sid = '$_'
EOT
print <<EOT;
+ Created discussion for poll"$poll_data{$_}->{question}" ($_/$discussions{$_})
EOT
}
# Only want del active the first time thru this, if it's active
# at all.
$del = 0 if $del;
}
# Make sure we get the latest poll. This should only return an array
# with one value. Note that this is a TRANSFER of data, so use of
# %vars here, is incorrect.
my $newpoll_ar = $dbh_old->selectall_arrayref(<<EOT);
SELECT value FROM vars WHERE name='currentqid'
EOT
$dbh_new->do(<<EOT);
UPDATE vars SET value=$polls{$newpoll_ar->[0][0]} WHERE name='currentqid'
EOT
}
####################################
# Full conversion subroutine.
sub convert {
# Remember that the order of processing is CRITICAL.
# First we merge vars between the two sites. See details in the
# subroutine def.
merge_vars();
# These are direct copy tables that don't depend on anything else.
duplicate('content_filters', 1);
duplicate('tzcodes', 'tz');
duplicate('menus', 1) if $process_menus;
duplicate('topics', 1);
reload_keys('topics', \%topics, 'tid', 'alttext');
duplicate('sections', 1);
for (@user_tables) {
duplicate($_, 0);
}
# Stories: Merge with storiestuff data, uid, aid, sid, tid
# removal of all text fields into their own table (story_text).
update_stories();
duplicate('pollquestions', 1, \%poll_data);
reload_keys('pollquestions', \%polls, 'qid', 'date');
update_pollresults(\%poll_data, 1);
# Done with polls by this point.
%skip_polls = ();
copy_comments();
%polls = (); %poll_data = ();
# Moderatorlog: Fix sid, cid.
duplicate('moderatorlog', 1);
# Desperately needed, if you have a site the size of Slashdot, you're
# probably running out of memory right about now.
%discussions = ();
%comments = ();
# Submissions: Fix uid, tid
duplicate('submissions', 1);
# do all the blocks stuff
duplicate('blocks', 'bid');
$dbh_new->do('INSERT INTO backup_blocks SELECT bid, block FROM blocks');
# Ignoring: formkeys, accesslog
duplicate('metamodlog', 1);
convert_plugins();
# CLEAN UP PHASE -- THE MISCELLANY OF QUERIES
#
# Deal with section_topics table (new for Fry).
print "\nPerforming clean up...\n\n";
print "Constructing section_topics\n";
$dbh_new->do(<<EOT);
DELETE FROM section_topics
EOT
$dbh_new->do(<<EOT);
INSERT INTO section_topics
SELECT distinct section, tid FROM topics, sections WHERE LENGTH(section) > 0
EOT
# Construct author_cache table (new for Fry).
print "Constructing authors_cache\n";
$dbh_new->do(<<EOT);
DELETE FROM authors_cache
EOT
$dbh_new->do(<<EOT);
REPLACE INTO authors_cache
SELECT users.uid, nickname, fakeemail, homepage, count(stories.uid), bio, author
FROM users, stories, users_info
WHERE stories.uid=users.uid AND users.uid=users_info.uid
GROUP BY stories.uid
EOT
# Delete 'author' entries from users_param.
print "Cleaning users_param of all 'author' entries...";
{
local $" = ',';
$dbh_new->do(<<EOT);
DELETE FROM users_param WHERE name='author'
EOT
}
print "Done.\n";
print "\nConversion completed.\n\n";
}
####################################
sub ask {
local $| = 1;
chomp(my $question = $_[0]);
print "\n", $question, " ";
chomp(my $answer = <STDIN>);
print "\n";
return $answer;
}
sub load_storystuff {
my ($hitlist) = @_;
print "Loading hitcounts into story metadata...\n";
my $sth_s = $dbh_old->prepare('SELECT sid, hits FROM storiestuff');
$sth_s->execute;
while (my $data = $sth_s->fetchrow_hashref) {
$hitlist->{$data->{sid}}{hits} = $data->{hits};
}
$sth_s->finish;
}
sub copy_comments {
my %dead_discussions;
my($lastdate, $cond) = ('', '');
# Is this sufficient or should this be SID based?
my $datecond = <<EOT;
date_format(date_sub(now(), interval 15 day), '%Y-%m-%d 00:00')
EOT
my $sth_d = $dbh_new->prepare("SELECT $datecond");
$sth_d->execute;
my($display) = $sth_d->fetchrow_array;
if ($opts{I}) {
$cond = "WHERE date > $datecond";
print "Deleting stale comments...\n";
my(@delete_sids) = @{$dbh_new->selectall_arrayref(<<EOT)};
SELECT cid FROM comments $cond
EOT
# Flatten resulting list into a list of CIDs to delete.
@delete_sids = map { $_ = $_->[0] } @delete_sids;
my $deleted_comm = $#delete_sids + 1;
# We delete in batches to prevent the SQL buffer from
# overflowing.
my $batchsize = 100;
LOOP: { do {
my @batch = grep { defined($_) }
@delete_sids[0 .. $batchsize-1];
local $" = ',';
$dbh_new->do(<<EOT) if @batch;
DELETE FROM comment_text WHERE cid IN (@batch)
EOT
last LOOP if $#delete_sids < $batchsize - 1;
@delete_sids = @delete_sids[$batchsize..$#delete_sids];
} while (@delete_sids) };
$dbh_new->do("DELETE FROM comments $cond");
print "Copying comment data and texts from $display...\n";
} else {
print "Copying comment data and texts...\n";
}
my $sth_s = $dbh_old->prepare("SELECT * FROM comments $cond");
$sth_s->execute;
LOOP:
while (my $data = $sth_s->fetchrow_hashref) {
next if exists $dead_discussions{$data->{sid}};
my $sid = $discussions{$data->{sid}};
if (! $sid) {
$dead_discussions{$data->{sid}}++;
print <<EOT;
Skipping deleted discussion '$data->{sid}'
EOT
next LOOP;
}
my $insert_d = scalar join(', ',
map { $dbh_new->quote($data->{$_}) }
qw [pid date subject uid points lastmod reason]
);
my $insert_q = <<EOT;
INSERT INTO comments
(sid, pid, date, subject, uid, points, lastmod, reason)
VALUES ($sid, $insert_d)
EOT
$dbh_new->do($insert_q);
die "SQL: $insert_q\n" if $dbh_new->errstr;
my $new_cid = getLastInsertID();
# This only needs to be done for comments that are "live".
$comments{$data->{sid}}->{$data->{cid}} = $new_cid
if $data->{date} ge $display;