atom feed support
authorChristian Weiske <cweiske@cweiske.de>
Thu, 16 Apr 2015 16:20:41 +0000 (18:20 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 16 Apr 2015 16:20:41 +0000 (18:20 +0200)
README.rst
www/add-article.php
www/feed.php [new file with mode: 0644]
www/functions.php [new file with mode: 0644]
www/index.php

index 143ea49b2b92f34dad465f402de59b2d4d990f55..8b739572b67a57e030f20821be68c3ef8ded1f9c 100644 (file)
@@ -2,5 +2,5 @@
 PubSubHubbub tester
 *******************
 
-Simulates a blog with an h-feed.
-Sends notifications to the hub when a new article has been created.
+Simulates a blog with an atom and an h-feed.
+Sends a publish notification to the hub when a new article has been created.
index 6e9a03850491ff129354adbb8e3cb9d5911f98f2..5c925cf2f6226f41c873642e3f319018ebffa5e9 100644 (file)
@@ -31,37 +31,41 @@ HTM
 //echo "saved as " . $file . "\n";
 
 //hub-notification
-$params = array(
-    'hub.mode' => 'publish',
-    'hub.url'  => $self,
-);
-$enc = array();
-foreach ($params as $key => $val) {
-    $enc[] = urlencode($key) . '=' . urlencode($val);
-}
-$postMsg = implode('&', $enc);
+$arUrls = array($self, $self . 'feed.php');
+foreach ($arUrls as $url) {
+    $params = array(
+        'hub.mode' => 'publish',
+        'hub.url'  => $url,
+    );
+    $enc = array();
+    foreach ($params as $key => $val) {
+        $enc[] = urlencode($key) . '=' . urlencode($val);
+    }
+    $postMsg = implode('&', $enc);
 
-$ctx = stream_context_create(
-    array(
-        'http' => array(
-            'method' => 'POST',
-            'header' => array(
-                'Content-type: application/x-www-form-urlencoded',
-            ),
-            'content' => $postMsg,
-            'ignore_errors' => true,
+    $ctx = stream_context_create(
+        array(
+            'http' => array(
+                'method' => 'POST',
+                'header' => array(
+                    'Content-type: application/x-www-form-urlencoded',
+                ),
+                'content' => $postMsg,
+                'ignore_errors' => true,
+            )
         )
-    )
-);
+    );
 
-$res = file_get_contents($hub, false, $ctx);
-list($http, $code, $rest) = explode(' ', $http_response_header[0]);
-if (intval($code / 100) === 2) {
-    echo "notified hub\n";
-    header('Location: /');
-    exit();
+    $res = file_get_contents($hub, false, $ctx);
+    list($http, $code, $rest) = explode(' ', $http_response_header[0]);
+    if (intval($code / 100) !== 2) {
+        echo "Error notifying hub: HTTP status was not 2xx; got $code\n";
+        echo $res . "\n";
+        echo 'URL: ' . $url . "\n";
+    } else {
+        //echo "notified hub: $url\n";
+    }
 }
-
-echo "Error notifying hub: HTTP status was not 2xx; got $code\n";
-echo $res . "\n";
+header('Location: /');
+exit();
 ?>
diff --git a/www/feed.php b/www/feed.php
new file mode 100644 (file)
index 0000000..04bead9
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+require_once __DIR__ . '/../data/config.php';
+require_once __DIR__ . '/functions.php';
+header('Content-type: application/atom+xml');
+header('Link: <' . $hub . '>; rel="hub"');
+header('Link: <' . $self . 'feed.php>; rel="self"', false);
+
+$articles = loadArticles();
+reset($articles);
+$lastUpdate = key($articles);
+?>
+<?xml version="1.0" encoding="utf-8" ?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <title>PubSubHubbub tester</title>
+ <author>
+  <name>Someone</name>
+  <email>someone@example.org</email>
+ </author>
+ <link href="<?php echo $self; ?>"/>
+ <link rel="self" href="<?php echo $self; ?>feed.php"/>
+ <link rel="hub" href="<?php echo $hub; ?>"/>
+ <link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" />
+ <id><?php echo $self; ?>feed.php</id>
+ <updated><?php echo date('c', $lastUpdate); ?></updated>
+
+ <?php foreach ($articles as $article) { ?>
+ <entry>
+  <id><?php echo $article->file; ?></id>
+  <title><?php echo htmlspecialchars($article->title); ?></title>
+  <published><?php echo date('c', $article->time); ?></published>
+  <updated><?php echo date('c', $article->time); ?></updated>
+  <summary><?php echo substr($article->content, 0, 100); ?>&#8230;</summary>
+  <content type="xhtml">
+   <div xmlns="http://www.w3.org/1999/xhtml">
+    <?php echo $article->content; ?>
+   </div>
+  </content>
+  <link rel="alternate" type="text/html" href="<?php echo $self . $article->file; ?>" />
+ </entry>
+ <?php } ?>
+</feed>
diff --git a/www/functions.php b/www/functions.php
new file mode 100644 (file)
index 0000000..962012e
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+function loadArticles()
+{
+    $files = glob(__DIR__ . '/articles/*.htm');
+    $articles = array();
+    foreach ($files as $file) {
+        $content = file_get_contents($file);
+        $xml = simplexml_load_string($content);
+        $timestamp = strtotime(basename($file, '.htm'));
+        $articles[$timestamp] = (object) array(
+            'file' => 'articles/' . basename($file),
+            'title' => basename($file, '.htm'),
+            'content' => (string) $xml->body->div,
+            'time' => $timestamp,
+        );
+    }
+    krsort($articles);
+    $articles = array_slice($articles, 0, 10);
+    //FIXME: delete old ones
+    return $articles;
+}
+?>
index 0a205f80943041fc1f3540dd170fd3c00269f390..6a98ad05e37ff6af492657c4bf2b1803808bf22f 100644 (file)
@@ -1,30 +1,20 @@
 <?php
 require_once __DIR__ . '/../data/config.php';
+require_once __DIR__ . '/functions.php';
 header('Link: <' . $hub . '>; rel="hub"');
 header('Link: <' . $self . '>; rel="self"', false);
 
-$files = glob(__DIR__ . '/articles/*.htm');
-$articles = array();
-foreach ($files as $file) {
-    $content = file_get_contents($file);
-    $xml = simplexml_load_string($content);
-    $timestamp = strtotime(basename($file, '.htm'));
-    $articles[$timestamp] = (object) array(
-        'file' => 'articles/' . basename($file),
-        'title' => basename($file, '.htm'),
-        'content' => (string) $xml->body->div,
-        'time' => $timestamp,
-    );
-}
-krsort($articles);
+$articles = loadArticles();
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>PubSubHubbub tester</title>
+  <link rel="alternate" type="application/atom+xml" title="PubSubHubbub tester" href="feed.php" />
  </head>
  <body class="h-feed">
   <p>
    <a href="add-article.php">create new article</a>
+   | <a href="feed.php">atom feed</a>
   </p>
   <h1>Articles</h1>
   <?php foreach ($articles as $article) { ?>