]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/feedmunger.php
OStatus cleanup...
[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);
225         $notice->created = common_sql_date($entry->updated); // @fixme
226         $notice->is_local = Notice::GATEWAY;
227         $notice->source = 'feed';
228         
229         return $notice;
230     }
231
232     /**
233      * @param XML_Feed_Type $entry
234      * @return string notice text, within post size limit
235      */
236     function noticeFromEntry($entry)
237     {
238         $max = Notice::maxContent();
239         $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
240         $title = $entry->title;
241         $link = $entry->link;
242
243         // @todo We can get <category> entries like this:
244         // $cats = $entry->getCategory('category', array(0, true));
245         // but it feels like an awful hack. If it's accessible cleanly,
246         // try adding #hashtags from the categories/tags on a post.
247
248         $title = $entry->title;
249         $link = $this->getAltLink($entry);
250         if ($link) {
251             // Blog post or such...
252             // @todo Should we force a language here?
253             $format = _m('New post: "%1$s" %2$s');
254             $out = sprintf($format, $title, $link);
255
256             // Trim link if needed...
257             if (mb_strlen($out) > $max) {
258                 $link = common_shorten_url($link);
259                 $out = sprintf($format, $title, $link);
260             }
261
262             // Trim title if needed...
263             if (mb_strlen($out) > $max) {
264                 $used = mb_strlen($out) - mb_strlen($title);
265                 $available = $max - $used - mb_strlen($ellipsis);
266                 $title = mb_substr($title, 0, $available) . $ellipsis;
267                 $out = sprintf($format, $title, $link);
268             }
269         } else {
270             // No link? Consider a bare status update.
271             if (mb_strlen($title) > $max) {
272                 $available = $max - mb_strlen($ellipsis);
273                 $out = mb_substr($title, 0, $available) . $ellipsis;
274             } else {
275                 $out = $title;
276             }
277         }
278         
279         return $out;
280     }
281 }