forked from lottadot/haxlash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslash1toslash2.2
executable file
·1901 lines (1532 loc) · 51 KB
/
slash1toslash2.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-2005 by Open Source Technology Group. 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';
use vars qw(%my_conf);
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my %opts;
# Remember to doublecheck these match usage()!
usage('Options used incorrectly') unless getopts('Ihvu:', \%opts);
usage() if $opts{'h'};
version() if $opts{'v'};
usage('Need virtual user') unless $opts{'u'};
push @ARGV, './slashdotrc.pl' if !@ARGV;
usage("Need slashdotrc.pl file") unless my $rcfile = $ARGV[0];
####################################
# disclaimer
{
# We don't ask this if we are in incremental-mode.
last if $opts{I};
my $answer = ask(<<'EOT');
SLASH v1.0 (The Beast) to Slash v2.2 (Fry) Conversion Utility
based on original slash1toslash2 script by pudge
2.2 conversions by Cliff
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
# Turn off warnings while processing the RC file.
$^W = 0; require $rcfile; $^W = 1;
*my_conf = $Slash::conf{DEFAULT} = $Slash::conf{DEFAULT};
my $dbh_old = DBI->connect(@my_conf{qw[dsn dbuser dbpass]});
die "Can't open connection to existing 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
users_hits
);
my($vars, $del_users);
my(%ac_uid, %topics, %discussions, %polls, %comments, %skip_polls, %poll_data);
my(%story_authors);
my $usersub;
my (%conversions) = (
# This will probably not remain a straight copy for long.
'sections' => undef,
'blocks' => sub {
my($data, $skip_blocks) = @_;
return if exists $skip_blocks->{$data->{bid}};
if ($data->{bid} eq 'colors' || $data->{bid} =~ /_colors$/) {
my $nc = '[^,]+,?';
my $search = "($nc$nc$nc$nc)($nc$nc$nc$nc)";
$data->{block} =~
s/^$search$/$1#CCCCCC,$2,#CCCCCC/;
}
# Handle sitename changes in blocks.
my $searchfor = quotemeta($my_conf{rootdir});
$data->{block} =~ s/$searchfor/$vars->{rootdir}/g
if $data->{block};
# Why the schema as of 08/16/01 has no defaults for these, I
# don't know.
$data->{seclev} ||= 0;
$data->{section} ||= $vars->{defaultsection};
$data->{portal} ||= 0;
$data->{retrieve} ||= 0;
$data->{title} ||= '';
# Drop columns.
delete $data->{aid};
delete $data->{blockbak};
return $data;
},
'tzcodes' => sub {
my($data) = @_;
$data->{off_set} = $data->{offset};
delete $data->{offset};
$data->{tz} = uc $data->{tz};
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->{flags} = 'dirty';
if (!$opts{I}) {
# Reset the flag only if necessary. Pretty much we
# want to set things dirty and clean them up later.
my $ws = $story_hash->{$data->{sid}}{writestatus} || 0;
if ($ws == 10) { $data->{type} = 'archived';
$data->{flags} = 'ok' }
elsif ($ws == 5) { $data->{flags} = 'delete' }
elsif ($ws == 1) { $data->{flags} = 'dirty' }
else { $data->{flags} = 'ok' }
}
$data->{'ALLOWED_FIELDS'} = [qw(
id sid title url topic ts type uid commentcount
)];
return $data
},
'users' => ($usersub = sub {
my($data) = @_;
# This should even handle the cases of new additions to MAIN
# code (read: Slashdot-only) -- If the MAIN based site uses
# these fields, they'll get copied, if they don't, no problem.
# Resolve UID collisions (anonymous user). Remember we drop
# any records from UID $ac_uid{$ac_uid{-1}}, since they aren't
# supposed to exist.
return if $ac_uid{$data->{uid}} &&
$data->{uid} == $ac_uid{$data->{uid}};
$data->{uid} = $ac_uid{$data->{uid}}
if !$opts{I} && exists $ac_uid{$data->{uid}};
$data->{seclev} = 1
if exists $data->{seclev} && $data->{seclev} == 0;
$data->{tzcode} = uc($data->{tzcode})
if exists $data->{tzcode};
$data->{passwd} = md5_hex($data->{passwd})
if exists $data->{passwd};
if ($data->{exaid}) {
my @auth_uids;
for (split /,/, $data->{exaid}) {
push @auth_uids, $story_authors{$_}
if $story_authors{$_};
}
$data->{exaid} = join ',', @auth_uids;
}
# Valid fields for ALL user_* tables since they all use
# the same filter. Anything not in this list will be dropped
# from $data at INSERT time.
$data->{ALLOWED_FIELDS} = [qw(
uid nickname realemail fakeemail homepage passwd sig
seclev matchname newpasswd
points posttype defaultpoints highlightthresh
maxcommentsize hardthresh clbig clsmall reparent nosigs
commentlimit commentspill commentsort noscores mode
threshold
extid exaid exsect exboxes maxstories noboxes
totalmods realname bio tokens lastgranted karma maillist
totalcomments lastmm lastaccess lastmmid m2fair m2unfair
m2unfairvotes upmods downmods session_login
willing dfid tzcode noicons light mylinks lang
)];
return $data;
}),
'users_comments' => $usersub,
'users_index' => $usersub,
'users_info' => $usersub,
'users_prefs' => $usersub,
'stories' => sub {
my($data, $hitlist, $storylist) = @_;
$data->{uid} = $story_authors{lc($data->{aid})};
# This is fatal.
die <<EOT if !$data->{uid};
NULL UID DETECTED FOR AUTHOR '$data->{aid}' IN STORY '$data->{sid}'
EOT
# 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 writestatus when
# we do a full import.
if (!$opts{I}) {
my $ws = $data->{writestatus} || 0;
if ($ws == 10) { $data->{writestatus} = 'archived' }
elsif ($ws == 5) { $data->{writestatus} = 'delete' }
elsif ($ws == 1) { $data->{writestatus} = 'dirty' }
else { $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}};
# Program should accept a default topic for polls.
$data->{'topic'} = -1;
# 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) = @_;
# Fix UID.
$data->{uid} = $ac_uid{$data->{uid}}
if exists $ac_uid{$data->{uid}};
$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) = @_;
# Fix UID.
$data->{uid} = $ac_uid{$data->{uid}}
if exists $ac_uid{$data->{uid}};
$data->{tid} = $topics{$data->{tid}} if $data->{tid};
$data->{tid} ||= $vars->{defaulttopic};
$data->{$_} ||= '' for (qw(comment name email note story));
return $data;
},
'metamodlog' => sub {
# copy straight, but fix uid when handling anonymous user (or
# user which may be moved around as result of AC UID).
my($data) = @_;
$data->{uid} = $ac_uid{$data->{uid}}
if exists $ac_uid{$data->{uid}};
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($sth_d);
my @prefs = qw(
absolutedir comment_minscore
comment_maxscore defaultsection defaulttopic rootdir archive_delay
);
push @prefs, 'anonymous_coward_uid' if $opts{I};
# Get AC UID.
{
local $" = ',';
map { $_ = $dbh_new->quote($_) } @prefs;
$sth_d = $dbh_new->prepare(<<EOT);
SELECT name, value FROM vars WHERE name IN (@prefs)
EOT
}
$sth_d->execute();
my $err = $dbh_new->errstr;
die "Error in retrieving variable settings: $err\n" if $err;
while (my $ar = $sth_d->fetchrow_arrayref) {
$ac_uid{-1} = $ar->[1]
if $ar->[0] eq 'anonymous_coward_uid';
$vars->{$ar->[0]} = $ar->[1];
}
if ($opts{I}) {
incremental();
# We're done.
exit 0;
}
####################################
# Questions: fix AC UID.
$ac_uid{-1} = ask(<<'EOT');
Please select a UID for the anonymous user of the site. It is probably
best if it is the lowest positive integer that is unused on the old
Slash 1.0 site. Enter the integer here (enter "1" to do no change):
EOT
$ac_uid{-1} ||= 1;
die "[$ac_uid{-1}] is not an integer.\n" unless $ac_uid{-1} =~ /^\d+$/;
$del_users = ask(<<'EOT') =~ /^y/i;
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 $_");
}
}
if ($ac_uid{-1} != 1) {
for (@user_tables) {
last if $del_users;
$dbh_new->do("DELETE FROM $_ WHERE uid=1");
}
$dbh_new->do(<<EOT);
UPDATE vars SET value=$ac_uid{-1} WHERE name='anonymous_coward_uid'
EOT
}
convert();
}
sub incremental {
# Reload topic data for the rest of the show.
reload_keys('topics', \%topics, 'tid', 'alttext');
# Stories.
#
# Determine list of authors, before story processing.
get_authors();
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)) {
# users_param, users_hits tables does not exist in BEAST's
# schema.
#
next if $table =~ /^(?:users_param|users_hits)$/;
update($table);
}
%topics=();
users_keys();
}
# 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);
map { s/^(database=)?([^;]+);?(.+)?$/$2/; }
my($old_name, $new_name)=($dbh_old->{Name}, $dbh_new->{Name});
# 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";
# Preserve min discussion ID of deleted records so we can
# properly delete discussion_hitparade.
my $sth_d = $dbh_new->prepare(<<EOT);
SELECT min(id) FROM discussions $cond
EOT
$sth_d->execute;
my($min_did) = $sth_d->fetchrow_array;
$deleted_discussions =
$dbh_new->do("DELETE FROM discussions $cond");
$dbh_new->do(<<EOT) if $min_did;
DELETE FROM discussion_hitparade WHERE discussion >= $min_did
EOT
}
$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);
# 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.
my $newpoll_ar = $dbh_old->selectall_arrayref(<<EOT);
SELECT value FROM vars WHERE name='currentqid'
EOT
# Remember we need to change the key for Fry.
$dbh_new->do(<<EOT);
UPDATE vars SET value=$polls{$newpoll_ar->[0][0]} WHERE name='currentqid'
EOT
}
####################################
# Full conversion subroutine.
sub convert {
# abusers, Actually, this should be converted!!!
# First, we determine if we need to do some creative user renumbering
# when dealing with the AC user.
fix_ac();
# Sections: This probably will NOT stay a straight copy for long.
duplicate('sections', 1);
# offset -> off_set
duplicate('tzcodes', 'tz');
duplicate('content_filters', 1);
# Replace topics dropping the character based tid for the new sequence.
duplicate('topics', 1);
reload_keys('topics', \%topics, 'tid', 'alttext');
# Deal with section_topics table (new for Fry).
$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
for (@user_tables) {
# users_param does not exist in MAIN.
next if $_ =~ /^(?:users_param|users_hits)$/;
duplicate($_, 0);
}
# put keys into users_param
users_keys();
# update aid's etc.
fix_authors();
duplicate('pollquestions', 1, \%poll_data);
reload_keys('pollquestions', \%polls, 'qid', 'date');
update_pollresults(\%poll_data, 1);
# Done with polls by this point.
%skip_polls = ();
# Stories: Merge with storiestuff data, uid, aid, sid, tid
# removal of all text fields into their own table (story_text).
update_stories();
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
copy_blocks();
fix_sectionblocks();
fix_vars();
# Ignoring: formkeys, accesslog
# Metamodlog: This should go fine as a direct copy fixing the AC uid.
duplicate('metamodlog', 1);
}
####################################
sub ask {
local $| = 1;
chomp(my $question = $_[0]);
print "\n", $question, " ";
chomp(my $answer = <STDIN>);
print "\n";
return $answer;
}
sub fix_ac {
print "Checking user database...\n";
# First, check to see if the desired UID is occupied...
my $sth_s = $dbh_old->prepare(<<EOT);
SELECT uid FROM users WHERE uid=$ac_uid{-1}
EOT
$sth_s->execute;
my ($uid) = $sth_s->fetchrow_array;
$sth_s->finish;
# If so, we must find the first free UID.
if ($uid) {
# Now do a user count.
$sth_s = $dbh_old->prepare("SELECT max(uid) FROM users");
$sth_s->execute;
my($maxuid) = $sth_s->fetchrow_array;
$sth_s->finish;
# Find the first gap.
$sth_s = $dbh_old->prepare(<<EOT);
SELECT uid FROM users WHERE uid >= 1 ORDER BY uid
EOT
$sth_s->execute;
my $lastuid;
while (my $c = $sth_s->fetchrow_arrayref) {
if (! $lastuid) {
$lastuid = $c->[0];
next;
}
last if $c->[0] != $lastuid + 1;
$lastuid = $c->[0];
}
$lastuid++;
printf <<EOT;
Existing user found at UID #$ac_uid{-1} will be moved to UID #$lastuid
EOT
$ac_uid{$ac_uid{-1}} = $lastuid;
}
}
####################################
# get vars out of slashdotrc.pl
sub fix_vars {
print "Processing vars\n";
# Some of these 'm2_*' vars should be removed.
for (qw(mailfrom siteadmin siteadmin_name smtp_server
sitename slogan mainfontface updatemin
archive_delay submiss_ts articles_only
allow_anonymous use_dept max_depth
defaultsection http_proxy fancyboxwidth
story_expire titlebar_width run_ads
authors_unlimited m2_comments m2_maxunfair
m2_toomanyunfair m2_bonus m2_penalty
m2_userpercentage comment_minscore
comment_maxscore submission_bonus goodkarma
badkarma maxkarma metamod_sum maxtokens
tokensperpoint maxpoints stir tokenspercomment
down_moderations post_limit max_posts_allowed
max_submissions_allowed submission_speed_limit
formkey_timeframe m2_mincheck m2_maxbonus
)) {
my $value = $dbh_new->quote($my_conf{$_});
$dbh_new->do("UPDATE vars SET value=$value WHERE name='$_'");
}
# don't overwrite new vars descriptions with old ones
my $vars = $dbh_old->selectall_arrayref("SELECT name,value FROM vars");
for my $var (@$vars) {
my @data = map { $dbh_new->quote($_) } @$var;
$dbh_new->do("UPDATE vars SET value=$data[1] WHERE name=$data[0]");
}
}
####################################
# copy blocks, skipping certain blocks and excluding some fields,
# fixing some data
sub copy_blocks {
my %skip_blocks = map { ($_, 1) } qw(
admin_footer admin_header comment commentswarning
edit_filter emailsponsor fancybox footer header
index index2 light_comment light_fancybox
light_footer light_header light_index light_story
light_story_link light_story_trailer light_titlebar
list_filters_footer list_filters_header mainmenu
menu motd newusermsg organisation pollitem
portalmap postvote story story_link story_trailer
storymore submit_after submit_before titlebar
userlogin
);
duplicate('blocks', 'bid', \%skip_blocks);
$dbh_new->do('INSERT INTO backup_blocks SELECT bid, block FROM blocks');
}
####################################
# add old sectionblocks data to blocks table
sub fix_sectionblocks {
print "Processing sectionblocks\n";
my $sth_s = $dbh_old->prepare("SELECT * FROM sectionblocks");
$sth_s->execute;
while (my $data = $sth_s->fetchrow_hashref) {
my $bid = $dbh_new->quote($data->{bid});
delete $data->{bid};
my $insert = sprintf("UPDATE blocks SET %s WHERE bid=$bid",
join ', ',
map { "$_=" . $dbh_new->quote($data->{$_}) }
keys %$data);
$dbh_new->do($insert);
my $err = $dbh_new->errstr;
die $err if $err;
}
}
####################################
# get users keys into users_param table
sub users_keys {
my($max_uid, $cond) = (0, '');
if ($opts{I}) {
my $sth_d = $dbh_new->prepare(<<EOT);
SELECT max(uid) FROM users_param WHERE name='pubkey'
EOT
$sth_d->execute();
($max_uid) = $sth_d->fetchrow_array;
$cond = "WHERE uid > $max_uid";
print "Processing users_keys from #$max_uid...\n";
} else {
print "Processing users_keys...\n";
}
my $users = $dbh_old->selectall_arrayref(<<EOT);
SELECT uid,pubkey FROM users_key $cond
EOT
for my $user (@$users) {
next if !$user->[1];
my $string = $dbh_new->quote($user->[1]);
my $sql = <<EOT;
INSERT INTO users_param (uid,name,value)
VALUES ($user->[0], 'pubkey', $string)
EOT
$dbh_new->do($sql);
print "SQL: $sql\n" if $dbh_new->errstr;
}
}
####################################
# bunch of things to get users fixed up
sub fix_authors {
my $authors = $dbh_old->selectall_arrayref(<<'EOT');
SELECT aid,seclev,lasttitle,section,deletedsubmissions
FROM authors
WHERE name != 'All Authors'
EOT
my(@not_found);
for my $author (@$authors) {
(my $matchname = lc $author->[0]) =~ s/[^a-zA-Z0-9]//g;
# Take the FIRST OCCURANCE, only.
next if $story_authors{$matchname};
# Note that we order by UID since there CAN be MORE than one.
my $uid = $dbh_old->selectrow_array(<<EOT);
SELECT uid
FROM users
WHERE matchname='$matchname'
ORDER BY uid
EOT
if ($uid) {
$story_authors{$matchname} = $uid;
my @data = map { $dbh_new->quote($_) } @$author;
$dbh_new->do(<<EOT);
UPDATE users SET seclev=$data[1] WHERE uid=$uid
EOT
$dbh_new->do(<<EOT);
INSERT INTO users_param (uid,name,value) VALUES ($uid, 'author', '1')
EOT
$dbh_new->do(<<EOT) if $data[2] && $data[2] ne "NULL";
INSERT INTO users_param (uid,name,value) VALUES ($uid, 'lasttitle', $data[2])
EOT
$dbh_new->do(<<EOT) if $data[3] && $data[3] ne "NULL";
INSERT INTO users_param (uid,name,value) VALUES ($uid, 'section', $data[3])
EOT