10 |
use Encode; |
use Encode; |
11 |
use HTTP::Date; |
use HTTP::Date; |
12 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
13 |
|
use File::Slurp; |
14 |
|
use File::MMagic::XS; |
15 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
16 |
|
|
17 |
my $debug = 1; |
my $debug = 0; |
18 |
my $max = 999; |
my $max = 999; |
19 |
|
|
20 |
my $page; |
my $page; |
22 |
|
|
23 |
my @page_names; |
my @page_names; |
24 |
|
|
25 |
|
print "Collecting pages...\n"; |
26 |
|
|
27 |
find({ |
find({ |
28 |
wanted => sub { |
wanted => sub { |
29 |
my $path = $File::Find::name; |
my $path = $File::Find::name; |
30 |
return unless -f $path; |
return unless -f $path; |
31 |
|
|
32 |
warn "# $path\n"; |
warn "+ $path\n"; |
33 |
my $ref = XMLin( $path ) || die "can't open $path: $!"; |
my $ref = XMLin( $path, |
34 |
|
KeyAttr => { |
35 |
|
'attachment' => '+name', |
36 |
|
'meta' => 'name', |
37 |
|
}, |
38 |
|
ForceArray => [ 'attachment', 'widget' ], |
39 |
|
) || die "can't open $path: $!"; |
40 |
|
|
41 |
|
warn "## $path = ",dump( $ref ) if $debug; |
42 |
|
|
43 |
my $name = $ref->{name} || die "no name in $path"; |
my $name = $ref->{name} || die "no name in $path"; |
44 |
|
|
50 |
return; |
return; |
51 |
} |
} |
52 |
|
|
53 |
my $w = $ref->{widgets}->{widget}; |
my $data; |
54 |
|
|
55 |
|
foreach my $w ( @{ $ref->{widgets}->{widget} } ) { |
56 |
|
|
57 |
|
warn "## w = ",dump( $w ) if $debug; |
58 |
|
|
59 |
|
$data .= "\n----\n" if $data; |
60 |
|
$data .= $w->{data} || die "no data?"; |
61 |
|
} |
62 |
|
|
63 |
my $data = |
my $attachments; |
|
defined( $w->{data} ) ? $w->{data} : |
|
|
defined( $w->{Body}->{date} ) ? $w->{Body}->{date} : |
|
|
die "no data in $path ",dump( $ref ); |
|
64 |
|
|
65 |
# empty data is returned like empty hash. yack. |
if ( my $a = $ref->{attachment} ) { |
66 |
$data = "\n" if ref($data) eq 'HASH' and ! keys %$data; |
foreach my $name ( keys %$a ) { |
67 |
|
my $full_path = $path; |
68 |
|
$full_path =~ s,pages/,attachments/,; |
69 |
|
$full_path .= '.' . $name; |
70 |
|
die "$full_path doesn't exist" unless -e $full_path; |
71 |
|
push @$attachments, { |
72 |
|
full_path => $full_path, |
73 |
|
name => ( $name || $a->{$name}->{desc} || 'noname' ), |
74 |
|
}; |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
$page->{ $name } = { |
$page->{ $name } = { |
79 |
content => convert_markup( $data ), |
content => convert_markup( $data ), |
80 |
date => convert_date( $date ), |
date => convert_date( $date ), |
81 |
|
attachments => $attachments, |
82 |
}; |
}; |
83 |
|
|
|
# strip path from page name |
|
84 |
$name =~ s,^.+/([^/]+)$,$1,; |
$name =~ s,^.+/([^/]+)$,$1,; |
85 |
push @page_names, $name; |
push @page_names, $name; |
86 |
|
|
|
warn "## $path = ",dump( $ref ) if $debug; |
|
87 |
}, |
}, |
88 |
|
no_chdir=>1, |
89 |
}, shift @ARGV || '.'); |
}, shift @ARGV || '.'); |
90 |
|
|
91 |
my @pages = ( keys %$page ); |
my @pages = ( keys %$page ); |
98 |
username => 'tamtam', |
username => 'tamtam', |
99 |
password => 'import', |
password => 'import', |
100 |
server => 'http://saturn.ffzg.hr/', |
server => 'http://saturn.ffzg.hr/', |
101 |
|
workspace => 'razmjenavjestina', |
102 |
); |
); |
|
$Rester->workspace('razmjenavjestina'); |
|
103 |
$Rester->put_workspacetag('TamTam'); |
$Rester->put_workspacetag('TamTam'); |
104 |
|
|
105 |
sub convert_date { |
sub convert_date { |
153 |
$body =~ s,(\S+)----,$1\n----,gs; |
$body =~ s,(\S+)----,$1\n----,gs; |
154 |
$body =~ s,----(\S+),----\n$1,gs; |
$body =~ s,----(\S+),----\n$1,gs; |
155 |
|
|
156 |
|
# attachments |
157 |
|
$body =~ s,\[attachment:([^\]]+)(gif|png|jpg|jpeg)\],{image: $1$2},gis; |
158 |
|
$body =~ s,\[attachment:([^\]]+)\],{file: $1},gs; |
159 |
|
|
160 |
return $body; |
return $body; |
161 |
} |
} |
162 |
|
|
163 |
my $count = 0; |
my $count = 0; |
164 |
|
|
165 |
|
my $m = File::MMagic::XS->new; |
166 |
|
|
167 |
foreach my $name ( keys %$page ) { |
foreach my $name ( keys %$page ) { |
168 |
last if $count++ == $max; |
last if $count++ == $max; |
169 |
|
|
191 |
$body .= qq{ |
$body .= qq{ |
192 |
---- |
---- |
193 |
|
|
194 |
Original sa http://www.razmjenavjestina.org/$full_name zadnja promjena {date: $date} |
Original http://www.razmjenavjestina.org/$full_name {date: $date} |
195 |
}; |
}; |
196 |
|
|
197 |
Encode::_utf8_off( $body ); |
Encode::_utf8_off( $body ); |
198 |
|
|
199 |
$Rester->put_page( $name, { |
$Rester->put_page( $name, { content => $body, date => $date }); |
200 |
content => $body, |
print "$name $date\n"; |
|
date => $date, |
|
|
}); |
|
|
print "+ $name $date\n"; |
|
201 |
foreach ( @tags ) { |
foreach ( @tags ) { |
202 |
$Rester->put_pagetag( $name, $_ ); |
$Rester->put_pagetag( $name, $_ ); |
203 |
print "+ $name [$_]\n"; |
print "+ tag $_\n"; |
204 |
|
} |
205 |
|
foreach my $a ( @{ $p->{attachments} } ) { |
206 |
|
my $type = $m->get_mime( $a->{full_path} ); |
207 |
|
my $content = read_file( $a->{full_path} ); |
208 |
|
print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n"; |
209 |
|
$Rester->post_attachment($name, $a->{name}, $content, $type ); |
210 |
} |
} |
211 |
|
|
212 |
} |
} |