summaryrefslogtreecommitdiff
path: root/bin/keyword-export.pl
diff options
context:
space:
mode:
Diffstat (limited to 'bin/keyword-export.pl')
-rwxr-xr-xbin/keyword-export.pl156
1 files changed, 156 insertions, 0 deletions
diff --git a/bin/keyword-export.pl b/bin/keyword-export.pl
new file mode 100755
index 0000000..4f04579
--- /dev/null
+++ b/bin/keyword-export.pl
@@ -0,0 +1,156 @@
+#!/usr/bin/perl
+
+use lib "/var/www/vhosts/carbonpictures.com/bucky/lib";
+use Bucky;
+
+$dbh = DBI->connect ($dsn);
+
+# get keyword + threads
+my $target = $ARGV[0];
+
+if ($target > 0)
+ {
+ my ($t) = get_thread($target);
+ export_thread($t);
+ }
+else
+ {
+ export_keyword($target);
+ }
+
+$dbh->disconnect();
+
+sub export_keyword
+ {
+ my ($keyword) = @_;
+
+ print "Exporting keyword $keyword\n";
+ my $kw = get_keyword($keyword);
+ if ($kw == -1)
+ { print "No such keyword: $kw\n"; quit_out(); }
+ my $threads = get_full_threads_by_keyword($keyword);
+
+ # mkdir keyword, chdir
+ mkdir ("$keyword");
+ chdir ("$keyword");
+
+ open FILE, ">keyword" or die $!;
+ foreach my $key (keys %$kw)
+ {
+ print FILE "$key:".$kw->{$key}."\n";
+ }
+ close FILE;
+
+ # foreach thread:
+ foreach my $thread (@$threads)
+ {
+ export_thread($thread);
+ }
+
+ chdir("..");
+ }
+
+sub export_thread
+ {
+ my ($thread) = @_;
+
+ # get all comments & files
+ my $comments = get_comments($thread->{id});
+ my $files = get_files($thread->{id});
+
+ mkdir ("$thread->{id}");
+ chdir ("$thread->{id}");
+
+ print "thread $thread->{id} -- $thread->{title}\n";
+ print "comments: $thread->{comments}\n";
+ print "files: $thread->{files}\n";
+
+ open THREAD, ">thread" or die $!;
+ foreach my $key (keys %$thread)
+ {
+ print THREAD "$key:".$thread->{$key}."\n";
+ }
+ print THREAD "\n";
+ close THREAD;
+
+ # cat each comment & file to text files based on id
+ foreach my $comment (keys %$comments)
+ {
+ my $c = $comments->{$comment};
+ open COMMENT, ">comment.".$c->{id} or die $!;
+ foreach my $key (keys %$c)
+ {
+ next if ($key eq "comment");
+ print COMMENT "$key:".$c->{$key}."\n";
+ }
+ print COMMENT "\n";
+ print COMMENT $c->{comment};
+ close COMMENT;
+ }
+
+ foreach my $f (@$files)
+ {
+ open FILE, ">file.".$f->{id} or die $!;
+ foreach my $key (keys %$f)
+ {
+ print FILE "$key:".$f->{$key}."\n";
+ }
+ close FILE;
+ }
+
+ if (-e "$data_path/$thread->{id}")
+ {
+ print "Copying ...\n";
+ mkdir ("data/");
+ system ("/bin/cp -r $data_path/$thread->{id}/* data/")
+ }
+
+ chdir ("..");
+ print "\n";
+ }
+
+sub get_full_threads_by_keyword
+ {
+ my ($keyword, $private) = @_;
+ my @rows;
+ my $query;
+ my @keys = qw(id title username keyword createdate lastmodified size private allowed flagged color);
+ my $rows = 0;
+
+ $keyword = $dbh->quote($keyword);
+ $query = "SELECT id,title,username,keyword,createdate,lastmodified,size,private,allowed,flagged,color FROM threads WHERE keyword=$keyword";
+ if (defined($private) && $private)
+ {
+ $query .= " AND (ISNULL(private) OR private = '')";
+ }
+
+ print $query."<br>" if ($DEBUG);
+ $sth = $dbh->prepare($query);
+ $sth->execute();
+ while (my (@row) = $sth->fetchrow_array())
+ {
+ my %temphash;
+ for (my $i = 0; $i < @row; $i++)
+ {
+ $temphash{$keys[$i]} = $row[$i];
+ }
+ $rows[$rows] = \%temphash;
+ $rows++;
+ }
+ $sth->finish();
+
+ if ($rows == 0)
+ {
+ print "No threads!\n" if ($DEBUG);
+ return -1;
+ }
+
+ for (my $i = 0; $i < @rows; $i++)
+ {
+ $rows[$i]{comments} = count_comments($rows[$i]{id});
+ $rows[$i]{files} = count_files($rows[$i]{id});
+ }
+
+ return \@rows;
+ }
+