]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/feedmunger.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[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 ostatusProfile()
87     {
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();
93         if ($salmon) {
94             $profile->salmonuri = $salmon;
95         }
96         return $profile;
97     }
98
99     function getAtomLink($item, $attribs=array())
100     {
101         // XML_Feed_Parser gets confused by multiple <link> elements.
102         $dom = $item->model;
103
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');
112                 if ($href) {
113                     $matches = 0;
114                     foreach ($attribs as $name => $val) {
115                         $attrib = $node->attributes->getNamedItem($name);
116                         if ($attrib && $attrib->value == $val) {
117                             $matches++;
118                         }
119                     }
120                     if ($matches == count($attribs)) {
121                         return $href->value;
122                     }
123                 }
124             }
125         }
126         return false;
127     }
128
129     function getRssLink($item)
130     {
131         // XML_Feed_Parser gets confused by multiple <link> elements.
132         $dom = $item->model;
133
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;
142             }
143         }
144         return false;
145     }
146
147     function getAltLink($item)
148     {
149         // Check for an atom link...
150         $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
151         if (!$link) {
152             $link = $this->getRssLink($item);
153         }
154         return $link;
155     }
156
157     function getHubLink()
158     {
159         return $this->getAtomLink($this->feed, array('rel' => 'hub'));
160     }
161
162     function getSalmonLink()
163     {
164         return $this->getAtomLink($this->feed, array('rel' => 'salmon'));
165     }
166
167     function getSelfLink()
168     {
169         return $this->getAtomLink($this->feed, array('rel' => 'self'));
170     }
171
172     /**
173      * Get an appropriate avatar image source URL, if available.
174      * @return mixed string or false
175      */
176     function getAvatar()
177     {
178         $logo = $this->feed->logo;
179         if ($logo) {
180             return $logo;
181         }
182         $icon = $this->feed->icon;
183         if ($icon) {
184             return $icon;
185         }
186         return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
187     }
188
189     function profile($preview=false)
190     {
191         if ($preview) {
192             $profile = new FeedSubPreviewProfile();
193         } else {
194             $profile = new Profile();
195         }
196         
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);
203
204         if ($preview) {
205             $profile->avatar = $this->getAvatar();
206         }
207         
208         // @todo tags from categories
209         // @todo lat/lon/location?
210
211         return $profile;
212     }
213
214     function notice($index=1, $preview=false)
215     {
216         $entry = $this->feed->getEntryByOffset($index);
217         if (!$entry) {
218             return null;
219         }
220
221         if ($preview) {
222             $notice = new FeedSubPreviewNotice($this->profile(true));
223             $notice->id = -1;
224         } else {
225             $notice = new Notice();
226             $notice->profile_id = $this->profileIdForEntry($index);
227         }
228
229         $link = $this->getAltLink($entry);
230         if (empty($link)) {
231             if (preg_match('!^https?://!', $entry->id)) {
232                 $link = $entry->id;
233                 common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
234             }
235         }
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';
243
244         $location = $this->getLocation($entry);
245         if ($location) {
246             if ($location->location_id) {
247                 $notice->location_ns = $location->location_ns;
248                 $notice->location_id = $location->location_id;
249             }
250             $notice->lat = $location->lat;
251             $notice->lon = $location->lon;
252         }
253
254         return $notice;
255     }
256
257     function profileIdForEntry($index=1)
258     {
259         // hack hack hack
260         // should get profile for this entry's author...
261         $remote = Ostatus_profile::staticGet('feeduri', $this->getSelfLink());
262         if ($feed) {
263             return $feed->profile_id;
264         } else {
265             throw new Exception("Can't find feed profile");
266         }
267     }
268
269     /**
270      * Parse location given as a GeoRSS-simple point, if provided.
271      * http://www.georss.org/simple
272      *
273      * @param feed item $entry
274      * @return mixed Location or false
275      */
276     function getLocation($entry)
277     {
278         $dom = $entry->model;
279         $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
280         
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);
292                 }
293             }
294             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
295         }
296
297         return false;
298     }
299
300     /**
301      * @param XML_Feed_Type $entry
302      * @return string notice text, within post size limit
303      */
304     function noticeFromEntry($entry)
305     {
306         $max = Notice::maxContent();
307         $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
308         $title = $entry->title;
309         $link = $entry->link;
310
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.
315
316         $title = $entry->title;
317         $link = $this->getAltLink($entry);
318         if ($link) {
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);
323
324             // Trim link if needed...
325             if (mb_strlen($out) > $max) {
326                 $link = common_shorten_url($link);
327                 $out = sprintf($format, $title, $link);
328             }
329
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);
336             }
337         } else {
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;
342             } else {
343                 $out = $title;
344             }
345         }
346         
347         return $out;
348     }
349 }