[5] | 1 | package BookmarkApp; |
---|
| 2 | use strict; |
---|
| 3 | use warnings; |
---|
| 4 | use base qw{CGI::Application}; |
---|
| 5 | |
---|
[6] | 6 | use CGI::Application::Plugin::TT; |
---|
| 7 | |
---|
[5] | 8 | use Encode; |
---|
[19] | 9 | use HTTP::Date qw{time2isoz}; |
---|
[5] | 10 | use JSON; |
---|
| 11 | use Bookmarks; |
---|
| 12 | |
---|
[7] | 13 | use URI; |
---|
| 14 | my $base_uri = URI->new; |
---|
| 15 | $base_uri->scheme('http'); |
---|
[17] | 16 | $base_uri->host($ENV{HTTP_X_FORWARDED_HOST} || $ENV{SERVER_NAME}); |
---|
[7] | 17 | $base_uri->port($ENV{SERVER_PORT}); |
---|
| 18 | $base_uri->path($ENV{SCRIPT_NAME} . '/'); |
---|
| 19 | |
---|
[15] | 20 | my $dbname = 'fk.db'; |
---|
[5] | 21 | my $bookmarks = Bookmarks->new({ |
---|
[15] | 22 | dbname => $dbname, |
---|
[24] | 23 | base_uri => $base_uri, |
---|
[5] | 24 | }); |
---|
| 25 | |
---|
| 26 | sub setup { |
---|
| 27 | my $self = shift; |
---|
| 28 | $self->mode_param(path_info => 1); |
---|
| 29 | $self->run_modes([qw{ |
---|
| 30 | list |
---|
[9] | 31 | feed |
---|
[5] | 32 | view |
---|
[30] | 33 | view_field |
---|
[5] | 34 | edit |
---|
| 35 | }]); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | sub list { |
---|
| 39 | my $self = shift; |
---|
| 40 | my $q = $self->query; |
---|
| 41 | |
---|
| 42 | # check for a uri param, and if there is one present, |
---|
| 43 | # see if a bookmark for that URI already exists |
---|
| 44 | if (defined(my $uri = $q->param('uri'))) { |
---|
| 45 | my $bookmark = $bookmarks->get_bookmark({ uri => $uri }); |
---|
| 46 | if ($bookmark) { |
---|
| 47 | # redirect to the view of the existing bookmark |
---|
| 48 | $self->header_type('redirect'); |
---|
| 49 | $self->header_props( |
---|
| 50 | -uri => $q->url . '/' . $bookmark->{id}, |
---|
| 51 | ); |
---|
| 52 | return; |
---|
| 53 | } else { |
---|
| 54 | # bookmark was not found; show the form to create a new bookmark |
---|
| 55 | $bookmark->{uri} = $uri; |
---|
| 56 | $bookmark->{title} = $q->param('title'); |
---|
| 57 | $self->header_props( |
---|
| 58 | -type => 'text/html', |
---|
| 59 | -charset => 'UTF-8', |
---|
| 60 | -status => 404, |
---|
| 61 | ); |
---|
[6] | 62 | return $self->tt_process( |
---|
[5] | 63 | 'bookmark.tt', |
---|
| 64 | $bookmark, |
---|
| 65 | ); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | # list all the bookmarks |
---|
| 70 | my $format = $q->param('format') || 'html'; |
---|
[35] | 71 | |
---|
| 72 | my @tags = grep { $_ ne '' } $q->param('tag'); |
---|
[13] | 73 | my $limit = $q->param('limit'); |
---|
| 74 | my $offset = $q->param('offset'); |
---|
[26] | 75 | my @resources = $bookmarks->get_bookmarks({ |
---|
[20] | 76 | tag => \@tags, |
---|
[13] | 77 | limit => $limit, |
---|
| 78 | offset => $offset, |
---|
| 79 | }); |
---|
[35] | 80 | my @all_tags = $bookmarks->get_tags({ selected => $tags[0] }); |
---|
[20] | 81 | my @cotags = $bookmarks->get_cotags({ tag => \@tags }); |
---|
[31] | 82 | |
---|
| 83 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
---|
[5] | 84 | |
---|
| 85 | if ($format eq 'json') { |
---|
| 86 | $self->header_props( |
---|
| 87 | -type => 'application/json', |
---|
| 88 | -charset => 'UTF-8', |
---|
| 89 | ); |
---|
| 90 | return decode_utf8( |
---|
[25] | 91 | JSON->new->utf8->convert_blessed->encode({ |
---|
[7] | 92 | bookmarks => \@resources, |
---|
[5] | 93 | }) |
---|
| 94 | ); |
---|
[19] | 95 | } elsif ($format eq 'xbel') { |
---|
| 96 | require XML::XBEL; |
---|
| 97 | #TODO: conditional support; if XML::XBEL is not present, return a 5xx response |
---|
| 98 | |
---|
| 99 | my $xbel = XML::XBEL->new; |
---|
| 100 | |
---|
| 101 | $xbel->new_document({ |
---|
[31] | 102 | title => $title, |
---|
[19] | 103 | }); |
---|
| 104 | |
---|
| 105 | for my $bookmark (@resources) { |
---|
[36] | 106 | my $cdatetime = time2isoz $bookmark->ctime; |
---|
| 107 | my $mdatetime = time2isoz $bookmark->mtime; |
---|
[19] | 108 | # make the timestamps W3C-correct |
---|
| 109 | s/ /T/ foreach ($cdatetime, $mdatetime); |
---|
| 110 | |
---|
| 111 | $xbel->add_bookmark({ |
---|
[36] | 112 | href => $bookmark->uri, |
---|
| 113 | title => $bookmark->title, |
---|
| 114 | desc => 'Tags: ' . join(', ', @{ $bookmark->tags }), |
---|
[19] | 115 | added => $cdatetime, |
---|
| 116 | #XXX: are we sure that modified is the mtime of the bookmark or the resource? |
---|
| 117 | modified => $mdatetime, |
---|
| 118 | }); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | $self->header_props( |
---|
| 122 | -type => 'application/xml', |
---|
| 123 | -charset => 'UTF-8', |
---|
| 124 | ); |
---|
| 125 | |
---|
| 126 | return $xbel->toString; |
---|
[36] | 127 | } elsif ($format eq 'text') { |
---|
| 128 | $self->header_props( |
---|
| 129 | -type => 'text/uri-list', |
---|
| 130 | -charset => 'UTF-8', |
---|
| 131 | ); |
---|
| 132 | return join '', |
---|
| 133 | map { |
---|
| 134 | sprintf "# %s\n# Tags: %s\n%s\n", |
---|
| 135 | $_->title, |
---|
| 136 | join(', ', @{ $_->tags }), |
---|
| 137 | $_->uri |
---|
| 138 | } @resources; |
---|
[5] | 139 | } else { |
---|
| 140 | $self->header_props( |
---|
| 141 | -type => 'text/html', |
---|
| 142 | -charset => 'UTF-8', |
---|
| 143 | ); |
---|
| 144 | |
---|
| 145 | # set the base URL, adding a trailing slash if needed |
---|
| 146 | my $base_url = $q->url; |
---|
| 147 | $base_url .= '/' if $base_url =~ m{/bookmarks$}; |
---|
| 148 | |
---|
[22] | 149 | my @links = ( |
---|
| 150 | { |
---|
| 151 | text => 'Link', |
---|
| 152 | type => 'text/html', |
---|
| 153 | rel => 'self', |
---|
| 154 | query => { |
---|
| 155 | tag => \@tags, |
---|
| 156 | }, |
---|
| 157 | }, |
---|
| 158 | { |
---|
| 159 | text => 'JSON', |
---|
| 160 | type => 'application/json', |
---|
| 161 | rel => 'alternate', |
---|
| 162 | query => { |
---|
| 163 | tag => \@tags, |
---|
| 164 | format => 'json', |
---|
| 165 | }, |
---|
| 166 | }, |
---|
| 167 | { |
---|
| 168 | text => 'XBEL', |
---|
| 169 | type => 'application/xml', |
---|
| 170 | rel => 'alternate', |
---|
| 171 | query => { |
---|
| 172 | tag => \@tags, |
---|
| 173 | format => 'xbel', |
---|
| 174 | }, |
---|
| 175 | }, |
---|
| 176 | { |
---|
| 177 | text => 'Atom', |
---|
| 178 | type => 'application/atom+xml', |
---|
| 179 | rel => 'alternate', |
---|
| 180 | path => 'feed', |
---|
| 181 | query => { |
---|
| 182 | tag => \@tags, |
---|
| 183 | }, |
---|
| 184 | }, |
---|
| 185 | ); |
---|
| 186 | for my $link (@links) { |
---|
| 187 | $link->{href} = URI->new_abs($link->{path} || '', $base_uri); |
---|
| 188 | $link->{href}->query_form($link->{query}); |
---|
| 189 | } |
---|
[31] | 190 | |
---|
[6] | 191 | return $self->tt_process( |
---|
[5] | 192 | 'list.tt', |
---|
| 193 | { |
---|
| 194 | base_url => $base_url, |
---|
[31] | 195 | title => $title, |
---|
[35] | 196 | selected_tag => $tags[0], |
---|
[20] | 197 | search_tags => \@tags, |
---|
[22] | 198 | links => \@links, |
---|
[20] | 199 | all_tags => \@all_tags, |
---|
[5] | 200 | cotags => \@cotags, |
---|
| 201 | resources => \@resources, |
---|
| 202 | }, |
---|
| 203 | ); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[9] | 207 | sub feed { |
---|
| 208 | my $self = shift; |
---|
| 209 | my $q = $self->query; |
---|
| 210 | |
---|
[35] | 211 | my @tags = grep { $_ ne '' } $q->param('tag'); |
---|
[31] | 212 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
---|
| 213 | |
---|
[33] | 214 | require XML::Atom; |
---|
| 215 | $XML::Atom::DefaultVersion = "1.0"; |
---|
| 216 | |
---|
[9] | 217 | require XML::Atom::Feed; |
---|
| 218 | require XML::Atom::Entry; |
---|
| 219 | require XML::Atom::Link; |
---|
| 220 | |
---|
| 221 | my $feed = XML::Atom::Feed->new; |
---|
[31] | 222 | $feed->title($title); |
---|
[32] | 223 | |
---|
| 224 | my $feed_uri = URI->new_abs('feed', $base_uri); |
---|
| 225 | $feed_uri->query_form(tag => \@tags); |
---|
| 226 | $feed->id($feed_uri->canonical); |
---|
| 227 | |
---|
| 228 | my $self_link = XML::Atom::Link->new; |
---|
| 229 | $self_link->type('application/atom+xml'); |
---|
| 230 | $self_link->rel('self'); |
---|
| 231 | $self_link->href($feed_uri->canonical); |
---|
| 232 | $feed->add_link($self_link); |
---|
| 233 | |
---|
[31] | 234 | my $html_link = XML::Atom::Link->new; |
---|
| 235 | $html_link->type('text/html'); |
---|
| 236 | $html_link->rel('alternate'); |
---|
[32] | 237 | my $html_uri = $base_uri->clone; |
---|
| 238 | $html_uri->query_form(tag => \@tags); |
---|
| 239 | $html_link->href($html_uri->canonical); |
---|
[31] | 240 | $feed->add_link($html_link); |
---|
[9] | 241 | |
---|
| 242 | # construct a feed from the most recent 12 bookmarks |
---|
[31] | 243 | for my $bookmark ($bookmarks->get_bookmarks({ tag => \@tags, limit => 12 })) { |
---|
[9] | 244 | my $entry = XML::Atom::Entry->new; |
---|
[31] | 245 | $entry->id($bookmark->bookmark_uri->canonical); |
---|
| 246 | $entry->title($bookmark->title); |
---|
[9] | 247 | my $link = XML::Atom::Link->new; |
---|
[31] | 248 | $link->href($bookmark->uri); |
---|
[9] | 249 | $entry->add_link($link); |
---|
[31] | 250 | $entry->summary('Tags: ' . join(', ', @{ $bookmark->tags })); |
---|
[33] | 251 | my $mdatetime = time2isoz $bookmark->mtime; |
---|
| 252 | # make the timestamp W3C-correct |
---|
| 253 | $mdatetime =~ s/ /T/; |
---|
| 254 | $entry->updated($mdatetime); |
---|
[9] | 255 | $feed->add_entry($entry); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | $self->header_props( |
---|
| 259 | -type => 'application/atom+xml', |
---|
| 260 | -charset => 'UTF-8', |
---|
| 261 | ); |
---|
| 262 | return $feed->as_xml; |
---|
| 263 | } |
---|
| 264 | |
---|
[5] | 265 | sub view { |
---|
| 266 | my $self = shift; |
---|
| 267 | my $id = $self->param('id'); |
---|
[7] | 268 | my $format = $self->query->param('format') || 'html'; |
---|
[5] | 269 | |
---|
| 270 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
| 271 | if ($bookmark) { |
---|
[30] | 272 | if ($format eq 'json') { |
---|
[5] | 273 | $self->header_props( |
---|
[30] | 274 | -type => 'application/json', |
---|
[5] | 275 | -charset => 'UTF-8', |
---|
| 276 | ); |
---|
[30] | 277 | return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); |
---|
[5] | 278 | } else { |
---|
[30] | 279 | # display the bookmark form for this bookmark |
---|
| 280 | $bookmark->{exists} = 1; |
---|
| 281 | $bookmark->{created} = "Created " . localtime($bookmark->ctime); |
---|
| 282 | $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; |
---|
| 283 | $self->header_props( |
---|
| 284 | -type => 'text/html', |
---|
| 285 | -charset => 'UTF-8', |
---|
| 286 | ); |
---|
| 287 | return $self->tt_process( |
---|
| 288 | 'bookmark.tt', |
---|
| 289 | $bookmark, |
---|
| 290 | ); |
---|
| 291 | } |
---|
| 292 | } else { |
---|
| 293 | $self->header_props( |
---|
| 294 | -type => 'text/html', |
---|
| 295 | -charset => 'UTF-8', |
---|
| 296 | -status => 404, |
---|
| 297 | ); |
---|
| 298 | return "Bookmark $id Not Found"; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | sub view_field { |
---|
| 303 | my $self = shift; |
---|
| 304 | my $id = $self->param('id'); |
---|
| 305 | my $field = $self->param('field'); |
---|
| 306 | |
---|
| 307 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
| 308 | if ($bookmark) { |
---|
| 309 | # respond with just the requested field as plain text |
---|
| 310 | my $value = eval { $bookmark->$field }; |
---|
| 311 | if ($@) { |
---|
| 312 | if ($@ =~ /Can't locate object method/) { |
---|
[7] | 313 | $self->header_props( |
---|
[30] | 314 | -type => 'text/plain', |
---|
[7] | 315 | -charset => 'UTF-8', |
---|
[30] | 316 | -status => 404, |
---|
[7] | 317 | ); |
---|
[30] | 318 | return qq{"$field" is not a valid bookmark data field}; |
---|
[7] | 319 | } else { |
---|
[30] | 320 | die $@; |
---|
[7] | 321 | } |
---|
[5] | 322 | } |
---|
[30] | 323 | $self->header_props( |
---|
| 324 | -type => 'text/plain', |
---|
| 325 | -charset => 'UTF-8', |
---|
| 326 | ); |
---|
| 327 | return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; |
---|
[5] | 328 | } else { |
---|
| 329 | $self->header_props( |
---|
| 330 | -type => 'text/html', |
---|
| 331 | -charset => 'UTF-8', |
---|
| 332 | -status => 404, |
---|
| 333 | ); |
---|
| 334 | return "Bookmark $id Not Found"; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | #TODO: split this into edit and create methods |
---|
| 339 | sub edit { |
---|
| 340 | my $self = shift; |
---|
| 341 | my $q = $self->query; |
---|
| 342 | #TODO: get the bookmark based on the id and edit it directly? |
---|
| 343 | #TODO: deal with changing URIs |
---|
| 344 | my $uri = $q->param('uri'); |
---|
| 345 | my $title = $q->param('title'); |
---|
| 346 | my @tags = split ' ', $q->param('tags'); |
---|
| 347 | $bookmarks->add({ |
---|
| 348 | uri => $uri, |
---|
| 349 | title => $title, |
---|
| 350 | tags => \@tags, |
---|
| 351 | }); |
---|
| 352 | |
---|
| 353 | =begin |
---|
| 354 | |
---|
| 355 | my $location = URI->new($q->url); |
---|
| 356 | $location->query_form(uri => $uri) if defined $q->url_param('uri'); |
---|
| 357 | $location->fragment('updated'); |
---|
| 358 | |
---|
| 359 | =cut |
---|
| 360 | |
---|
| 361 | # return to the form |
---|
| 362 | $self->header_type('redirect'); |
---|
| 363 | $self->header_props( |
---|
| 364 | -uri => $ENV{REQUEST_URI}, |
---|
| 365 | -status => 303, |
---|
| 366 | ); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | 1; |
---|