| [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'; |
|---|
| 71 | my $tag = $q->param('tag'); |
|---|
| [20] | 72 | my @tags = $q->param('tag'); |
|---|
| 73 | # special case: handle the empty tag |
|---|
| 74 | if (@tags == 1 && $tags[0] eq '') { |
|---|
| 75 | @tags = (); |
|---|
| 76 | } |
|---|
| [13] | 77 | my $limit = $q->param('limit'); |
|---|
| 78 | my $offset = $q->param('offset'); |
|---|
| [26] | 79 | my @resources = $bookmarks->get_bookmarks({ |
|---|
| [20] | 80 | tag => \@tags, |
|---|
| [13] | 81 | limit => $limit, |
|---|
| 82 | offset => $offset, |
|---|
| 83 | }); |
|---|
| [5] | 84 | my @all_tags = $bookmarks->get_tags({ selected => $tag }); |
|---|
| [20] | 85 | my @cotags = $bookmarks->get_cotags({ tag => \@tags }); |
|---|
| [31] | 86 | |
|---|
| 87 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
|---|
| [5] | 88 | |
|---|
| 89 | if ($format eq 'json') { |
|---|
| 90 | $self->header_props( |
|---|
| 91 | -type => 'application/json', |
|---|
| 92 | -charset => 'UTF-8', |
|---|
| 93 | ); |
|---|
| 94 | return decode_utf8( |
|---|
| [25] | 95 | JSON->new->utf8->convert_blessed->encode({ |
|---|
| [7] | 96 | bookmarks => \@resources, |
|---|
| [5] | 97 | }) |
|---|
| 98 | ); |
|---|
| [19] | 99 | } elsif ($format eq 'xbel') { |
|---|
| 100 | require XML::XBEL; |
|---|
| 101 | #TODO: conditional support; if XML::XBEL is not present, return a 5xx response |
|---|
| 102 | |
|---|
| 103 | my $xbel = XML::XBEL->new; |
|---|
| 104 | |
|---|
| 105 | $xbel->new_document({ |
|---|
| [31] | 106 | title => $title, |
|---|
| [19] | 107 | }); |
|---|
| 108 | |
|---|
| 109 | for my $bookmark (@resources) { |
|---|
| 110 | my $cdatetime = time2isoz $bookmark->{ctime}; |
|---|
| 111 | my $mdatetime = time2isoz $bookmark->{mtime}; |
|---|
| 112 | # make the timestamps W3C-correct |
|---|
| 113 | s/ /T/ foreach ($cdatetime, $mdatetime); |
|---|
| 114 | |
|---|
| 115 | $xbel->add_bookmark({ |
|---|
| 116 | href => $bookmark->{uri}, |
|---|
| 117 | title => $bookmark->{title}, |
|---|
| 118 | desc => 'Tags: ' . join(', ', @{ $bookmark->{tags} }), |
|---|
| 119 | added => $cdatetime, |
|---|
| 120 | #XXX: are we sure that modified is the mtime of the bookmark or the resource? |
|---|
| 121 | modified => $mdatetime, |
|---|
| 122 | }); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | $self->header_props( |
|---|
| 126 | -type => 'application/xml', |
|---|
| 127 | -charset => 'UTF-8', |
|---|
| 128 | ); |
|---|
| 129 | |
|---|
| 130 | return $xbel->toString; |
|---|
| [5] | 131 | } else { |
|---|
| 132 | $self->header_props( |
|---|
| 133 | -type => 'text/html', |
|---|
| 134 | -charset => 'UTF-8', |
|---|
| 135 | ); |
|---|
| 136 | |
|---|
| 137 | # set the base URL, adding a trailing slash if needed |
|---|
| 138 | my $base_url = $q->url; |
|---|
| 139 | $base_url .= '/' if $base_url =~ m{/bookmarks$}; |
|---|
| 140 | |
|---|
| [22] | 141 | my @links = ( |
|---|
| 142 | { |
|---|
| 143 | text => 'Link', |
|---|
| 144 | type => 'text/html', |
|---|
| 145 | rel => 'self', |
|---|
| 146 | query => { |
|---|
| 147 | tag => \@tags, |
|---|
| 148 | }, |
|---|
| 149 | }, |
|---|
| 150 | { |
|---|
| 151 | text => 'JSON', |
|---|
| 152 | type => 'application/json', |
|---|
| 153 | rel => 'alternate', |
|---|
| 154 | query => { |
|---|
| 155 | tag => \@tags, |
|---|
| 156 | format => 'json', |
|---|
| 157 | }, |
|---|
| 158 | }, |
|---|
| 159 | { |
|---|
| 160 | text => 'XBEL', |
|---|
| 161 | type => 'application/xml', |
|---|
| 162 | rel => 'alternate', |
|---|
| 163 | query => { |
|---|
| 164 | tag => \@tags, |
|---|
| 165 | format => 'xbel', |
|---|
| 166 | }, |
|---|
| 167 | }, |
|---|
| 168 | { |
|---|
| 169 | text => 'Atom', |
|---|
| 170 | type => 'application/atom+xml', |
|---|
| 171 | rel => 'alternate', |
|---|
| 172 | path => 'feed', |
|---|
| 173 | query => { |
|---|
| 174 | tag => \@tags, |
|---|
| 175 | }, |
|---|
| 176 | }, |
|---|
| 177 | ); |
|---|
| 178 | for my $link (@links) { |
|---|
| 179 | $link->{href} = URI->new_abs($link->{path} || '', $base_uri); |
|---|
| 180 | $link->{href}->query_form($link->{query}); |
|---|
| 181 | } |
|---|
| [31] | 182 | |
|---|
| [6] | 183 | return $self->tt_process( |
|---|
| [5] | 184 | 'list.tt', |
|---|
| 185 | { |
|---|
| 186 | base_url => $base_url, |
|---|
| [31] | 187 | title => $title, |
|---|
| [5] | 188 | selected_tag => $tag, |
|---|
| [20] | 189 | search_tags => \@tags, |
|---|
| [22] | 190 | links => \@links, |
|---|
| [20] | 191 | all_tags => \@all_tags, |
|---|
| [5] | 192 | cotags => \@cotags, |
|---|
| 193 | resources => \@resources, |
|---|
| 194 | }, |
|---|
| 195 | ); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| [9] | 199 | sub feed { |
|---|
| 200 | my $self = shift; |
|---|
| 201 | my $q = $self->query; |
|---|
| 202 | |
|---|
| 203 | my $tag = $q->param('tag'); |
|---|
| [31] | 204 | my @tags = $q->param('tag'); |
|---|
| 205 | # special case: handle the empty tag |
|---|
| 206 | if (@tags == 1 && $tags[0] eq '') { |
|---|
| 207 | @tags = (); |
|---|
| 208 | } |
|---|
| [9] | 209 | |
|---|
| [31] | 210 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
|---|
| 211 | |
|---|
| [9] | 212 | require XML::Atom::Feed; |
|---|
| 213 | require XML::Atom::Entry; |
|---|
| 214 | require XML::Atom::Link; |
|---|
| 215 | |
|---|
| 216 | my $feed = XML::Atom::Feed->new; |
|---|
| [31] | 217 | $feed->title($title); |
|---|
| [17] | 218 | $feed->id($base_uri->canonical . 'feed'); |
|---|
| [31] | 219 | my $html_link = XML::Atom::Link->new; |
|---|
| 220 | $html_link->type('text/html'); |
|---|
| 221 | $html_link->rel('alternate'); |
|---|
| 222 | $html_link->href($base_uri->canonical); |
|---|
| 223 | $feed->add_link($html_link); |
|---|
| [9] | 224 | |
|---|
| 225 | # construct a feed from the most recent 12 bookmarks |
|---|
| [31] | 226 | for my $bookmark ($bookmarks->get_bookmarks({ tag => \@tags, limit => 12 })) { |
|---|
| [9] | 227 | my $entry = XML::Atom::Entry->new; |
|---|
| [31] | 228 | $entry->id($bookmark->bookmark_uri->canonical); |
|---|
| 229 | $entry->title($bookmark->title); |
|---|
| [9] | 230 | my $link = XML::Atom::Link->new; |
|---|
| [31] | 231 | $link->href($bookmark->uri); |
|---|
| [9] | 232 | $entry->add_link($link); |
|---|
| [31] | 233 | $entry->summary('Tags: ' . join(', ', @{ $bookmark->tags })); |
|---|
| [9] | 234 | $feed->add_entry($entry); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | $self->header_props( |
|---|
| 238 | -type => 'application/atom+xml', |
|---|
| 239 | -charset => 'UTF-8', |
|---|
| 240 | ); |
|---|
| 241 | return $feed->as_xml; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| [5] | 244 | sub view { |
|---|
| 245 | my $self = shift; |
|---|
| 246 | my $id = $self->param('id'); |
|---|
| [7] | 247 | my $format = $self->query->param('format') || 'html'; |
|---|
| [5] | 248 | |
|---|
| 249 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
|---|
| 250 | if ($bookmark) { |
|---|
| [30] | 251 | if ($format eq 'json') { |
|---|
| [5] | 252 | $self->header_props( |
|---|
| [30] | 253 | -type => 'application/json', |
|---|
| [5] | 254 | -charset => 'UTF-8', |
|---|
| 255 | ); |
|---|
| [30] | 256 | return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); |
|---|
| [5] | 257 | } else { |
|---|
| [30] | 258 | # display the bookmark form for this bookmark |
|---|
| 259 | $bookmark->{exists} = 1; |
|---|
| 260 | $bookmark->{created} = "Created " . localtime($bookmark->ctime); |
|---|
| 261 | $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; |
|---|
| 262 | $self->header_props( |
|---|
| 263 | -type => 'text/html', |
|---|
| 264 | -charset => 'UTF-8', |
|---|
| 265 | ); |
|---|
| 266 | return $self->tt_process( |
|---|
| 267 | 'bookmark.tt', |
|---|
| 268 | $bookmark, |
|---|
| 269 | ); |
|---|
| 270 | } |
|---|
| 271 | } else { |
|---|
| 272 | $self->header_props( |
|---|
| 273 | -type => 'text/html', |
|---|
| 274 | -charset => 'UTF-8', |
|---|
| 275 | -status => 404, |
|---|
| 276 | ); |
|---|
| 277 | return "Bookmark $id Not Found"; |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | sub view_field { |
|---|
| 282 | my $self = shift; |
|---|
| 283 | my $id = $self->param('id'); |
|---|
| 284 | my $field = $self->param('field'); |
|---|
| 285 | |
|---|
| 286 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
|---|
| 287 | if ($bookmark) { |
|---|
| 288 | # respond with just the requested field as plain text |
|---|
| 289 | my $value = eval { $bookmark->$field }; |
|---|
| 290 | if ($@) { |
|---|
| 291 | if ($@ =~ /Can't locate object method/) { |
|---|
| [7] | 292 | $self->header_props( |
|---|
| [30] | 293 | -type => 'text/plain', |
|---|
| [7] | 294 | -charset => 'UTF-8', |
|---|
| [30] | 295 | -status => 404, |
|---|
| [7] | 296 | ); |
|---|
| [30] | 297 | return qq{"$field" is not a valid bookmark data field}; |
|---|
| [7] | 298 | } else { |
|---|
| [30] | 299 | die $@; |
|---|
| [7] | 300 | } |
|---|
| [5] | 301 | } |
|---|
| [30] | 302 | $self->header_props( |
|---|
| 303 | -type => 'text/plain', |
|---|
| 304 | -charset => 'UTF-8', |
|---|
| 305 | ); |
|---|
| 306 | return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; |
|---|
| [5] | 307 | } else { |
|---|
| 308 | $self->header_props( |
|---|
| 309 | -type => 'text/html', |
|---|
| 310 | -charset => 'UTF-8', |
|---|
| 311 | -status => 404, |
|---|
| 312 | ); |
|---|
| 313 | return "Bookmark $id Not Found"; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | #TODO: split this into edit and create methods |
|---|
| 318 | sub edit { |
|---|
| 319 | my $self = shift; |
|---|
| 320 | my $q = $self->query; |
|---|
| 321 | #TODO: get the bookmark based on the id and edit it directly? |
|---|
| 322 | #TODO: deal with changing URIs |
|---|
| 323 | my $uri = $q->param('uri'); |
|---|
| 324 | my $title = $q->param('title'); |
|---|
| 325 | my @tags = split ' ', $q->param('tags'); |
|---|
| 326 | $bookmarks->add({ |
|---|
| 327 | uri => $uri, |
|---|
| 328 | title => $title, |
|---|
| 329 | tags => \@tags, |
|---|
| 330 | }); |
|---|
| 331 | |
|---|
| 332 | =begin |
|---|
| 333 | |
|---|
| 334 | my $location = URI->new($q->url); |
|---|
| 335 | $location->query_form(uri => $uri) if defined $q->url_param('uri'); |
|---|
| 336 | $location->fragment('updated'); |
|---|
| 337 | |
|---|
| 338 | =cut |
|---|
| 339 | |
|---|
| 340 | # return to the form |
|---|
| 341 | $self->header_type('redirect'); |
|---|
| 342 | $self->header_props( |
|---|
| 343 | -uri => $ENV{REQUEST_URI}, |
|---|
| 344 | -status => 303, |
|---|
| 345 | ); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | 1; |
|---|