/[couchdb]/scripts/reblog2couchdb.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 /scripts/reblog2couchdb.pl

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

revision 14 by dpavlin, Fri Aug 8 19:19:12 2008 UTC revision 15 by dpavlin, Thu Aug 28 21:53:49 2008 UTC
# Line 7  use Data::Dump qw/dump/; Line 7  use Data::Dump qw/dump/;
7    
8  $|++;  $|++;
9    
10    my $debug = @ARGV ? 1 : 0;
11    
12  my $database = 'reblog';  my $database = 'reblog';
13    
14  my $dbi = "DBI:mysql:database=$database";  my $dbi = "DBI:mysql:database=$database";
# Line 20  my $sql = qq{ Line 22  my $sql = qq{
22                  feeds.title as feed_title,                  feeds.title as feed_title,
23                  feeds.link as feed_link,                  feeds.link as feed_link,
24                  feeds.description as feed_description                  feeds.description as feed_description
 --              t.value_long as tags  
25          from items          from items
26          join items_userdata on items.id = item_id          join items_userdata on items.id = item_id
27          join feeds on items.feed_id = feeds.id          join feeds on items.feed_id = feeds.id
 --      left outer join items_userdata as t on items.id = t.item_id and t.label='tags'  
28          where items.id > ? and items_userdata.label = 'published' and items_userdata.value_numeric = 1          where items.id > ? and items_userdata.label = 'published' and items_userdata.value_numeric = 1
29          order by items.id asc          order by items.id asc
30          limit 1000          limit 1000
31  };  };
32    
33    my $sql_tags = qq{
34    select
35            items_userdata.item_id,
36            t.value_long as tags,
37            items_userdata.timestamp
38    from items_userdata
39    join items_userdata as t
40            on items_userdata.item_id = t.item_id and t.label='tags'
41    where
42            items_userdata.item_id > ? and
43            items_userdata.label = 'published' and
44            items_userdata.value_numeric = 1
45    order by items_userdata.item_id asc
46    };
47    
48  my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');  my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');
49    
50  $c->testConnection or die "The server cannot be reached";  $c->testConnection or die "The server cannot be reached";
# Line 44  $status->create if $@; Line 59  $status->create if $@;
59  print "status ",dump( $status->{data} ), "\n";  print "status ",dump( $status->{data} ), "\n";
60    
61  my $last_row = $status->{data}->{last_row_id} || 0;  my $last_row = $status->{data}->{last_row_id} || 0;
62    $last_row = 0 if $debug;
63    
64  sub commit_last_row {  sub commit_last_row {
65          warn "commit_last_row $last_row\n";          warn "commit_last_row $last_row\n";
# Line 51  sub commit_last_row { Line 67  sub commit_last_row {
67          $status->update;          $status->update;
68  }  }
69    
70  my $dbh = DBI->connect($dbi,"","") || die $DBI::errstr;  my $dbh = DBI->connect($dbi,"","",{ RaiseError => 1 });
71    
72  print "Fetching items from $dbi id > $last_row\n";  print "Fetching items from $dbi id > $last_row\n";
73    
74  my $sth = $dbh->prepare($sql) || die $dbh->errstr();  my $sth = $dbh->prepare($sql);
75  $sth->execute( $last_row ) || die $sth->errstr();  $sth->execute( $last_row );
76    
77  warn dump( $sth->{NAME} );  warn dump( $sth->{NAME} );
78    
79  print "found ",$sth->rows," items to process...\n";  print "found ",$sth->rows," items to process...\n";
80    
81  my $pk = 'id';  my $sth_tags = $dbh->prepare($sql_tags);
82    $sth_tags->execute( $last_row );
83    print "found ",$sth_tags->rows, " tags found...\n";
84    
85  my $count = 0;  my $count = 0;
86    
87    my $row_tags = $sth_tags->fetchrow_hashref();
88    
89  while (my $row = $sth->fetchrow_hashref() ) {  while (my $row = $sth->fetchrow_hashref() ) {
90          my $_id = $row->{_id} || die "row needs _id";          my $_id = $row->{_id} || die "row needs _id";
91          my $doc = $db->newDoc( $_id );          my $doc = $db->newDoc( $_id );
92    
93            while ( $row_tags && $row_tags->{item_id} < $row->{_id} ) {
94                    $row_tags = $sth_tags->fetchrow_hashref();
95                    warn "## got tags: ",dump( $row_tags ) if $debug;
96            }
97    
98          sub row2doc {          sub row2doc {
99                  my ( $row, $doc ) = @_;                  my ( $row, $doc ) = @_;
100                  my $a = delete( $row->{xml} );                  my $a = delete( $row->{xml} );
101                  $doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a;                  $doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a;
102                  my $a = delete( $row->{content} );                  $a = delete( $row->{content} );
103                  $doc->addAttachment( 'content.html', 'text/html', $a ) if $a;                  $doc->addAttachment( 'content.html', 'text/html', $a ) if $a;
104                    if ( $row_tags && $row_tags->{item_id} == $row->{_id} ) {
105                            $row->{tags} = [ split(/\s+/, $row_tags->{tags} ) ];
106                            warn "++ ",$row->{item_id}, dump( $row->{tags} );
107                    }
108                  $doc->{data} = $row;                  $doc->{data} = $row;
109                    warn "## ",dump( $row ) if $debug;
110                  return $doc;                  return $doc;
111          }          }
112    

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

  ViewVC Help
Powered by ViewVC 1.1.26