summaryrefslogtreecommitdiff
path: root/bin/keyword-import.pl
diff options
context:
space:
mode:
Diffstat (limited to 'bin/keyword-import.pl')
-rwxr-xr-xbin/keyword-import.pl252
1 files changed, 252 insertions, 0 deletions
diff --git a/bin/keyword-import.pl b/bin/keyword-import.pl
new file mode 100755
index 0000000..1a377bb
--- /dev/null
+++ b/bin/keyword-import.pl
@@ -0,0 +1,252 @@
+#!/usr/bin/perl
+
+use lib "/bucky/lib";
+use Bucky;
+
+$dbh = DBI->connect ($dsn);
+
+# get keyword + threads
+my $target = $ARGV[0];
+my @dirs;
+my %threads, %comments, %files;
+our %keywords = get_keywords();
+my $keyword;
+
+if ($target > 0)
+ {
+ push @dirs, $target if (-e $target && -d $target);
+ }
+else
+ {
+ $keyword = $ARGV[0];
+ print "Importing keyword $keyword\n";
+ @dirs = dir_list("$keyword/");
+ chdir("$keyword/");
+ }
+
+# read in all data...
+foreach my $dir (@dirs)
+ {
+ next if ($dir eq "keyword");
+ my @files = dir_list("$dir/");
+ my $t, $c, $f;
+
+ print "Reading in thread $dir ...\n";
+ foreach my $filename (@files)
+ {
+ next if ($filename !~ /(thread|comment|file)/);
+ my ($type, $id) = split /\./, $filename, 2;
+ my $file = read_file("$dir/$filename");
+
+ if ($type eq "thread")
+ {
+ $t = flat2thread($file);
+ $threads{$t->{id}} = $t;
+ print "t.$t->{id} ";
+ }
+ elsif ($type eq "comment")
+ {
+ $c = flat2comment($file);
+ $comments{$c->{id}} = $c;
+ print "c.$c->{id} ";
+ }
+ elsif ($type eq "file")
+ {
+ $f = flat2file($file);
+ if (-e "$dir/data/$f->{filename}")
+ {
+ $files{$f->{id}} = $f;
+ print "f.$f->{id} ";
+ }
+ else
+ {
+ print "No data for $f->{filename} !\n"
+ }
+ }
+ }
+ print "$t->{title}";
+ print "\n";
+ }
+
+print "\n";
+
+# now that data is all in a structure,
+# parse through threads, comments, files IN ORDER
+# get "real" thread ids to update "thread" when you next encounter it...
+# as comments are created, keep track of "real ids".. update "parent_id != -1" where appropriate
+# as files are created, update parent_id and move files
+
+# if keyword does not exist, create it.
+#my $realk = get_keyword($keyword);
+#if ($realk == -1)
+# {
+# my $file = read_file("keyword");
+# my $k = flat2thread($file);
+# instantiate_keyword($k);
+# }
+
+my %realthread = (-1 => -1);
+my %realcomment = (-1 => -1);
+my %realfile = (-1 => -1);
+
+foreach my $tid (sort { $a <=> $b } keys %threads)
+ {
+ my $t = $threads{$tid};
+ my $fakeid = instantiate_thread($t);
+ print "thread $t->{id} => $fakeid\n";
+ $realthread{$t->{id}} = $fakeid;
+ $ttot++;
+ }
+
+foreach my $cid (sort { $a <=> $b } keys %comments)
+ {
+ my $c = $comments{$cid};
+ my $fakeid = instantiate_comment($c, $realthread{$c->{thread}}, $realcomment{$c->{parent_id}});
+ print "comment $c->{id} => $fakeid\n";
+ print " .. new thread: $c->{thread} -> $realthread{$c->{thread}}\n";
+ print " .. new parent: $c->{parent_id} -> $realcomment{$c->{parent_id}}\n"
+ if ($c->{parent_id} != -1);
+ $realcomment{$c->{id}} = $fakeid;
+ $ctot++;
+ }
+
+foreach my $fid (sort { $a <=> $b } keys %files)
+ {
+ my $f = $files{$fid};
+ my $realt = $realthread{$f->{parent_id}};
+ my $fakeid = instantiate_file($f, $realt);
+ mkdir ("$data_path/$realt/") unless (-e "$data_path/$realt/");
+ mkdir ("$data_path/$realt/.thumb/") unless (-e "$data_path/$realt/.thumb/");
+ system("/bin/chmod 777 $data_path/$realt/.thumb");
+ system ("/bin/cp \"$f->{parent_id}/data/$f->{filename}\" $data_path/$realt/");
+ print "file $f->{id} => $fakeid\n";
+
+ print " .. new thread: $f->{parent_id} -> $realthread{$f->{parent_id}}\n";
+ $realfile{$f->{id}} = $fakeid;
+ $ftot++;
+ $sizetot += $f->{size};
+ }
+
+print "Import successful!\nTotal: $ttot.t $ctot.c $ftot.f ($sizetot b.)\n";
+
+$dbh->disconnect();
+
+###########################
+
+sub instantiate_keyword
+ {
+ my ($k) = @_;
+ my %nk =
+ (
+ keyword => $k->{keyword},
+ threads => " ",
+ owner => $k->{username},
+ public => $k->{public},
+ agglutinate => $k->{agglutinate},
+ color => $k->{color}
+ );
+ add_keyword(\%nk);
+ }
+
+sub instantiate_thread
+ {
+ my ($t) = @_;
+ my %nt =
+ (
+ title => $t->{title},
+ username => $t->{username},
+ keyword => (exists ($keywords{$t->{keyword}}) ? $t->{keyword} : ""),
+ createdate => $t->{createdate},
+ lastmodified => $t->{lastmodified},
+ size => $t->{size},
+ private => $t->{private},
+ allowed => $t->{allowed},
+ flagged => $t->{flagged},
+ color => $t->{color},
+ display => $t->{display}
+ );
+ my $thread_id = add_thread_by_hash(\%nt);
+ die if ($thread_id == -1);
+ return $thread_id;
+ }
+
+sub instantiate_comment
+ {
+ my ($c, $newt, $newp) = @_;
+ my $comment_id = add_comment($newt, $newp, $c->{username}, $c->{comment}, $c->{date});
+ return $comment_id;
+ }
+
+sub instantiate_file
+ {
+ my ($f, $newt) = @_;
+ my $file_id = add_file($newt, $f->{username}, $f->{filename}, $f->{size}, $f->{date});
+ return $file_id;
+ }
+
+###########################
+
+sub dir_list
+ {
+ my ($d) = @_;
+ opendir (DIR, $d) or die "couldn't list: $d, $!";
+ @files = grep (!/^\./, sort readdir (DIR));
+ closedir DIR;
+ return @files;
+ }
+
+sub flat2hash
+ {
+ my ($file) = @_;
+ my %hash;
+ foreach my $line (@$file)
+ {
+ $line =~ s/\r//;
+ chomp $line;
+ last if (!$line);
+ last if ($line eq "\n");
+ my ($k, $v) = split /:/, $line, 2;
+ $hash{$k} = $v;
+ }
+ return \%hash;
+ }
+
+sub flat2comment
+ {
+ my ($file) = @_;
+ my $comment_hash = flat2hash($file);
+ my $reading = 0;
+ foreach my $line (@$file)
+ {
+ $line =~ s/\r//;
+ if (!$line && !$reading)
+ { $reading = 1; next; }
+ next unless ($reading);
+ $comment_hash->{comment} .= $line;
+ }
+ return $comment_hash;
+ }
+
+sub flat2file
+ {
+ my ($file) = @_;
+ return flat2hash($file);
+ }
+
+sub flat2thread
+ {
+ my ($file) = @_;
+ return flat2hash($file);
+ }
+
+sub read_file
+ {
+ my ($file) = @_;
+ my @out;
+
+ open F, $file or die "problem with $file $!";
+ @out=<F>;
+ close F;
+ return \@out;
+ }
+