1 | package Bookmarks; |
---|
2 | |
---|
3 | use Moose; |
---|
4 | use SQL::Interp qw{:all}; |
---|
5 | use URI; |
---|
6 | use Bookmark; |
---|
7 | use Bookmarks::List; |
---|
8 | |
---|
9 | has dbh => ( is => 'rw' ); |
---|
10 | has base_uri => ( is => 'ro', isa => 'URI' ); |
---|
11 | |
---|
12 | has _sth_tags_from_uri => ( |
---|
13 | is => 'ro', |
---|
14 | init_arg => undef, |
---|
15 | lazy => 1, |
---|
16 | default => sub { $_[0]->dbh->prepare('select tag from tags where uri = ? order by tag'); }, |
---|
17 | ); |
---|
18 | |
---|
19 | sub BUILD { |
---|
20 | my $self = shift; |
---|
21 | my $args = shift; |
---|
22 | |
---|
23 | if (!$self->dbh) { |
---|
24 | if ($args->{dbname}) { |
---|
25 | require DBI; |
---|
26 | $self->dbh(DBI->connect("dbi:SQLite:dbname=$$args{dbname}", "", "", { RaiseError => 1, PrintError => 0 })); |
---|
27 | # enable foreign key support (requires DBD::SQLite 1.26_05 or above (sqlite 3.6.19 or above)) |
---|
28 | $self->dbh->do('pragma foreign_keys = on;'); |
---|
29 | } else { |
---|
30 | #TODO: figure out how to make croak play nice with Moose to get the user-visible caller line |
---|
31 | die "No dbh or dbname specified in the constructor"; |
---|
32 | } |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | sub create_tables { |
---|
37 | my $self = shift; |
---|
38 | require File::Slurp; |
---|
39 | my $table_definitions = File::Slurp::read_file('bookmarks.sql'); |
---|
40 | $self->dbh->{sqlite_allow_multiple_statements} = 1; |
---|
41 | $self->dbh->do($table_definitions); |
---|
42 | $self->dbh->{sqlite_allow_multiple_statements} = 0; |
---|
43 | return $self; |
---|
44 | } |
---|
45 | |
---|
46 | sub get_bookmark { |
---|
47 | my $self = shift; |
---|
48 | my $params = shift; |
---|
49 | |
---|
50 | # look for bookmark by id or uri |
---|
51 | my $sth; |
---|
52 | if ($params->{id}) { |
---|
53 | $sth = $self->dbh->prepare('select id,resources.uri,title,ctime,mtime from bookmarks join resources on bookmarks.uri=resources.uri where id=?'); |
---|
54 | $sth->execute($params->{id}); |
---|
55 | } elsif ($params->{uri}) { |
---|
56 | $sth = $self->dbh->prepare('select id,resources.uri,title,ctime,mtime from bookmarks join resources on bookmarks.uri=resources.uri where resources.uri=?'); |
---|
57 | $sth->execute($params->{uri}); |
---|
58 | } else { |
---|
59 | die "Must specify either id or uri"; |
---|
60 | } |
---|
61 | my $bookmark = $sth->fetchrow_hashref; |
---|
62 | return unless $bookmark; |
---|
63 | |
---|
64 | return Bookmark->new({ |
---|
65 | %$bookmark, |
---|
66 | exists => 1, |
---|
67 | tags => [ $self->get_tags({ uri => $bookmark->{uri} }) ], |
---|
68 | base_uri => $self->base_uri, |
---|
69 | }); |
---|
70 | } |
---|
71 | |
---|
72 | sub get_bookmarks { |
---|
73 | my $self = shift; |
---|
74 | my $params = shift; |
---|
75 | my $query = $params->{query}; |
---|
76 | my $tags = $params->{tag} || []; |
---|
77 | my $limit = $params->{limit}; |
---|
78 | my $offset = $params->{offset}; |
---|
79 | |
---|
80 | # build the query |
---|
81 | my @sql; |
---|
82 | |
---|
83 | if (!ref $tags) { |
---|
84 | $tags = [ $tags ]; |
---|
85 | } |
---|
86 | if (@$tags) { |
---|
87 | my $intersect = 0; |
---|
88 | for my $tag (@{ $tags }) { |
---|
89 | push @sql, 'intersect' if $intersect; |
---|
90 | push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; |
---|
91 | push @sql, 'join tags on resources.uri = tags.uri where tags.tag =', \$tag; |
---|
92 | $intersect++; |
---|
93 | } |
---|
94 | } else { |
---|
95 | push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; |
---|
96 | } |
---|
97 | if ($query) { |
---|
98 | push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; |
---|
99 | } |
---|
100 | push @sql, 'order by ctime desc'; |
---|
101 | push @sql, ('limit', \$limit) if $limit; |
---|
102 | # an offset is only allowed if we have a limit clause |
---|
103 | push @sql, ('offset', \$offset) if $limit && $offset; |
---|
104 | |
---|
105 | my ($sql, @bind) = sql_interp(@sql); |
---|
106 | #die $sql; |
---|
107 | |
---|
108 | my $sth_resource = $self->dbh->prepare($sql); |
---|
109 | $sth_resource->execute(@bind); |
---|
110 | |
---|
111 | my @resources; |
---|
112 | while (my $resource = $sth_resource->fetchrow_hashref) { |
---|
113 | push @resources, Bookmark->new({ |
---|
114 | %$resource, |
---|
115 | tags => [ $self->get_tags({ uri => $resource->{uri} }) ], |
---|
116 | base_uri => $self->base_uri, |
---|
117 | }); |
---|
118 | } |
---|
119 | return Bookmarks::List->new({ |
---|
120 | bookmarks => $self, |
---|
121 | tags => $tags, |
---|
122 | query => $query, |
---|
123 | limit => $limit, |
---|
124 | offset => $offset, |
---|
125 | results => \@resources, |
---|
126 | }); |
---|
127 | } |
---|
128 | |
---|
129 | sub get_tags { |
---|
130 | my $self = shift; |
---|
131 | my $params = shift; |
---|
132 | if (my $uri = $params->{uri}) { |
---|
133 | # get the tags for a particular URI |
---|
134 | $self->_sth_tags_from_uri->execute($uri); |
---|
135 | return map { $$_[0] } @{ $self->_sth_tags_from_uri->fetchall_arrayref }; |
---|
136 | } else { |
---|
137 | # return all tags |
---|
138 | my $tag = $params->{selected}; |
---|
139 | my $sth_all_tags = $self->dbh->prepare('select tag, count(tag) as count, tag = ? as selected from tags group by tag order by tag'); |
---|
140 | $sth_all_tags->execute($tag); |
---|
141 | my $all_tags = $sth_all_tags->fetchall_arrayref({}); |
---|
142 | return @{ $all_tags }; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | sub get_cotags { |
---|
147 | my $self = shift; |
---|
148 | my $params = shift; |
---|
149 | my $query = $params->{query}; |
---|
150 | my $tags = $params->{tag} || []; |
---|
151 | if (!ref $tags) { |
---|
152 | $tags = [ $tags ]; |
---|
153 | } |
---|
154 | my @sql; |
---|
155 | |
---|
156 | push @sql, 'select tag, count(tag) as count from tags'; |
---|
157 | push @sql, 'join resources on tags.uri = resources.uri' if $query; |
---|
158 | |
---|
159 | # build the where clause |
---|
160 | if (@$tags) { |
---|
161 | push @sql, 'where tags.uri in ('; |
---|
162 | my $intersect = 0; |
---|
163 | for my $tag (@{ $tags }) { |
---|
164 | push @sql, 'intersect' if $intersect; |
---|
165 | push @sql, 'select uri from tags where tag = ', \$tag; |
---|
166 | $intersect++; |
---|
167 | } |
---|
168 | push @sql, ') and tag not in ', $tags, ''; |
---|
169 | } |
---|
170 | if ($query) { |
---|
171 | push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; |
---|
172 | } |
---|
173 | |
---|
174 | push @sql, 'group by tag order by tag'; |
---|
175 | |
---|
176 | my ($sql, @bind) = sql_interp(@sql); |
---|
177 | my $sth = $self->dbh->prepare($sql); |
---|
178 | $sth->execute(@bind); |
---|
179 | return @{ $sth->fetchall_arrayref({}) }; |
---|
180 | } |
---|
181 | |
---|
182 | sub get_last_modified_time { |
---|
183 | my $self = shift; |
---|
184 | my $sth = $self->dbh->prepare('select mtime from bookmarks order by mtime desc limit 1'); |
---|
185 | $sth->execute; |
---|
186 | my ($mtime) = $sth->fetchrow_array; |
---|
187 | return $mtime; |
---|
188 | } |
---|
189 | |
---|
190 | sub add { |
---|
191 | my $self = shift; |
---|
192 | my $bookmark = shift; |
---|
193 | |
---|
194 | my $uri = $bookmark->{uri}; |
---|
195 | my $title = $bookmark->{title}; |
---|
196 | my $ctime = $bookmark->{ctime} || time; |
---|
197 | my $mtime = $bookmark->{mtime} || $ctime; |
---|
198 | my $id = $bookmark->{id}; |
---|
199 | |
---|
200 | # create an entry for the resource |
---|
201 | my $sth_resource = $self->dbh->prepare('insert into resources (uri, title) values (?, ?)'); |
---|
202 | eval { |
---|
203 | $sth_resource->execute($uri, $title); |
---|
204 | }; |
---|
205 | if ($@) { |
---|
206 | if ($@ =~ /column uri is not unique/) { |
---|
207 | # this is not truly an error condition; the resource is already listed |
---|
208 | # update the title instead |
---|
209 | my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?'); |
---|
210 | $sth_update->execute($title, $uri); |
---|
211 | } else { |
---|
212 | die $@; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | # create the bookmark |
---|
217 | my $bookmark_exists = 0; |
---|
218 | my ($sql_bookmark, @bind_bookmark) = sql_interp( |
---|
219 | 'insert into bookmarks', { ($id ? (id => $id) : ()), uri => $uri, ctime => $ctime, mtime => $mtime } |
---|
220 | ); |
---|
221 | my $sth_bookmark = $self->dbh->prepare($sql_bookmark); |
---|
222 | eval { |
---|
223 | $sth_bookmark->execute(@bind_bookmark); |
---|
224 | }; |
---|
225 | if ($@) { |
---|
226 | if ($@ =~ /column uri is not unique/) { |
---|
227 | # this is not truly an error condition; the bookmark was already there |
---|
228 | # set this flag so that later we can update the mtime if tags change |
---|
229 | $bookmark_exists = 1; |
---|
230 | } else { |
---|
231 | die $@; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | my $changed_tags = $self->_update_tags($uri, $bookmark->{tags}); |
---|
236 | |
---|
237 | if ($bookmark_exists && $changed_tags) { |
---|
238 | # update the mtime if the bookmark already existed but the tags were changed |
---|
239 | my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); |
---|
240 | $sth_update->execute($mtime, $uri); |
---|
241 | } |
---|
242 | |
---|
243 | # return the newly created or updated bookmark |
---|
244 | return $self->get_bookmark({ uri => $uri }); |
---|
245 | } |
---|
246 | |
---|
247 | sub update { |
---|
248 | my $self = shift; |
---|
249 | my $bookmark = shift; |
---|
250 | |
---|
251 | my $mtime = time; |
---|
252 | |
---|
253 | my $changed_uri = 0; |
---|
254 | my $sth_current = $self->dbh->prepare('select uri from bookmarks where id = ?'); |
---|
255 | $sth_current->execute($bookmark->id); |
---|
256 | my ($stored_uri) = $sth_current->fetchrow_array; |
---|
257 | |
---|
258 | if ($stored_uri ne $bookmark->uri) { |
---|
259 | # the URI has changed |
---|
260 | my $sth_update_uri = $self->dbh->prepare('update resources set uri = ? where uri = ?'); |
---|
261 | $sth_update_uri->execute($bookmark->uri, $stored_uri); |
---|
262 | $changed_uri++; |
---|
263 | } |
---|
264 | |
---|
265 | # update the title |
---|
266 | # TODO: only do this if the title has changed |
---|
267 | # TODO: should we update mtime if the title changes? |
---|
268 | my $sth_update = $self->dbh->prepare('update resources set title = ? where uri = ?'); |
---|
269 | $sth_update->execute($bookmark->title, $bookmark->uri); |
---|
270 | |
---|
271 | my $changed_tags = $self->_update_tags($bookmark->uri, $bookmark->tags); |
---|
272 | |
---|
273 | if ($changed_uri or $changed_tags) { |
---|
274 | # update the mtime if the bookmark already existed but the tags were changed |
---|
275 | my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?'); |
---|
276 | $sth_update->execute($mtime, $bookmark->uri); |
---|
277 | } |
---|
278 | |
---|
279 | # return the bookmark |
---|
280 | return $bookmark; |
---|
281 | } |
---|
282 | |
---|
283 | sub _update_tags { |
---|
284 | my $self = shift; |
---|
285 | my ($uri, $tags) = @_; |
---|
286 | |
---|
287 | my $changed_tags = 0; |
---|
288 | my %new_tags = map { $_ => 1 } @{ $tags }; |
---|
289 | my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?'); |
---|
290 | my $sth_insert_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)'); |
---|
291 | my $sth_current_tags = $self->dbh->prepare('select tag from tags where uri = ?'); |
---|
292 | $sth_current_tags->execute($uri); |
---|
293 | while (my ($tag) = $sth_current_tags->fetchrow_array) { |
---|
294 | if (!$new_tags{$tag}) { |
---|
295 | # if a current tag is not in the new tags, remove it from the database |
---|
296 | $sth_delete_tag->execute($uri, $tag); |
---|
297 | $changed_tags++; |
---|
298 | } else { |
---|
299 | # if a new tag is already in the database, remove it from the list of tags to add |
---|
300 | delete $new_tags{$tag}; |
---|
301 | } |
---|
302 | } |
---|
303 | for my $tag (keys %new_tags) { |
---|
304 | $sth_insert_tag->execute($uri, $tag); |
---|
305 | $changed_tags++; |
---|
306 | } |
---|
307 | |
---|
308 | # how many tags have changed? |
---|
309 | return $changed_tags; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | # module returns true |
---|
314 | 1; |
---|