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 DBI; |
---|
9 | use Encode; |
---|
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{SERVER_NAME}); |
---|
17 | $base_uri->port($ENV{SERVER_PORT}); |
---|
18 | $base_uri->path($ENV{SCRIPT_NAME} . '/'); |
---|
19 | |
---|
20 | my $dbname = 'new.db'; |
---|
21 | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); |
---|
22 | my $bookmarks = Bookmarks->new({ |
---|
23 | dbh => $dbh, |
---|
24 | base_uri => $base_uri->canonical, |
---|
25 | }); |
---|
26 | |
---|
27 | sub setup { |
---|
28 | my $self = shift; |
---|
29 | $self->mode_param(path_info => 1); |
---|
30 | $self->run_modes([qw{ |
---|
31 | list |
---|
32 | feed |
---|
33 | view |
---|
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 @resources = $bookmarks->get_resources({ tag => $tag }); |
---|
73 | my @all_tags = $bookmarks->get_tags({ selected => $tag }); |
---|
74 | my @cotags = $bookmarks->get_cotags({ tag => $tag }); |
---|
75 | |
---|
76 | if ($format eq 'json') { |
---|
77 | $self->header_props( |
---|
78 | -type => 'application/json', |
---|
79 | -charset => 'UTF-8', |
---|
80 | ); |
---|
81 | return decode_utf8( |
---|
82 | encode_json({ |
---|
83 | bookmarks => \@resources, |
---|
84 | }) |
---|
85 | ); |
---|
86 | } else { |
---|
87 | $self->header_props( |
---|
88 | -type => 'text/html', |
---|
89 | -charset => 'UTF-8', |
---|
90 | ); |
---|
91 | |
---|
92 | # set the base URL, adding a trailing slash if needed |
---|
93 | my $base_url = $q->url; |
---|
94 | $base_url .= '/' if $base_url =~ m{/bookmarks$}; |
---|
95 | |
---|
96 | return $self->tt_process( |
---|
97 | 'list.tt', |
---|
98 | { |
---|
99 | base_url => $base_url, |
---|
100 | selected_tag => $tag, |
---|
101 | tags => \@all_tags, |
---|
102 | cotags => \@cotags, |
---|
103 | resources => \@resources, |
---|
104 | }, |
---|
105 | ); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | sub feed { |
---|
110 | my $self = shift; |
---|
111 | my $q = $self->query; |
---|
112 | |
---|
113 | my $tag = $q->param('tag'); |
---|
114 | |
---|
115 | require XML::Atom::Feed; |
---|
116 | require XML::Atom::Entry; |
---|
117 | require XML::Atom::Link; |
---|
118 | |
---|
119 | my $feed = XML::Atom::Feed->new; |
---|
120 | $feed->title('Bookmarks'); |
---|
121 | $feed->id($base_uri . 'feed'); |
---|
122 | |
---|
123 | # construct a feed from the most recent 12 bookmarks |
---|
124 | for my $bookmark ($bookmarks->get_resources({ tag => $tag, limit => 12 })) { |
---|
125 | my $entry = XML::Atom::Entry->new; |
---|
126 | $entry->id($bookmark->{bookmark_uri}); |
---|
127 | $entry->title($bookmark->{title}); |
---|
128 | my $link = XML::Atom::Link->new; |
---|
129 | $link->href($bookmark->{uri}); |
---|
130 | $entry->add_link($link); |
---|
131 | $entry->summary('Tags: ' . join(', ', @{ $bookmark->{tags} })); |
---|
132 | $feed->add_entry($entry); |
---|
133 | } |
---|
134 | |
---|
135 | $self->header_props( |
---|
136 | -type => 'application/atom+xml', |
---|
137 | -charset => 'UTF-8', |
---|
138 | ); |
---|
139 | return $feed->as_xml; |
---|
140 | } |
---|
141 | |
---|
142 | sub view { |
---|
143 | my $self = shift; |
---|
144 | my $id = $self->param('id'); |
---|
145 | my $field = $self->param('field'); |
---|
146 | my $format = $self->query->param('format') || 'html'; |
---|
147 | |
---|
148 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
149 | if ($bookmark) { |
---|
150 | if ($field) { |
---|
151 | # respond with just the requested field as plain text |
---|
152 | $self->header_props( |
---|
153 | -type => 'text/plain', |
---|
154 | -charset => 'UTF-8', |
---|
155 | ); |
---|
156 | my $value = $bookmark->{$field}; |
---|
157 | return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; |
---|
158 | } else { |
---|
159 | if ($format eq 'json') { |
---|
160 | $self->header_props( |
---|
161 | -type => 'application/json', |
---|
162 | -charset => 'UTF-8', |
---|
163 | ); |
---|
164 | return decode_utf8(encode_json($bookmark)); |
---|
165 | } else { |
---|
166 | # display the bookmark form for this bookmark |
---|
167 | $bookmark->{exists} = 1; |
---|
168 | $bookmark->{created} = "Created " . localtime($bookmark->{ctime}); |
---|
169 | $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime}; |
---|
170 | $self->header_props( |
---|
171 | -type => 'text/html', |
---|
172 | -charset => 'UTF-8', |
---|
173 | ); |
---|
174 | return $self->tt_process( |
---|
175 | 'bookmark.tt', |
---|
176 | $bookmark, |
---|
177 | ); |
---|
178 | } |
---|
179 | } |
---|
180 | } else { |
---|
181 | $self->header_props( |
---|
182 | -type => 'text/html', |
---|
183 | -charset => 'UTF-8', |
---|
184 | -status => 404, |
---|
185 | ); |
---|
186 | return "Bookmark $id Not Found"; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | #TODO: split this into edit and create methods |
---|
191 | sub edit { |
---|
192 | my $self = shift; |
---|
193 | my $q = $self->query; |
---|
194 | #TODO: get the bookmark based on the id and edit it directly? |
---|
195 | #TODO: deal with changing URIs |
---|
196 | my $uri = $q->param('uri'); |
---|
197 | my $title = $q->param('title'); |
---|
198 | my @tags = split ' ', $q->param('tags'); |
---|
199 | $bookmarks->add({ |
---|
200 | uri => $uri, |
---|
201 | title => $title, |
---|
202 | tags => \@tags, |
---|
203 | }); |
---|
204 | |
---|
205 | =begin |
---|
206 | |
---|
207 | my $location = URI->new($q->url); |
---|
208 | $location->query_form(uri => $uri) if defined $q->url_param('uri'); |
---|
209 | $location->fragment('updated'); |
---|
210 | |
---|
211 | =cut |
---|
212 | |
---|
213 | # return to the form |
---|
214 | $self->header_type('redirect'); |
---|
215 | $self->header_props( |
---|
216 | -uri => $ENV{REQUEST_URI}, |
---|
217 | -status => 303, |
---|
218 | ); |
---|
219 | } |
---|
220 | |
---|
221 | 1; |
---|