/[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 15 by dpavlin, Wed Dec 12 18:14:19 2007 UTC revision 22 by dpavlin, Thu Dec 13 11:42:24 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    
21    GetOptions(
22            'debug+' => \$debug,
23            'max=i' => \$max,
24    );
25    
26  my $page;  my $page;
27  my $page_date;  my $page_date;
28    
29  my @page_names;  my @page_names;
30    
31    print "Collecting pages...\n";
32    
33  find({  find({
34          wanted => sub {          wanted => sub {
35                  my $path = $File::Find::name;                  my $path = $File::Find::name;
36                  return unless -f $path;                  return unless -f $path;
37    
38                  warn "# $path\n";                  warn "+ $path\n";
39                  my $ref = XMLin( $path ) || die "can't open $path: $!";                  my $ref = XMLin( $path,
40                            KeyAttr => {
41                                    'attachment' => '+name',
42                                    'meta' => 'name',
43                            },
44                            ForceArray => [ 'attachment', 'meta', 'widget' ],
45                    ) || die "can't open $path: $!";
46    
47                    warn "## $path = ",dump( $ref ) if $debug;
48    
49                  my $name = $ref->{name} || die "no name in $path";                  my $name = $ref->{name} || die "no name in $path";
50    
# Line 36  find({ Line 55  find({
55                          warn "SKIP: no LastModified in $path $name";                          warn "SKIP: no LastModified in $path $name";
56                          return;                          return;
57                  }                  }
58                  my $data =  
59                          $ref->{widgets}->{widget}->{data} ||                  my $data;
60                          $ref->{widgets}->{widget}->{Body}->{data} ||  
61                          die "no data in $path ",dump( $ref );                  foreach my $w ( @{ $ref->{widgets}->{widget} } ) {
62    
63                            warn "## w = ",dump( $w ) if $debug;
64    
65                            $data .= "\n----\n" if $data;
66                            $data .= $w->{data} || die "no data?";
67                    }
68    
69                    my $attachments;
70    
71                    if ( my $a = $ref->{attachment} ) {
72                            foreach my $name ( keys %$a ) {
73                                    my $full_path = $path;
74                                    $full_path =~ s,pages/,attachments/,;
75                                    $full_path .= '.' . $name;
76                                    die "$full_path doesn't exist" unless -e $full_path;
77                                    push @$attachments, {
78                                            full_path => $full_path,
79                                            name => ( $name || $a->{$name}->{desc} || 'noname' ),
80                                    };
81                            }
82                    }
83    
84                  $page->{ $name } = {                  $page->{ $name } = {
85                          content => convert_markup( $data ),                          content => convert_markup( $data ),
86                          date => convert_date( $date ),                          date => convert_date( $date ),
87                            attachments => $attachments,
88                  };                  };
89    
                 # strip path from page name  
90                  $name =~ s,^.+/([^/]+)$,$1,;                  $name =~ s,^.+/([^/]+)$,$1,;
91                  push @page_names, $name;                  push @page_names, $name;
92    
 #               warn dump( $ref );  
93          },          },
94            no_chdir=>1,
95  }, shift @ARGV || '.');  }, shift @ARGV || '.');
96    
97  my @pages = ( keys %$page );  my @pages = ( keys %$page );
# Line 64  my $Rester = Socialtext::Resting->new( Line 104  my $Rester = Socialtext::Resting->new(
104          username => 'tamtam',          username => 'tamtam',
105          password => 'import',          password => 'import',
106          server   => 'http://saturn.ffzg.hr/',          server   => 'http://saturn.ffzg.hr/',
107            workspace => 'razmjenavjestina',
108  );  );
 $Rester->workspace('razmjenavjestina');  
109  $Rester->put_workspacetag('TamTam');  $Rester->put_workspacetag('TamTam');
110    
111  sub convert_date {  sub convert_date {
# Line 119  sub convert_markup { Line 159  sub convert_markup {
159          $body =~ s,(\S+)----,$1\n----,gs;          $body =~ s,(\S+)----,$1\n----,gs;
160          $body =~ s,----(\S+),----\n$1,gs;          $body =~ s,----(\S+),----\n$1,gs;
161    
162            # attachments
163            $body =~ s,\[attachment:([^\]]+)(gif|png|jpg|jpeg)\],{image: $1$2},gis;
164            $body =~ s,\[attachment:([^\]]+)\],{file: $1},gs;
165    
166          return $body;          return $body;
167  }  }
168    
169  my $count = 0;  my $count = 0;
170    
171    my $m = File::MMagic::XS->new;
172    
173  foreach my $name ( keys %$page ) {  foreach my $name ( keys %$page ) {
174          last if $count++ == $max;          last if $count++ == $max;
175    
176          my $p = $page->{$name};          my $p = $page->{$name};
177    
178            warn "## $name = ",dump( $p ) if $debug;
179    
180          my $body = $p->{content} || die "no content?";          my $body = $p->{content} || die "no content?";
181          my $date = $p->{date} || die "no date?";          my $date = $p->{date} || die "no date?";
182    
# Line 142  foreach my $name ( keys %$page ) { Line 191  foreach my $name ( keys %$page ) {
191          }          }
192    
193          # link named pages          # link named pages
194          $body =~ s,$page_link_re,[$1],gs;          $body =~ s,\b$page_link_re\b,[$1],gs;
195            $body =~ s,``,,gs;
196    
197          $body .= qq{          $body .= qq{
198    
199  ----  ----
200    
201  Original sa http://www.razmjenavjestina.org/$full_name zadnja promjena {date: $date}  "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
202  };  };
203    
204          Encode::_utf8_off( $body );          Encode::_utf8_off( $body );
205    
206          $Rester->put_page( $name, {          $Rester->put_page( $name, { content => $body, date => $date });
207                  content => $body,          print "$name $date\n";
                 date => $date,  
         });  
         print "+ $name $date\n";  
208          foreach ( @tags ) {          foreach ( @tags ) {
209                  $Rester->put_pagetag( $name, $_ );                  $Rester->put_pagetag( $name, $_ );
210                  print "+ $name [$_]\n";                  print "+ tag $_\n";
211            }
212            foreach my $a ( @{ $p->{attachments} } ) {
213                    my $type = $m->get_mime( $a->{full_path} );
214                    my $content = read_file( $a->{full_path} );
215                    print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
216                    $Rester->post_attachment($name, $a->{name}, $content, $type );
217          }          }
218    
219  }  }

Legend:
Removed from v.15  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26