<?php if (!defined('PmWiki')) exit();
/*  Copyright 2009 Steve Crisp http://www.scoobypedia.co.uk
    This file is a plug-in to PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.

    This script adds georss feed to wiki pages.  This module is typically
    activated from a local configuration file via a line such as

    References:
*/

$RecipeInfo['GeoRss']['Version'] = '20091103';

# Create a combined, space and comma separated Lat/Lng string for use in 'geo:point' output below
$FmtPV['$LatLongPoint'] = 'empty($page["latitude"]) ? "" : ($page["latitude"]." ".@$page["longitude"])';
$FmtPV['$LongCommaLatPoint'] = 'empty($page["latitude"]) ? "" : ($page["longitude"].",".@$page["latitude"])';

## Settings for ?action=georssdc
## Note we add two forms of GeoRSS output
SDVA($FeedFmt['georssdc']['feed'], array(
  '_header' => 'Content-type: text/xml; charset="$Charset"',
  '_start' => '<?xml version="1.0" encoding="$Charset"?'.'>
<!DOCTYPE rdf:RDF PUBLIC "-//DUBLIN CORE//DCMES DTD 2002/07/31//EN"
    "http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss">'."\n",
  '_end' => "</rdf:RDF>\n"));
SDVA($FeedFmt['georssdc']['item'], array(
  '_start' => "<rdf:Description rdf:about=\"{\$PageUrl}\">\n",
  'dc:title' => '{$Title}',
  'dc:identifier' => '{$PageUrl}',
  'dc:date' => '$ItemISOTime',
  'dc:type' => 'Text',
  'dc:format' => 'text/html',
  'dc:description' => '{$Description}',
  'dc:subject' => "<dc:subject>\$Category</dc:subject>\n",
  'dc:publisher' => '$WikiTitle',
  'dc:author' => '{$LastModifiedBy}',
#  'geo:lat' => '{$Latitude}',
#  'geo:long' => '{$Longitude}',
  'georss:point' => '{$LatLongPoint}',
  '_end' => "</rdf:Description>\n"));


## RSS 2.0 settings for ?action=georss
## Note we add two forms of GeoRSS output
SDVA($FeedFmt['georss']['feed'], array(
  '_header' => 'Content-type: text/xml; charset="$Charset"',
  '_start' => '<?xml version="1.0" encoding="$Charset"?'.'>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss">
<channel>'."\n",
  '_end' => "</channel>\n</rss>\n",
  'title' => '$WikiTitle | {$Group} / {$Title}',
  'link' => '{$PageUrl}?action=georss',
  'description' => '{$Group}.{$Title}',
#  'geo:lat'  => '{$Latitude}',
#  'geo:long'  => '{$Longitude}',
  'georss:point' => '{$LatLongPoint}',
  'lastBuildDate' => '$FeedRSSTime'));
SDVA($FeedFmt['georss']['item'], array(
  '_start' => "<item>\n",
  '_end' => "</item>\n",
  'title' => '{$Group} / {$Title}',
  'link' => '{$PageUrl}',
  'description' => '{$Description}',
#  'geo:lat'  => '{$Latitude}',
#  'geo:long'  => '{$Longitude}',
  'georss:point' => '{$LatLongPoint}',
  'dc:contributor' => '{$LastModifiedBy}',
  'dc:date' => '$ItemISOTime',
  'pubDate' => '$ItemRSSTime',
  'enclosure' => 'RSSEnclosure'));

## KML settings for ?action=kml when using pagelists
SDVA($FeedFmt['kml']['feed'], array(
  '_header' => 'Content-type: application/vnd.google-earth.kml+xml',
  '_start' => '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>'."\n",
  '_end' => "</Document>\n</kml>\n"));
SDVA($FeedFmt['kml']['item'], array(
  '_start' => "<Placemark>\n",
  '_end' => "</Placemark>\n",
  'name' => '{$Group} / {$Title}',
  'descripstart' => '<description><![CDATA['.'<a href="{$PageUrl}">Link Back</a>,<br>'.']]>{$Description}',
  'descripend' => '</description>'."\n",
  'pointstart' => '<Point>'."\n",
  'coordinates' => '{$LongCommaLatPoint}',
  'pointend' => '</Point>'."\n"));

## KML settings for ?action=kml when using single page
SDVA($FeedFmt['pagekml']['feed'], array(
  '_header' => 'Content-type: application/vnd.google-earth.kml+xml',
  '_start' => '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>'."\n",
  'placestart' => "<Placemark>\n",
  'name' => '{$Group} / {$Title}',
  'descripstart' => '<description><![CDATA['.'<a href="{$PageUrl}">Link Back</a>,<br>'.']]>{$Description}',
  'descripend' => '</description>'."\n",
  'pointstart' => '<Point>'."\n",
  'coordinates' => '{$LongCommaLatPoint}',
  'pointend' => '</Point>'."\n",
  'placeend' => "</Placemark>\n",
  '_end' => "</Document>\n</kml>\n"));
SDVA($FeedFmt['pagekml']['item'], array());


if ($action == 'georss')  include_once("scripts/feeds.php");  # RSS 2.0
if ($action == 'georssdc') include_once("scripts/feeds.php");  # Dublin Core
if ($action == 'kml')  include_once("$FarmD/cookbook/georssfeeds.php");
if ($action == 'pagekml')  include_once("$FarmD/cookbook/georssfeeds.php");

$FmtPV['$Latitude'] = '@$page["latitude"]'; 
$SaveProperties[] = 'latitude';
$FmtPV['$Longitude'] = '@$page["longitude"]';
$SaveProperties[] = 'longitude';

Markup('coord', 'directives',
 "/\\(:coord\\s+(.*?)\\s+(.*?)\\s*:\\)/ei",
 "PZZ(SetCoord(\$pagename, PSS('$1'), PSS('$2')))");

function SetCoord($pagename, $lat, $long) {
 foreach (array($lat,$long) as $k => $v) {
   if (strstr($v,'=')) {
     $arg = explode('=', $v);
     if ($arg[0]=='lat') $latitude = $arg[1];
     elseif ($arg[0]=='lon') $longitude = $arg[1];
   } else {
     $latitude = $lat;
     $longitude = $long;
   }
 }
 SetProperty($pagename, 'latitude', $latitude, ', ');
 SetProperty($pagename, 'longitude', $longitude, ', ');
}

$HTMLHeaderFmt['georssicbmmeta'] = 'function:GeoRssPrintMetaTags';
function GeoRssPrintMetaTags($pagename, $args) {
  global $PCache;
  $x = str_replace("'", '&#039;', @$PCache[$pagename]["=p_latitude"]);
  $y = str_replace("'", '&#039;', @$PCache[$pagename]["=p_longitude"]);
  print "<meta name='geo.position' content='$x;$y' />\n";
  print "<meta name='ICBM' content='$x, $y' />\n";
}