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