]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
3b9f0fb4747bb1579a0dab00e553e8df6cd48c54
[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         $this->notices = $this->getNotices($this->limit);
101         $this->showRss();
102     }
103
104     /**
105      * Get the notices to output in this stream
106      *
107      * @return array an array of Notice objects sorted in reverse chron
108      */
109
110     function getNotices()
111     {
112         return array();
113     }
114
115     /**
116      * Get a description of the channel
117      *
118      * Returns an array with the following
119      * @return array
120      */
121
122     function getChannel()
123     {
124         return array('url' => '',
125                      'title' => '',
126                      'link' => '',
127                      'description' => '');
128     }
129
130     function getImage()
131     {
132         return null;
133     }
134
135     function showRss()
136     {
137         $this->initRss();
138         $this->showChannel();
139         $this->showImage();
140
141         foreach ($this->notices as $n) {
142             $this->showItem($n);
143         }
144
145         $this->showCreators();
146         $this->endRss();
147     }
148
149     function showChannel()
150     {
151
152         $channel = $this->getChannel();
153         $image = $this->getImage();
154
155         $this->elementStart('channel', array('rdf:about' => $channel['url']));
156         $this->element('title', null, $channel['title']);
157         $this->element('link', null, $channel['link']);
158         $this->element('description', null, $channel['description']);
159         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
160
161         if ($image) {
162             $this->element('image', array('rdf:resource' => $image));
163         }
164
165         $this->elementStart('items');
166         $this->elementStart('rdf:Seq');
167
168         foreach ($this->notices as $notice) {
169             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
170         }
171
172         $this->elementEnd('rdf:Seq');
173         $this->elementEnd('items');
174
175         $this->elementEnd('channel');
176     }
177
178     function showImage()
179     {
180         $image = $this->getImage();
181         if ($image) {
182             $channel = $this->getChannel();
183             $this->elementStart('image', array('rdf:about' => $image));
184             $this->element('title', null, $channel['title']);
185             $this->element('link', null, $channel['link']);
186             $this->element('url', null, $image);
187             $this->elementEnd('image');
188         }
189     }
190
191     // XXX: Surely there should be a common function to do this?
192     function extract_tags ($string)
193     {
194         $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($string), $match);
195         if (!count)
196         {
197             return array();
198         }
199
200         $rv = array();
201         foreach ($match[1] as $tag)
202         {
203             $rv[] = common_canonical_tag($tag);
204         } 
205
206         return array_unique($rv);
207     }
208          
209     function showItem($notice)
210     {
211         $profile = Profile::staticGet($notice->profile_id);
212         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
213         $creator_uri = common_profile_uri($profile);
214         $this->elementStart('item', array('rdf:about' => $notice->uri,
215                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
216         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
217         $this->element('title', null, $title);
218         $this->element('link', null, $nurl);
219         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
220         if ($notice->rendered) {
221             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
222         }
223         $this->element('dc:date', null, common_date_w3dtf($notice->created));
224         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
225         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
226         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
227         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
228         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
229         if ($notice->reply_to) {
230             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
231             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
232         }
233         $attachments = $notice->attachments();
234         if($attachments){
235             foreach($attachments as $attachment){
236                 if ($attachment->isEnclosure()) {
237                     // DO NOT move xmlns declaration to root element. Making it
238                     // the default namespace here improves compatibility with
239                     // real-world feed readers.
240                     $attribs = array(
241                         'rdf:resource' => $attachment->url,
242                         'url' => $attachment->url,
243                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
244                         );
245                     if ($attachment->title) {
246                         $attribs['dc:title'] = $attachment->title;
247                     }
248                     if ($attachment->modified) {
249                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
250                     }
251                     if ($attachment->size) {
252                         $attribs['length'] = $attachment->size;
253                     }
254                     if ($attachment->mimetype) {
255                         $attribs['type'] = $attachment->mimetype;
256                     }
257                     $this->element('enclosure', $attribs);
258                 }
259                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
260             }
261         }
262         $tags = $this->extract_tags($notice->content);
263         if (!empty($tags)) {
264             foreach ($tags as $tag)
265             {
266                 $tagpage = common_local_url('tag', array('tag' => $tag));
267                 $tagrss  = common_local_url('tagrss', array('tag' => $tag));
268                 $this->elementStart('ctag:tagged');
269                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$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         }
276         $this->elementEnd('item');
277         $this->creators[$creator_uri] = $profile;
278     }
279
280     function showCreators()
281     {
282         foreach ($this->creators as $uri => $profile) {
283             $id = $profile->id;
284             $nickname = $profile->nickname;
285             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
286             $this->element('foaf:nick', null, $nickname);
287             if ($profile->fullname) {
288                 $this->element('foaf:name', null, $profile->fullname);
289             }
290             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
291             $avatar = $profile->avatarUrl();
292             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
293             $this->elementEnd('foaf:Agent');
294         }
295     }
296
297     function initRss()
298     {
299         $channel = $this->getChannel();
300         header('Content-Type: application/rdf+xml');
301
302         $this->startXml();
303         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
304                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
305                                               'xmlns:dc' =>
306                                               'http://purl.org/dc/elements/1.1/',
307                                               'xmlns:cc' =>
308                                               'http://creativecommons.org/ns#',
309                                               'xmlns:content' =>
310                                               'http://purl.org/rss/1.0/modules/content/',
311                                               'xmlns:ctag' =>
312                                               'http://commontag.org/ns#',
313                                               'xmlns:foaf' =>
314                                               'http://xmlns.com/foaf/0.1/',
315                                               'xmlns:sioc' =>
316                                               'http://rdfs.org/sioc/ns#',
317                                               'xmlns:sioct' =>
318                                               'http://rdfs.org/sioc/types#',
319                                               'xmlns:rdfs' =>
320                                               'http://www.w3.org/2000/01/rdf-schema#',
321                                               'xmlns:laconica' =>
322                                               'http://laconi.ca/ont/',
323                                               'xmlns' => 'http://purl.org/rss/1.0/'));
324         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
325         $this->element('sioc:name', null, common_config('site', 'name'));
326         $this->elementStart('sioc:space_of');
327         $this->element('sioc:Container', array('rdf:about' =>
328                                                $channel['url']));
329         $this->elementEnd('sioc:space_of');
330         $this->elementEnd('sioc:Site');
331     }
332
333     function endRss()
334     {
335         $this->elementEnd('rdf:RDF');
336     }
337
338     /**
339      * When was this page last modified?
340      *
341      */
342
343     function lastModified()
344     {
345         if (empty($this->notices)) {
346             return null;
347         }
348
349         if (count($this->notices) == 0) {
350             return null;
351         }
352
353         // FIXME: doesn't handle modified profiles, avatars, deleted notices
354
355         return strtotime($this->notices[0]->created);
356     }
357 }
358