#!/usr/bin/perl
# Blosxom Plugin: entries_index_tagged
# Author(s): Eric Sherman <enkidu@primitiveworker.org>
#            Rael Dornfest <rael@oreilly.com> 
# Version: 2003-05-18 (v0.2)
# Documentation: See the bottom of this file or type: perldoc entries_index_tagged
#
# This plugin replaces the default blosxom entries function, and Rael's entries_index plugin.

# TODO make a switch which defaults to off for the additional modules, update the docs in this file

package entries_index_tagged;

# --- Configurable variables -----
# --------------------------------

# the tag that specifies the creation date of this story
$timestamp_tag = "meta-creation_timestamp:" unless defined $timestamp_tag;
# another tag which does the same as the above, except is human writable
# in a format compatible with Time::ParseDate , e.g., meta-creation_date: 8/27/2005 16:23:30
$date_tag = "meta-creation_date:" unless defined $date_tag;

use File::stat;
use File::Find;
use Data::Dumper;
use CGI qw/:standard/;

use Time::ParseDate;

sub start {
	1;
}

sub entries {
	return sub {
		my(%files, %indexes);

		if ( open ENTRIES, "$blosxom::plugin_state_dir/.entries_index_tagged.index" ) {
			my $index = join '', <ENTRIES>;
			close ENTRIES;
			$index =~ /\$VAR1 = \{/ and eval($index) and !$@ and %files = %$VAR1;
		}

		for my $file (keys %files) { -f $file or do { $reindex++; delete $files{$file} }; }

		find(
			sub {
				my $d; 
				my $curr_depth = $File::Find::dir =~ tr[/][]; 
				if ( $blosxom::depth and $curr_depth > $blosxom::depth ) {
					$files{$File::Find::name} and delete $files{$File::Find::name};
					return;
				}

				$File::Find::name =~ m!^$blosxom::datadir/(?:(.*)/)?(.+)\.$blosxom::file_extension$!
					and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name)
				# to show or not to show future entries
					and (
					$blosxom::show_future_entries
						or stat($File::Find::name)->mtime <= time
				) 
					and ( $files{$File::Find::name} || ++$reindex )
					and ( $files{$File::Find::name} = $files{$File::Find::name} || decodeDate($File::Find::name) || stat($File::Find::name)->mtime )
			
				# Static
					and (
					param('-all') 
						or !-f "$blosxom::static_dir/$1/index." . $blosxom::static_flavours[0]
						or stat("$blosxom::static_dir/$1/index." . $blosxom::static_flavours[0])->mtime < stat($File::Find::name)->mtime
				)
					and $indexes{$1} = 1
					and $d = join('/', (blosxom::nice_date($files{$File::Find::name}))[5,2,3])
					and $indexes{$d} = $d
					and $blosxom::static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$blosxom::file_extension" } = 1;
			}, $blosxom::datadir
		);

		if ( $reindex ) {
			if ( open ENTRIES, "> $blosxom::plugin_state_dir/.entries_index_tagged.index" ) {
				print ENTRIES Dumper \%files;
				close ENTRIES;
			} else {
				warn "couldn't > $blosxom::plugin_state_dir/.entries_index_tagged.index: $!\n";
			}
		}

		return (\%files, \%indexes);
	};

}

# returns a unix-style mtime from a tag within the file
sub decodeDate {
	my ($thisFile) = $_;
	my $mtime = 0;

	open (FILE, $thisFile); 
	my @lines = <FILE>;	 
	close (FILE);

	# read the header and return the first time tag found
	foreach my $thisLine (@lines) {

		# the timestamp tag takes precedence if both are present
 
		# grab the mtime from the timestamp tag if it exists
		if ($thisLine =~ /^$timestamp_tag\s+.*$/) {
			($mtime) = $thisLine =~ m/^$timestamp_tag\s+([0-9]+)$/;
			return $mtime;
		}
		
		# grab the mtime from the date tag if it exists
		if ($thisLine =~ /^$date_tag\s+.*$/) {
			my ($dateString) = $thisLine =~ m/^$date_tag\s+(.*)$/;
			$mtime = parsedate($dateString);
			return $mtime;
		}
		
		# stop reading this file when we get to the end of the header
		# (the first blank line)
		last if $thisLine =~ /^\s*$/;
	}

	# no tag
	return 0;
}

1;

__END__

=head1 NAME

Blosxom Plug-in: entries_index_tagged

=head1 SYNOPSIS

Purpose: Preserves original creation timestamp on weblog entries, 
allowing for editing of entries without altering the original 
creation time.

Maintains an index ($blosxom::plugin_state_dir/.entries_index.index) of 
filenames and their creation times.  Adds new entries to the index 
the first time Blosxom encounters them (read: is run after their 
creation).

This is a hack based on Rael's original entries_index.  This version
will set the date based on a meta tag, if found.  By default, the tag is
called meta-creation_timestamp.  It is recommended that you also install
the meta plugin so that the tag will not be displayed with the story.

The tag should be set to an mtime-style integer.  For instance, 
meta-creation_timestamp: 105114630 corresponds with April 23, 2003 9:05pm.
You may create this timestamp manually or with the encodeBlogDates perl 
script designed for this purpose (see below).  

This functionality is useful if you upload your stories to your host via a
method that does not preserve the original file's mtime, like FTP.  

Replaces the default $blosxom::entries subroutine, and consequently
Rael's entries_index plugin.  In other words, don't use entries_index if
you intend to use this plugin.

=head1 VERSION

2004-05-18 (v0.2)

=head1 AUTHORS

Eric Sherman (decodeDate) <enkidu@primitiveworker.org>, http://primitiveworker.org
Rael Dornfest (initial entries_index code) <rael@oreilly.com>, http://www.raelity.org/

=head1 SEE ALSO

encodeBlogDates script: http://primitiveworker.org/files/development/blosxom/entries_index_tagged/encodeBlogDates
entries_index plugin: http://www.raelity.org/apps/blosxom/plugins/indexing/entries_index.individual
meta plugin: http://www.raelity.org/apps/blosxom/plugins/meta/meta.individual
this plugin: http://primitiveworker.org/blo.g/development/blosxom/entries_index_tagged/
Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml

=head1 BUGS

Address bug reports and comments to the Blosxom mailing list 
[http://www.yahoogroups.com/group/blosxom].

=head1 LICENSE

this Blosxom Plug-in
Copyright 2003, Eric Sherman

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.
