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