]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
Add <category> to RSS 2.0, use the same tag finding method for RSS 1.0 as for Atom...
[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     function showItem($notice)
197     {
198         $profile = Profile::staticGet($notice->profile_id);
199         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
200         $creator_uri = common_profile_uri($profile);
201         $this->elementStart('item', array('rdf:about' => $notice->uri,
202                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
203         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
204         $this->element('title', null, $title);
205         $this->element('link', null, $nurl);
206         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
207         if ($notice->rendered) {
208             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
209         }
210         $this->element('dc:date', null, common_date_w3dtf($notice->created));
211         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
212         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
213         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
214         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
215         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
216         if ($notice->reply_to) {
217             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
218             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
219         }
220         if (!empty($notice->conversation)) {
221             $conversationurl = common_local_url('conversation',
222                                          array('id' => $notice->conversation));
223             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
224         }
225         $attachments = $notice->attachments();
226         if($attachments){
227             foreach($attachments as $attachment){
228                 if ($attachment->isEnclosure()) {
229                     // DO NOT move xmlns declaration to root element. Making it
230                     // the default namespace here improves compatibility with
231                     // real-world feed readers.
232                     $attribs = array(
233                         'rdf:resource' => $attachment->url,
234                         'url' => $attachment->url,
235                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
236                         );
237                     if ($attachment->title) {
238                         $attribs['dc:title'] = $attachment->title;
239                     }
240                     if ($attachment->modified) {
241                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
242                     }
243                     if ($attachment->size) {
244                         $attribs['length'] = $attachment->size;
245                     }
246                     if ($attachment->mimetype) {
247                         $attribs['type'] = $attachment->mimetype;
248                     }
249                     $this->element('enclosure', $attribs);
250                 }
251                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
252             }
253         }
254
255         $tag = new Notice_tag();
256         $tag->notice_id = $notice->id;
257         if ($tag->find()) {
258             $entry['tags']=array();
259             while ($tag->fetch()) {
260                 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
261
262                 if ( in_array($tag, $this->tags_already_output) ) {
263                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
264                     continue;
265                 }
266
267                 $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
268                 $this->elementStart('ctag:tagged');
269                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
270                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
271                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
272                 $this->elementEnd('ctag:Tag');
273                 $this->elementEnd('ctag:tagged');
274
275                 $this->tags_already_output[] = $tag->tag;
276             }
277         }
278         $this->elementEnd('item');
279         $this->creators[$creator_uri] = $profile;
280     }
281
282     function showCreators()
283     {
284         foreach ($this->creators as $uri => $profile) {
285             $id = $profile->id;
286             $nickname = $profile->nickname;
287             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
288             $this->element('foaf:nick', null, $nickname);
289             if ($profile->fullname) {
290                 $this->element('foaf:name', null, $profile->fullname);
291             }
292             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
293             $avatar = $profile->avatarUrl();
294             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
295             $this->elementEnd('foaf:Agent');
296         }
297     }
298
299     function initRss()
300     {
301         $channel = $this->getChannel();
302         header('Content-Type: application/rdf+xml');
303
304         $this->startXml();
305         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
306                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
307                                               'xmlns:dc' =>
308                                               'http://purl.org/dc/elements/1.1/',
309                                               'xmlns:cc' =>
310                                               'http://creativecommons.org/ns#',
311                                               'xmlns:content' =>
312                                               'http://purl.org/rss/1.0/modules/content/',
313                                               'xmlns:ctag' =>
314                                               'http://commontag.org/ns#',
315                                               'xmlns:foaf' =>
316                                               'http://xmlns.com/foaf/0.1/',
317                                               'xmlns:sioc' =>
318                                               'http://rdfs.org/sioc/ns#',
319                                               'xmlns:sioct' =>
320                                               'http://rdfs.org/sioc/types#',
321                                               'xmlns:rdfs' =>
322                                               'http://www.w3.org/2000/01/rdf-schema#',
323                                               'xmlns:laconica' =>
324                                               'http://laconi.ca/ont/',
325                                               'xmlns' => 'http://purl.org/rss/1.0/'));
326         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
327         $this->element('sioc:name', null, common_config('site', 'name'));
328         $this->elementStart('sioc:space_of');
329         $this->element('sioc:Container', array('rdf:about' =>
330                                                $channel['url']));
331         $this->elementEnd('sioc:space_of');
332         $this->elementEnd('sioc:Site');
333     }
334
335     function endRss()
336     {
337         $this->elementEnd('rdf:RDF');
338     }
339
340     /**
341      * When was this page last modified?
342      *
343      */
344
345     function lastModified()
346     {
347         if (empty($this->notices)) {
348             return null;
349         }
350
351         if (count($this->notices) == 0) {
352             return null;
353         }
354
355         // FIXME: doesn't handle modified profiles, avatars, deleted notices
356
357         return strtotime($this->notices[0]->created);
358     }
359 }
360