public/rss: implement JSON output format

This commit is contained in:
Andrew Dolgov 2012-09-10 11:19:38 +04:00
parent 46b781491b
commit 2ebf38a9bd

View file

@ -2,7 +2,7 @@
class Handler_Public extends Handler { class Handler_Public extends Handler {
private function generate_syndicated_feed($owner_uid, $feed, $is_cat, private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
$limit, $search, $search_mode, $match_on, $view_mode = false) { $limit, $search, $search_mode, $match_on, $view_mode = false, $format = 'atom') {
require_once "lib/MiniTemplator.class.php"; require_once "lib/MiniTemplator.class.php";
@ -34,6 +34,7 @@ class Handler_Public extends Handler {
if (!$feed_site_url) $feed_site_url = get_self_url_prefix(); if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
if ($format == 'atom') {
$tpl = new MiniTemplator; $tpl = new MiniTemplator;
$tpl->readTemplateFromFile("templates/generated_feed.txt"); $tpl->readTemplateFromFile("templates/generated_feed.txt");
@ -101,7 +102,73 @@ class Handler_Public extends Handler {
$tpl->addBlock('feed'); $tpl->addBlock('feed');
$tpl->generateOutputToString($tmp); $tpl->generateOutputToString($tmp);
header("Content-Type: text/xml; charset=utf-8");
print $tmp; print $tmp;
} else if ($format == 'json') {
$feed = array();
$feed['title'] = $feed_title;
$feed['version'] = VERSION;
$feed['feed_url'] = $feed_self_url;
if (PUBSUBHUBBUB_HUB && $feed == -2) {
$feed['hub_url'] = PUBSUBHUBBUB_HUB;
}
$feed['self_url'] = get_self_url_prefix();
$feed['articles'] = array();
while ($line = db_fetch_assoc($result)) {
$article = array();
$article['id'] = $line['link'];
$article['link'] = $line['link'];
$article['title'] = $line['title'];
$article['excerpt'] = truncate_string(strip_tags($line["content_preview"]), 100, '...');
$article['content'] = sanitize($this->link, $line["content_preview"], false, $owner_uid);
$article['updated'] = date('c', strtotime($line["updated"]));
if ($line['note']) $article['note'] = $line['note'];
if ($article['author']) $article['author'] = $line['author'];
$tags = get_article_tags($this->link, $line["id"], $owner_uid);
if (count($tags) > 0) {
$article['tags'] = array();
foreach ($tags as $tag) {
array_push($article['tags'], $tag);
}
}
$enclosures = get_article_enclosures($this->link, $line["id"]);
if (count($enclosures) > 0) {
$article['enclosures'] = array();
foreach ($enclosures as $e) {
$type = $e['content_type'];
$url = $e['content_url'];
$length = $e['duration'];
array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length));
}
}
array_push($feed['articles'], $article);
}
header("Content-Type: text/plain; charset=utf-8");
print json_encode($feed);
} else {
header("Content-Type: text/plain; charset=utf-8");
print json_encode(array("error" => array("message" => "Unknown format")));
}
} }
function getUnread() { function getUnread() {
@ -267,8 +334,6 @@ class Handler_Public extends Handler {
} }
function rss() { function rss() {
header("Content-Type: text/xml; charset=utf-8");
$feed = db_escape_string($_REQUEST["id"]); $feed = db_escape_string($_REQUEST["id"]);
$key = db_escape_string($_REQUEST["key"]); $key = db_escape_string($_REQUEST["key"]);
$is_cat = $_REQUEST["is_cat"] != false; $is_cat = $_REQUEST["is_cat"] != false;
@ -279,6 +344,10 @@ class Handler_Public extends Handler {
$search_mode = db_escape_string($_REQUEST["smode"]); $search_mode = db_escape_string($_REQUEST["smode"]);
$view_mode = db_escape_string($_REQUEST["view-mode"]); $view_mode = db_escape_string($_REQUEST["view-mode"]);
$format = db_escape_string($_REQUEST['format']);
if (!$format) $format = 'atom';
if (SINGLE_USER_MODE) { if (SINGLE_USER_MODE) {
authenticate_user($this->link, "admin", null); authenticate_user($this->link, "admin", null);
} }
@ -295,7 +364,7 @@ class Handler_Public extends Handler {
if ($owner_id) { if ($owner_id) {
$this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit, $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
$search, $search_mode, $match_on, $view_mode); $search, $search_mode, $match_on, $view_mode, $format);
} else { } else {
header('HTTP/1.1 403 Forbidden'); header('HTTP/1.1 403 Forbidden');
} }