/[socialtext-import]/tamtam/tamtam2socialtext.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /tamtam/tamtam2socialtext.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 16 by dpavlin, Wed Dec 12 18:35:40 2007 UTC revision 27 by dpavlin, Sat Dec 15 13:52:40 2007 UTC
# Line 10  use Socialtext::Resting; Line 10  use Socialtext::Resting;
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 Getopt::Long;
16  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
17    
18    my $debug = 0;
19  my $max = 999;  my $max = 999;
20    my $attachments = 0;
21    
22    my @create_tags = (qw/
23    Trazi
24    Nudi
25    SvakodnevneDovitljivosti
26    G33koSkop
27    /);
28    
29    GetOptions(
30            'debug+' => \$debug,
31            'max=i' => \$max,
32            'attachments' => \$attachments,
33    );
34    
35  my $page;  my $page;
36  my $page_date;  my $page_date;
37    
38  my @page_names;  my @page_names;
39    
40    print "Importing $max pages", $attachments ? " with attachments" : "", "...\n";
41    
42  find({  find({
43          wanted => sub {          wanted => sub {
44                  my $path = $File::Find::name;                  my $path = $File::Find::name;
45                  return unless -f $path;                  return unless -f $path;
46    
47                  warn "# $path\n";                  warn "+ $path\n";
48                  my $ref = XMLin( $path ) || die "can't open $path: $!";                  my $ref = XMLin( $path,
49                            KeyAttr => {
50                                    'attachment' => '+name',
51                                    'meta' => 'name',
52                            },
53                            ForceArray => [ 'attachment', 'meta', 'widget' ],
54                    ) || die "can't open $path: $!";
55    
56                    warn "## $path = ",dump( $ref ) if $debug;
57    
58                  my $name = $ref->{name} || die "no name in $path";                  my $name = $ref->{name} || die "no name in $path";
59    
# Line 36  find({ Line 64  find({
64                          warn "SKIP: no LastModified in $path $name";                          warn "SKIP: no LastModified in $path $name";
65                          return;                          return;
66                  }                  }
67                  my $data =  
68                          $ref->{widgets}->{widget}->{data} ||                  my $data;
69                          $ref->{widgets}->{widget}->{Body}->{data} ||  
70                          die "no data in $path ",dump( $ref );                  foreach my $w ( @{ $ref->{widgets}->{widget} } ) {
71    
72                            warn "## w = ",dump( $w ) if $debug;
73    
74                            $data .= "\n----\n" if $data;
75                            $data .= $w->{data} || die "no data?";
76                    }
77    
78                    my $attachments;
79    
80                    if ( my $a = $ref->{attachment} ) {
81                            foreach my $name ( keys %$a ) {
82                                    my $full_path = $path;
83                                    $full_path =~ s,pages/,attachments/,;
84                                    $full_path .= '.' . $name;
85                                    die "$full_path doesn't exist" unless -e $full_path;
86                                    push @$attachments, {
87                                            full_path => $full_path,
88                                            name => ( $name || $a->{$name}->{desc} || 'noname' ),
89                                    };
90                            }
91                    }
92    
93                  $page->{ $name } = {                  $page->{ $name } = {
94                          content => convert_markup( $data ),                          content => convert_markup( $data ),
95                            original => $data,
96                          date => convert_date( $date ),                          date => convert_date( $date ),
97                            attachments => $attachments,
98                  };                  };
99    
                 # strip path from page name  
100                  $name =~ s,^.+/([^/]+)$,$1,;                  $name =~ s,^.+/([^/]+)$,$1,;
101                  push @page_names, $name;                  push @page_names, $name;
102    
 #               warn dump( $ref );  
103          },          },
104            no_chdir=>1,
105  }, shift @ARGV || '.');  }, shift @ARGV || '.');
106    
107  my @pages = ( keys %$page );  my @pages = ( keys %$page );
# Line 64  my $Rester = Socialtext::Resting->new( Line 114  my $Rester = Socialtext::Resting->new(
114          username => 'tamtam',          username => 'tamtam',
115          password => 'import',          password => 'import',
116          server   => 'http://saturn.ffzg.hr/',          server   => 'http://saturn.ffzg.hr/',
117            workspace => 'razmjenavjestina',
118  );  );
 $Rester->workspace('razmjenavjestina');  
119  $Rester->put_workspacetag('TamTam');  $Rester->put_workspacetag('TamTam');
120    
121  sub convert_date {  sub convert_date {
# Line 91  sub surround { Line 141  sub surround {
141    
142  sub pre {  sub pre {
143          my $text = shift;          my $text = shift;
144          $text =~ s/^{{{//;          $text =~ s/^{{{\s*//s;
145          $text =~ s/}}}$//;          $text =~ s/\s*}}}$//s;
146          return '.pre' . $text . '.pre';          return "\n.pre\n" . $text . "\n.pre\n";
147  }  }
148    
149  sub convert_markup {  sub convert_markup {
# Line 119  sub convert_markup { Line 169  sub convert_markup {
169          $body =~ s,(\S+)----,$1\n----,gs;          $body =~ s,(\S+)----,$1\n----,gs;
170          $body =~ s,----(\S+),----\n$1,gs;          $body =~ s,----(\S+),----\n$1,gs;
171    
172            # attachments
173            $body =~ s,\[attachment:([^\]]+)(gif|png|jpg|jpeg)\],{image: $1$2},gis;
174            $body =~ s,\[attachment:([^\]]+)\],{file: $1},gs;
175    
176          return $body;          return $body;
177  }  }
178    
179  my $count = 0;  my $count = 0;
180    
181    my $m = File::MMagic::XS->new;
182    
183  foreach my $name ( keys %$page ) {  foreach my $name ( keys %$page ) {
184          last if $count++ == $max;          last if $count++ == $max;
185    
186          my $p = $page->{$name};          my $p = $page->{$name};
187    
188            warn "## $name = ",dump( $p ) if $debug;
189    
190          my $body = $p->{content} || die "no content?";          my $body = $p->{content} || die "no content?";
191          my $date = $p->{date} || die "no date?";          my $date = $p->{date} || die "no date?";
192    
# Line 146  foreach my $name ( keys %$page ) { Line 205  foreach my $name ( keys %$page ) {
205          $body =~ s,``,,gs;          $body =~ s,``,,gs;
206    
207          $body .= qq{          $body .= qq{
208    
209  ----  ----
210    
211  Original sa http://www.razmjenavjestina.org/$full_name zadnja promjena {date: $date}  "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
212  };  };
213    
214          Encode::_utf8_off( $body );          Encode::_utf8_off( $body );
215    
216          $Rester->put_page( $name, {          print "$name $date\n";
217                  content => $body,  
218                  date => $date,          # original markup
219          });          $Rester->put_page( $name, { content => $p->{original}, date => $date });
220          print "+ $name $date\n";  
221            foreach my $t ( @create_tags ) {
222                    push @tags, $t if $full_name =~ m/$t/i;
223            }
224    
225          foreach ( @tags ) {          foreach ( @tags ) {
226                  $Rester->put_pagetag( $name, $_ );                  $Rester->put_pagetag( $name, $_, { date => $date } );
227                  print "+ $name [$_]\n";                  print "+ tag $_\n";
228            }
229    
230            if ( $attachments ) {
231                    foreach my $a ( @{ $p->{attachments} } ) {
232                            my $type = $m->get_mime( $a->{full_path} );
233                            my $content = read_file( $a->{full_path} );
234                            print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
235                            $Rester->post_attachment($name, $a->{name}, $content, $type );
236                    }
237          }          }
238    
239            # converted page
240            $Rester->put_page( $name, { content => $body, date => $date });
241    
242  }  }
243    

Legend:
Removed from v.16  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26