# Blosxom Plugin: lastread -*- perl -*- # Author: Steve Schwarz # 2007-DEC-23 0.4 Fixed locking # 2007-JUN-21 0.3 Fixed unshifting of arrays... # 2007-JUN-21 0.3 Fixed unshifting of arrays... # 2007-JUN-21 0.2 Ignore non-existent paths # (this will break paths via redirect plugin). # Allow configuring Agents/Referers to ignore # 2007-JUN-21 0.1 initial version. package lastread; # # Plugin to track the last stories directly accessed by users. Maintains a # fixed length list and tries to write disk as infrequently as possible. # # -- Configuration -- # # Name of file holding array of paths and link in order of most recently read # 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/lastread"; # 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; # Ignore all page loads via these agents: @ignore_agents = ('Googlebot', 'Mediapartners', 'Feedfetcher', 'msnbot', 'Yahoo! Slurp', 'Attentio', 'IRLbot', 'Twiceler', 'StackRambler', 'Java', 'Topix.net', 'speedyspider', 'scout', 'moreover', 'voila', 'Technorati', 'kinjabot', 'Magpie', 'Gigabot', 'cazoodle', 'bot.bot', 'voyager', 'yetibot', 'sogou', 'topicblogs', 'fastsearch' ); # use $lastread::links to get the last $num_displayed links # The work is done in the head() you can put $lastread::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) = @_; if (open(FILE, "$blosxom::datadir/$path\.$blosxom::file_extension")) { my $title = ; close(FILE); chomp($title); return "
$title
"; } return ''; } sub head { my($pkg, $path, $head_ref) = @_; eval "require Storable"; if ($@) { print STDERR "lastread disabled, Storable package not available\n"; return 0; } if (!Storable->can('lock_retrieve')) { print STDERR "lastread disabled, Storable::lock_retrieve() not available\n"; return 0; } my @cache; my $cacheref = \@cache; if (-r $file_name) { $cacheref = Storable::lock_retrieve($file_name); } # build the list prior to this page $links = join "", map { @{$_}[1] } @{$cacheref}; # Update file with current path $path = '/' if (!$path); # convert root path to "/" return 0 if !($path =~ /\.$blosxom::flavour$/); # only want flavored paths return 0 if $path eq '/'; # Ignore reads from bots $agent = $ENV{'HTTP_USER_AGENT'}; foreach my $ignore (@ignore_agents) { return 0 if ($agent =~ /$ignore/); } $path =~ s/\.$blosxom::flavour$//; # strip flavour from end of path # Now see if this entry already exists in the list my $num = @{$cacheref}; if ($num == 0) { # no file yet create the array my $link = build_link($path); return 0 if $link eq ''; unshift @{$cacheref}, [$path, $link]; } 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 my $link = build_link($path); return 0 if $link eq ''; unshift @{$cacheref}, [$path, $link]; } } 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;