package OpenGuides::RDF::Reader;
use strict;

    use Exporter ();
    $VERSION     = '0.05';
    @ISA         = qw(Exporter);
    @EXPORT      = qw(parse_rdf);
    @EXPORT_OK   = qw(parse_rdf);
    %EXPORT_TAGS = (all => [qw(parse_rdf)]);

use XML::Simple;

sub parse_rdf {
    my $xml = shift;

    my $rdf = XMLin( $xml, 
    	ForceArray => [qw/foaf:based_near dc:subject/ ],
	GroupTags => {'dc:source' => 'rdf:resource',
			'foaf:homepage' => 'rdf:resource' },
	#NumericEscape => 2,

    my $geo;
    my $desc = $rdf->{'rdf:Description'};
    if (ref $desc eq 'ARRAY') {
    	$geo = $desc->[1];
	$desc = $desc->[0];
    else {
    	$geo = $rdf->{'geo:SpatialThing'};
    my %descmap = (
    	username => 'dc:contributor',
	changed => 'dc:date',
	version => 'wiki:version',
	source => 'dc:source',
    my %geomap = (
    	country => 'country',
	city => 'city',
	address => 'address',
	postcode => 'postalCode',
	phone => 'phone',
	fax => 'fax',
	website => 'foaf:homepage',
	opening_hours_text => 'chefmoz:Hours',
	longitude => 'geo:long',
	latitude => 'geo:lat',
	category => 'dc:subject',
	summary => 'dc:description',
    my %out;
    $out{$_} = $desc->{$descmap{$_}} for keys %descmap;
    $out{$_} = $geo->{$geomap{$_}} for keys %geomap;
    $out{locale} = [ map {$_->{'wn:Neighborhood'}{'foaf:name'}}
    	@{$geo->{'foaf:based_near'}} ] if exists $geo->{'foaf:based_near'};


=head1 NAME

OpenGuides::RDF::Reader - Parse and return OpenGuides metadata from RDF


  use OpenGuides::RDF::Reader;
  use WWW::Mechanize;
  my $agent = WWW::Mechanize->new;
  my %metadata = parse_rdf($agent->content);


The L<OpenGuides> software deliberately exposes data collected on the town wiki
sites, making it available to other websites as RDF / XML. This functionality is
provided by the module L<OpenGuides::RDF> supplied in the OpenGuides distribution.

What OpenGuides::RDF::Reader does is the reverse process, i.e. take XML RDF data
and turn it back into a hash with keys comprising the metadata fields in OpenGuides.
The main use of this is for guide replication. 

=head2 parse_rdf

This exported subroutine takes a string containing RDF and returns a list of
metadata key value pairs.

=head1 BUGS

Please report any bugs in this module using or posting to
bugs-openguides-rdf-reader (at)

=head1 SUPPORT

For discussion of all matters relating to OpenGuides, there is a mailing list

=head1 AUTHOR

	Ivor Williams
	ivorw-openguides (at)


This program is free software licensed under the...

	The General Public License (GPL)
	Version 2, June 1991

The full text of the license can be found in the
LICENSE file included with this module.

=head1 SEE ALSO



# The preceding line will help the module return a true value