]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
9015589439ed4f59f0383cd2b47be8574bda8f48
[quix0rs-gnu-social.git] / lib / rssaction.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Base class for RSS 1.0 feed actions
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Mail
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Earle Martin <earle@downlode.org>
26  * @copyright 2008-9 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) { exit(1); }
32
33 define('DEFAULT_RSS_LIMIT', 48);
34
35 class Rss10Action extends Action
36 {
37     # This will contain the details of each feed item's author and be used to generate SIOC data.
38
39     var $creators = array();
40     var $limit = DEFAULT_RSS_LIMIT;
41     var $notices = null;
42     var $tags_already_output = array();
43
44     /**
45      * Constructor
46      *
47      * Just wraps the Action constructor.
48      *
49      * @param string  $output URI to output to, default = stdout
50      * @param boolean $indent Whether to indent output, default true
51      *
52      * @see Action::__construct
53      */
54
55     function __construct($output='php://output', $indent=true)
56     {
57         parent::__construct($output, $indent);
58     }
59
60     /**
61      * Do we need to write to the database?
62      *
63      * @return boolean true
64      */
65
66     function isReadonly()
67     {
68         return true;
69     }
70
71     /**
72      * Read arguments and initialize members
73      *
74      * @param array $args Arguments from $_REQUEST
75      * @return boolean success
76      */
77
78     function prepare($args)
79     {
80         parent::prepare($args);
81         $this->limit = (int) $this->trimmed('limit');
82         if ($this->limit == 0) {
83             $this->limit = DEFAULT_RSS_LIMIT;
84         }
85         return true;
86     }
87
88     /**
89      * Handle a request
90      *
91      * @param array $args Arguments from $_REQUEST
92      *
93      * @return void
94      */
95
96     function handle($args)
97     {
98         // Parent handling, including cache check
99         parent::handle($args);
100         // Get the list of notices
101         if (empty($this->tag)) {
102             $this->notices = $this->getNotices($this->limit);
103         } else {
104             $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
105         }
106         $this->showRss();
107     }
108
109     /**
110      * Get the notices to output in this stream
111      *
112      * @return array an array of Notice objects sorted in reverse chron
113      */
114
115     function getNotices()
116     {
117         return array();
118     }
119
120     /**
121      * Get a description of the channel
122      *
123      * Returns an array with the following
124      * @return array
125      */
126
127     function getChannel()
128     {
129         return array('url' => '',
130                      'title' => '',
131                      'link' => '',
132                      'description' => '');
133     }
134
135     function getImage()
136     {
137         return null;
138     }
139
140     function showRss()
141     {
142         $this->initRss();
143         $this->showChannel();
144         $this->showImage();
145
146         foreach ($this->notices as $n) {
147             $this->showItem($n);
148         }
149
150         $this->showCreators();
151         $this->endRss();
152     }
153
154     function showChannel()
155     {
156
157         $channel = $this->getChannel();
158         $image = $this->getImage();
159
160         $this->elementStart('channel', array('rdf:about' => $channel['url']));
161         $this->element('title', null, $channel['title']);
162         $this->element('link', null, $channel['link']);
163         $this->element('description', null, $channel['description']);
164         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
165
166         if ($image) {
167             $this->element('image', array('rdf:resource' => $image));
168         }
169
170         $this->elementStart('items');
171         $this->elementStart('rdf:Seq');
172
173         foreach ($this->notices as $notice) {
174             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
175         }
176
177         $this->elementEnd('rdf:Seq');
178         $this->elementEnd('items');
179
180         $this->elementEnd('channel');
181     }
182
183     function showImage()
184     {
185         $image = $this->getImage();
186         if ($image) {
187             $channel = $this->getChannel();
188             $this->elementStart('image', array('rdf:about' => $image));
189             $this->element('title', null, $channel['title']);
190             $this->element('link', null, $channel['link']);
191             $this->element('url', null, $image);
192             $this->elementEnd('image');
193         }
194     }
195
196     // XXX: Surely there should be a common function to do this?
197     function extract_tags ($string)
198     {
199         $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($string), $match);
200         if (!count)
201         {
202             return array();
203         }
204
205         $rv = array();
206         foreach ($match[1] as $tag)
207         {
208             $rv[] = common_canonical_tag($tag);
209         } 
210
211         return array_unique($rv);
212     }
213          
214     function showItem($notice)
215     {
216         $profile = Profile::staticGet($notice->profile_id);
217         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
218         $creator_uri = common_profile_uri($profile);
219         $this->elementStart('item', array('rdf:about' => $notice->uri,
220                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
221         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
222         $this->element('title', null, $title);
223         $this->element('link', null, $nurl);
224         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
225         if ($notice->rendered) {
226             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
227         }
228         $this->element('dc:date', null, common_date_w3dtf($notice->created));
229         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
230         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
231         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
232         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
233         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
234         if ($notice->reply_to) {
235             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
236             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
237         }
238         if (!empty($notice->conversation)) {
239             $conversationurl = common_local_url('conversation',
240                                          array('id' => $notice->conversation));
241             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
242         }
243         $attachments = $notice->attachments();
244         if($attachments){
245             foreach($attachments as $attachment){
246                 if ($attachment->isEnclosure()) {
247                     // DO NOT move xmlns declaration to root element. Making it
248                     // the default namespace here improves compatibility with
249                     // real-world feed readers.
250                     $attribs = array(
251                         'rdf:resource' => $attachment->url,
252                         'url' => $attachment->url,
253                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
254                         );
255                     if ($attachment->title) {
256                         $attribs['dc:title'] = $attachment->title;
257                     }
258                     if ($attachment->modified) {
259                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
260                     }
261                     if ($attachment->size) {
262                         $attribs['length'] = $attachment->size;
263                     }
264                     if ($attachment->mimetype) {
265                         $attribs['type'] = $attachment->mimetype;
266                     }
267                     $this->element('enclosure', $attribs);
268                 }
269                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
270             }
271         }
272         $tags = $this->extract_tags($notice->content);
273         if (!empty($tags)) {
274             foreach ($tags as $tag)
275             {
276                 $tagpage = common_local_url('tag', array('tag' => $tag));
277
278                 if ( in_array($tag, $this->tags_already_output) ) {
279                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
280                     continue;
281                 }
282
283                 $tagrss  = common_local_url('tagrss', array('tag' => $tag));
284                 $this->elementStart('ctag:tagged');
285                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag));
286                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
287                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
288                 $this->elementEnd('ctag:Tag');
289                 $this->elementEnd('ctag:tagged');
290
291                 $this->tags_already_output[] = $tag;
292             }
293         }
294         $this->elementEnd('item');
295         $this->creators[$creator_uri] = $profile;
296     }
297
298     function showCreators()
299     {
300         foreach ($this->creators as $uri => $profile) {
301             $id = $profile->id;
302             $nickname = $profile->nickname;
303             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
304             $this->element('foaf:nick', null, $nickname);
305             if ($profile->fullname) {
306                 $this->element('foaf:name', null, $profile->fullname);
307             }
308             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
309             $avatar = $profile->avatarUrl();
310             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
311             $this->elementEnd('foaf:Agent');
312         }
313     }
314
315     function initRss()
316     {
317         $channel = $this->getChannel();
318         header('Content-Type: application/rdf+xml');
319
320         $this->startXml();
321         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
322                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
323                                               'xmlns:dc' =>
324                                               'http://purl.org/dc/elements/1.1/',
325                                               'xmlns:cc' =>
326                                               'http://creativecommons.org/ns#',
327                                               'xmlns:content' =>
328                                               'http://purl.org/rss/1.0/modules/content/',
329                                               'xmlns:ctag' =>
330                                               'http://commontag.org/ns#',
331                                               'xmlns:foaf' =>
332                                               'http://xmlns.com/foaf/0.1/',
333                                               'xmlns:sioc' =>
334                                               'http://rdfs.org/sioc/ns#',
335                                               'xmlns:sioct' =>
336                                               'http://rdfs.org/sioc/types#',
337                                               'xmlns:rdfs' =>
338                                               'http://www.w3.org/2000/01/rdf-schema#',
339                                               'xmlns:laconica' =>
340                                               'http://laconi.ca/ont/',
341                                               'xmlns' => 'http://purl.org/rss/1.0/'));
342         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
343         $this->element('sioc:name', null, common_config('site', 'name'));
344         $this->elementStart('sioc:space_of');
345         $this->element('sioc:Container', array('rdf:about' =>
346                                                $channel['url']));
347         $this->elementEnd('sioc:space_of');
348         $this->elementEnd('sioc:Site');
349     }
350
351     function endRss()
352     {
353         $this->elementEnd('rdf:RDF');
354     }
355
356     /**
357      * When was this page last modified?
358      *
359      */
360
361     function lastModified()
362     {
363         if (empty($this->notices)) {
364             return null;
365         }
366
367         if (count($this->notices) == 0) {
368             return null;
369         }
370
371         // FIXME: doesn't handle modified profiles, avatars, deleted notices
372
373         return strtotime($this->notices[0]->created);
374     }
375 }
376