]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/feedmunger.php
OStatus fix: include feed profile at notice text processing time, fixes replies
[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     function getSelfLink()
159     {
160         return $this->getAtomLink($this->feed, array('rel' => 'self'));
161     }
162
163     /**
164      * Get an appropriate avatar image source URL, if available.
165      * @return mixed string or false
166      */
167     function getAvatar()
168     {
169         $logo = $this->feed->logo;
170         if ($logo) {
171             return $logo;
172         }
173         $icon = $this->feed->icon;
174         if ($icon) {
175             return $icon;
176         }
177         return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
178     }
179
180     function profile($preview=false)
181     {
182         if ($preview) {
183             $profile = new FeedSubPreviewProfile();
184         } else {
185             $profile = new Profile();
186         }
187         
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);
194
195         if ($preview) {
196             $profile->avatar = $this->getAvatar();
197         }
198         
199         // @todo tags from categories
200         // @todo lat/lon/location?
201
202         return $profile;
203     }
204
205     function notice($index=1, $preview=false)
206     {
207         $entry = $this->feed->getEntryByOffset($index);
208         if (!$entry) {
209             return null;
210         }
211
212         if ($preview) {
213             $notice = new FeedSubPreviewNotice($this->profile(true));
214             $notice->id = -1;
215         } else {
216             $notice = new Notice();
217             $notice->profile_id = $this->profileIdForEntry($index);
218         }
219
220         $link = $this->getAltLink($entry);
221         if (empty($link)) {
222             if (preg_match('!^https?://!', $entry->id)) {
223                 $link = $entry->id;
224                 common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
225             }
226         }
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';
234
235         $location = $this->getLocation($entry);
236         if ($location) {
237             if ($location->location_id) {
238                 $notice->location_ns = $location->location_ns;
239                 $notice->location_id = $location->location_id;
240             }
241             $notice->lat = $location->lat;
242             $notice->lon = $location->lon;
243         }
244
245         return $notice;
246     }
247
248     function profileIdForEntry($index=1)
249     {
250         // hack hack hack
251         // should get profile for this entry's author...
252         $feed = new Feedinfo();
253         $feed->feeduri = $self;
254         $feed = Feedinfo::staticGet('feeduri', $this->getSelfLink());
255         if ($feed) {
256             return $feed->profile_id;
257         } else {
258             throw new Exception("Can't find feed profile");
259         }
260     }
261
262     /**
263      * @param feed item $entry
264      * @return mixed Location or false
265      */
266     function getLocation($entry)
267     {
268         $dom = $entry->model;
269         $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
270         
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);
279                 }
280             }
281             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
282         }
283
284         return false;
285     }
286
287     /**
288      * @param XML_Feed_Type $entry
289      * @return string notice text, within post size limit
290      */
291     function noticeFromEntry($entry)
292     {
293         $max = Notice::maxContent();
294         $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
295         $title = $entry->title;
296         $link = $entry->link;
297
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.
302
303         $title = $entry->title;
304         $link = $this->getAltLink($entry);
305         if ($link) {
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);
310
311             // Trim link if needed...
312             if (mb_strlen($out) > $max) {
313                 $link = common_shorten_url($link);
314                 $out = sprintf($format, $title, $link);
315             }
316
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);
323             }
324         } else {
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;
329             } else {
330                 $out = $title;
331             }
332         }
333         
334         return $out;
335     }
336 }