source: bookmarks/trunk/BookmarkApp.pm @ 13

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