| 1 | package Bookmarks; |
|---|
| 2 | |
|---|
| 3 | use Class::Accessor 'antlers'; |
|---|
| 4 | use SQL::Interp qw{:all}; |
|---|
| 5 | use Bookmark; |
|---|
| 6 | |
|---|
| 7 | has dbh => ( is => 'ro' ); |
|---|
| 8 | has base_uri => ( is => 'ro' ); |
|---|
| 9 | |
|---|
| 10 | sub 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 | |
|---|
| 35 | sub get_resources { |
|---|
| 36 | my $self = shift; |
|---|
| 37 | my $params = shift; |
|---|
| 38 | my $tag = $params->{tag}; |
|---|
| 39 | my $limit = $params->{limit}; |
|---|
| 40 | my $offset = $params->{offset}; |
|---|
| 41 | |
|---|
| 42 | my ($sql, @bind) = sql_interp( |
|---|
| 43 | 'select * from resources join bookmarks on resources.uri = bookmarks.uri', |
|---|
| 44 | ($tag ? ('join tags on resources.uri = tags.uri where tags.tag =', \$tag) : ''), |
|---|
| 45 | 'order by ctime desc', |
|---|
| 46 | ($limit ? ('limit', \$limit) : ()), |
|---|
| 47 | # an offset is only allowed if we have a limit clause |
|---|
| 48 | ($limit && $offset ? ('offset', \$offset) : ()), |
|---|
| 49 | ); |
|---|
| 50 | |
|---|
| 51 | my $sth_resource = $self->dbh->prepare($sql); |
|---|
| 52 | $sth_resource->execute(@bind); |
|---|
| 53 | |
|---|
| 54 | my $sth_tag = $self->dbh->prepare('select tag from tags where uri = ? order by tag'); |
|---|
| 55 | my @resources; |
|---|
| 56 | while (my $resource = $sth_resource->fetchrow_hashref) { |
|---|
| 57 | $sth_tag->execute($resource->{uri}); |
|---|
| 58 | $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; |
|---|
| 59 | if ($self->base_uri) { |
|---|
| 60 | $resource->{bookmark_uri} = $self->base_uri . $resource->{id}; |
|---|
| 61 | } |
|---|
| 62 | push @resources, $resource; |
|---|
| 63 | } |
|---|
| 64 | return @resources; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | sub get_tags { |
|---|
| 68 | my $self = shift; |
|---|
| 69 | my $params = shift; |
|---|
| 70 | my $tag = $params->{selected}; |
|---|
| 71 | my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); |
|---|
| 72 | $sth_all_tags->execute($tag); |
|---|
| 73 | my $all_tags = $sth_all_tags->fetchall_arrayref({}); |
|---|
| 74 | return @{ $all_tags }; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub get_cotags { |
|---|
| 78 | my $self = shift; |
|---|
| 79 | my $params = shift; |
|---|
| 80 | my $tag = $params->{tag}; |
|---|
| 81 | 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'); |
|---|
| 82 | $sth->execute($tag, $tag); |
|---|
| 83 | return @{ $sth->fetchall_arrayref({}) }; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub add { |
|---|
| 87 | my $self = shift; |
|---|
| 88 | my $bookmark = shift; |
|---|
| 89 | |
|---|
| 90 | my $uri = $bookmark->{uri}; |
|---|
| 91 | my $title = $bookmark->{title}; |
|---|
| 92 | #TODO: accept a ctime or mtime |
|---|
| 93 | my $mtime = my $ctime = $bookmark->{ctime} || time; |
|---|
| 94 | |
|---|
| 95 | # create an entry for the resource |
|---|
| 96 | my $sth_resource = $self->dbh->prepare('insert into resources (uri, title) values (?, ?)'); |
|---|
| 97 | eval { |
|---|
| 98 | $sth_resource->execute($uri, $title); |
|---|
| 99 | }; |
|---|
| 100 | if ($@) { |
|---|
| 101 | if ($@ =~ /column uri is not unique/) { |
|---|
| 102 | # this is not truly an error condition; the resource is already listed |
|---|
| 103 | # update the title instead |
|---|
| 104 | my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?'); |
|---|
| 105 | $sth_update->execute($title, $uri); |
|---|
| 106 | } else { |
|---|
| 107 | die $@; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | # create the bookmark |
|---|
| 112 | my $sth_bookmark = $self->dbh->prepare('insert into bookmarks (uri, ctime, mtime) values (?, ?, ?)'); |
|---|
| 113 | eval { |
|---|
| 114 | $sth_bookmark->execute($uri, $ctime, $mtime); |
|---|
| 115 | }; |
|---|
| 116 | if ($@) { |
|---|
| 117 | if ($@ =~ /column uri is not unique/) { |
|---|
| 118 | # this is not truly an error condition; the bookmark was already there |
|---|
| 119 | # update the mtime instead |
|---|
| 120 | # TODO: only update mtime if the tag list is changed? |
|---|
| 121 | my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); |
|---|
| 122 | $sth_update->execute($mtime, $uri); |
|---|
| 123 | } else { |
|---|
| 124 | die $@; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} }; |
|---|
| 129 | my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?'); |
|---|
| 130 | my $sth_insert_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)'); |
|---|
| 131 | my $sth_current_tags = $self->dbh->prepare('select tag from tags where uri = ?'); |
|---|
| 132 | $sth_current_tags->execute($uri); |
|---|
| 133 | while (my ($tag) = $sth_current_tags->fetchrow_array) { |
|---|
| 134 | if (!$new_tags{$tag}) { |
|---|
| 135 | # if a current tag is not in the new tags, remove it from the database |
|---|
| 136 | $sth_delete_tag->execute($uri, $tag); |
|---|
| 137 | } else { |
|---|
| 138 | # if a new tag is already in the database, remove it from the list of tags to add |
|---|
| 139 | delete $new_tags{$tag}; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | for my $tag (keys %new_tags) { |
|---|
| 143 | $sth_insert_tag->execute($uri, $tag); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | =begin |
|---|
| 147 | |
|---|
| 148 | # clear all tags |
|---|
| 149 | my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ?'); |
|---|
| 150 | $sth_delete_tag->execute($uri); |
|---|
| 151 | my $sth_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)'); |
|---|
| 152 | for my $tag (@{ $bookmark->{tags} }) { |
|---|
| 153 | #print $tag, "\n"; |
|---|
| 154 | # prevent duplicate (uri,tag) pairs in the database |
|---|
| 155 | # TODO: should POST with a set of tags ever remove tags? |
|---|
| 156 | eval { |
|---|
| 157 | $sth_tag->execute($uri, $tag); |
|---|
| 158 | }; |
|---|
| 159 | if ($@) { |
|---|
| 160 | if ($@ =~ /columns uri, tag are not unique/) { |
|---|
| 161 | # this is not truly an error condition; the tag was already there |
|---|
| 162 | } else { |
|---|
| 163 | die $@; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | =cut |
|---|
| 169 | |
|---|
| 170 | # return the newly created or updated bookmark |
|---|
| 171 | return $self->get_bookmark({ uri => $uri }); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | # module returns true |
|---|
| 175 | 1; |
|---|