summaryrefslogtreecommitdiff
path: root/bin/username-export.pl
blob: 38024ed5174a8513608e1cfe080ed238be11d7a2 (plain)
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
#!/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;
	}