* Document a bit
[lhc/web/wiklou.git] / includes / Feed.php
1 <?php
2
3 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 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 General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20
21 /**
22 * @defgroup Feed Feed
23 *
24 * Basic support for outputting syndication feeds in RSS, other formats.
25 * Contain a feed class as well as classes to build rss / atom ... feeds
26 * Available feeds are defined in Defines.php
27 *
28 * @file
29 */
30
31 /**
32 * A base class for basic support for outputting syndication feeds in RSS and other formats.
33 *
34 * @ingroup Feed
35 */
36 class FeedItem {
37 /**#@+
38 * @var string
39 * @private
40 */
41 var $Title = 'Wiki';
42 var $Description = '';
43 var $Url = '';
44 var $Date = '';
45 var $Author = '';
46 /**#@-*/
47
48 /**
49 * Constructor
50 *
51 * @param $Title String: Item's title
52 * @param $Description String
53 * @param $Url String: URL uniquely designating the item.
54 * @param $Date String: Item's date
55 * @param $Author String: Author's user name
56 * @param $Comments String
57 */
58 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
59 $this->Title = $Title;
60 $this->Description = $Description;
61 $this->Url = $Url;
62 $this->Date = $Date;
63 $this->Author = $Author;
64 $this->Comments = $Comments;
65 }
66
67 /**
68 * Encode $string so that it can be safely embedded in a XML document
69 *
70 * @param $string String: string to encode
71 * @return String
72 */
73 public function xmlEncode( $string ) {
74 $string = str_replace( "\r\n", "\n", $string );
75 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
76 return htmlspecialchars( $string );
77 }
78
79 /**
80 * Get the title of this item; already xml-encoded
81 *
82 * @return String
83 */
84 public function getTitle() {
85 return $this->xmlEncode( $this->Title );
86 }
87
88 /**
89 * Get the URL of this item; already xml-encoded
90 *
91 * @return String
92 */
93 public function getUrl() {
94 return $this->xmlEncode( $this->Url );
95 }
96
97 /**
98 * Get the description of this item; already xml-encoded
99 *
100 * @return String
101 */
102 public function getDescription() {
103 return $this->xmlEncode( $this->Description );
104 }
105
106 /**
107 * Get the language of this item
108 *
109 * @return String
110 */
111 public function getLanguage() {
112 global $wgContLanguageCode;
113 return $wgContLanguageCode;
114 }
115
116 /**
117 * Get the title of this item
118 *
119 * @return String
120 */
121 public function getDate() {
122 return $this->Date;
123 }
124
125 /**
126 * Get the author of this item; already xml-encoded
127 *
128 * @return String
129 */
130 public function getAuthor() {
131 return $this->xmlEncode( $this->Author );
132 }
133
134 /**
135 * Get the comment of this item; already xml-encoded
136 *
137 * @return String
138 */
139 public function getComments() {
140 return $this->xmlEncode( $this->Comments );
141 }
142
143 /**
144 * Quickie hack... strip out wikilinks to more legible form from the comment.
145 *
146 * @param $text String: wikitext
147 * @return String
148 */
149 public static function stripComment( $text ) {
150 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
151 }
152 /**#@-*/
153 }
154
155 /**
156 * @todo document (needs one-sentence top-level class description).
157 * @ingroup Feed
158 */
159 class ChannelFeed extends FeedItem {
160 /**#@+
161 * Abstract function, override!
162 * @abstract
163 */
164
165 /**
166 * Generate Header of the feed
167 */
168 function outHeader() {
169 # print "<feed>";
170 }
171
172 /**
173 * Generate an item
174 * @param $item
175 */
176 function outItem( $item ) {
177 # print "<item>...</item>";
178 }
179
180 /**
181 * Generate Footer of the feed
182 */
183 function outFooter() {
184 # print "</feed>";
185 }
186 /**#@-*/
187
188 /**
189 * Setup and send HTTP headers. Don't send any content;
190 * content might end up being cached and re-sent with
191 * these same headers later.
192 *
193 * This should be called from the outHeader() method,
194 * but can also be called separately.
195 */
196 public function httpHeaders() {
197 global $wgOut;
198
199 # We take over from $wgOut, excepting its cache header info
200 $wgOut->disable();
201 $mimetype = $this->contentType();
202 header( "Content-type: $mimetype; charset=UTF-8" );
203 $wgOut->sendCacheControl();
204
205 }
206
207 /**
208 * Return an internet media type to be sent in the headers.
209 *
210 * @return string
211 * @private
212 */
213 function contentType() {
214 global $wgRequest;
215 $ctype = $wgRequest->getVal('ctype','application/xml');
216 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
217 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
218 }
219
220 /**
221 * Output the initial XML headers with a stylesheet for legibility
222 * if someone finds it in a browser.
223 * @private
224 */
225 function outXmlHeader() {
226 global $wgStylePath, $wgStyleVersion;
227
228 $this->httpHeaders();
229 echo '<?xml version="1.0"?>' . "\n";
230 echo '<?xml-stylesheet type="text/css" href="' .
231 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
232 '"?' . ">\n";
233 }
234 }
235
236 /**
237 * Generate a RSS feed
238 *
239 * @ingroup Feed
240 */
241 class RSSFeed extends ChannelFeed {
242
243 /**
244 * Format a date given a timestamp
245 *
246 * @param $ts Integer: timestamp
247 * @return String: date string
248 */
249 function formatTime( $ts ) {
250 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
251 }
252
253 /**
254 * Ouput an RSS 2.0 header
255 */
256 function outHeader() {
257 global $wgVersion;
258
259 $this->outXmlHeader();
260 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
261 <channel>
262 <title><?php print $this->getTitle() ?></title>
263 <link><?php print $this->getUrl() ?></link>
264 <description><?php print $this->getDescription() ?></description>
265 <language><?php print $this->getLanguage() ?></language>
266 <generator>MediaWiki <?php print $wgVersion ?></generator>
267 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
268 <?php
269 }
270
271 /**
272 * Output an RSS 2.0 item
273 * @param $item FeedItem: item to be output
274 */
275 function outItem( $item ) {
276 ?>
277 <item>
278 <title><?php print $item->getTitle() ?></title>
279 <link><?php print $item->getUrl() ?></link>
280 <description><?php print $item->getDescription() ?></description>
281 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
282 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
283 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
284 </item>
285 <?php
286 }
287
288 /**
289 * Ouput an RSS 2.0 footer
290 */
291 function outFooter() {
292 ?>
293 </channel>
294 </rss><?php
295 }
296 }
297
298 /**
299 * Generate an Atom feed
300 *
301 * @ingroup Feed
302 */
303 class AtomFeed extends ChannelFeed {
304 /**
305 * @todo document
306 */
307 function formatTime( $ts ) {
308 // need to use RFC 822 time format at least for rss2.0
309 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
310 }
311
312 /**
313 * Outputs a basic header for Atom 1.0 feeds.
314 */
315 function outHeader() {
316 global $wgVersion;
317
318 $this->outXmlHeader();
319 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
320 <id><?php print $this->getFeedId() ?></id>
321 <title><?php print $this->getTitle() ?></title>
322 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
323 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
324 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
325 <subtitle><?php print $this->getDescription() ?></subtitle>
326 <generator>MediaWiki <?php print $wgVersion ?></generator>
327
328 <?php
329 }
330
331 /**
332 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
333 * for every feed we create. For now just use the URL, but who
334 * can tell if that's right? If we put options on the feed, do we
335 * have to change the id? Maybe? Maybe not.
336 *
337 * @return string
338 * @private
339 */
340 function getFeedId() {
341 return $this->getSelfUrl();
342 }
343
344 /**
345 * Atom 1.0 requests a self-reference to the feed.
346 * @return string
347 * @private
348 */
349 function getSelfUrl() {
350 global $wgRequest;
351 return htmlspecialchars( $wgRequest->getFullRequestURL() );
352 }
353
354 /**
355 * Output a given item.
356 * @param $item
357 */
358 function outItem( $item ) {
359 global $wgMimeType;
360 ?>
361 <entry>
362 <id><?php print $item->getUrl() ?></id>
363 <title><?php print $item->getTitle() ?></title>
364 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
365 <?php if( $item->getDate() ) { ?>
366 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
367 <?php } ?>
368
369 <summary type="html"><?php print $item->getDescription() ?></summary>
370 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
371 </entry>
372
373 <?php /* FIXME need to add comments
374 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
375 */
376 }
377
378 /**
379 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
380 */
381 function outFooter() {?>
382 </feed><?php
383 }
384 }