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