]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
Merge branch 'master' of /var/www/trunk
[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         $this->limit = (int) $this->trimmed('limit');
79         if ($this->limit == 0) {
80             $this->limit = DEFAULT_RSS_LIMIT;
81         }
82         return true;
83     }
84         
85     /**
86      * Handle a request
87      * 
88      * @param array $args Arguments from $_REQUEST
89      * 
90      * @return void
91      */
92     
93     function handle($args)
94     {
95         parent::handle($args);
96         $this->show_rss($limit);
97     }
98
99     /**
100      * Get the notices to output in this stream
101      * 
102      * @return array an array of Notice objects sorted in reverse chron
103      */
104     
105     function getNotices()
106     {
107         return array();
108     }
109
110     /**
111      * Get a description of the channel
112      * 
113      * Returns an array with the following 
114      * @return array 
115     function get_channel()
116     {
117         return array('url' => '',
118                      'title' => '',
119                      'link' => '',
120                      'description' => '');
121     }
122
123     function get_image()
124     {
125         return null;
126     }
127
128     function show_rss($limit=0)
129     {
130         $notices = $this->get_notices($limit);
131
132         $this->init_rss();
133         $this->show_channel($notices);
134         $this->show_image();
135
136         foreach ($notices as $n) {
137             $this->show_item($n);
138         }
139
140         $this->show_creators();
141         $this->end_rss();
142     }
143
144     function show_channel($notices)
145     {
146
147         $channel = $this->get_channel();
148         $image = $this->get_image();
149
150         common_element_start('channel', array('rdf:about' => $channel['url']));
151         common_element('title', null, $channel['title']);
152         common_element('link', null, $channel['link']);
153         common_element('description', null, $channel['description']);
154         common_element('cc:licence', array('rdf:resource' => common_config('license','url')));
155
156         if ($image) {
157             common_element('image', array('rdf:resource' => $image));
158         }
159
160         common_element_start('items');
161         common_element_start('rdf:Seq');
162
163         foreach ($notices as $notice) {
164             common_element('sioct:MicroblogPost', array('rdf:resource' => $notice->uri));
165         }
166
167         common_element_end('rdf:Seq');
168         common_element_end('items');
169
170         common_element_end('channel');
171     }
172
173     function show_image()
174     {
175         $image = $this->get_image();
176         if ($image) {
177             $channel = $this->get_channel();
178             common_element_start('image', array('rdf:about' => $image));
179             common_element('title', null, $channel['title']);
180             common_element('link', null, $channel['link']);
181             common_element('url', null, $image);
182             common_element_end('image');
183         }
184     }
185
186     function show_item($notice)
187     {
188         $profile = Profile::staticGet($notice->profile_id);
189         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
190         $creator_uri = common_profile_uri($profile);
191         common_element_start('item', array('rdf:about' => $notice->uri));
192         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
193         common_element('title', null, $title);
194         common_element('link', null, $nurl);
195         common_element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
196         common_element('dc:date', null, common_date_w3dtf($notice->created));
197         common_element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
198         common_element('sioc:has_creator', array('rdf:resource' => $creator_uri));
199         common_element('laconica:postIcon', array('rdf:resource' => common_profile_avatar_url($profile)));
200         common_element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
201         common_element_end('item');
202         $this->creators[$creator_uri] = $profile;
203     }
204
205     function show_creators()
206     {
207         foreach ($this->creators as $uri => $profile) {
208             $id = $profile->id;
209             $nickname = $profile->nickname;
210             common_element_start('sioc:User', array('rdf:about' => $uri));
211             common_element('foaf:nick', null, $nickname);
212             if ($profile->fullname) {
213                 common_element('foaf:name', null, $profile->fullname);
214             }
215             common_element('sioc:id', null, $id);
216             $avatar = common_profile_avatar_url($profile);
217             common_element('sioc:avatar', array('rdf:resource' => $avatar));
218             common_element_end('sioc:User');
219         }
220     }
221
222     function init_rss()
223     {
224         $channel = $this->get_channel();
225         header('Content-Type: application/rdf+xml');
226
227         common_start_xml();
228         common_element_start('rdf:RDF', array('xmlns:rdf' =>
229                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
230                                               'xmlns:dc' =>
231                                               'http://purl.org/dc/elements/1.1/',
232                                               'xmlns:cc' =>
233                                               'http://web.resource.org/cc/',
234                                               'xmlns:content' =>
235                                               'http://purl.org/rss/1.0/modules/content/',
236                                               'xmlns:foaf' =>
237                                               'http://xmlns.com/foaf/0.1/',
238                                               'xmlns:sioc' =>
239                                               'http://rdfs.org/sioc/ns#',
240                                               'xmlns:sioct' =>
241                                               'http://rdfs.org/sioc/types#',
242                                               'xmlns:laconica' =>
243                                               'http://laconi.ca/ont/',
244                                               'xmlns' => 'http://purl.org/rss/1.0/'));
245         common_element_start('sioc:Site', array('rdf:about' => common_root_url()));
246         common_element('sioc:name', null, common_config('site', 'name'));
247         common_element_start('sioc:container_of');
248         common_element('sioc:Container', array('rdf:about' =>
249                                                $channel['url']));
250         common_element_end('sioc:container_of');
251         common_element_end('sioc:Site');
252     }
253
254     function end_rss()
255     {
256         common_element_end('rdf:RDF');
257     }
258 }