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