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}; |
---|
10 | use JSON; |
---|
11 | use Bookmarks; |
---|
12 | |
---|
13 | use URI; |
---|
14 | my $base_uri = URI->new; |
---|
15 | $base_uri->scheme('http'); |
---|
16 | $base_uri->host($ENV{HTTP_X_FORWARDED_HOST} || $ENV{SERVER_NAME}); |
---|
17 | $base_uri->port($ENV{SERVER_PORT}); |
---|
18 | $base_uri->path($ENV{SCRIPT_NAME} . '/'); |
---|
19 | |
---|
20 | my $dbname = 'fk.db'; |
---|
21 | my $bookmarks = Bookmarks->new({ |
---|
22 | dbname => $dbname, |
---|
23 | base_uri => $base_uri, |
---|
24 | }); |
---|
25 | |
---|
26 | sub setup { |
---|
27 | my $self = shift; |
---|
28 | $self->mode_param(path_info => 1); |
---|
29 | $self->run_modes([qw{ |
---|
30 | list |
---|
31 | feed |
---|
32 | view |
---|
33 | view_field |
---|
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 | ); |
---|
62 | return $self->tt_process( |
---|
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'); |
---|
72 | my @tags = $q->param('tag'); |
---|
73 | # special case: handle the empty tag |
---|
74 | if (@tags == 1 && $tags[0] eq '') { |
---|
75 | @tags = (); |
---|
76 | } |
---|
77 | my $limit = $q->param('limit'); |
---|
78 | my $offset = $q->param('offset'); |
---|
79 | my @resources = $bookmarks->get_bookmarks({ |
---|
80 | tag => \@tags, |
---|
81 | limit => $limit, |
---|
82 | offset => $offset, |
---|
83 | }); |
---|
84 | my @all_tags = $bookmarks->get_tags({ selected => $tag }); |
---|
85 | my @cotags = $bookmarks->get_cotags({ tag => \@tags }); |
---|
86 | |
---|
87 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
---|
88 | |
---|
89 | if ($format eq 'json') { |
---|
90 | $self->header_props( |
---|
91 | -type => 'application/json', |
---|
92 | -charset => 'UTF-8', |
---|
93 | ); |
---|
94 | return decode_utf8( |
---|
95 | JSON->new->utf8->convert_blessed->encode({ |
---|
96 | bookmarks => \@resources, |
---|
97 | }) |
---|
98 | ); |
---|
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({ |
---|
106 | title => $title, |
---|
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; |
---|
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 | |
---|
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 | } |
---|
182 | |
---|
183 | return $self->tt_process( |
---|
184 | 'list.tt', |
---|
185 | { |
---|
186 | base_url => $base_url, |
---|
187 | title => $title, |
---|
188 | selected_tag => $tag, |
---|
189 | search_tags => \@tags, |
---|
190 | links => \@links, |
---|
191 | all_tags => \@all_tags, |
---|
192 | cotags => \@cotags, |
---|
193 | resources => \@resources, |
---|
194 | }, |
---|
195 | ); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | sub feed { |
---|
200 | my $self = shift; |
---|
201 | my $q = $self->query; |
---|
202 | |
---|
203 | my $tag = $q->param('tag'); |
---|
204 | my @tags = $q->param('tag'); |
---|
205 | # special case: handle the empty tag |
---|
206 | if (@tags == 1 && $tags[0] eq '') { |
---|
207 | @tags = (); |
---|
208 | } |
---|
209 | |
---|
210 | my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); |
---|
211 | |
---|
212 | require XML::Atom; |
---|
213 | $XML::Atom::DefaultVersion = "1.0"; |
---|
214 | |
---|
215 | require XML::Atom::Feed; |
---|
216 | require XML::Atom::Entry; |
---|
217 | require XML::Atom::Link; |
---|
218 | |
---|
219 | my $feed = XML::Atom::Feed->new; |
---|
220 | $feed->title($title); |
---|
221 | |
---|
222 | my $feed_uri = URI->new_abs('feed', $base_uri); |
---|
223 | $feed_uri->query_form(tag => \@tags); |
---|
224 | $feed->id($feed_uri->canonical); |
---|
225 | |
---|
226 | my $self_link = XML::Atom::Link->new; |
---|
227 | $self_link->type('application/atom+xml'); |
---|
228 | $self_link->rel('self'); |
---|
229 | $self_link->href($feed_uri->canonical); |
---|
230 | $feed->add_link($self_link); |
---|
231 | |
---|
232 | my $html_link = XML::Atom::Link->new; |
---|
233 | $html_link->type('text/html'); |
---|
234 | $html_link->rel('alternate'); |
---|
235 | my $html_uri = $base_uri->clone; |
---|
236 | $html_uri->query_form(tag => \@tags); |
---|
237 | $html_link->href($html_uri->canonical); |
---|
238 | $feed->add_link($html_link); |
---|
239 | |
---|
240 | # construct a feed from the most recent 12 bookmarks |
---|
241 | for my $bookmark ($bookmarks->get_bookmarks({ tag => \@tags, limit => 12 })) { |
---|
242 | my $entry = XML::Atom::Entry->new; |
---|
243 | $entry->id($bookmark->bookmark_uri->canonical); |
---|
244 | $entry->title($bookmark->title); |
---|
245 | my $link = XML::Atom::Link->new; |
---|
246 | $link->href($bookmark->uri); |
---|
247 | $entry->add_link($link); |
---|
248 | $entry->summary('Tags: ' . join(', ', @{ $bookmark->tags })); |
---|
249 | my $mdatetime = time2isoz $bookmark->mtime; |
---|
250 | # make the timestamp W3C-correct |
---|
251 | $mdatetime =~ s/ /T/; |
---|
252 | $entry->updated($mdatetime); |
---|
253 | $feed->add_entry($entry); |
---|
254 | } |
---|
255 | |
---|
256 | $self->header_props( |
---|
257 | -type => 'application/atom+xml', |
---|
258 | -charset => 'UTF-8', |
---|
259 | ); |
---|
260 | return $feed->as_xml; |
---|
261 | } |
---|
262 | |
---|
263 | sub view { |
---|
264 | my $self = shift; |
---|
265 | my $id = $self->param('id'); |
---|
266 | my $format = $self->query->param('format') || 'html'; |
---|
267 | |
---|
268 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
269 | if ($bookmark) { |
---|
270 | if ($format eq 'json') { |
---|
271 | $self->header_props( |
---|
272 | -type => 'application/json', |
---|
273 | -charset => 'UTF-8', |
---|
274 | ); |
---|
275 | return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); |
---|
276 | } else { |
---|
277 | # display the bookmark form for this bookmark |
---|
278 | $bookmark->{exists} = 1; |
---|
279 | $bookmark->{created} = "Created " . localtime($bookmark->ctime); |
---|
280 | $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; |
---|
281 | $self->header_props( |
---|
282 | -type => 'text/html', |
---|
283 | -charset => 'UTF-8', |
---|
284 | ); |
---|
285 | return $self->tt_process( |
---|
286 | 'bookmark.tt', |
---|
287 | $bookmark, |
---|
288 | ); |
---|
289 | } |
---|
290 | } else { |
---|
291 | $self->header_props( |
---|
292 | -type => 'text/html', |
---|
293 | -charset => 'UTF-8', |
---|
294 | -status => 404, |
---|
295 | ); |
---|
296 | return "Bookmark $id Not Found"; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | sub view_field { |
---|
301 | my $self = shift; |
---|
302 | my $id = $self->param('id'); |
---|
303 | my $field = $self->param('field'); |
---|
304 | |
---|
305 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
306 | if ($bookmark) { |
---|
307 | # respond with just the requested field as plain text |
---|
308 | my $value = eval { $bookmark->$field }; |
---|
309 | if ($@) { |
---|
310 | if ($@ =~ /Can't locate object method/) { |
---|
311 | $self->header_props( |
---|
312 | -type => 'text/plain', |
---|
313 | -charset => 'UTF-8', |
---|
314 | -status => 404, |
---|
315 | ); |
---|
316 | return qq{"$field" is not a valid bookmark data field}; |
---|
317 | } else { |
---|
318 | die $@; |
---|
319 | } |
---|
320 | } |
---|
321 | $self->header_props( |
---|
322 | -type => 'text/plain', |
---|
323 | -charset => 'UTF-8', |
---|
324 | ); |
---|
325 | return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; |
---|
326 | } else { |
---|
327 | $self->header_props( |
---|
328 | -type => 'text/html', |
---|
329 | -charset => 'UTF-8', |
---|
330 | -status => 404, |
---|
331 | ); |
---|
332 | return "Bookmark $id Not Found"; |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | #TODO: split this into edit and create methods |
---|
337 | sub edit { |
---|
338 | my $self = shift; |
---|
339 | my $q = $self->query; |
---|
340 | #TODO: get the bookmark based on the id and edit it directly? |
---|
341 | #TODO: deal with changing URIs |
---|
342 | my $uri = $q->param('uri'); |
---|
343 | my $title = $q->param('title'); |
---|
344 | my @tags = split ' ', $q->param('tags'); |
---|
345 | $bookmarks->add({ |
---|
346 | uri => $uri, |
---|
347 | title => $title, |
---|
348 | tags => \@tags, |
---|
349 | }); |
---|
350 | |
---|
351 | =begin |
---|
352 | |
---|
353 | my $location = URI->new($q->url); |
---|
354 | $location->query_form(uri => $uri) if defined $q->url_param('uri'); |
---|
355 | $location->fragment('updated'); |
---|
356 | |
---|
357 | =cut |
---|
358 | |
---|
359 | # return to the form |
---|
360 | $self->header_type('redirect'); |
---|
361 | $self->header_props( |
---|
362 | -uri => $ENV{REQUEST_URI}, |
---|
363 | -status => 303, |
---|
364 | ); |
---|
365 | } |
---|
366 | |
---|
367 | 1; |
---|