From 6e570a8440108ac0d16253d35543be19b1c2a713 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 28 Aug 2009 14:42:51 -0400 Subject: [PATCH 1/2] Added 2 new events: StartApiRss and StartApiAtom --- EVENTS.txt | 7 +++++++ lib/twitterapi.php | 10 +++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/EVENTS.txt b/EVENTS.txt index 05d1725855..121ae175d0 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -247,3 +247,10 @@ StartLoadDoc: before loading a help doc (hook this to show your own documentatio EndLoadDoc: after loading a help doc (hook this to modify other documentation) - $title: title of the document - $output: HTML output to show + +StartApiRss: after the rss element is started +- $action: action object being shown + +StartApiAtom: after the element is started +- $action: action object being shown + diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 9055d8b982..4612f74e9f 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -595,7 +595,6 @@ class TwitterapiAction extends Action $this->init_document('rss'); - $this->elementStart('channel'); $this->element('title', null, $title); $this->element('link', null, $link); if (!is_null($suplink)) { @@ -621,7 +620,6 @@ class TwitterapiAction extends Action } } - $this->elementEnd('channel'); $this->end_twitter_rss(); } @@ -668,7 +666,6 @@ class TwitterapiAction extends Action $this->init_document('rss'); - $this->elementStart('channel'); $this->element('title', null, $title); $this->element('link', null, $link); $this->element('description', null, $subtitle); @@ -687,7 +684,6 @@ class TwitterapiAction extends Action } } - $this->elementEnd('channel'); $this->end_twitter_rss(); } @@ -944,11 +940,14 @@ class TwitterapiAction extends Action function init_twitter_rss() { $this->startXML(); - $this->elementStart('rss', array('version' => '2.0')); + $this->elementStart('rss', array('version' => '2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom')); + $this->elementStart('channel'); + Event::handle('StartApiRss', array($this)); } function end_twitter_rss() { + $this->elementEnd('channel'); $this->elementEnd('rss'); $this->endXML(); } @@ -960,6 +959,7 @@ class TwitterapiAction extends Action $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US', 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0')); + Event::handle('StartApiAtom', array($this)); } function end_twitter_atom() From 72a60d63814188c7e282d678e281070070be5df2 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 28 Aug 2009 14:43:31 -0400 Subject: [PATCH 2/2] Added a PubSubHubBub plugin --- plugins/PubSubHubBub/PubSubHubBubPlugin.php | 122 ++++++++++++++++++++ plugins/PubSubHubBub/publisher.php | 86 ++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 plugins/PubSubHubBub/PubSubHubBubPlugin.php create mode 100644 plugins/PubSubHubBub/publisher.php diff --git a/plugins/PubSubHubBub/PubSubHubBubPlugin.php b/plugins/PubSubHubBub/PubSubHubBubPlugin.php new file mode 100644 index 0000000000..013a234d73 --- /dev/null +++ b/plugins/PubSubHubBub/PubSubHubBubPlugin.php @@ -0,0 +1,122 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @copyright 2009 Craig Andrews http://candrews.integralblue.com + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +define('DEFAULT_HUB','http://2pubsubhubbub.appspot.com'); + +require_once(INSTALLDIR.'/plugins/PubSubHubBub/publisher.php'); + +class PubSubHubBubPlugin extends Plugin +{ + private $hub; + + function __construct() + { + parent::__construct(); + } + + function onInitializePlugin(){ + $this->hub = common_config('PubSubHubBub', 'hub'); + if(empty($this->hub)){ + $this->hub = DEFAULT_HUB; + } + } + + function onStartApiAtom($action){ + $action->element('link',array('rel'=>'hub','href'=>$this->hub),null); + } + + function onStartApiRss($action){ + $action->element('atom:link',array('rel'=>'hub','href'=>$this->hub),null); + } + + function onEndNoticeSave($notice){ + $publisher = new Publisher($this->hub); + + $feeds = array(); + + //public timeline feeds + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'public_timeline.rss')); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'public_timeline.atom')); + + //author's own feeds + $user = User::staticGet('id',$notice->profile_id); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss')); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom')); + + //tag feeds + $tag = new Notice_tag(); + $tag->notice_id = $notice->id; + if ($tag->find()) { + while ($tag->fetch()) { + $feeds[]=common_local_url('api',array('apiaction' => 'tags','method' => 'timeline', 'argument'=>$tag->tag.'.atom')); + $feeds[]=common_local_url('api',array('apiaction' => 'tags','method' => 'timeline', 'argument'=>$tag->tag.'.rss')); + } + } + + //group feeds + $group_inbox = new Group_inbox(); + $group_inbox->notice_id = $notice->id; + if ($group_inbox->find()) { + while ($group_inbox->fetch()) { + $group = User_group::staticGet('id',$group_inbox->group_id); + $feeds[]=common_local_url('api',array('apiaction' => 'groups','method' => 'timeline','argument' => $group->nickname.'.rss')); + $feeds[]=common_local_url('api',array('apiaction' => 'groups','method' => 'timeline','argument' => $group->nickname.'.atom')); + } + } + + //feed of each user that subscribes to the notice's author + $notice_inbox = new Notice_inbox(); + $notice_inbox->notice_id = $notice->id; + if ($notice_inbox->find()) { + while ($notice_inbox->fetch()) { + $user = User::staticGet('id',$notice_inbox->user_id); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss')); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom')); + } + } + + /* TODO: when the reply page gets RSS and ATOM feeds, implement this + //feed of user replied to + if($notice->reply_to){ + $user = User::staticGet('id',$notice->reply_to); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss')); + $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom')); + }*/ + + foreach(array_unique($feeds) as $feed){ + if(! $publisher->publish_update($feed)){ + common_log_line(LOG_WARNING,$feed.' was not published to hub at '.$this->hub.':'.$publisher->last_response()); + } + } + } +} diff --git a/plugins/PubSubHubBub/publisher.php b/plugins/PubSubHubBub/publisher.php new file mode 100644 index 0000000000..f176a9b8a4 --- /dev/null +++ b/plugins/PubSubHubBub/publisher.php @@ -0,0 +1,86 @@ +hub_url = $hub_url; + } + + // accepts either a single url or an array of urls + public function publish_update($topic_urls, $http_function = false) { + if (!isset($topic_urls)) + throw new Exception('Please specify a topic url'); + + // check that we're working with an array + if (!is_array($topic_urls)) { + $topic_urls = array($topic_urls); + } + + // set the mode to publish + $post_string = "hub.mode=publish"; + // loop through each topic url + foreach ($topic_urls as $topic_url) { + + // lightweight check that we're actually working w/ a valid url + if (!preg_match("|^https?://|i",$topic_url)) + throw new Exception('The specified topic url does not appear to be valid: '.$topic_url); + + // append the topic url parameters + $post_string .= "&hub.url=".urlencode($topic_url); + } + + // make the http post request and return true/false + // easy to over-write to use your own http function + if ($http_function) + return $http_function($this->hub_url,$post_string); + else + return $this->http_post($this->hub_url,$post_string); + } + + // returns any error message from the latest request + public function last_response() { + return $this->last_response; + } + + // default http function that uses curl to post to the hub endpoint + private function http_post($url, $post_string) { + + // add any additional curl options here + $options = array(CURLOPT_URL => $url, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $post_string, + CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0"); + + $ch = curl_init(); + curl_setopt_array($ch, $options); + + $response = curl_exec($ch); + $this->last_response = $response; + $info = curl_getinfo($ch); + + curl_close($ch); + + // all good + if ($info['http_code'] == 204) + return true; + return false; + } +} + +?> \ No newline at end of file