# foafroll.pl
#   Copyright 2004, Daiji Hirata All Right Reserved.
#

use XML::FOAF;

# my $url = 'http://hirata.typepad.com/foaf.rdf';
# my $url = 'http://uva.jp/dh/mt/foaf.rdf';
my $url;
my $URL_CHECK_UPDATE = 'http://ping.bloggers.jp/update?url=';
my $TIME_CHECK_UPDATE = 60*60*24;

MT::Template::Context->add_container_tag( FOAFRoll => \&foafroll );
MT::Template::Context->add_tag( FOAFRollName => \&foafroll_name );
MT::Template::Context->add_tag( FOAFRollURL => \&foafroll_url );
MT::Template::Context->add_conditional_tag( FOAFRollIfUpdate => \&foafroll_update );


sub foafroll {
    my ($ctx, $args, $cond) = @_;

    if ($args->{url}) {
        $url = $args->{url};
    }
    my $foaf = XML::FOAF->new(URI->new($url));
    unless ($foaf) {
        return $ctx->error(XML::FOAF->errstr);
    }

    my $builder = $ctx->stash('builder');
    my $tokens = $ctx->stash('tokens');
    my $out;
    if ($args->{update}) {
        $ctx->stash('_foafroll_update', $args->{update});
    }
    my $people = $foaf->person->knows;

    foreach my $person (@{$people}) {
        $ctx->stash('_foafroll_person', $person);
        my $o = $builder->build($ctx, $tokens, $cond);
        return $ctx->error($builder->errstr) unless defined $o;
        $out .= $o if $o;
    }
    return $out;
}
        
sub foafroll_name {
    my ($ctx, $args, $cond) = @_;
    my $person = $ctx->stash('_foafroll_person');
    unless ($person) {
        return undef;
    }
    $person->name;
}

sub foafroll_url {
    my ($ctx, $args, $cond) = @_;
    my $person = $ctx->stash('_foafroll_person');
    unless ($person) {
        return undef;
    }
    $person->homepage;
}

sub foafroll_update {
    my ($ctx, $args, $cond) = @_;
    my $person = $ctx->stash('_foafroll_person');
    my $update = $ctx->stash('_foafroll_update') || $TIME_CHECK_UPDATE;
    my $url = $person->homepage;
    if ($args->{update}) {
        $update = $args->{update};
    }
    require LWP::UserAgent;
    my $ua = LWP::UserAgent->new();
    my $res = $ua->get($URL_CHECK_UPDATE . $url);
    my $elapsed;
    if ($res->is_success) {
        require XML::Simple;
        my $xs = XMLin($res->content);
        $elapsed = $xs->{weblog}->{elapsed};
    }
    if ($elapsed && $elapsed < $update ) {
        return 1;
    }
    return 0;
}

1;

__END__

=head1 NAME

foafroll.pl - A Movable Type plugin to create 'blogroll' from FOAF

=head1 SYNOPSIS

    <MTFOAFRoll url="http://uva.jp/dh/mt/foaf.rdf" update="86400">
      <$MTFOAFRollName$>: <$MTFOAFRollURL$> <MTFOAFRollIfUpdate>new</MTFOAFRollIfUpdate><br />
    </MTFOAFRoll>


=head1 USAGE

=head2 Container Tags

<MTFOAFRoll> Loop for knows people.  Below two arguments are available.

=over 4 

=item * url

You must set the FOAF url like:
    <MTFOAFRoll url="http://uva.jp/dh/mt/foaf.rdf">
The FOAF file must be valid and have knows elements.

=item * update    

You may set the second to update.
    <MTFOAFRoll url="http://uva.jp/dh/mt/foaf.rdf" update="86400">
<MTFOAFRollIfUpdate> uses the second.

=back

=head2 Other Tags

=over 4

=item * <MTFOAFRollName>

Name of friend.

=item * <MTFOAFRollURL>

URL of the homepage of friend.

=item * <MTFOAFRollIfUpdate>

Condition Tag whether the homepage of friend is updated or not.

=back

=cut
