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)
86 function ostatusProfile()
88 $profile = new Ostatus_profile();
89 $profile->feeduri = $this->url;
90 $profile->homeuri = $this->feed->link;
91 $profile->huburi = $this->getHubLink();
92 $salmon = $this->getSalmonLink();
94 $profile->salmonuri = $salmon;
99 function getAtomLink($item, $attribs=array())
101 // XML_Feed_Parser gets confused by multiple <link> elements.
104 // Note that RSS feeds would embed an <atom:link> so this should work for both.
105 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
106 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
107 $links = $dom->getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link');
108 for ($i = 0; $i < $links->length; $i++) {
109 $node = $links->item($i);
110 if ($node->hasAttributes()) {
111 $href = $node->attributes->getNamedItem('href');
114 foreach ($attribs as $name => $val) {
115 $attrib = $node->attributes->getNamedItem($name);
116 if ($attrib && $attrib->value == $val) {
120 if ($matches == count($attribs)) {
129 function getRssLink($item)
131 // XML_Feed_Parser gets confused by multiple <link> elements.
134 // Note that RSS feeds would embed an <atom:link> so this should work for both.
135 /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
136 // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
137 $links = $dom->getElementsByTagName('link');
138 for ($i = 0; $i < $links->length; $i++) {
139 $node = $links->item($i);
140 if (!$node->hasAttributes()) {
141 return $node->textContent;
147 function getAltLink($item)
149 // Check for an atom link...
150 $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
152 $link = $this->getRssLink($item);
157 function getHubLink()
159 return $this->getAtomLink($this->feed, array('rel' => 'hub'));
162 function getSalmonLink()
164 return $this->getAtomLink($this->feed, array('rel' => 'salmon'));
167 function getSelfLink()
169 return $this->getAtomLink($this->feed, array('rel' => 'self'));
173 * Get an appropriate avatar image source URL, if available.
174 * @return mixed string or false
178 $logo = $this->feed->logo;
182 $icon = $this->feed->icon;
186 return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
189 function profile($preview=false)
192 $profile = new FeedSubPreviewProfile();
194 $profile = new Profile();
197 // @todo validate/normalize nick?
198 $profile->nickname = $this->feed->title;
199 $profile->fullname = $this->feed->title;
200 $profile->homepage = $this->getAltLink($this->feed);
201 $profile->bio = $this->feed->description;
202 $profile->profileurl = $this->getAltLink($this->feed);
205 $profile->avatar = $this->getAvatar();
208 // @todo tags from categories
209 // @todo lat/lon/location?
214 function notice($index=1, $preview=false)
216 $entry = $this->feed->getEntryByOffset($index);
222 $notice = new FeedSubPreviewNotice($this->profile(true));
225 $notice = new Notice();
226 $notice->profile_id = $this->profileIdForEntry($index);
229 $link = $this->getAltLink($entry);
231 if (preg_match('!^https?://!', $entry->id)) {
233 common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
236 $notice->uri = $link;
237 $notice->url = $link;
238 $notice->content = $this->noticeFromEntry($entry);
239 $notice->rendered = common_render_content($notice->content, $notice); // @fixme this is failing on group posts
240 $notice->created = common_sql_date($entry->updated); // @fixme
241 $notice->is_local = Notice::GATEWAY;
242 $notice->source = 'feed';
244 $location = $this->getLocation($entry);
246 if ($location->location_id) {
247 $notice->location_ns = $location->location_ns;
248 $notice->location_id = $location->location_id;
250 $notice->lat = $location->lat;
251 $notice->lon = $location->lon;
257 function profileIdForEntry($index=1)
260 // should get profile for this entry's author...
261 $remote = Ostatus_profile::staticGet('feeduri', $this->getSelfLink());
263 return $feed->profile_id;
265 throw new Exception("Can't find feed profile");
270 * Parse location given as a GeoRSS-simple point, if provided.
271 * http://www.georss.org/simple
273 * @param feed item $entry
274 * @return mixed Location or false
276 function getLocation($entry)
278 $dom = $entry->model;
279 $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
281 for ($i = 0; $i < $points->length; $i++) {
282 $point = $points->item(0)->textContent;
283 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
284 $point = preg_replace('/\s+/', ' ', $point);
285 $point = trim($point);
286 $coords = explode(' ', $point);
287 if (count($coords) == 2) {
288 list($lat, $lon) = $coords;
289 if (is_numeric($lat) && is_numeric($lon)) {
290 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
291 return Location::fromLatLon($lat, $lon);
294 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
301 * @param XML_Feed_Type $entry
302 * @return string notice text, within post size limit
304 function noticeFromEntry($entry)
306 $max = Notice::maxContent();
307 $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
308 $title = $entry->title;
309 $link = $entry->link;
311 // @todo We can get <category> entries like this:
312 // $cats = $entry->getCategory('category', array(0, true));
313 // but it feels like an awful hack. If it's accessible cleanly,
314 // try adding #hashtags from the categories/tags on a post.
316 $title = $entry->title;
317 $link = $this->getAltLink($entry);
319 // Blog post or such...
320 // @todo Should we force a language here?
321 $format = _m('New post: "%1$s" %2$s');
322 $out = sprintf($format, $title, $link);
324 // Trim link if needed...
325 if (mb_strlen($out) > $max) {
326 $link = common_shorten_url($link);
327 $out = sprintf($format, $title, $link);
330 // Trim title if needed...
331 if (mb_strlen($out) > $max) {
332 $used = mb_strlen($out) - mb_strlen($title);
333 $available = $max - $used - mb_strlen($ellipsis);
334 $title = mb_substr($title, 0, $available) . $ellipsis;
335 $out = sprintf($format, $title, $link);
338 // No link? Consider a bare status update.
339 if (mb_strlen($title) > $max) {
340 $available = $max - mb_strlen($ellipsis);
341 $out = mb_substr($title, 0, $available) . $ellipsis;