]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/feedmunger.php
fix up hub queueing to work w/ stomp queues
[quix0rs-gnu-social.git] / plugins / OStatus / lib / feedmunger.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package FeedSubPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
26
27 class FeedSubPreviewNotice extends Notice
28 {
29     protected $fetched = true;
30
31     function __construct($profile)
32     {
33         $this->profile = $profile;
34         $this->profile_id = 0;
35     }
36     
37     function getProfile()
38     {
39         return $this->profile;
40     }
41     
42     function find()
43     {
44         return true;
45     }
46     
47     function fetch()
48     {
49         $got = $this->fetched;
50         $this->fetched = false;
51         return $got;
52     }
53 }
54
55 class FeedSubPreviewProfile extends Profile
56 {
57     function getAvatar($width, $height=null)
58     {
59         return new FeedSubPreviewAvatar($width, $height, $this->avatar);
60     }
61 }
62
63 class FeedSubPreviewAvatar extends Avatar
64 {
65     function __construct($width, $height, $remote)
66     {
67         $this->remoteImage = $remote;
68     }
69
70     function displayUrl() {
71         return $this->remoteImage;
72     }
73 }
74
75 class FeedMunger
76 {
77     /**
78      * @param XML_Feed_Parser $feed
79      */
80     function __construct($feed, $url=null)
81     {
82         $this->feed = $feed;
83         $this->url = $url;
84     }
85     
86     function feedinfo()
87     {
88         $feedinfo = new Feedinfo();
89         $feedinfo->feeduri = $this->url;
90         $feedinfo->homeuri = $this->feed->link;
91         $feedinfo->huburi = $this->getHubLink();
92         return $feedinfo;
93     }
94
95     function getAtomLink($item, $attribs=array())
96     {
97         // XML_Feed_Parser gets confused by multiple <link> elements.
98         $dom = $item->model;
99
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');
108                 if ($href) {
109                     $matches = 0;
110                     foreach ($attribs as $name => $val) {
111                         $attrib = $node->attributes->getNamedItem($name);
112                         if ($attrib && $attrib->value == $val) {
113                             $matches++;
114                         }
115                     }
116                     if ($matches == count($attribs)) {
117                         return $href->value;
118                     }
119                 }
120             }
121         }
122         return false;
123     }
124
125     function getRssLink($item)
126     {
127         // XML_Feed_Parser gets confused by multiple <link> elements.
128         $dom = $item->model;
129
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;
138             }
139         }
140         return false;
141     }
142
143     function getAltLink($item)
144     {
145         // Check for an atom link...
146         $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
147         if (!$link) {
148             $link = $this->getRssLink($item);
149         }
150         return $link;
151     }
152
153     function getHubLink()
154     {
155         return $this->getAtomLink($this->feed, array('rel' => 'hub'));
156     }
157
158     /**
159      * Get an appropriate avatar image source URL, if available.
160      * @return mixed string or false
161      */
162     function getAvatar()
163     {
164         $logo = $this->feed->logo;
165         if ($logo) {
166             return $logo;
167         }
168         $icon = $this->feed->icon;
169         if ($icon) {
170             return $icon;
171         }
172         return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
173     }
174
175     function profile($preview=false)
176     {
177         if ($preview) {
178             $profile = new FeedSubPreviewProfile();
179         } else {
180             $profile = new Profile();
181         }
182         
183         // @todo validate/normalize nick?
184         $profile->nickname   = $this->feed->title;
185         $profile->fullname   = $this->feed->title;
186         $profile->homepage   = $this->getAltLink($this->feed);
187         $profile->bio        = $this->feed->description;
188         $profile->profileurl = $this->getAltLink($this->feed);
189
190         if ($preview) {
191             $profile->avatar = $this->getAvatar();
192         }
193         
194         // @todo tags from categories
195         // @todo lat/lon/location?
196
197         return $profile;
198     }
199
200     function notice($index=1, $preview=false)
201     {
202         $entry = $this->feed->getEntryByOffset($index);
203         if (!$entry) {
204             return null;
205         }
206
207         if ($preview) {
208             $notice = new FeedSubPreviewNotice($this->profile(true));
209             $notice->id = -1;
210         } else {
211             $notice = new Notice();
212         }
213
214         $link = $this->getAltLink($entry);
215         if (empty($link)) {
216             if (preg_match('!^https?://!', $entry->id)) {
217                 $link = $entry->id;
218                 common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
219             }
220         }
221         $notice->uri = $link;
222         $notice->url = $link;
223         $notice->content = $this->noticeFromEntry($entry);
224         $notice->rendered = common_render_content($notice->content, $notice); // @fixme this is failing on group posts
225         $notice->created = common_sql_date($entry->updated); // @fixme
226         $notice->is_local = Notice::GATEWAY;
227         $notice->source = 'feed';
228
229         $location = $this->getLocation($entry);
230         if ($location) {
231             if ($location->location_id) {
232                 $notice->location_ns = $location->location_ns;
233                 $notice->location_id = $location->location_id;
234             }
235             $notice->lat = $location->lat;
236             $notice->lon = $location->lon;
237         }
238
239         return $notice;
240     }
241
242     /**
243      * @param feed item $entry
244      * @return mixed Location or false
245      */
246     function getLocation($entry)
247     {
248         $dom = $entry->model;
249         $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
250         
251         for ($i = 0; $i < $points->length; $i++) {
252             $point = trim($points->item(0)->textContent);
253             $coords = explode(' ', $point);
254             if (count($coords) == 2) {
255                 list($lat, $lon) = $coords;
256                 if (is_numeric($lat) && is_numeric($lon)) {
257                     common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
258                     return Location::fromLatLon($lat, $lon);
259                 }
260             }
261             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
262         }
263
264         return false;
265     }
266
267     /**
268      * @param XML_Feed_Type $entry
269      * @return string notice text, within post size limit
270      */
271     function noticeFromEntry($entry)
272     {
273         $max = Notice::maxContent();
274         $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
275         $title = $entry->title;
276         $link = $entry->link;
277
278         // @todo We can get <category> entries like this:
279         // $cats = $entry->getCategory('category', array(0, true));
280         // but it feels like an awful hack. If it's accessible cleanly,
281         // try adding #hashtags from the categories/tags on a post.
282
283         $title = $entry->title;
284         $link = $this->getAltLink($entry);
285         if ($link) {
286             // Blog post or such...
287             // @todo Should we force a language here?
288             $format = _m('New post: "%1$s" %2$s');
289             $out = sprintf($format, $title, $link);
290
291             // Trim link if needed...
292             if (mb_strlen($out) > $max) {
293                 $link = common_shorten_url($link);
294                 $out = sprintf($format, $title, $link);
295             }
296
297             // Trim title if needed...
298             if (mb_strlen($out) > $max) {
299                 $used = mb_strlen($out) - mb_strlen($title);
300                 $available = $max - $used - mb_strlen($ellipsis);
301                 $title = mb_substr($title, 0, $available) . $ellipsis;
302                 $out = sprintf($format, $title, $link);
303             }
304         } else {
305             // No link? Consider a bare status update.
306             if (mb_strlen($title) > $max) {
307                 $available = $max - mb_strlen($ellipsis);
308                 $out = mb_substr($title, 0, $available) . $ellipsis;
309             } else {
310                 $out = $title;
311             }
312         }
313         
314         return $out;
315     }
316 }