]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
Improve output for files/attachments.
[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
43     /**
44      * Constructor
45      *
46      * Just wraps the Action constructor.
47      *
48      * @param string  $output URI to output to, default = stdout
49      * @param boolean $indent Whether to indent output, default true
50      *
51      * @see Action::__construct
52      */
53
54     function __construct($output='php://output', $indent=true)
55     {
56         parent::__construct($output, $indent);
57     }
58
59     /**
60      * Do we need to write to the database?
61      *
62      * @return boolean true
63      */
64
65     function isReadonly()
66     {
67         return true;
68     }
69
70     /**
71      * Read arguments and initialize members
72      *
73      * @param array $args Arguments from $_REQUEST
74      * @return boolean success
75      */
76
77     function prepare($args)
78     {
79         parent::prepare($args);
80         $this->limit = (int) $this->trimmed('limit');
81         if ($this->limit == 0) {
82             $this->limit = DEFAULT_RSS_LIMIT;
83         }
84         return true;
85     }
86
87     /**
88      * Handle a request
89      *
90      * @param array $args Arguments from $_REQUEST
91      *
92      * @return void
93      */
94
95     function handle($args)
96     {
97         // Parent handling, including cache check
98         parent::handle($args);
99         // Get the list of notices
100         if (empty($this->tag)) {
101             $this->notices = $this->getNotices($this->limit);
102         } else {
103             $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
104         }
105         $this->showRss();
106     }
107
108     /**
109      * Get the notices to output in this stream
110      *
111      * @return array an array of Notice objects sorted in reverse chron
112      */
113
114     function getNotices()
115     {
116         return array();
117     }
118
119     /**
120      * Get a description of the channel
121      *
122      * Returns an array with the following
123      * @return array
124      */
125
126     function getChannel()
127     {
128         return array('url' => '',
129                      'title' => '',
130                      'link' => '',
131                      'description' => '');
132     }
133
134     function getImage()
135     {
136         return null;
137     }
138
139     function showRss()
140     {
141         $this->initRss();
142         $this->showChannel();
143         $this->showImage();
144
145         foreach ($this->notices as $n) {
146             $this->showItem($n);
147         }
148
149         $this->showCreators();
150         $this->endRss();
151     }
152
153     function showChannel()
154     {
155
156         $channel = $this->getChannel();
157         $image = $this->getImage();
158
159         $this->elementStart('channel', array('rdf:about' => $channel['url']));
160         $this->element('title', null, $channel['title']);
161         $this->element('link', null, $channel['link']);
162         $this->element('description', null, $channel['description']);
163         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
164
165         if ($image) {
166             $this->element('image', array('rdf:resource' => $image));
167         }
168
169         $this->elementStart('items');
170         $this->elementStart('rdf:Seq');
171
172         foreach ($this->notices as $notice) {
173             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
174         }
175
176         $this->elementEnd('rdf:Seq');
177         $this->elementEnd('items');
178
179         $this->elementEnd('channel');
180     }
181
182     function showImage()
183     {
184         $image = $this->getImage();
185         if ($image) {
186             $channel = $this->getChannel();
187             $this->elementStart('image', array('rdf:about' => $image));
188             $this->element('title', null, $channel['title']);
189             $this->element('link', null, $channel['link']);
190             $this->element('url', null, $image);
191             $this->elementEnd('image');
192         }
193     }
194
195     function showItem($notice)
196     {
197         $profile = Profile::staticGet($notice->profile_id);
198         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
199         $creator_uri = common_profile_uri($profile);
200         $this->elementStart('item', array('rdf:about' => $notice->uri,
201                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
202         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
203         $this->element('title', null, $title);
204         $this->element('link', null, $nurl);
205         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
206         if ($notice->rendered) {
207             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
208         }
209         $this->element('dc:date', null, common_date_w3dtf($notice->created));
210         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
211         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
212         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
213         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
214         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
215         if ($notice->reply_to) {
216             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
217             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
218         }
219         $attachments = $notice->attachments();
220         if($attachments){
221             foreach($attachments as $attachment){
222                 if (isset($attachment->filename)) {
223                     // DO NOT move xmlns declaration to root element. Making it
224                     // the default namespace here improves compatibility with
225                     // real-world feed readers.
226                     $attribs = array(
227                         'rdf:resource' => $attachment->url,
228                         'url' => $attachment->url,
229                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
230                         );
231                     if ($attachment->title) {
232                         $attribs['dc:title'] = $attachment->title;
233                     }
234                     if ($attachment->modified) {
235                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
236                     }
237                     if ($attachment->size) {
238                         $attribs['length'] = $attachment->size;
239                     }
240                     if ($attachment->mimetype) {
241                         $attribs['type'] = $attachment->mimetype;
242                     }
243                     $this->element('enclosure', $attribs);
244                 }
245                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
246             }
247         }
248
249         $this->elementEnd('item');
250         $this->creators[$creator_uri] = $profile;
251     }
252
253     function showCreators()
254     {
255         foreach ($this->creators as $uri => $profile) {
256             $id = $profile->id;
257             $nickname = $profile->nickname;
258             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
259             $this->element('foaf:nick', null, $nickname);
260             if ($profile->fullname) {
261                 $this->element('foaf:name', null, $profile->fullname);
262             }
263             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
264             $avatar = $profile->avatarUrl();
265             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
266             $this->elementEnd('foaf:Agent');
267         }
268     }
269
270     function initRss()
271     {
272         $channel = $this->getChannel();
273         header('Content-Type: application/rdf+xml');
274
275         $this->startXml();
276         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
277                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
278                                               'xmlns:dc' =>
279                                               'http://purl.org/dc/elements/1.1/',
280                                               'xmlns:cc' =>
281                                               'http://creativecommons.org/ns#',
282                                               'xmlns:content' =>
283                                               'http://purl.org/rss/1.0/modules/content/',
284                                               'xmlns:foaf' =>
285                                               'http://xmlns.com/foaf/0.1/',
286                                               'xmlns:sioc' =>
287                                               'http://rdfs.org/sioc/ns#',
288                                               'xmlns:sioct' =>
289                                               'http://rdfs.org/sioc/types#',
290                                               'xmlns:laconica' =>
291                                               'http://laconi.ca/ont/',
292                                               'xmlns' => 'http://purl.org/rss/1.0/'));
293         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
294         $this->element('sioc:name', null, common_config('site', 'name'));
295         $this->elementStart('sioc:space_of');
296         $this->element('sioc:Container', array('rdf:about' =>
297                                                $channel['url']));
298         $this->elementEnd('sioc:space_of');
299         $this->elementEnd('sioc:Site');
300     }
301
302     function endRss()
303     {
304         $this->elementEnd('rdf:RDF');
305     }
306
307     /**
308      * When was this page last modified?
309      *
310      */
311
312     function lastModified()
313     {
314         if (empty($this->notices)) {
315             return null;
316         }
317
318         if (count($this->notices) == 0) {
319             return null;
320         }
321
322         // FIXME: doesn't handle modified profiles, avatars, deleted notices
323
324         return strtotime($this->notices[0]->created);
325     }
326 }
327