summaryrefslogtreecommitdiff
path: root/bin/keyword-import.pl
blob: 1a377bb2200fc23684e0e7de36081f8f7e971242 (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
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
#!/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;
    }