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;
46 * Just wraps the Action constructor.
48 * @param string $output URI to output to, default = stdout
49 * @param boolean $indent Whether to indent output, default true
51 * @see Action::__construct
54 function __construct($output='php://output', $indent=true)
56 parent::__construct($output, $indent);
60 * Do we need to write to the database?
62 * @return boolean true
71 * Read arguments and initialize members
73 * @param array $args Arguments from $_REQUEST
74 * @return boolean success
77 function prepare($args)
79 parent::prepare($args);
80 $this->limit = (int) $this->trimmed('limit');
81 if ($this->limit == 0) {
82 $this->limit = DEFAULT_RSS_LIMIT;
90 * @param array $args Arguments from $_REQUEST
95 function handle($args)
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);
103 $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
109 * Get the notices to output in this stream
111 * @return array an array of Notice objects sorted in reverse chron
114 function getNotices()
120 * Get a description of the channel
122 * Returns an array with the following
126 function getChannel()
128 return array('url' => '',
131 'description' => '');
142 $this->showChannel();
145 foreach ($this->notices as $n) {
149 $this->showCreators();
153 function showChannel()
156 $channel = $this->getChannel();
157 $image = $this->getImage();
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')));
166 $this->element('image', array('rdf:resource' => $image));
169 $this->elementStart('items');
170 $this->elementStart('rdf:Seq');
172 foreach ($this->notices as $notice) {
173 $this->element('rdf:li', array('rdf:resource' => $notice->uri));
176 $this->elementEnd('rdf:Seq');
177 $this->elementEnd('items');
179 $this->elementEnd('channel');
184 $image = $this->getImage();
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');
195 // XXX: Surely there should be a common function to do this?
196 function extract_tags ($string)
198 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($string), $match);
205 foreach ($match[1] as $tag)
207 $rv[] = common_canonical_tag($tag);
210 return array_unique($rv);
213 function showItem($notice)
215 $profile = Profile::staticGet($notice->profile_id);
216 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
217 $creator_uri = common_profile_uri($profile);
218 $this->elementStart('item', array('rdf:about' => $notice->uri,
219 'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
220 $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
221 $this->element('title', null, $title);
222 $this->element('link', null, $nurl);
223 $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
224 if ($notice->rendered) {
225 $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
227 $this->element('dc:date', null, common_date_w3dtf($notice->created));
228 $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
229 $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
230 $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
231 $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
232 $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
233 if ($notice->reply_to) {
234 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
235 $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
237 $attachments = $notice->attachments();
239 foreach($attachments as $attachment){
240 if ($attachment->isEnclosure()) {
241 // DO NOT move xmlns declaration to root element. Making it
242 // the default namespace here improves compatibility with
243 // real-world feed readers.
245 'rdf:resource' => $attachment->url,
246 'url' => $attachment->url,
247 'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
249 if ($attachment->title) {
250 $attribs['dc:title'] = $attachment->title;
252 if ($attachment->modified) {
253 $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
255 if ($attachment->size) {
256 $attribs['length'] = $attachment->size;
258 if ($attachment->mimetype) {
259 $attribs['type'] = $attachment->mimetype;
261 $this->element('enclosure', $attribs);
263 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
266 $tags = $this->extract_tags($notice->content);
268 foreach ($tags as $tag)
270 $tagpage = common_local_url('tag', array('tag' => $tag));
271 $tagrss = common_local_url('tagrss', array('tag' => $tag));
272 $this->elementStart('ctag:tagged');
273 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag));
274 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
275 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
276 $this->elementEnd('ctag:Tag');
277 $this->elementEnd('ctag:tagged');
280 $this->elementEnd('item');
281 $this->creators[$creator_uri] = $profile;
284 function showCreators()
286 foreach ($this->creators as $uri => $profile) {
288 $nickname = $profile->nickname;
289 $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
290 $this->element('foaf:nick', null, $nickname);
291 if ($profile->fullname) {
292 $this->element('foaf:name', null, $profile->fullname);
294 $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
295 $avatar = $profile->avatarUrl();
296 $this->element('foaf:depiction', array('rdf:resource' => $avatar));
297 $this->elementEnd('foaf:Agent');
303 $channel = $this->getChannel();
304 header('Content-Type: application/rdf+xml');
307 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
308 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
310 'http://purl.org/dc/elements/1.1/',
312 'http://creativecommons.org/ns#',
314 'http://purl.org/rss/1.0/modules/content/',
316 'http://commontag.org/ns#',
318 'http://xmlns.com/foaf/0.1/',
320 'http://rdfs.org/sioc/ns#',
322 'http://rdfs.org/sioc/types#',
324 'http://www.w3.org/2000/01/rdf-schema#',
326 'http://laconi.ca/ont/',
327 'xmlns' => 'http://purl.org/rss/1.0/'));
328 $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
329 $this->element('sioc:name', null, common_config('site', 'name'));
330 $this->elementStart('sioc:space_of');
331 $this->element('sioc:Container', array('rdf:about' =>
333 $this->elementEnd('sioc:space_of');
334 $this->elementEnd('sioc:Site');
339 $this->elementEnd('rdf:RDF');
343 * When was this page last modified?
347 function lastModified()
349 if (empty($this->notices)) {
353 if (count($this->notices) == 0) {
357 // FIXME: doesn't handle modified profiles, avatars, deleted notices
359 return strtotime($this->notices[0]->created);