From 0aa6e3511f7f267131db3054c9e50168de3b0754 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 14 Oct 2012 03:45:57 +0000 Subject: remove sprintf's add sitemap.xml new view dispatcher --- Blogsum/Config.pm.dist | 1 - examples/httpd-blogsum.conf | 8 +-- index.cgi | 134 +++++++++++++++++++++++--------------------- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/Blogsum/Config.pm.dist b/Blogsum/Config.pm.dist index 493b12b..753f8e2 100644 --- a/Blogsum/Config.pm.dist +++ b/Blogsum/Config.pm.dist @@ -21,7 +21,6 @@ our $blog_subtitle = 'My New Blog'; our $blog_url = 'http://www.example.com/'; our $blog_owner = 'user@example.com'; our $blog_rights = 'Copyright © 2012 Example User'; -our $feed_updates = 'hourly'; our $captcha_api_server = 'http://www.google.com/recaptcha/api'; our $captcha_pubkey = ''; our $captcha_seckey = ''; diff --git a/examples/httpd-blogsum.conf b/examples/httpd-blogsum.conf index f7f669c..0c3c81e 100644 --- a/examples/httpd-blogsum.conf +++ b/examples/httpd-blogsum.conf @@ -5,10 +5,10 @@ Options +FollowSymlinks RewriteEngine On - RewriteRule ^/rss.xml$ /index.cgi?rss=1 [PT,QSA] - RewriteRule ^/rss2.xml$ /index.cgi?rss=2 [PT,QSA] - RewriteRule ^/Page/([^/]+)$ /index.cgi?page=$1 [PT,QSA] - RewriteRule ^/Tags/([^/]+)$ /index.cgi?search=$1 [PT,QSA] + RewriteRule ^/rss.xml$ /index.cgi?view=rss [PT,QSA] + RewriteRule ^/sitemap.xml$ /index.cgi?view=sitemap [PT,QSA] + RewriteRule ^/Page/([^/]+)$ /index.cgi?view=article&page=$1 [PT,QSA] + RewriteRule ^/Tags/([^/]+)$ /index.cgi?view=article&tag=$1 [PT,QSA] RewriteRule ^/([0-9]{4})/([0-9]{2})/([^/]+)$ /index.cgi?view=article&year=$1&month=$2&uri=$3 [PT,QSA] RewriteRule ^/([0-9]{4})/([0-9]{2})/?$ /index.cgi?view=article&year=$1&month=$2 [PT,QSA] RewriteRule ^/([0-9]{4})/?$ /index.cgi?view=article&year=$1 [PT,QSA] diff --git a/index.cgi b/index.cgi index 4ccb704..442b3ae 100755 --- a/index.cgi +++ b/index.cgi @@ -32,6 +32,7 @@ ########################### # pragmas and vars # ########################### + use strict; use Blogsum::Config; my $database = $Blogsum::Config::database; @@ -42,7 +43,6 @@ my $blog_url = $Blogsum::Config::blog_url; $blog_url .= '/' unless ($blog_url =~ /^.*\/$/); my $blog_owner = $Blogsum::Config::blog_owner; my $blog_rights = $Blogsum::Config::blog_rights; -my $feed_updates = $Blogsum::Config::feed_updates; my $captcha_api_server = $Blogsum::Config::captcha_api_server; my $captcha_pubkey = $Blogsum::Config::captcha_pubkey; my $captcha_seckey = $Blogsum::Config::captcha_seckey; @@ -57,17 +57,29 @@ my $max_tags_in_cloud = $Blogsum::Config::max_tags_in_cloud; my $page_not_found_error = $Blogsum::Config::page_not_found_error; $page_not_found_error ||= '404 page not found'; - ########################### # main execution # ########################### + +my %dispatch = ( + 'article' => \&output_article, + 'sitemap' => \&output_sitemap, + 'rss' => \&output_rss, +); my $cgi = CGI->new; $cgi->charset('UTF-8'); my $dbh = DBI->connect("DBI:SQLite:dbname=$database", '', '', { RaiseError => 1 }) || die $DBI::errstr; my $template = HTML::Template->new(filename => "themes/${blog_theme}/index.tmpl", die_on_bad_params => 0); -if ($cgi->param('rss')) { - output_rss(); -} else { +my $view = $cgi->param('view'); +$view = 'article' unless exists $dispatch{$view}; +$dispatch{$view}(); +$dbh->disconnect; + +########################### +# subfunctions # +########################### + +sub output_article { read_comment() if $comments_allowed; my $articles = get_articles(); my $archives = get_archives(); @@ -95,70 +107,68 @@ if ($cgi->param('rss')) { } print $cgi->header(@status), $template->output; } -$dbh->disconnect; - - -########################### -# subfunctions # -########################### sub output_rss { - - my $version = ($cgi->param('rss') == 2) ? '2.0' : '1.0'; - my $rss = XML::RSS->new( version => $version ); + my $rss = XML::RSS->new( version => '2.0' ); $rss->channel ( title => $blog_title, link => $blog_url, description => $blog_subtitle, - dc => { - subject => $blog_title, - creator => $blog_owner, - publisher => $blog_owner, - rights => $blog_rights, - language => 'en-us', - }, - syn => { - updatePeriod => $feed_updates, - updateFrequency => 1, - updateBase => '1901-01-01T00:00+00:00', - } + managingEditor => $blog_owner, + webMaster => $blog_owner, + copyright => $blog_rights, + language => 'en', ); my $articles = get_articles(); for my $item (@{$articles}) { - $item->{'date'} =~ /(\d{4})\-(\d{2})\-\d{2} \d{2}\:\d{2}\:\d{2}/; - ($item->{'year'}, $item->{'month'}) = ($1, $2); - my $link = sprintf("%s%s/%s/%s", $blog_url, $item->{'year'}, $item->{'month'}, $item->{'uri'}); - - if ($version eq '2.0') { - $rss->add_item ( - title => $item->{'title'}, - link => $link, - description => $item->{'body'}, - author => $item->{'author'}, - comments => "${link}#comments", - pubDate => POSIX::strftime("%a, %d %b %Y %H:%M:%S %z (%Z)", gmtime($item->{'epoch'})), - category => @{[split(/, */, $item->{'tags'})]}, - ); - } else { - $rss->add_item ( - title => $item->{'title'}, - link => $link, - description => $item->{'body'}, - dc => { - subject => $blog_title, - creator => $item->{'author'}, - date => POSIX::strftime("%a, %d %b %Y %H:%M:%S %z (%Z)", gmtime($item->{'epoch'})), - }, - ); - } + my $link = $blog_url . $item->{'year'} . '/' . $item->{'month'} . '/' . $item->{'uri'}; + $rss->add_item ( + title => $item->{'title'}, + link => $link, + description => $item->{'body'}, + author => $item->{'author'}, + comments => "${link}#comments", + pubDate => POSIX::strftime("%a, %d %b %Y %H:%M:%S %z (%Z)", gmtime($item->{'epoch'})), + category => \@{[split(/, */, $item->{'tags'})]}, + ); } print $cgi->header('application/rss+xml'), $rss->as_string; } -sub get_articles { +sub output_sitemap { + my $map = WWW::Google::SiteMap->new; + + $map->pretty(1); + $map->add( + loc => $blog_url, + priority => 1.0, + ); + + $articles_per_page = -1; + my $articles = get_articles(); + for my $item (@{$articles}) { + my $link = $blog_url . $item->{'year'} . '/' . $item->{'month'} . '/' . $item->{'uri'}; + $map->add ( + loc => $link, + priority => 0.8, + ); + + } + + my $tagcloud = get_tag_cloud(); + for my $item (@{$tagcloud}) { + my $link = $blog_url . 'Tags/' . $item->{'tag'}; + $map->add ( + loc => $link, + priority => 0.1 + $item->{'scale'} / 10, + ); + } + print $cgi->header('application/xml'), $map->xml; +} +sub get_articles { my $page = 1; my $offset = 0; my $limit_clause; @@ -186,7 +196,7 @@ sub get_articles { } else { $where_clause .= "\%' AND enabled=1 "; } - } elsif ($cgi->param('search')) { + } elsif ($cgi->param('tag')) { $where_clause .= "WHERE (tags LIKE ? OR author LIKE ?) AND enabled=1 "; $j++; @@ -204,8 +214,8 @@ sub get_articles { if ($j == 3) { $sth->execute($cgi->param('uri')) || die $dbh->errstr; - } elsif ($cgi->param('search')) { - my $search_tag = sprintf("%%%s%%", $cgi->param('search')); + } elsif ($cgi->param('tag')) { + my $search_tag = '%' . $cgi->param('tag') . '%'; $sth->execute($search_tag, $search_tag) || die $dbh->errstr; } elsif ($cgi->param('id')) { $sth->execute($cgi->param('id')) || die $dbh->errstr; @@ -218,7 +228,7 @@ sub get_articles { $result->{'date'} =~ /(\d{4})\-(\d{2})\-\d{2} \d{2}\:\d{2}\:\d{2}/; ($result->{'year'}, $result->{'month'}) = ($1, $2); # cut off readmore if we're on the front page - if (($result->{'body'} =~ //) && ($j < 3) && !($cgi->param('rss'))) { + if (($result->{'body'} =~ //) && ($j < 3) && ($cgi->param('view') ne 'rss')) { $result->{'body'} =~ /(.*)\<\!\-\-readmore\-\-\>/s; $result->{'body'} = $1; $result->{'readmore'}++; @@ -243,11 +253,10 @@ sub get_articles { } sub get_archives { - my %history; my @archives; my @archives_compressed; - my $current_month = $cgi->param('month') || sprintf("%0.2d", ((localtime)[4] + 1)); + my $current_month = $cgi->param('month') || ((localtime)[4] + 1); my $current_year = $cgi->param('year') || ((localtime)[5] + 1900); my %months = ( '01' => 'January', @@ -264,7 +273,7 @@ sub get_archives { '12' => 'December', ); - my $query = 'SELECT * FROM articles WHERE enabled=1 ORDER BY date DESC'; + my $query = 'SELECT title, date, uri FROM articles WHERE enabled=1 ORDER BY date DESC'; my $sth = $dbh->prepare($query); $sth->execute || die $dbh->errstr; while (my $result = $sth->fetchrow_hashref) { @@ -319,7 +328,6 @@ sub get_archives { } sub format_tags { - my $tags = shift; my @tags; @@ -331,7 +339,6 @@ sub format_tags { } sub read_comment { - if ($cgi->param('recaptcha_challenge_field') && $cgi->param('recaptcha_response_field') && $cgi->param('comment') && $cgi->param('id')) { # test our captcha @@ -415,7 +422,6 @@ sub verify_captcha { } sub get_comments { - my %args = @_; my $query = 'SELECT * FROM comments WHERE article_id=? AND enabled=? ORDER BY date ASC'; @@ -429,7 +435,6 @@ sub get_comments { } sub get_tag_cloud { - my $query = 'SELECT tags FROM articles WHERE enabled=1'; my $sth = $dbh->prepare($query); $sth->execute || die $dbh->errstr; @@ -457,5 +462,4 @@ sub get_tag_cloud { } return( \@tag_cloud_data ); - } -- cgit v1.2.3