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