<?php if (!defined('PmWiki')) exit();

/**	=== Bloge-Feeds ===
 *	Copyright 2009 Eemeli Aro <eemeli@gmail.com>
 *
 *	Blog-like Atom & RSS feeds
 *
 *	Developed and tested using PmWiki 2.2.x
 *
 *	To install, add the following to your configuration file:

		include_once("$FarmD/cookbook/bloge-feeds.php");

 *	Usage: http://example.com/?action=[atom|rss]&[trail|group|link|name]=
 *
 *	This is a part of the Bloge bundle of recipes, but may be used by itself.
 *	For more information, please see the online documentation at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge-Feeds and at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge
 *
 *	This program is free software; 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.
 */

$RecipeInfo['Bloge-Feeds']['Version'] = '2009-08-11';

$FmtPV['$CreateISOTime'] = 'gmstrftime($GLOBALS["TimeISOZFmt"], @$page["ctime"])';
$FmtPV['$UpdatedISOTime'] = 'gmstrftime($GLOBALS["TimeISOZFmt"], max(@$page["ctime"], $page["time"]))';
$FmtPV['$UpdatedRSSTime'] = 'gmdate($GLOBALS["RSSTimeFmt"], max(@$page["ctime"], $page["time"]))';

function BlogeFeedItemText($pagename, &$page, $tag) {
	global $BlogeFeedJump;
	$fullpage = RetrieveAuthPage($pagename, 'read', FALSE, READPAGE_CURRENT);
	if (!$fullpage) return '';
	$text = $fullpage['text'];
	SDV($BlogeFeedJump, 'jump');
	if (!empty($BlogeFeedJump)) $text = preg_replace(
		"/\n[^\n]*\\[\\[#$BlogeFeedJump\\]\\].*$/s",
		"\n\n[[{\$FullName}|$[Continue reading]...]]",
		$text, 1);
	$content = trim(preg_replace(
		array( "! class='(?:vspace|urllink|)'!", "!\n</([a-z]+)>\n?!", "!\b((?:href|src)=['\"])/!" ),
		array( '',                               "</$1>\n",            "$1http://{$_SERVER['SERVER_NAME']}/" ),
		MarkupToHTML($pagename, $text) ));
	return ($tag=='content')
		? "<content type=\"html\"><![CDATA[$content]]></content>\n"
		: "<$tag><![CDATA[$content]]></$tag>\n";
}

SDVA($FeedFmt['atom']['feed'], array(
  'subtitle' => '{$Description}',
  'link' => "<link rel=\"self\" href=\"{\$PageUrl}?action=atom\" />\n",
  'id' => '{$PageUrl}',
  'logo' => ''));
SDVA($FeedFmt['atom']['item'], array(
  'published' => '{$CreateISOTime}',
  'updated' => '{$UpdatedISOTime}',
  'content' => 'BlogeFeedItemText'));

SDVA($FeedFmt['rss']['feed'], array(
  '_start' => "<?xml version=\"1.0\" encoding=\"\$Charset\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
<channel>\n",
  'title' => '$WikiTitle',
  'link' => '{$PageUrl}',
  'atom:link' => "<atom:link rel=\"self\" href=\"{\$PageUrl}?action=rss\" />\n",
  'description' => '{$Description}'));
SDVA($FeedFmt['rss']['item'], array(
  'guid' => '{$PageUrl}',
  'title' => '{$Title}',
  'link' => '{$PageUrl}',
  'description' => 'BlogeFeedItemText',
  'dc:date' => '{$UpdatedISOTime}',
  'pubDate' => '{$UpdatedRSSTime}'));

include_once("$FarmD/scripts/feeds.php");