source: bookmarks/trunk/index.cgi @ 2

Last change on this file since 2 was 2, checked in by peter, 12 years ago

Added exisiting bookmarks project files

  • Property svn:executable set to *
File size: 4.9 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use Encode;
5use URI;
6use CGI;
7use YAML;
8use DBI;
9use Template;
10use JSON;
11use Bookmarks;
12
13my $q = CGI->new;
14my $template = Template->new;
15
16my $method = $ENV{REQUEST_METHOD};
17
18# bookmarklet to add a bookmark via the browser
19# javascript:(function(){window.open("http://grim.ath.cx/~peter/bookmarks?uri="+document.location+"&title="+document.title,"edit_bookmark","width=800,height=250")})()
20
21my $dbname = 'new.db';
22my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 });
23
24my $bookmarks = Bookmarks->new({
25    dbh => $dbh,
26});
27
28=begin new style?
29
30if ($ENV{PATH_INFO} =~ m{^/(\d+)(?:/(uri|title|tags))?\b}) {
31    require Resource::Bookmark;
32    my $resource = Resource::Bookmark->new({
33        q => $q,
34        id => $1,
35        field => $2,
36        bookmarks => $bookmarks,
37    });
38    $resource->$method();
39}
40exit;
41
42=cut
43
44my %resource = (
45    GET => sub {
46        my ($q, $dbh) = @_;
47        if ($ENV{PATH_INFO} =~ m{^/(\d+)(?:/(uri|title|tags))?\b}) {
48            my $id = $1;
49            my $field = $2;
50            my $bookmark = $bookmarks->get_bookmark({ id => $id });
51            if ($bookmark) {
52                if ($field) {
53                    print $q->header(
54                        -type    => 'text/plain',
55                        -charset => 'UTF-8',
56                    );
57                    my $value = $bookmark->{$field};
58                    print ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value;
59                } else {
60                    $bookmark->{exists} = 1;
61                    $bookmark->{created} = "Created " . localtime($bookmark->{ctime});
62                    $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime};
63                    print $q->header(
64                        -type    => 'text/html',
65                        -charset => 'UTF-8',
66                    );
67                    $template->process(
68                        'bookmark.tt',
69                        $bookmark
70                    );
71                }
72            } else {
73                print $q->header(
74                    -type    => 'text/html',
75                    -charset => 'UTF-8',
76                    -status  => 404,
77                );
78                print "Not Found";
79            }
80        } elsif (defined(my $uri = $q->param('uri'))) {
81            my $bookmark = $bookmarks->get_bookmark({ uri => $uri });
82            if ($bookmark) {
83                #TODO: is there a better base URL to use?
84                print $q->redirect($q->url . '/' . $bookmark->{id});
85            } else {
86                # bookmark was not found; show the form to create a new bookmark
87                $bookmark->{uri} = $uri;
88                $bookmark->{title} = $q->param('title');
89                print $q->header(
90                    -type    => 'text/html',
91                    -charset => 'UTF-8',
92                    -status  => 404,
93                );
94                $template->process(
95                    'bookmark.tt',
96                    $bookmark
97                );
98            }
99        } else {
100            #print $q->header('text/html');
101            #print "TODO: list bookmarks";
102            #return;
103            # list all the resources
104            my $format = $q->param('format');
105            my $tag = $q->param('tag');
106            my @resources = $bookmarks->get_resources({ tag => $tag });
107            my @all_tags = $bookmarks->get_tags({ selected => $tag });
108            my @cotags = $bookmarks->get_cotags({ tag => $tag });
109
110            if ($format eq 'json') {
111                print $q->header(
112                    -type    => 'application/json',
113                    -charset => 'UTF-8',
114                );
115                print decode_utf8(
116                    encode_json({
117                        resources => \@resources,
118                    })
119                );
120            } else {
121                print $q->header(
122                    -type    => 'text/html',
123                    -charset => 'UTF-8',
124                );
125                $template->process(
126                    'list.tt',
127                    {
128                        selected_tag => $tag,
129                        tags         => \@all_tags,
130                        cotags       => \@cotags,
131                        resources    => \@resources,
132                    },
133                );
134            }
135        }
136    },
137    POST => sub {
138        #TODO: deal with changing URIs
139        my ($q, $dbh) = @_;
140        my $uri = $q->param('uri');
141        my $title = $q->param('title');
142        my @tags = split ' ', $q->param('tags');
143        $bookmarks->add({
144            uri   => $uri,
145            title => $title,
146            tags  => \@tags,
147        });
148
149        my $location = URI->new($q->url);
150        $location->query_form(uri => $uri) if defined $q->url_param('uri');
151        $location->fragment('updated');
152        print $q->redirect($ENV{REQUEST_URI}); #$location);
153    },
154);
155
156$resource{$method}->($q, $dbh);
157
158
Note: See TracBrowser for help on using the repository browser.