Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Pubsubhub to a default plugin #620

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions application/FeedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ class FeedBuilder
*/
protected $hideDates;

/**
* @var string PubSub hub URL.
*/
protected $pubsubhubUrl;

/**
* @var string server locale.
*/
Expand Down Expand Up @@ -120,7 +115,6 @@ public function buildData()
}

$data['language'] = $this->getTypeLanguage();
$data['pubsubhub_url'] = $this->pubsubhubUrl;
$data['last_update'] = $this->getLatestDateFormatted();
$data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
// Remove leading slash from REQUEST_URI.
Expand Down Expand Up @@ -182,16 +176,6 @@ protected function buildItem($link, $pageaddr)
return $link;
}

/**
* Assign PubSub hub URL.
*
* @param string $pubsubhubUrl PubSub hub url.
*/
public function setPubsubhubUrl($pubsubhubUrl)
{
$this->pubsubhubUrl = $pubsubhubUrl;
}

/**
* Set this to true to use permalinks instead of direct links.
*
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.5",
"shaarli/netscape-bookmark-parser": "1.*",
"erusev/parsedown": "1.6",
"slim/slim": "^3.0"
"slim/slim": "^3.0",
"pubsubhubbub/publisher": "dev-master"
},
"require-dev": {
"phpmd/phpmd" : "@stable",
Expand Down
5 changes: 0 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,6 @@ function renderPage($conf, $pluginManager, $LINKSDB)
$feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0)));
$feedGenerator->setHideDates($conf->get('privacy.hide_timestamps') && !isLoggedIn());
$feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$conf->get('feed.rss_permalinks'));
$pshUrl = $conf->get('config.PUBSUBHUB_URL');
if (!empty($pshUrl)) {
$feedGenerator->setPubsubhubUrl($pshUrl);
}
$data = $feedGenerator->buildData();

// Process plugin hook.
Expand Down Expand Up @@ -1289,7 +1285,6 @@ function renderPage($conf, $pluginManager, $LINKSDB)

$LINKSDB[$id] = $link;
$LINKSDB->save($conf->get('resource.page_cache'));
pubsubhub($conf);

// If we are called from the bookmarklet, we must close the popup:
if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) {
Expand Down
20 changes: 20 additions & 0 deletions plugins/pubsubhubbub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# PubSubHubbub plugin

Enable this plugin to notify a Hub everytime you add or edit a link.

This allow hub subcribers to receive update notifications in real time,
which is useful for feed syndication service which supports PubSubHubbub.

## Public Hub

By default, Shaarli will use [Google's public hub](http://pubsubhubbub.appspot.com/).

[Here](https://github.com/pubsubhubbub/PubSubHubbub/wiki/Hubs) is a list of public hubs.

You can also host your own PubSubHubbub server implementation, such as [phubb](https://github.com/cweiske/phubb).

## cURL

While there is a fallback function to notify the hub, it's recommended that
you have PHP cURL extension enabled to use this plugin.

1 change: 1 addition & 0 deletions plugins/pubsubhubbub/hub.atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel="hub" href="%s" />
1 change: 1 addition & 0 deletions plugins/pubsubhubbub/hub.rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<atom:link rel="hub" href="%s" />
2 changes: 2 additions & 0 deletions plugins/pubsubhubbub/pubsubhubbub.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description="Enable PubSubHubbub feed publishing."
parameters="PUBSUBHUB_URL"
101 changes: 101 additions & 0 deletions plugins/pubsubhubbub/pubsubhubbub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* PubSubHubbub plugin.
*
* PubSub is a protocol which fasten up RSS fetching:
* - Every time a new link is posted, Shaarli notify the hub.
* - The hub notify all feed subscribers that a new link has been posted.
* - Subscribers retrieve the new link.
*/

use pubsubhubbub\publisher\Publisher;

/**
* Plugin init function - set the hub to the default appspot one.
*
* @param ConfigManager $conf instance.
*/
function pubsubhubbub_init($conf)
{
$hub = $conf->get('plugins.PUBSUBHUB_URL');
if (empty($hub)) {
// Default hub.
$conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
}
}


/**
* Render feed hook.
* Adds the hub URL in ATOM and RSS feed.
*
* @param array $data Template data.
* @param ConfigManager $conf instance.
*
* @return array updated template data.
*/
function hook_pubsubhubbub_render_feed($data, $conf)
{
$feedType = $data['_PAGE_'] == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM;
$template = file_get_contents(PluginManager::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml');
$data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL'));

return $data;
}

/**
* Save link hook.
* Publish to the hub when a link is saved.
*
* @param array $data Template data.
* @param ConfigManager $conf instance.
*
* @return array unaltered data.
*/
function hook_pubsubhubbub_save_link($data, $conf)
{
$feeds = array(
index_url($_SERVER) .'?do=atom',
index_url($_SERVER) .'?do=rss',
);

$httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
try {
$p = new Publisher($conf->get('plugins.PUBSUBHUB_URL'));
$p->publish_update($feeds, $httpPost);
} catch (Exception $e) {
error_log('Could not publish to PubSubHubbub: ' . $e->getMessage());
}

return $data;
}

/**
* Http function used to post to the hub endpoint without cURL extension.
*
* @param string $url Hub endpoint.
* @param string $postString String to POST.
*
* @return bool
*
* @throws Exception An error occurred.
*/
function nocurl_http_post($url, $postString) {
$params = array('http' => array(
'method' => 'POST',
'content' => $postString,
'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
));

$context = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $context);
if (!$fp) {
throw new Exception('Could not post to '. $url);
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception('Bad response from the hub '. $url);
}
return $response;
}
14 changes: 0 additions & 14 deletions tests/FeedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function testRSSBuildData()
$data = $feedBuilder->buildData();
// Test headers (RSS)
$this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
$this->assertEmpty($data['pubsubhub_url']);
$this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
$this->assertEquals(true, $data['show_dates']);
$this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
Expand Down Expand Up @@ -210,19 +209,6 @@ public function testBuildDataHideDates()
$this->assertTrue($data['show_dates']);
}

/**
* Test buildData with hide dates settings.
*/
public function testBuildDataPubsubhub()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
}

/**
* Test buildData when Shaarli is served from a subdirectory
*/
Expand Down
54 changes: 54 additions & 0 deletions tests/plugins/PluginPubsubhubbubTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require_once 'plugins/pubsubhubbub/pubsubhubbub.php';
require_once 'application/Router.php';

/**
* Class PluginPubsubhubbubTest
* Unit test for the pubsubhubbub plugin
*/
class PluginPubsubhubbubTest extends PHPUnit_Framework_TestCase
{
/**
* @var string Config file path (without extension).
*/
protected static $configFile = 'tests/utils/config/configJson';

/**
* Reset plugin path
*/
function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}

/**
* Test render_feed hook with an RSS feed.
*/
function testPubSubRssRenderFeed()
{
$hub = 'http://domain.hub';
$conf = new ConfigManager(self::$configFile);
$conf->set('plugins.PUBSUBHUB_URL', $hub);
$data['_PAGE_'] = Router::$PAGE_FEED_RSS;

$data = hook_pubsubhubbub_render_feed($data, $conf);
$expected = '<atom:link rel="hub" href="'. $hub .'" />';
$this->assertEquals($expected, $data['feed_plugins_header'][0]);
}

/**
* Test render_feed hook with an ATOM feed.
*/
function testPubSubAtomRenderFeed()
{
$hub = 'http://domain.hub';
$conf = new ConfigManager(self::$configFile);
$conf->set('plugins.PUBSUBHUB_URL', $hub);
$data['_PAGE_'] = Router::$PAGE_FEED_ATOM;

$data = hook_pubsubhubbub_render_feed($data, $conf);
$expected = '<link rel="hub" href="'. $hub .'" />';
$this->assertEquals($expected, $data['feed_plugins_header'][0]);
}
}
11 changes: 6 additions & 5 deletions tpl/feed.atom.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
<updated>{$last_update}</updated>
{/if}
<link rel="self" href="{$self_link}#" />
{if="!empty($pubsubhub_url)"}
<!-- PubSubHubbub Discovery -->
<link rel="hub" href="{$pubsubhub_url}#" />
<!-- End Of PubSubHubbub Discovery -->
{/if}
{loop="$feed_plugins_header"}
{$value}
{/loop}
<author>
<name>{$index_url}</name>
<uri>{$index_url}</uri>
Expand All @@ -34,6 +32,9 @@
{loop="$value.taglist"}
<category scheme="{$index_url}?searchtags=" term="{$value|strtolower}" label="{$value}" />
{/loop}
{loop="$value.feed_plugins"}
{$value}
{/loop}
</entry>
{/loop}
</feed>
10 changes: 6 additions & 4 deletions tpl/feed.rss.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
<copyright>{$index_url}</copyright>
<generator>Shaarli</generator>
<atom:link rel="self" href="{$self_link}" />
{if="!empty($pubsubhub_url)"}
<!-- PubSubHubbub Discovery -->
<atom:link rel="hub" href="{$pubsubhub_url}" />
{/if}
{loop="$feed_plugins_header"}
{$value}
{/loop}
{loop="$links"}
<item>
<title>{$value.title}</title>
Expand All @@ -29,6 +28,9 @@
{loop="$value.taglist"}
<category domain="{$index_url}?searchtags=">{$value}</category>
{/loop}
{loop="$value.feed_plugins"}
{$value}
{/loop}
</item>
{/loop}
</channel>
Expand Down