diff options
Diffstat (limited to 'bin/username-export.pl')
| -rwxr-xr-x | bin/username-export.pl | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/bin/username-export.pl b/bin/username-export.pl new file mode 100755 index 0000000..38024ed --- /dev/null +++ b/bin/username-export.pl @@ -0,0 +1,165 @@ +#!/usr/bin/perl + +$DEBUG = 1; + +use lib "../cgi-bin"; +use localbucky; + +$dbh = DBI->connect ($dsn); + +# get keyword + threads + +if (@ARGV > 0) + { + export_users(@ARGV); + } + +$dbh->disconnect(); + +sub export_users + { + print "Exporting users @_\n"; + my $threads = get_full_threads_by_username(@_); + + print "Got " . @$threads . " threads"; + + # mkdir username, chdir + mkdir ($_[0]); + chdir ($_[0]); + + # 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_username + { + my @usernames = @_; + my @rows; + my $query; + my @keys = qw(id title username keyword createdate lastmodified size private allowed flagged color); + my $rc = 0; + + my $threadz = {}; + for my $username (@usernames) { + print $username . "\n"; + my $quser = $dbh->quote($username); + + $query = "SELECT thread FROM comments WHERE username=$quser GROUP BY thread"; + print $query . "\n"; + $sth = $dbh->prepare($query); + $sth->execute(); + while (my (@row) = $sth->fetchrow_array()) { + $threadz->{$row[0]} = 1; + } + + $query = "SELECT thread FROM files WHERE username=$quser GROUP BY thread"; + print $query . "\n"; + $sth = $dbh->prepare($query); + $sth->execute(); + while (my (@row) = $sth->fetchrow_array()) { + $threadz->{$row[0]} = 1; + } + } + my $values = join ",", sort keys %$threadz; + + $query = "SELECT id,title,username,keyword,createdate,lastmodified,size,private,allowed,flagged,color FROM threads WHERE id IN ($values)"; +# 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[$rc] = \%temphash; + $rc++; + } + $sth->finish(); + + if ($rc == 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; + } + + |
