cleanup json generation and dump xml from it
[NoSQL-toys.git] / reblog / reblog-published.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use DBI;
5 use Data::Dump qw/dump/;
6 use JSON::XS;
7
8 $|++;
9
10 my $debug = $ENV{DEBUG} || 0;
11
12 my $database = 'reblog';
13
14 my ( $offset, $limit ) = @ARGV;
15 $offset ||= 0;
16 $limit  ||= $ENV{LIMIT} || 1000;
17
18 my $dbi = "DBI:mysql:database=$database";
19 $dbi .= ";host=127.0.0.1;port=13306";   # XXX over ssh
20
21 my $dbh = DBI->connect($dbi,"","",{ RaiseError => 1 });
22
23 warn "# published_items";
24
25 $dbh->do(qq{
26         create temporary table published_items as
27         select
28                 item_id
29         from
30                 items_userdata
31         where
32                 label = 'published' and
33                 value_numeric = 1
34 });
35
36 my $sql = qq{
37         select
38                 i.id as item_id,
39                 md5(i.guid) as _id,
40
41                 i.guid,
42                 i.link,
43                 i.title,
44                 i.content,
45                 i.author,
46                 i.category,
47                 i.modified,
48                 i.timestamp,
49                 i.insert_timestamp,
50
51                 f.url as feed_url,
52                 f.title as feed_title,
53                 f.link as feed_link,
54                 f.description as feed_description
55         from items i
56         join published_items p on i.id = p.item_id
57         join feeds f on i.feed_id = f.id
58         where i.id > ?
59         order by i.id asc
60         limit $limit
61         offset $offset
62 };
63
64 my $sql_tags = qq{
65 select
66         items_userdata.item_id,
67         value_long as tags,
68         timestamp
69 from items_userdata
70 join published_items p
71         on items_userdata.item_id = p.item_id and label='tags'
72 where
73         items_userdata.item_id > ?
74 order by items_userdata.item_id asc
75 };
76
77 my $last_row = 0; # FIXME
78 $last_row = 0 if $debug;
79
80 warn "Fetching items from $dbi id > $last_row [$offset $limit]\n";
81
82 my $sth = $dbh->prepare($sql);
83 $sth->execute( $last_row );
84
85 my @columns = @{ $sth->{NAME} };
86 warn "# columns ",dump( @columns );
87
88 my @feed = grep { s/^feed_// } @columns;
89
90 warn "found ",$sth->rows," items to process...\n";
91
92 my $sth_tags = $dbh->prepare($sql_tags);
93 $sth_tags->execute( $last_row );
94 warn "found ",$sth_tags->rows, " tags found...\n";
95
96 my $count = 0;
97
98 my $row_tags = $sth_tags->fetchrow_hashref();
99
100 while (my $row = $sth->fetchrow_hashref() ) {
101         my $_id = $row->{_id} || "c$count";
102         $_id =~ s{\W+}{_}g;
103         $_id =~ s{_+$}{};
104
105         my $feed;
106         $feed->{$_} = delete $row->{ "feed_$_" } foreach @feed;
107         $row->{feed} = $feed;
108
109         while ( $row_tags && $row_tags->{item_id} < $row->{item_id} ) {
110                 if ( $row_tags = $sth_tags->fetchrow_hashref() ) {
111                         warn "## got tags: ",dump( $row_tags ) if $debug;
112                         $row->{tags} = [ split(/\s+/, $row_tags->{tags} ) ];
113                 } else {
114                         warn "end of tags";
115                         $row_tags = undef;
116                 }
117         }
118
119 #       warn dump($row);
120         print encode_json($row),$/;
121
122 }
123
124