Changeset 5 in bookmarks for trunk/index.cgi
- Timestamp:
- 03/23/12 22:29:38 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/index.cgi
r3 r5 1 1 #!/usr/bin/perl -w 2 2 use strict; 3 4 use Encode;5 use URI;6 use CGI;7 use YAML;8 use DBI;9 use Template;10 use JSON;11 use Bookmarks;12 13 my $q = CGI->new;14 my $template = Template->new;15 16 my $method = $ENV{REQUEST_METHOD};17 3 18 4 # bookmarklet to add a bookmark via the browser 19 5 # javascript:(function(){window.open("http://grim.ath.cx/~peter/bookmarks?uri="+document.location+"&title="+document.title,"edit_bookmark","width=800,height=250")})() 20 6 21 my $dbname = 'new.db'; 22 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); 23 24 my $bookmarks = Bookmarks->new({ 25 dbh => $dbh, 26 }); 27 28 =begin new style? 29 30 if ($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 } 40 exit; 41 42 =cut 43 44 my %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 # list all the bookmarks 101 my $format = $q->param('format') || 'html'; 102 my $tag = $q->param('tag'); 103 my @resources = $bookmarks->get_resources({ tag => $tag }); 104 my @all_tags = $bookmarks->get_tags({ selected => $tag }); 105 my @cotags = $bookmarks->get_cotags({ tag => $tag }); 106 107 if ($format eq 'json') { 108 print $q->header( 109 -type => 'application/json', 110 -charset => 'UTF-8', 111 ); 112 print decode_utf8( 113 encode_json({ 114 resources => \@resources, 115 }) 116 ); 117 } else { 118 print $q->header( 119 -type => 'text/html', 120 -charset => 'UTF-8', 121 ); 122 123 # set the base URL, adding a trailing slash if needed 124 my $base_url = $q->url; 125 $base_url .= '/' if $base_url =~ m{/bookmarks$}; 126 127 $template->process( 128 'list.tt', 129 { 130 base_url => $base_url, 131 selected_tag => $tag, 132 tags => \@all_tags, 133 cotags => \@cotags, 134 resources => \@resources, 135 }, 136 ); 137 } 138 } 139 }, 140 POST => sub { 141 #TODO: deal with changing URIs 142 my ($q, $dbh) = @_; 143 my $uri = $q->param('uri'); 144 my $title = $q->param('title'); 145 my @tags = split ' ', $q->param('tags'); 146 $bookmarks->add({ 147 uri => $uri, 148 title => $title, 149 tags => \@tags, 150 }); 151 152 my $location = URI->new($q->url); 153 $location->query_form(uri => $uri) if defined $q->url_param('uri'); 154 $location->fragment('updated'); 155 print $q->redirect($ENV{REQUEST_URI}); #$location); 156 }, 7 use CGI::Application::Dispatch; 8 CGI::Application::Dispatch->dispatch( 9 table => [ 10 '[get]' => { app => 'BookmarkApp', rm => 'list' }, 11 ':id/:field?[get]' => { app => 'BookmarkApp', rm => 'view' }, 12 ':id?[post]' => { app => 'BookmarkApp', rm => 'edit' }, 13 ], 157 14 ); 158 159 $resource{$method}->($q, $dbh);160 161
Note: See TracChangeset
for help on using the changeset viewer.