]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
Update version number
[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
42     /**
43      * Constructor
44      *
45      * Just wraps the Action constructor.
46      *
47      * @param string  $output URI to output to, default = stdout
48      * @param boolean $indent Whether to indent output, default true
49      *
50      * @see Action::__construct
51      */
52
53     function __construct($output='php://output', $indent=true)
54     {
55         parent::__construct($output, $indent);
56     }
57
58     /**
59      * Do we need to write to the database?
60      *
61      * @return boolean true
62      */
63
64     function isReadonly()
65     {
66         return true;
67     }
68
69     /**
70      * Read arguments and initialize members
71      *
72      * @param array $args Arguments from $_REQUEST
73      * @return boolean success
74      */
75
76     function prepare($args)
77     {
78         parent::prepare($args);
79         $this->limit = (int) $this->trimmed('limit');
80         if ($this->limit == 0) {
81             $this->limit = DEFAULT_RSS_LIMIT;
82         }
83         return true;
84     }
85
86     /**
87      * Handle a request
88      *
89      * @param array $args Arguments from $_REQUEST
90      *
91      * @return void
92      */
93
94     function handle($args)
95     {
96         parent::handle($args);
97         $this->showRss($this->limit);
98     }
99
100     /**
101      * Get the notices to output in this stream
102      *
103      * @return array an array of Notice objects sorted in reverse chron
104      */
105
106     function getNotices()
107     {
108         return array();
109     }
110
111     /**
112      * Get a description of the channel
113      *
114      * Returns an array with the following
115      * @return array
116      */
117
118     function getChannel()
119     {
120         return array('url' => '',
121                      'title' => '',
122                      'link' => '',
123                      'description' => '');
124     }
125
126     function getImage()
127     {
128         return null;
129     }
130
131     function showRss($limit=0)
132     {
133         $notices = $this->getNotices($limit);
134
135         $this->initRss();
136         $this->showChannel($notices);
137         $this->showImage();
138
139         foreach ($notices as $n) {
140             $this->showItem($n);
141         }
142
143         $this->showCreators();
144         $this->endRss();
145     }
146
147     function showChannel($notices)
148     {
149
150         $channel = $this->getChannel();
151         $image = $this->getImage();
152
153         $this->elementStart('channel', array('rdf:about' => $channel['url']));
154         $this->element('title', null, $channel['title']);
155         $this->element('link', null, $channel['link']);
156         $this->element('description', null, $channel['description']);
157         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
158
159         if ($image) {
160             $this->element('image', array('rdf:resource' => $image));
161         }
162
163         $this->elementStart('items');
164         $this->elementStart('rdf:Seq');
165
166         foreach ($notices as $notice) {
167             $this->element('sioct:MicroblogPost', array('rdf:resource' => $notice->uri));
168         }
169
170         $this->elementEnd('rdf:Seq');
171         $this->elementEnd('items');
172
173         $this->elementEnd('channel');
174     }
175
176     function showImage()
177     {
178         $image = $this->getImage();
179         if ($image) {
180             $channel = $this->getChannel();
181             common_element_start('image', array('rdf:about' => $image));
182             common_element('title', null, $channel['title']);
183             common_element('link', null, $channel['link']);
184             common_element('url', null, $image);
185             common_element_end('image');
186         }
187     }
188
189     function showItem($notice)
190     {
191         $profile = Profile::staticGet($notice->profile_id);
192         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
193         $creator_uri = common_profile_uri($profile);
194         $this->elementStart('item', array('rdf:about' => $notice->uri));
195         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
196         $this->element('title', null, $title);
197         $this->element('link', null, $nurl);
198         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
199         $this->element('dc:date', null, common_date_w3dtf($notice->created));
200         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
201         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri));
202         $this->element('laconica:postIcon', array('rdf:resource' => common_profile_avatar_url($profile)));
203         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
204         $this->elementEnd('item');
205         $this->creators[$creator_uri] = $profile;
206     }
207
208     function showCreators()
209     {
210         foreach ($this->creators as $uri => $profile) {
211             $id = $profile->id;
212             $nickname = $profile->nickname;
213             $this->elementStart('sioc:User', array('rdf:about' => $uri));
214             $this->element('foaf:nick', null, $nickname);
215             if ($profile->fullname) {
216                 $this->element('foaf:name', null, $profile->fullname);
217             }
218             $this->element('sioc:id', null, $id);
219             $avatar = common_profile_avatar_url($profile);
220             $this->element('sioc:avatar', array('rdf:resource' => $avatar));
221             $this->elementEnd('sioc:User');
222         }
223     }
224
225     function initRss()
226     {
227         $channel = $this->getChannel();
228         header('Content-Type: application/rdf+xml');
229
230         $this->startXml();
231         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
232                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
233                                               'xmlns:dc' =>
234                                               'http://purl.org/dc/elements/1.1/',
235                                               'xmlns:cc' =>
236                                               'http://web.resource.org/cc/',
237                                               'xmlns:content' =>
238                                               'http://purl.org/rss/1.0/modules/content/',
239                                               'xmlns:foaf' =>
240                                               'http://xmlns.com/foaf/0.1/',
241                                               'xmlns:sioc' =>
242                                               'http://rdfs.org/sioc/ns#',
243                                               'xmlns:sioct' =>
244                                               'http://rdfs.org/sioc/types#',
245                                               'xmlns:laconica' =>
246                                               'http://laconi.ca/ont/',
247                                               'xmlns' => 'http://purl.org/rss/1.0/'));
248         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
249         $this->element('sioc:name', null, common_config('site', 'name'));
250         $this->elementStart('sioc:container_of');
251         $this->element('sioc:Container', array('rdf:about' =>
252                                                $channel['url']));
253         $this->elementEnd('sioc:container_of');
254         $this->elementEnd('sioc:Site');
255     }
256
257     function endRss()
258     {
259         $this->elementEnd('rdf:RDF');
260     }
261 }
262