3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package FeedSubPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
27 class FeedSubPreviewNotice extends Notice
29 protected $fetched = true;
31 function __construct($profile)
33 //parent::__construct(); // uhhh?
34 $this->profile = $profile;
39 return $this->profile;
49 $got = $this->fetched;
50 $this->fetched = false;
55 class FeedSubPreviewProfile extends Profile
57 function getAvatar($width, $height=null)
59 return new FeedSubPreviewAvatar($width, $height);
63 class FeedSubPreviewAvatar extends Avatar
65 function displayUrl() {
66 return common_path('plugins/FeedSub/images/48px-Feed-icon.svg.png');
73 * @param XML_Feed_Parser $feed
75 function __construct($feed, $url=null)
83 $feedinfo = new Feedinfo();
84 $feedinfo->feeduri = $this->url;
85 $feedinfo->homeuri = $this->feed->link;
86 $feedinfo->huburi = $this->getHubLink();
90 function getAtomLink($item, $attribs=array())
92 // XML_Feed_Parser gets confused by multiple <link> elements.
95 // Note that RSS feeds would embed an <atom:link> so this should work for both.
96 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
97 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
98 $links = $dom->getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link');
99 for ($i = 0; $i < $links->length; $i++) {
100 $node = $links->item($i);
101 if ($node->hasAttributes()) {
102 $href = $node->attributes->getNamedItem('href');
105 foreach ($attribs as $name => $val) {
106 $attrib = $node->attributes->getNamedItem($name);
107 if ($attrib && $attrib->value == $val) {
111 if ($matches == count($attribs)) {
120 function getRssLink($item)
122 // XML_Feed_Parser gets confused by multiple <link> elements.
125 // Note that RSS feeds would embed an <atom:link> so this should work for both.
126 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
127 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
128 $links = $dom->getElementsByTagName('link');
129 for ($i = 0; $i < $links->length; $i++) {
130 $node = $links->item($i);
131 if (!$node->hasAttributes()) {
132 return $node->textContent;
138 function getAltLink($item)
140 // Check for an atom link...
141 $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
143 $link = $this->getRssLink($item);
148 function getHubLink()
150 return $this->getAtomLink($this->feed, array('rel' => 'hub'));
153 function profile($preview=false)
156 $profile = new FeedSubPreviewProfile();
158 $profile = new Profile();
161 // @todo validate/normalize nick?
162 $profile->nickname = $this->feed->title;
163 $profile->fullname = $this->feed->title;
164 $profile->homepage = $this->getAltLink($this->feed);
165 $profile->bio = $this->feed->description;
166 $profile->profileurl = $this->getAltLink($this->feed);
168 // @todo tags from categories
169 // @todo lat/lon/location?
174 function notice($index=1, $preview=false)
176 $entry = $this->feed->getEntryByOffset($index);
182 $notice = new FeedSubPreviewNotice($this->profile(true));
185 $notice = new Notice();
188 $link = $this->getAltLink($entry);
189 $notice->uri = $link;
190 $notice->url = $link;
191 $notice->content = $this->noticeFromEntry($entry);
192 $notice->rendered = common_render_content($notice->content, $notice);
193 $notice->created = common_sql_date($entry->updated); // @fixme
194 $notice->is_local = Notice::GATEWAY;
195 $notice->source = 'feed';
201 * @param XML_Feed_Type $entry
202 * @return string notice text, within post size limit
204 function noticeFromEntry($entry)
206 $title = $entry->title;
207 $link = $entry->link;
209 // @todo We can get <category> entries like this:
210 // $cats = $entry->getCategory('category', array(0, true));
211 // but it feels like an awful hack. If it's accessible cleanly,
212 // try adding #hashtags from the categories/tags on a post.
214 // @todo Should we force a language here?
215 $format = _m('New post: "%1$s" %2$s');
216 $title = $entry->title;
217 $link = $this->getAltLink($entry);
218 $out = sprintf($format, $title, $link);
220 // Trim link if needed...
221 $max = Notice::maxContent();
222 if (mb_strlen($out) > $max) {
223 $link = common_shorten_url($link);
224 $out = sprintf($format, $title, $link);
227 // Trim title if needed...
228 if (mb_strlen($out) > $max) {
229 $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
230 $used = mb_strlen($out) - mb_strlen($title);
231 $available = $max - $used - mb_strlen($ellipsis);
232 $title = mb_substr($title, 0, $available) . $ellipsis;
233 $out = sprintf($format, $title, $link);