# MKDoc RSS 1.00 Headline Plugin
# Copyright Chris Croome, <chris@mkdoc.com>, 13 April 2001
# This script is released under the Perl Artistic Licence
#
# create an RSS 1.00 file
# (http://purl.org/rss/1.0/)
# using XML::RSS (http://search.cpan.org/search?dist=XML-RSS)
#
# ./rss100headline.rdf
#   max 15 children, listed by date modified, non-nested, infinite depth
#     documents fields; uri, title
package flo::plugin::XML::RDF::RSS::100::Headline;
use strict;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
use XML::RSS;
use lib::util::Text2Html;
use flo::Template::ISODate;
use lib::cgi::CGI;
use flo::Object;
use flo::CGI;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;

sub main
{
    my $class = shift;
    my $cgi = lib::cgi::CGI->get;
    
    # if this module is not concerned, let's return ASAP
    my $config = $class->config;
    my $http_name = $config->{http_name};
    
    my $q_http_name = quotemeta ($http_name);
    my $raw_path_info = raw_path_info;
    return unless ( ($raw_path_info =~ /.*\/$q_http_name$/o) );

    # makes all uri's absolute
    $flo::CGI::FULL_URI = 1;
    
    # otherwise, let's process the request
    $raw_path_info =~ s/$q_http_name$//;
    my $path_info = $raw_path_info;
    
    # retrieves the current document
    my $document_table = table ('Document');
    my $current_document = $document_table->get (Full_Path => $path_info);
    return unless (defined $current_document);
    
    # what is the ID of this document ?
    my $current_document_id = $current_document->{ID};

    # this stuff is to get the URI of the current document
    my $cgiz = $cgi->new;
    $cgiz->path_info ($current_document->{Full_Path});
    $current_document->{URI} = $cgiz->self_url;
    
    # We need the name of the creator of the current document
    my $editor_table = table ('Editor');
    my $editor = $editor_table->get ( ID => $current_document->{Editor_Created_ID});
    my $current_document_editor = text2xml (($editor->{Family_Name}) . ", " . ($editor->{First_Name})); 

    # Subject needs splitting with ; not ,
    my $current_document_subject = text2xml ($current_document->{Keywords});
    $current_document_subject =~ s/,/;/g;
    
    # Create a RSS 1.0 output
    my $rss = new XML::RSS (version => '1.0');

    # Munge the MySQL date into the W3C format (http://www.w3.org/TR/NOTE-datetime)
    # this should really be done some better way... but it works...
    my $doc_date = ($current_document->{Date_Created});
    $doc_date   =~ s/ /T/;
    $doc_date   =~ s/$/Z/;
    
    $rss->channel (
		   title       => text2xml ($current_document->{Title}),
		   link        => $current_document->{URI},
		   description => text2xml ($current_document->{Description}),
		   dc => {
		       date       => $doc_date, 
		       subject    => $current_document_subject, 
                       creator    => $current_document_editor,
		       publisher  => text2xml ($current_document->{Publisher}),
		       rights     => text2xml ($current_document->{Rights}),
		       language   => text2xml ($current_document->{Lang}),
		   },
		   syn => {
		       updatePeriod     => "daily", # values: hourly, daily, weekly, monthly, yearly
		       updateFrequency  => "1",
		       updateBase       => "2001-04-13T12:00Z",
		   },
		   );
    
    $rss->textinput(
		    title        => "Search",
		    description  => "Use this form to search " . text2xml ($current_document->{Title}),
		    name         => "q",
		    link         => "$current_document->{URI}" . "search.html",
		    );
    
    # This can be used for a RSS image
    #       $rss->image(
    #                          title  => text2xml ($current_document->{Title}),
    #                          url    => "http://mkdoc.com/images/rss.gif",
    #                          link   => $current_document->{URI},
    #       );
    
    # we want all documents the Parent_ID of which is current's document ID
    # my $cond = { Parent_ID => $current_document_id };
    
    # get all documents that are decendants of the current document
    use lib::sql::Condition;
    my $cond = new lib::sql::Condition;
    $cond->add ('Full_Path', 'LIKE', $path_info . '_%');
    my $children_query = $document_table->select
	(
 	 cols   => [ qw /ID Title Full_Path Description Keywords Date_Created Editor_Created_ID Publisher Rights Lang/ ],
	 # cols   =>  '*', #use this for all
	 sort   => [ qw/Date_Created/ ],
	 desc   => 1,
	 page   => [ 1, 15 ], # max 15 documents
	 where  => $cond
	 );
    # This is for the document creator
    my $headline_creator_table = table ('Editor');
    
    # retrieve the results in an array.
    # Now, each element of @children_document is a hash reference which has
    # table attribute name as keys and record field values as values
    my @children_document = $children_query->fetch_all;
    my $cgix = $cgi->new;
    
    foreach my $headline (@children_document) {
	
        # This is for getting the URI
        $cgix->path_info ($headline->{Full_Path});
        $headline->{URI} = $cgix->self_url;

        # This is for the document creator
        my $headline_creator = $headline_creator_table->get ( ID => $headline->{Editor_Created_ID});
        my $headline_document_editor = text2xml (($headline_creator->{Family_Name}) . ", " . ($headline_creator->{First_Name}));
 
        # Subject needs splitting with ; not ,
        my $headline_subject = text2xml ($headline->{Keywords});
        $headline_subject =~ s/,/;/g; 

        # Munge the MySQL date into the W3C format (http://www.w3.org/TR/NOTE-datetime)
        # this should really be done some better way... but it works...
        my $headline_date = ($headline->{Date_Created});
        $headline_date    =~ s/ /T/;
        $headline_date    =~ s/$/Z/;

        $rss->add_item
         (
          title       => text2xml ($headline->{Title}),
          link        => text2xml ($headline->{URI}),
          description => text2xml ($headline->{Description}),
          dc => {
           subject    => $headline_subject, 
           date       => $headline_date, 
           publisher  => text2xml ($headline->{Publisher}),
           rights     => text2xml ($headline->{Rights}),
           language   => text2xml ($headline->{Lang}),
           creator    => $headline_document_editor,
          },

         );
    }

    print "Content-Type: application/xml; charset=ISO-8859-1\n\n";
#    print "Content-Type: text/plain; charset=ISO-8859-1\n\n";

    print $rss->as_string;

#    print Dumper ($current_document) . "\n";

    return 'TERMINATE';


}


sub full_title
{
    my $document = shift;
    my $document_table = table ('Document');

    my $title = $document->{Title};
    while ($document->{Parent_ID})
    {
        $document = $document_table->get ( ID => $document->{Parent_ID} );
        $title = $document->{Title} . ':' . $title;
    }
    return $title;
}


1;

