Implemented “Top 25″ feature…
Posted : November 11, 2004 at 6:11 pm [America/Los_Angeles]

Simply put, it displays the “Top 25 Most Viewed Entries” on this blog. As I always do, in the spirit of sharing, here’s the Perl code that pulled off the trick. If you’ve a PHP solution that you would like to share, let me know:
#!/usr/bin/perl -w
use strict;
my($MT_DIR);
BEGIN {
if ($0 =~ m!(.*[/\])!) {
$MT_DIR = $1;
} else {
$MT_DIR = ‘./’;
}
unshift @INC, $MT_DIR . ‘lib’;
unshift @INC, $MT_DIR . ‘extlib’;
}
use DBI;
use Template;
use CGI;
use MT;
use MT::Entry;
use Date::Manip;
use vars qw($dbh $tt %data);
# Flush STDOUT
$|=1;
# Start the return content-type
print “Content-type: text/htmlnn”;
# Get the CGI instance set up and read in the values
my ($cgi) = new CGI();
#my ($pids) = $cgi->param(’valid_pids’);
# Business logic
&initialize($MT_DIR);
my $entries_ref = &getPopularEntries();
my $body;
my @all = ();
my $index = 1;
foreach my $entry (@$entries_ref) {
my ($title, $created_on, $category_label, $permalink) = &getBlogDetails($entry->{‘id’});
my $date = &ParseDateString($created_on);
if (! $date) {
$date = “N/A”;
}
$created_on = &UnixDate($date, “At %T on %b %e, %Y (%Z)”);
@all = (@all, {index => $index,
id => $entry->{‘id’},
title => $title,
created_on => $created_on,
category_label => $category_label,
permalink => $permalink,
count => $entry->{‘count’}
});
$index++;
}
$data{‘all’} = @all;
# Send the response back to the client
my $tmpl_filename = “index.html”;
$tt->process(“$tmpl_filename”, %data) ||
&cgiDie(“Template process failed: “, $tt->error(), “n”);
# Cleanup
&cleanup();
######################################################################
# Initialize DB
#######################################################################
sub initialize() {
my($Dir) = @_ if @_;
# Get the template instantiated and ready to go
$tt = Template->new({
INCLUDE_PATH => “$Dir” . “html_templates”,
INTERPOLATE => 0,
}) || &cgiDie(“$Template::ERRORn”);
%data = (title => ‘Most Viewed Entries’);
# Initialize connection to MySQL’s mt database
$dbh = DBI->connect(“DBI:mysql:host=localhost;database=<your-db-name>”,
“<your-userid>”,“<your-password>”,
{PrintError=>0,
RaiseError=>1,
AutoCommit=>1});
}
######################################################################
# Get Blog Details given an ID
#######################################################################
sub getBlogDetails() {
my ($blog_id) = @_ if @_;
my $mt = MT->new;
my $entry = MT::Entry->load($blog_id);
my $category = $entry->category;
my ($title, $created_on, $category_label, $permalink) = ($entry->title, $entry->created_on,
$category->label, $entry->permalink);
return ($title, $created_on, $category_label, $permalink);
}
######################################################################
# Get top 25 most popular entries
#######################################################################
sub getPopularEntries() {
my ($mysql) = “SELECT entry_id, count(entry_id) “;
$mysql .= “FROM mt_entryhits “;
$mysql .= “GROUP BY entry_id “;
$mysql .= “ORDER BY 2 desc “;
$mysql .= “LIMIT 0,24″;
my $sth;
$sth = $dbh->prepare(“$mysql”);
$sth->execute();
my @entries = ();
my @row_rec;
while(@row_rec = $sth->fetchrow_array()) {
@entries = (@entries, { id => $row_rec[0], count => $row_rec[1] });
}
# Release the statement handle
$sth->finish;
return @entries;
}
#######################################################################################
# Perform DB cleanups
#######################################################################################
sub cleanup {
if(defined($dbh)) {
$dbh->disconnect();
}
}
Enjoy!
- Anand
Category: Application Development
Viewed: 1046 times
1 Comment
A final test of the new CAPTCHA (spam prevention) feature..
Posted by: Anand Sharma at November 12, 2004 @ 12:32 pm