source: bookmarks/trunk/BookmarkApp.pm @ 49

Last change on this file since 49 was 49, checked in by peter, 11 years ago

added a CSV format response

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