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 $this->profile = $profile;
34 $this->profile_id = 0;
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, $this->avatar);
63 class FeedSubPreviewAvatar extends Avatar
65 function __construct($width, $height, $remote)
67 $this->remoteImage = $remote;
70 function displayUrl() {
71 return $this->remoteImage;
78 * @param XML_Feed_Parser $feed
80 function __construct($feed, $url=null)
88 $feedinfo = new Feedinfo();
89 $feedinfo->feeduri = $this->url;
90 $feedinfo->homeuri = $this->feed->link;
91 $feedinfo->huburi = $this->getHubLink();
95 function getAtomLink($item, $attribs=array())
97 // XML_Feed_Parser gets confused by multiple <link> elements.
100 // Note that RSS feeds would embed an <atom:link> so this should work for both.
101 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
102 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
103 $links = $dom->getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link');
104 for ($i = 0; $i < $links->length; $i++) {
105 $node = $links->item($i);
106 if ($node->hasAttributes()) {
107 $href = $node->attributes->getNamedItem('href');
110 foreach ($attribs as $name => $val) {
111 $attrib = $node->attributes->getNamedItem($name);
112 if ($attrib && $attrib->value == $val) {
116 if ($matches == count($attribs)) {
125 function getRssLink($item)
127 // XML_Feed_Parser gets confused by multiple <link> elements.
130 // Note that RSS feeds would embed an <atom:link> so this should work for both.
131 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
132 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
133 $links = $dom->getElementsByTagName('link');
134 for ($i = 0; $i < $links->length; $i++) {
135 $node = $links->item($i);
136 if (!$node->hasAttributes()) {
137 return $node->textContent;
143 function getAltLink($item)
145 // Check for an atom link...
146 $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
148 $link = $this->getRssLink($item);
153 function getHubLink()
155 return $this->getAtomLink($this->feed, array('rel' => 'hub'));
158 function getSelfLink()
160 return $this->getAtomLink($this->feed, array('rel' => 'self'));
164 * Get an appropriate avatar image source URL, if available.
165 * @return mixed string or false
169 $logo = $this->feed->logo;
173 $icon = $this->feed->icon;
177 return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
180 function profile($preview=false)
183 $profile = new FeedSubPreviewProfile();
185 $profile = new Profile();
188 // @todo validate/normalize nick?
189 $profile->nickname = $this->feed->title;
190 $profile->fullname = $this->feed->title;
191 $profile->homepage = $this->getAltLink($this->feed);
192 $profile->bio = $this->feed->description;
193 $profile->profileurl = $this->getAltLink($this->feed);
196 $profile->avatar = $this->getAvatar();
199 // @todo tags from categories
200 // @todo lat/lon/location?
205 function notice($index=1, $preview=false)
207 $entry = $this->feed->getEntryByOffset($index);
213 $notice = new FeedSubPreviewNotice($this->profile(true));
216 $notice = new Notice();
217 $notice->profile_id = $this->profileIdForEntry($index);
220 $link = $this->getAltLink($entry);
222 if (preg_match('!^https?://!', $entry->id)) {
224 common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
227 $notice->uri = $link;
228 $notice->url = $link;
229 $notice->content = $this->noticeFromEntry($entry);
230 $notice->rendered = common_render_content($notice->content, $notice); // @fixme this is failing on group posts
231 $notice->created = common_sql_date($entry->updated); // @fixme
232 $notice->is_local = Notice::GATEWAY;
233 $notice->source = 'feed';
235 $location = $this->getLocation($entry);
237 if ($location->location_id) {
238 $notice->location_ns = $location->location_ns;
239 $notice->location_id = $location->location_id;
241 $notice->lat = $location->lat;
242 $notice->lon = $location->lon;
248 function profileIdForEntry($index=1)
251 // should get profile for this entry's author...
252 $feed = new Feedinfo();
253 $feed->feeduri = $self;
254 $feed = Feedinfo::staticGet('feeduri', $this->getSelfLink());
256 return $feed->profile_id;
258 throw new Exception("Can't find feed profile");
263 * @param feed item $entry
264 * @return mixed Location or false
266 function getLocation($entry)
268 $dom = $entry->model;
269 $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
271 for ($i = 0; $i < $points->length; $i++) {
272 $point = trim($points->item(0)->textContent);
273 $coords = explode(' ', $point);
274 if (count($coords) == 2) {
275 list($lat, $lon) = $coords;
276 if (is_numeric($lat) && is_numeric($lon)) {
277 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
278 return Location::fromLatLon($lat, $lon);
281 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
288 * @param XML_Feed_Type $entry
289 * @return string notice text, within post size limit
291 function noticeFromEntry($entry)
293 $max = Notice::maxContent();
294 $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
295 $title = $entry->title;
296 $link = $entry->link;
298 // @todo We can get <category> entries like this:
299 // $cats = $entry->getCategory('category', array(0, true));
300 // but it feels like an awful hack. If it's accessible cleanly,
301 // try adding #hashtags from the categories/tags on a post.
303 $title = $entry->title;
304 $link = $this->getAltLink($entry);
306 // Blog post or such...
307 // @todo Should we force a language here?
308 $format = _m('New post: "%1$s" %2$s');
309 $out = sprintf($format, $title, $link);
311 // Trim link if needed...
312 if (mb_strlen($out) > $max) {
313 $link = common_shorten_url($link);
314 $out = sprintf($format, $title, $link);
317 // Trim title if needed...
318 if (mb_strlen($out) > $max) {
319 $used = mb_strlen($out) - mb_strlen($title);
320 $available = $max - $used - mb_strlen($ellipsis);
321 $title = mb_substr($title, 0, $available) . $ellipsis;
322 $out = sprintf($format, $title, $link);
325 // No link? Consider a bare status update.
326 if (mb_strlen($title) > $max) {
327 $available = $max - mb_strlen($ellipsis);
328 $out = mb_substr($title, 0, $available) . $ellipsis;