]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
change function headers to K&R style
[quix0rs-gnu-social.git] / lib / rssaction.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 define('DEFAULT_RSS_LIMIT', 48);
23
24 class Rss10Action extends Action {
25
26     # This will contain the details of each feed item's author and be used to generate SIOC data.
27     var $creators = array();
28
29     function is_readonly()
30     {
31         return true;
32     }
33
34     function handle($args)
35     {
36         parent::handle($args);
37         $limit = (int) $this->trimmed('limit');
38         if ($limit == 0) {
39             $limit = DEFAULT_RSS_LIMIT;
40         }
41         $this->show_rss($limit);
42     }
43
44     function init()
45     {
46         return true;
47     }
48
49     function get_notices()
50     {
51         return array();
52     }
53
54     function get_channel()
55     {
56         return array('url' => '',
57                      'title' => '',
58                      'link' => '',
59                      'description' => '');
60     }
61
62     function get_image()
63     {
64         return null;
65     }
66
67     function show_rss($limit=0)
68     {
69
70         if (!$this->init()) {
71             return;
72         }
73
74         $notices = $this->get_notices($limit);
75
76         $this->init_rss();
77         $this->show_channel($notices);
78         $this->show_image();
79
80         foreach ($notices as $n) {
81             $this->show_item($n);
82         }
83
84         $this->show_creators();
85         $this->end_rss();
86     }
87
88     function show_channel($notices)
89     {
90
91         $channel = $this->get_channel();
92         $image = $this->get_image();
93
94         common_element_start('channel', array('rdf:about' => $channel['url']));
95         common_element('title', null, $channel['title']);
96         common_element('link', null, $channel['link']);
97         common_element('description', null, $channel['description']);
98         common_element('cc:licence', array('rdf:resource' => common_config('license','url')));
99
100         if ($image) {
101             common_element('image', array('rdf:resource' => $image));
102         }
103
104         common_element_start('items');
105         common_element_start('rdf:Seq');
106
107         foreach ($notices as $notice) {
108             common_element('sioct:MicroblogPost', array('rdf:resource' => $notice->uri));
109         }
110
111         common_element_end('rdf:Seq');
112         common_element_end('items');
113
114         common_element_end('channel');
115     }
116
117     function show_image()
118     {
119         $image = $this->get_image();
120         if ($image) {
121             $channel = $this->get_channel();
122             common_element_start('image', array('rdf:about' => $image));
123             common_element('title', null, $channel['title']);
124             common_element('link', null, $channel['link']);
125             common_element('url', null, $image);
126             common_element_end('image');
127         }
128     }
129
130     function show_item($notice)
131     {
132         $profile = Profile::staticGet($notice->profile_id);
133         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
134         $creator_uri = common_profile_uri($profile);
135         common_element_start('item', array('rdf:about' => $notice->uri));
136         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
137         common_element('title', null, $title);
138         common_element('link', null, $nurl);
139         common_element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
140         common_element('dc:date', null, common_date_w3dtf($notice->created));
141         common_element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
142         common_element('sioc:has_creator', array('rdf:resource' => $creator_uri));
143         common_element('laconica:postIcon', array('rdf:resource' => common_profile_avatar_url($profile)));
144         common_element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
145         common_element_end('item');
146         $this->creators[$creator_uri] = $profile;
147     }
148
149     function show_creators()
150     {
151         foreach ($this->creators as $uri => $profile) {
152             $id = $profile->id;
153             $nickname = $profile->nickname;
154             common_element_start('sioc:User', array('rdf:about' => $uri));
155             common_element('foaf:nick', null, $nickname);
156             if ($profile->fullname) {
157                 common_element('foaf:name', null, $profile->fullname);
158             }
159             common_element('sioc:id', null, $id);
160             $avatar = common_profile_avatar_url($profile);
161             common_element('sioc:avatar', array('rdf:resource' => $avatar));
162             common_element_end('sioc:User');
163         }
164     }
165
166     function init_rss()
167     {
168         $channel = $this->get_channel();
169         header('Content-Type: application/rdf+xml');
170
171         common_start_xml();
172         common_element_start('rdf:RDF', array('xmlns:rdf' =>
173                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
174                                               'xmlns:dc' =>
175                                               'http://purl.org/dc/elements/1.1/',
176                                               'xmlns:cc' =>
177                                               'http://web.resource.org/cc/',
178                                               'xmlns:content' =>
179                                               'http://purl.org/rss/1.0/modules/content/',
180                                               'xmlns:foaf' =>
181                                               'http://xmlns.com/foaf/0.1/',
182                                               'xmlns:sioc' =>
183                                               'http://rdfs.org/sioc/ns#',
184                                               'xmlns:sioct' =>
185                                               'http://rdfs.org/sioc/types#',
186                                               'xmlns:laconica' =>
187                                               'http://laconi.ca/ont/',
188                                               'xmlns' => 'http://purl.org/rss/1.0/'));
189         common_element_start('sioc:Site', array('rdf:about' => common_root_url()));
190         common_element('sioc:name', null, common_config('site', 'name'));
191         common_element_start('sioc:container_of');
192         common_element('sioc:Container', array('rdf:about' =>
193                                                $channel['url']));
194         common_element_end('sioc:container_of');
195         common_element_end('sioc:Site');
196     }
197
198     function end_rss()
199     {
200         common_element_end('rdf:RDF');
201     }
202 }