[2] | 1 | package Bookmarks; |
---|
| 2 | |
---|
[15] | 3 | use Moose; |
---|
[71] | 4 | |
---|
[9] | 5 | use SQL::Interp qw{:all}; |
---|
[24] | 6 | use URI; |
---|
[71] | 7 | |
---|
[2] | 8 | use Bookmark; |
---|
[71] | 9 | use Bookmarks::Search; |
---|
[2] | 10 | |
---|
[15] | 11 | has dbh => ( is => 'rw' ); |
---|
[24] | 12 | has base_uri => ( is => 'ro', isa => 'URI' ); |
---|
[2] | 13 | |
---|
[25] | 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 | |
---|
[15] | 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 | |
---|
[47] | 38 | sub create_tables { |
---|
| 39 | my $self = shift; |
---|
| 40 | require File::Slurp; |
---|
[92] | 41 | require File::Basename; |
---|
| 42 | my $table_definitions = File::Slurp::read_file(File::Basename::dirname($INC{'Bookmarks.pm'}) . "/../bookmarks.sql"); |
---|
[47] | 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 | |
---|
[2] | 49 | sub get_bookmark { |
---|
| 50 | my $self = shift; |
---|
| 51 | my $params = shift; |
---|
[25] | 52 | |
---|
| 53 | # look for bookmark by id or uri |
---|
[2] | 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; |
---|
[25] | 65 | return unless $bookmark; |
---|
| 66 | |
---|
| 67 | return Bookmark->new({ |
---|
| 68 | %$bookmark, |
---|
[75] | 69 | exists => 1, |
---|
| 70 | tags => [ $self->get_tags({ uri => $bookmark->{uri} }) ], |
---|
| 71 | base_uri => $self->base_uri, |
---|
| 72 | collection => $self, |
---|
[25] | 73 | }); |
---|
[2] | 74 | } |
---|
| 75 | |
---|
[78] | 76 | sub _sql_where_clause { |
---|
[2] | 77 | my $self = shift; |
---|
[78] | 78 | my $search = shift; |
---|
[2] | 79 | |
---|
[78] | 80 | # build the where clause |
---|
[20] | 81 | my @sql; |
---|
[71] | 82 | if (@{ $search->tags }) { |
---|
[78] | 83 | push @sql, 'where resources.uri in'; |
---|
| 84 | # subquery to find tagged URIs |
---|
| 85 | push @sql, '('; |
---|
[20] | 86 | my $intersect = 0; |
---|
[71] | 87 | for my $tag (@{ $search->tags }) { |
---|
[20] | 88 | push @sql, 'intersect' if $intersect; |
---|
[78] | 89 | push @sql, 'select uri from tags where tag =', \$tag; |
---|
[20] | 90 | $intersect++; |
---|
| 91 | } |
---|
[78] | 92 | push @sql, ')'; |
---|
[20] | 93 | } |
---|
[71] | 94 | if ($search->query) { |
---|
| 95 | my $fuzzy_match = '%' . $search->query . '%'; |
---|
| 96 | push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; |
---|
[52] | 97 | } |
---|
[78] | 98 | return @sql; |
---|
| 99 | } |
---|
| 100 | |
---|
[106] | 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 | |
---|
[88] | 115 | sub search { |
---|
[78] | 116 | my $self = shift; |
---|
| 117 | my $params = shift || {}; |
---|
| 118 | my $search = Bookmarks::Search->new($params); |
---|
| 119 | |
---|
[106] | 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 | |
---|
[78] | 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); |
---|
[20] | 143 | push @sql, 'order by ctime desc'; |
---|
[106] | 144 | push @sql, ('limit', \$limit) if $limit; |
---|
[20] | 145 | # an offset is only allowed if we have a limit clause |
---|
[106] | 146 | push @sql, ('offset', \$offset) if $limit && $offset; |
---|
[20] | 147 | |
---|
| 148 | my ($sql, @bind) = sql_interp(@sql); |
---|
| 149 | |
---|
[9] | 150 | my $sth_resource = $self->dbh->prepare($sql); |
---|
| 151 | $sth_resource->execute(@bind); |
---|
| 152 | |
---|
[2] | 153 | my @resources; |
---|
| 154 | while (my $resource = $sth_resource->fetchrow_hashref) { |
---|
[25] | 155 | push @resources, Bookmark->new({ |
---|
| 156 | %$resource, |
---|
[75] | 157 | exists => 1, |
---|
| 158 | tags => [ $self->get_tags({ uri => $resource->{uri} }) ], |
---|
| 159 | base_uri => $self->base_uri, |
---|
| 160 | collection => $self, |
---|
[25] | 161 | }); |
---|
[2] | 162 | } |
---|
[88] | 163 | $search->results(\@resources); |
---|
| 164 | |
---|
| 165 | return $search; |
---|
[2] | 166 | } |
---|
| 167 | |
---|
| 168 | sub get_tags { |
---|
| 169 | my $self = shift; |
---|
| 170 | my $params = shift; |
---|
[25] | 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 | } |
---|
[2] | 183 | } |
---|
| 184 | |
---|
| 185 | sub get_cotags { |
---|
| 186 | my $self = shift; |
---|
| 187 | my $params = shift; |
---|
[71] | 188 | my $search = $params->{search}; |
---|
| 189 | |
---|
[20] | 190 | my @sql; |
---|
| 191 | |
---|
| 192 | push @sql, 'select tag, count(tag) as count from tags'; |
---|
[71] | 193 | push @sql, 'join resources on tags.uri = resources.uri' if $search->query; |
---|
[52] | 194 | |
---|
| 195 | # build the where clause |
---|
[71] | 196 | if (@{ $search->tags }) { |
---|
[52] | 197 | push @sql, 'where tags.uri in ('; |
---|
[20] | 198 | my $intersect = 0; |
---|
[71] | 199 | for my $tag (@{ $search->tags }) { |
---|
[20] | 200 | push @sql, 'intersect' if $intersect; |
---|
| 201 | push @sql, 'select uri from tags where tag = ', \$tag; |
---|
| 202 | $intersect++; |
---|
| 203 | } |
---|
[71] | 204 | push @sql, ') and tag not in ', $search->tags, ''; |
---|
[20] | 205 | } |
---|
[71] | 206 | if ($search->query) { |
---|
| 207 | my $fuzzy_match = '%' . $search->query . '%'; |
---|
| 208 | push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; |
---|
[52] | 209 | } |
---|
| 210 | |
---|
[20] | 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); |
---|
[2] | 216 | return @{ $sth->fetchall_arrayref({}) }; |
---|
| 217 | } |
---|
| 218 | |
---|
[57] | 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 | |
---|
[2] | 227 | sub add { |
---|
| 228 | my $self = shift; |
---|
| 229 | my $bookmark = shift; |
---|
| 230 | |
---|
[75] | 231 | #TODO: accept a pre-made Bookmark object in addition to a hash |
---|
[2] | 232 | my $uri = $bookmark->{uri}; |
---|
| 233 | my $title = $bookmark->{title}; |
---|
[15] | 234 | my $ctime = $bookmark->{ctime} || time; |
---|
| 235 | my $mtime = $bookmark->{mtime} || $ctime; |
---|
| 236 | my $id = $bookmark->{id}; |
---|
[2] | 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 |
---|
[28] | 255 | my $bookmark_exists = 0; |
---|
[15] | 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); |
---|
[2] | 260 | eval { |
---|
[15] | 261 | $sth_bookmark->execute(@bind_bookmark); |
---|
[2] | 262 | }; |
---|
| 263 | if ($@) { |
---|
| 264 | if ($@ =~ /column uri is not unique/) { |
---|
| 265 | # this is not truly an error condition; the bookmark was already there |
---|
[28] | 266 | # set this flag so that later we can update the mtime if tags change |
---|
| 267 | $bookmark_exists = 1; |
---|
[2] | 268 | } else { |
---|
| 269 | die $@; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
[46] | 273 | my $changed_tags = $self->_update_tags($uri, $bookmark->{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 | |
---|
[76] | 291 | # update the URI, if it has changed |
---|
[46] | 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 | |
---|
[76] | 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; |
---|
[46] | 309 | |
---|
[76] | 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 |
---|
[46] | 317 | my $changed_tags = $self->_update_tags($bookmark->uri, $bookmark->tags); |
---|
| 318 | |
---|
[76] | 319 | # update the mtime, if the bookmark has actually been changed |
---|
| 320 | if ($changed_uri or $changed_title or $changed_tags) { |
---|
[46] | 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 | |
---|
[28] | 334 | my $changed_tags = 0; |
---|
[46] | 335 | my %new_tags = map { $_ => 1 } @{ $tags }; |
---|
[2] | 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); |
---|
[28] | 344 | $changed_tags++; |
---|
[2] | 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); |
---|
[28] | 352 | $changed_tags++; |
---|
[2] | 353 | } |
---|
| 354 | |
---|
[46] | 355 | # how many tags have changed? |
---|
| 356 | return $changed_tags; |
---|
[2] | 357 | } |
---|
| 358 | |
---|
[46] | 359 | |
---|
[2] | 360 | # module returns true |
---|
| 361 | 1; |
---|