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.
//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();
?>
--- /dev/null
+<?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>
+ </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); ?>…</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>
--- /dev/null
+<?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;
+}
+?>
<?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) { ?>