3 * Laconica, the distributed open-source microblogging tool
5 * Base class for RSS 1.0 feed actions
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.
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.
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/>.
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/
31 if (!defined('LACONICA')) { exit(1); }
33 define('DEFAULT_RSS_LIMIT', 48);
35 class Rss10Action extends Action
37 # This will contain the details of each feed item's author and be used to generate SIOC data.
39 var $creators = array();
40 var $limit = DEFAULT_RSS_LIMIT;
42 var $tags_already_output = array();
47 * Just wraps the Action constructor.
49 * @param string $output URI to output to, default = stdout
50 * @param boolean $indent Whether to indent output, default true
52 * @see Action::__construct
55 function __construct($output='php://output', $indent=true)
57 parent::__construct($output, $indent);
61 * Do we need to write to the database?
63 * @return boolean true
72 * Read arguments and initialize members
74 * @param array $args Arguments from $_REQUEST
75 * @return boolean success
78 function prepare($args)
80 parent::prepare($args);
81 $this->limit = (int) $this->trimmed('limit');
82 if ($this->limit == 0) {
83 $this->limit = DEFAULT_RSS_LIMIT;
91 * @param array $args Arguments from $_REQUEST
96 function handle($args)
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);
104 $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
110 * Get the notices to output in this stream
112 * @return array an array of Notice objects sorted in reverse chron
115 function getNotices()
121 * Get a description of the channel
123 * Returns an array with the following
127 function getChannel()
129 return array('url' => '',
132 'description' => '');
143 $this->showChannel();
146 foreach ($this->notices as $n) {
150 $this->showCreators();
154 function showChannel()
157 $channel = $this->getChannel();
158 $image = $this->getImage();
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')));
167 $this->element('image', array('rdf:resource' => $image));
170 $this->elementStart('items');
171 $this->elementStart('rdf:Seq');
173 foreach ($this->notices as $notice) {
174 $this->element('rdf:li', array('rdf:resource' => $notice->uri));
177 $this->elementEnd('rdf:Seq');
178 $this->elementEnd('items');
180 $this->elementEnd('channel');
185 $image = $this->getImage();
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');
196 function showItem($notice)
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));
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));
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));
225 $attachments = $notice->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.
233 'rdf:resource' => $attachment->url,
234 'url' => $attachment->url,
235 'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
237 if ($attachment->title) {
238 $attribs['dc:title'] = $attachment->title;
240 if ($attachment->modified) {
241 $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
243 if ($attachment->size) {
244 $attribs['length'] = $attachment->size;
246 if ($attachment->mimetype) {
247 $attribs['type'] = $attachment->mimetype;
249 $this->element('enclosure', $attribs);
251 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
255 $tag = new Notice_tag();
256 $tag->notice_id = $notice->id;
258 $entry['tags']=array();
259 while ($tag->fetch()) {
260 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
262 if ( in_array($tag, $this->tags_already_output) ) {
263 $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
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');
275 $this->tags_already_output[] = $tag->tag;
278 $this->elementEnd('item');
279 $this->creators[$creator_uri] = $profile;
282 function showCreators()
284 foreach ($this->creators as $uri => $profile) {
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);
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');
301 $channel = $this->getChannel();
302 header('Content-Type: application/rdf+xml');
305 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
306 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
308 'http://purl.org/dc/elements/1.1/',
310 'http://creativecommons.org/ns#',
312 'http://purl.org/rss/1.0/modules/content/',
314 'http://commontag.org/ns#',
316 'http://xmlns.com/foaf/0.1/',
318 'http://rdfs.org/sioc/ns#',
320 'http://rdfs.org/sioc/types#',
322 'http://www.w3.org/2000/01/rdf-schema#',
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' =>
331 $this->elementEnd('sioc:space_of');
332 $this->elementEnd('sioc:Site');
337 $this->elementEnd('rdf:RDF');
341 * When was this page last modified?
345 function lastModified()
347 if (empty($this->notices)) {
351 if (count($this->notices) == 0) {
355 // FIXME: doesn't handle modified profiles, avatars, deleted notices
357 return strtotime($this->notices[0]->created);