1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use DBI; |
5 |
use CouchDB::Client; |
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
$|++; |
9 |
|
10 |
my $database = 'reblog'; |
11 |
|
12 |
my $connect = "DBI:mysql:database=$database"; |
13 |
#$connect = "DBI:mysql:database=$database;host=localhost;port=13306"; |
14 |
|
15 |
my $dbh = DBI->connect($connect,"","") || die $DBI::errstr; |
16 |
|
17 |
# select all posts which have been read or unread |
18 |
my $sql = qq{ |
19 |
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 |
27 |
join items_userdata on items.id = item_id |
28 |
-- where label = 'read' and value_numeric = ? |
29 |
join feeds on items.feed_id = feeds.id |
30 |
order by items.id asc |
31 |
-- limit 42 |
32 |
}; |
33 |
|
34 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
35 |
$sth->execute( 1 ) || die $sth->errstr(); |
36 |
|
37 |
warn dump( $sth->{NAME} ); |
38 |
|
39 |
print "found ",$sth->rows," items to process..."; |
40 |
|
41 |
my $c = CouchDB::Client->new(uri => 'http://localhost:5984/'); |
42 |
|
43 |
$c->testConnection or die "The server cannot be reached"; |
44 |
print "Running version " . $c->serverInfo->{version} . "\n"; |
45 |
my $db = $c->newDB( $database ); |
46 |
$db->create unless $c->dbExists( $database ); |
47 |
|
48 |
my @docs = $db->listDocs; |
49 |
my $row_id = shift @docs || 0; |
50 |
|
51 |
my $pk = 'id'; |
52 |
|
53 |
while (my $row = $sth->fetchrow_hashref() ) { |
54 |
my $_id = $row->{_id} || die "row needs _id"; |
55 |
my $doc = $db->newDoc( $_id ); |
56 |
|
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 }; |
70 |
if ( $@ ) { |
71 |
$doc->retrieve; |
72 |
row2doc( $row, $doc )->update; |
73 |
# eval { $doc->update }; |
74 |
warn ( $@ ? "ERROR $_id $@" : "updated $_id" ), $/; |
75 |
} else { |
76 |
warn "created ",dump( $row ),$/; |
77 |
} |
78 |
} |
79 |
|
80 |
__END__ |
81 |
|
82 |
$sql = qq{ |
83 |
update items_userdata |
84 |
set value_numeric = 1 |
85 |
where label = 'read' and item_id in ($ids) |
86 |
}; |
87 |
|
88 |
$dbh->do( $sql ); |
89 |
|