# Blosxom Plugin: lastcommented -*- perl -*-
# Author: Steve Schwarz
# 2008-FEB-29 0.4 Added param 'lastcommentedremove' to
# remove entry by index (base zero)
# 2007-DEC-23 0.3 Fixed locking
# 2007-JUN-23 0.2 Corrected unshift for duplicate entry case
# 2007-JUN-21 0.1 initial version.
package lastcommented;
#
# Plugin to track the last stories commented by users. Maintains a
# fixed length list and tries to write disk as infrequently as possible.
# Requires your writeback/comment plugin to provide a variable set
# whenever a comment is left. See sub story() below and customize as
# necessary.
#
# Invoke any page with ?lastcommentremove=0 to remove an entry from
# the displayed list. This functionality is disabled by the
# $lastcommentremove setting in the configuration below.
#
# -- Configuration --
#
# Name of file holding array of paths and link in order of most recently commented
# for a single story. Index pages are ignored.
#
# This file is created automatically in your plugins' state directory:
my $file_name = "$blosxom::plugin_state_dir/lastcommented";
# How many articles to maintain in the list:
my $num_displayed = 10;
# What flavour should be used in the generated link:
my $link_flavour = "html";
# Control if links are updated when an already existing page is requested.
# set to 1 to move existing links in the list to the first position.
# set to 0 to leave links as they are - lowest performance cost.
my $reorder = 1;
# Do you allow removal of comments from the url?
my $lastcommentremove = 0; # set to 1 to enable
# use $lastcommented::links to get the last $num_displayed links
# The work is done in the head() you can put $lastcommented::links in your head.flavour file
$links = "";
# See sub build_link if you want to modify the generated HTML.
#
# -------------------
use CGI qw/:standard/;
sub start {
# Add filters here to not increment for specific urls or flavours
# This filter ignores the rss, atom, trackback feed variants I provide
return (($blosxom::flavour =~ /rss/ or
$blosxom::flavour eq 'atom' or
$blosxom::flavour eq 'trackback') ? 0 : 1);
}
sub build_link {
my ($path, $title) = @_;
return "
";
}
my @cache;
my $cacheref = \@cache;
sub head {
eval "require Storable";
if ($@) {
print STDERR "lastcommented disabled, Storable package not available\n";
return 0;
}
if (!Storable->can('lock_retrieve')) {
print STDERR "lastcommented disabled, Storable::lock_retrieve() not available\n";
return 0;
}
if (-r $file_name) {
$cacheref = Storable::lock_retrieve($file_name);
}
if (defined CGI::param('lastcommentremove') && $lastcommentremove != 0) {
# remove the supplied argument
splice(@{$cacheref}, CGI::param('lastcommentremove'), 1);
Storable::lock_nstore($cacheref, $file_name);
}
# build the list prior to this page
$links = join "", map { @{$_}[1] } @{$cacheref};
return 1;
}
sub story {
my($pkg, $dir, $filename, $story_ref, $title_ref, $body_ref) = @_;
# Put your writeback plugin here and change the variables as necessary
return 0 if !(defined $writeback::postoccurred and $writeback::postoccurred == 1);
# Update file with current path
my $path = "$dir/$filename";
# Now see if this entry already exists in the list
my $num = @{$cacheref};
if ($num == 0) {
# no file yet create the array
unshift @{$cacheref}, [$path, build_link($path, $$title_ref)];
} elsif ($cacheref->[0][0] ne $path) {
my $modified = 0;
for my $i (0..@{$cacheref}-1) {
if ($cacheref->[$i][0] eq $path) {
return 0 if ($reorder != 1);
# remove from current location and we'll add back to front
my $old_entry = $cacheref->[$i];
splice(@{$cacheref}, $i, 1);
unshift(@{$cacheref}, [@{$old_entry}]);
$modified = 1;
last; # should only be in list once
}
}
if ($modified == 0) {
# push new path into first place in array
unshift @{$cacheref}, [$path, build_link($path, $$title_ref)];
}
} else {
# duplicate request nothing modified
return 0;
}
# slice to desired length
@{$cacheref} = (@$cacheref)[0..($num_displayed-1)];
# Store in network order to allow editing/transporting to/from different platforms
Storable::lock_nstore($cacheref, $file_name);
return 1;
}
1;
__END__
=head1 LICENSE
This Blosxom Plug-in
Copyright 2007, Steve Schwarz
(This license is the same as Blosxom's)
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
1;