]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FeedSub/feedmunger.php
Initial functional version of feed subscription plugin, currently supporting only...
[quix0rs-gnu-social.git] / plugins / FeedSub / 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         //parent::__construct(); // uhhh?
34         $this->profile = $profile;
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);
60     }
61 }
62
63 class FeedSubPreviewAvatar extends Avatar
64 {
65     function displayUrl() {
66         return common_path('plugins/FeedSub/images/48px-Feed-icon.svg.png');
67     }
68 }
69
70 class FeedMunger
71 {
72     /**
73      * @param XML_Feed_Parser $feed
74      */
75     function __construct($feed, $url=null)
76     {
77         $this->feed = $feed;
78         $this->url = $url;
79     }
80     
81     function feedinfo()
82     {
83         $feedinfo = new Feedinfo();
84         $feedinfo->feeduri = $this->url;
85         $feedinfo->homeuri = $this->feed->link;
86         $feedinfo->huburi = $this->getHubLink();
87         return $feedinfo;
88     }
89
90     function getAtomLink($item, $attribs=array())
91     {
92         // XML_Feed_Parser gets confused by multiple <link> elements.
93         $dom = $item->model;
94
95         // Note that RSS feeds would embed an <atom:link> so this should work for both.
96         /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
97         // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
98         $links = $dom->getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link');
99         for ($i = 0; $i < $links->length; $i++) {
100             $node = $links->item($i);
101             if ($node->hasAttributes()) {
102                 $href = $node->attributes->getNamedItem('href');
103                 if ($href) {
104                     $matches = 0;
105                     foreach ($attribs as $name => $val) {
106                         $attrib = $node->attributes->getNamedItem($name);
107                         if ($attrib && $attrib->value == $val) {
108                             $matches++;
109                         }
110                     }
111                     if ($matches == count($attribs)) {
112                         return $href->value;
113                     }
114                 }
115             }
116         }
117         return false;
118     }
119
120     function getRssLink($item)
121     {
122         // XML_Feed_Parser gets confused by multiple <link> elements.
123         $dom = $item->model;
124
125         // Note that RSS feeds would embed an <atom:link> so this should work for both.
126         /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
127         // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
128         $links = $dom->getElementsByTagName('link');
129         for ($i = 0; $i < $links->length; $i++) {
130             $node = $links->item($i);
131             if (!$node->hasAttributes()) {
132                 return $node->textContent;
133             }
134         }
135         return false;
136     }
137
138     function getAltLink($item)
139     {
140         // Check for an atom link...
141         $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
142         if (!$link) {
143             $link = $this->getRssLink($item);
144         }
145         return $link;
146     }
147
148     function getHubLink()
149     {
150         return $this->getAtomLink($this->feed, array('rel' => 'hub'));
151     }
152
153     function profile($preview=false)
154     {
155         if ($preview) {
156             $profile = new FeedSubPreviewProfile();
157         } else {
158             $profile = new Profile();
159         }
160         
161         // @todo validate/normalize nick?
162         $profile->nickname   = $this->feed->title;
163         $profile->fullname   = $this->feed->title;
164         $profile->homepage   = $this->getAltLink($this->feed);
165         $profile->bio        = $this->feed->description;
166         $profile->profileurl = $this->getAltLink($this->feed);
167         
168         // @todo tags from categories
169         // @todo lat/lon/location?
170
171         return $profile;
172     }
173
174     function notice($index=1, $preview=false)
175     {
176         $entry = $this->feed->getEntryByOffset($index);
177         if (!$entry) {
178             return null;
179         }
180         
181         if ($preview) {
182             $notice = new FeedSubPreviewNotice($this->profile(true));
183             $notice->id = -1;
184         } else {
185             $notice = new Notice();
186         }
187
188         $link = $this->getAltLink($entry);
189         $notice->uri = $link;
190         $notice->url = $link;
191         $notice->content = $this->noticeFromEntry($entry);
192         $notice->rendered = common_render_content($notice->content, $notice);
193         $notice->created = common_sql_date($entry->updated); // @fixme
194         $notice->is_local = Notice::GATEWAY;
195         $notice->source = 'feed';
196         
197         return $notice;
198     }
199
200     /**
201      * @param XML_Feed_Type $entry
202      * @return string notice text, within post size limit
203      */
204     function noticeFromEntry($entry)
205     {
206         $title = $entry->title;
207         $link = $entry->link;
208         
209         // @todo We can get <category> entries like this:
210         // $cats = $entry->getCategory('category', array(0, true));
211         // but it feels like an awful hack. If it's accessible cleanly,
212         // try adding #hashtags from the categories/tags on a post.
213         
214         // @todo Should we force a language here?
215         $format = dgettext("FeedSubPlugin", 'New post: "%1$s" %2$s');
216         $title = $entry->title;
217         $link = $this->getAltLink($entry);
218         $out = sprintf($format, $title, $link);
219         
220         // Trim link if needed...
221         $max = Notice::maxContent();
222         if (mb_strlen($out) > $max) {
223             $link = common_shorten_url($link);
224             $out = sprintf($format, $title, $link);
225         }
226
227         // Trim title if needed...
228         if (mb_strlen($out) > $max) {
229             $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
230             $used = mb_strlen($out) - mb_strlen($title);
231             $available = $max - $used - mb_strlen($ellipsis);
232             $title = mb_substr($title, 0, $available) . $ellipsis;
233             $out = sprintf($format, $title, $link);
234         }
235         
236         return $out;
237     }
238 }