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