/[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 2 by dpavlin, Tue Aug 5 13:28:54 2008 UTC revision 4 by dpavlin, Tue Aug 5 14:48:57 2008 UTC
# Line 17  my $dbh = DBI->connect($connect,"","") | Line 17  my $dbh = DBI->connect($connect,"","") |
17  # select all posts which have been read or unread  # select all posts which have been read or unread
18  my $sql = qq{  my $sql = qq{
19          select          select
20                  *                  items.id as _id,
21                    items.*,
22                    feeds.url as feed_url,
23                    feeds.title as feed_title,
24                    feeds.link as feed_link,
25                    feeds.description as feed_description
26          from items          from items
27  --      join items_userdata on id=item_id          join items_userdata on items.id = item_id
28  --      where label = 'read' and value_numeric = ?  --      where label = 'read' and value_numeric = ?
29  --      limit 3          join feeds on items.feed_id = feeds.id
30          order by id asc          order by items.id asc
31    --      limit 42
32  };  };
33    
34  my $sth = $dbh->prepare($sql) || die $dbh->errstr();  my $sth = $dbh->prepare($sql) || die $dbh->errstr();
35  $sth->execute( 1 ) || die $sth->errstr();  $sth->execute( 1 ) || die $sth->errstr();
36    
37    warn dump( $sth->{NAME} );
38    
39  print "found ",$sth->rows," items to process...";  print "found ",$sth->rows," items to process...";
40    
41  my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');  my $c = CouchDB::Client->new(uri => 'http://localhost:5984/');
# Line 43  my $row_id = shift @docs || 0; Line 51  my $row_id = shift @docs || 0;
51  my $pk = 'id';  my $pk = 'id';
52    
53  while (my $row = $sth->fetchrow_hashref() ) {  while (my $row = $sth->fetchrow_hashref() ) {
54          my $_id = $row->{$pk};          my $_id = $row->{_id} || die "row needs _id";
55          my $doc = $db->newDoc( $_id );          my $doc = $db->newDoc( $_id );
56          $doc->{data} = $row;  
57            sub row2doc {
58                    my ( $row, $doc ) = @_;
59                    my $a = delete( $row->{xml} );
60                    $doc->addAttachment( 'item.xml', 'application/xhtml+xml', $a ) if $a;
61                    my $a = delete( $row->{content} );
62                    $doc->addAttachment( 'content.html', 'text/html', $a ) if $a;
63                    $doc->{data} = $row;
64                    return $doc;
65            }
66    
67            row2doc( $row, $doc );
68    
69          eval { $doc->create };          eval { $doc->create };
70          if ( $@ ) {          if ( $@ ) {
71                  warn "can't create $_id $@";                  $doc->retrieve;
72                  eval { $doc->update };                  row2doc( $row, $doc )->update;
73                  warn $@ ? "can't update $_id $@" : "updated $_id";  #               eval { $doc->update };
74                    warn ( $@ ? "ERROR $_id $@" : "updated $_id" ), $/;
75          } else {          } else {
76                  warn "created ",dump( $row );                  warn "created ",dump( $row ),$/;
77          }          }
78  }  }
79    

Legend:
Removed from v.2  
changed lines
  Added in v.4

  ViewVC Help
Powered by ViewVC 1.1.26