source: bookmarks/trunk/Bookmarks.pm @ 12

Last change on this file since 12 was 9, checked in by peter, 12 years ago
  • use SQL::Interp to generate the SQL for Bookmarks::get_resources()
  • Bookmarks::get_resources() accepts a limit parameter to add a limit to the SQL query
  • added a feed run mode that returns an Atom feed of the most recent 12 bookmarks; can have an optional ?tag parameter that only looks for bookmarks with that tag
  • added an explicit "list" dispatch target that calls the `list run mode
File size: 5.9 KB
Line 
1package Bookmarks;
2
3use Class::Accessor 'antlers';
4use SQL::Interp qw{:all};
5use Bookmark;
6
7has dbh      => ( is => 'ro' );
8has base_uri => ( is => 'ro' );
9
10sub get_bookmark {
11    my $self = shift;
12    my $params = shift;
13    my $sth;
14    if ($params->{id}) {
15        $sth = $self->dbh->prepare('select id,resources.uri,title,ctime,mtime from bookmarks join resources on bookmarks.uri=resources.uri where id=?');
16        $sth->execute($params->{id});
17    } elsif ($params->{uri}) {
18        $sth = $self->dbh->prepare('select id,resources.uri,title,ctime,mtime from bookmarks join resources on bookmarks.uri=resources.uri where resources.uri=?');
19        $sth->execute($params->{uri});
20    } else {
21        die "Must specify either id or uri";
22    }
23    my $bookmark = $sth->fetchrow_hashref;
24    if ($bookmark) {
25        my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag');
26        $sth_tag->execute($bookmark->{uri});
27        $bookmark->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ];
28        if ($self->base_uri) {
29            $bookmark->{bookmark_uri} = $self->base_uri . $bookmark->{id};
30        }
31    }
32    return $bookmark;
33}
34
35sub get_resources {
36    my $self = shift;
37    my $params = shift;
38    my $tag = $params->{tag};
39    my $limit = $params->{limit};
40
41    my ($sql, @bind) = sql_interp(
42        'select * from resources join bookmarks on resources.uri = bookmarks.uri',
43        ($tag   ? ('join tags on resources.uri = tags.uri where tags.tag =', \$tag) : ''),
44        'order by ctime desc',
45        ($limit ? ('limit', \$limit) : ''),
46    );
47
48    my $sth_resource = $self->dbh->prepare($sql);
49    $sth_resource->execute(@bind);
50
51    my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag');
52    my @resources;
53    while (my $resource = $sth_resource->fetchrow_hashref) {
54        $sth_tag->execute($resource->{uri});
55        $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ];
56        if ($self->base_uri) {
57            $resource->{bookmark_uri} = $self->base_uri . $resource->{id};
58        }
59        push @resources, $resource;
60    }
61    return @resources;
62}
63
64sub get_tags {
65    my $self = shift;
66    my $params = shift;
67    my $tag = $params->{selected};
68    my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag');
69    $sth_all_tags->execute($tag);
70    my $all_tags = $sth_all_tags->fetchall_arrayref({});
71    return @{ $all_tags };
72}
73
74sub get_cotags {
75    my $self = shift;
76    my $params = shift;
77    my $tag = $params->{tag};
78    my $sth = $self->dbh->prepare('select tag, count(tag) as count from tags where tag != ? and uri in (select uri from tags where tag = ?) group by tag order by tag');
79    $sth->execute($tag, $tag);
80    return @{ $sth->fetchall_arrayref({}) };
81}
82
83sub add {
84    my $self = shift;
85    my $bookmark = shift;
86
87    my $uri = $bookmark->{uri};
88    my $title = $bookmark->{title};
89    #TODO: accept a ctime or mtime
90    my $mtime = my $ctime = $bookmark->{ctime} || time;
91
92    # create an entry for the resource
93    my $sth_resource = $self->dbh->prepare('insert into resources (uri, title) values (?, ?)');
94    eval {
95        $sth_resource->execute($uri, $title);
96    };
97    if ($@) {
98        if ($@ =~ /column uri is not unique/) {
99            # this is not truly an error condition; the resource is already listed
100            # update the title instead
101            my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?');
102            $sth_update->execute($title, $uri);
103        } else {
104            die $@;
105        }
106    }
107
108    # create the bookmark
109    my $sth_bookmark = $self->dbh->prepare('insert into bookmarks (uri, ctime, mtime) values (?, ?, ?)');
110    eval {
111        $sth_bookmark->execute($uri, $ctime, $mtime);
112    };
113    if ($@) {
114        if ($@ =~ /column uri is not unique/) {
115            # this is not truly an error condition; the bookmark was already there
116            # update the mtime instead
117            # TODO: only update mtime if the tag list is changed?
118            my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
119            $sth_update->execute($mtime, $uri);
120        } else {
121            die $@;
122        }
123    }
124
125    my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} };
126    my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?');
127    my $sth_insert_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)');
128    my $sth_current_tags = $self->dbh->prepare('select tag from tags where uri = ?');
129    $sth_current_tags->execute($uri);
130    while (my ($tag) = $sth_current_tags->fetchrow_array) {
131        if (!$new_tags{$tag}) {
132            # if a current tag is not in the new tags, remove it from the database
133            $sth_delete_tag->execute($uri, $tag);
134        } else {
135            # if a new tag is already in the database, remove it from the list of tags to add
136            delete $new_tags{$tag};
137        }
138    }
139    for my $tag (keys %new_tags) {
140        $sth_insert_tag->execute($uri, $tag);
141    }
142
143=begin
144
145    # clear all tags
146    my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ?');
147    $sth_delete_tag->execute($uri);
148    my $sth_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)');
149    for my $tag (@{ $bookmark->{tags} }) {
150        #print $tag, "\n";
151        # prevent duplicate (uri,tag) pairs in the database
152        # TODO: should POST with a set of tags ever remove tags?
153        eval {
154            $sth_tag->execute($uri, $tag);
155        };
156        if ($@) {
157            if ($@ =~ /columns uri, tag are not unique/) {
158                # this is not truly an error condition; the tag was already there
159            } else {
160                die $@;
161            }
162        }
163    }
164
165=cut
166
167    # return the newly created or updated bookmark
168    return $self->get_bookmark({ uri => $uri });
169}
170
171# module returns true
1721;
Note: See TracBrowser for help on using the repository browser.