From 22b10399aaa97061ed940f92f5b15f6aacfb1093 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 11 Feb 2009 11:37:50 -0500 Subject: Unify feeds definition in actions I got a little sick of trying to keep the export data and links synched in actions, so I made a common method, getFeeds(), which gets the feeds for both. It returns an array of Feed objects, which know about what their mime type is, title, location, all that jazz. I changed the FeedList class so it handles the new Feed objects instead of the old array of data. I changed all the actions that show feeds (I think...) so that they now use getFeeds() for all their feed needs. --- lib/action.php | 38 +++++++++++++++---- lib/feed.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/feedlist.php | 101 +++++++++----------------------------------------- 3 files changed, 159 insertions(+), 90 deletions(-) create mode 100644 lib/feed.php (limited to 'lib') diff --git a/lib/action.php b/lib/action.php index ce92addf5..bd38bf79c 100644 --- a/lib/action.php +++ b/lib/action.php @@ -225,9 +225,19 @@ class Action extends HTMLOutputter // lawsuit * * @return nothing */ + function showFeeds() { - // does nothing by default + $feeds = $this->getFeeds(); + + if ($feeds) { + foreach ($feeds as $feed) { + $this->element('link', array('rel' => $feed->rel(), + 'href' => $feed->url, + 'type' => $feed->mimeType(), + 'title' => $feed->title)); + } + } } /** @@ -540,15 +550,16 @@ class Action extends HTMLOutputter // lawsuit /** * Show export data feeds. * - * MAY overload if there are feeds - * - * @return nothing + * @return void */ + function showExportData() { - // is there structure to this? - // list of (visible!) feed links - // can we reuse list of feeds from showFeeds() ? + $feeds = $this->getFeeds(); + if ($feeds) { + $fl = new FeedList($this); + $fl->show($feeds); + } } /** @@ -924,4 +935,17 @@ class Action extends HTMLOutputter // lawsuit $this->elementEnd('div'); } } + + /** + * An array of feeds for this action. + * + * Returns an array of potential feeds for this action. + * + * @return array Feed object to show in head and links + */ + + function getFeeds() + { + return null; + } } diff --git a/lib/feed.php b/lib/feed.php new file mode 100644 index 000000000..466926844 --- /dev/null +++ b/lib/feed.php @@ -0,0 +1,110 @@ +. + * + * @category Feed + * @package Laconica + * @author Evan Prodromou + * @copyright 2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Data structure for feeds + * + * This structure is a helpful container for shipping around information about syndication feeds. + * + * @category Feed + * @package Laconica + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class Feed +{ + const RSS1 = 1; + const RSS2 = 2; + const ATOM = 3; + const FOAF = 4; + + var $type = null; + var $url = null; + var $title = null; + + function __construct($type, $url, $title) + { + $this->type = $type; + $this->url = $url; + $this->title = $title; + } + + function mimeType() + { + switch ($this->type) { + case Feed::RSS1: + return 'application/rdf+xml'; + case Feed::RSS2: + return 'application/rss+xml'; + case Feed::ATOM: + return 'application/atom+xml'; + case Feed::FOAF: + return 'application/rdf+xml'; + default: + return null; + } + } + + function typeName() + { + switch ($this->type) { + case Feed::RSS1: + return _('RSS 1.0'); + case Feed::RSS2: + return _('RSS 2.0'); + case Feed::ATOM: + return _('Atom'); + case Feed::FOAF: + return _('FOAF'); + default: + return null; + } + } + + function rel() + { + switch ($this->type) { + case Feed::RSS1: + case Feed::RSS2: + case Feed::ATOM: + return 'alternate'; + case Feed::FOAF: + return 'meta'; + default: + return null; + } + } +} diff --git a/lib/feedlist.php b/lib/feedlist.php index 8bfcb9c5a..927e43c33 100644 --- a/lib/feedlist.php +++ b/lib/feedlist.php @@ -50,7 +50,7 @@ if (!defined('LACONICA')) { class FeedList extends Widget { var $action = null; - + function __construct($action=null) { parent::__construct($action); @@ -64,8 +64,8 @@ class FeedList extends Widget $this->out->element('h2', null, _('Export data')); $this->out->elementStart('ul', array('class' => 'xoxo')); - foreach ($feeds as $key => $value) { - $this->feedItem($feeds[$key]); + foreach ($feeds as $feed) { + $this->feedItem($feed); } $this->out->elementEnd('ul'); @@ -74,92 +74,27 @@ class FeedList extends Widget function feedItem($feed) { - $nickname = $this->action->trimmed('nickname'); - - switch($feed['item']) { - case 'notices': default: - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "$nickname's ".$feed['version']." notice feed"; - $feed['textContent'] = "RSS"; - break; - - case 'allrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = $feed['version']." feed for $nickname and friends"; - $feed['textContent'] = "RSS"; - break; - - case 'repliesrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = $feed['version']." feed for replies to $nickname"; - $feed['textContent'] = "RSS"; - break; - - case 'publicrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "Public timeline ".$feed['version']." feed"; - $feed['textContent'] = "RSS"; - break; - - case 'publicatom': - $feed_classname = "atom"; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "Public timeline ".$feed['version']." feed"; - $feed['textContent'] = "Atom"; - break; - - case 'noticesearchrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = $feed['version']." feed for this notice search"; - $feed['textContent'] = "RSS"; - break; - - case 'tagrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = $feed['version']." feed for this tag"; - $feed['textContent'] = "RSS"; - break; + $classname = null; - case 'favoritedrss': - $feed_classname = $feed['type']; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "Favorited ".$feed['version']." feed"; - $feed['textContent'] = "RSS"; + switch ($feed->type) { + case Feed::RSS1: + case Feed::RSS2: + $classname = 'rss'; break; - - case 'foaf': - $feed_classname = "foaf"; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "$nickname's FOAF file"; - $feed['textContent'] = "FOAF"; + case Feed::ATOM: + $classname = 'atom'; break; - - case 'favoritesrss': - $feed_classname = "favorites"; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "Feed for favorites of $nickname"; - $feed['textContent'] = "RSS"; - break; - - case 'usertimeline': - $feed_classname = "atom"; - $feed_mimetype = "application/".$feed['type']."+xml"; - $feed_title = "$nickname's ".$feed['version']." notice feed"; - $feed['textContent'] = "Atom"; + case Feed::FOAF: + $classname = 'foaf'; break; } + $this->out->elementStart('li'); - $this->out->element('a', array('href' => $feed['href'], - 'class' => $feed_classname, - 'type' => $feed_mimetype, - 'title' => $feed_title), - $feed['textContent']); + $this->out->element('a', array('href' => $feed->url, + 'class' => $classname, + 'type' => $feed->mimeType(), + 'title' => $feed->title), + $feed->typeName()); $this->out->elementEnd('li'); } } -- cgit v1.2.3-54-g00ecf