3 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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
22 * Basic support for outputting syndication feeds in RSS, other formats.
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * Available feeds are defined in Defines.php
28 * A base class for basic support for outputting syndication feeds in RSS and other formats.
36 var $Description = '';
44 * @param $Url URL uniquely designating the item.
46 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
47 $this->Title
= $Title;
48 $this->Description
= $Description;
51 $this->Author
= $Author;
52 $this->Comments
= $Comments;
55 function xmlEncode( $string ) {
56 $string = str_replace( "\r\n", "\n", $string );
57 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
58 return htmlspecialchars( $string );
61 function getTitle() { return $this->xmlEncode( $this->Title
); }
62 function getUrl() { return $this->xmlEncode( $this->Url
); }
63 function getDescription() { return $this->xmlEncode( $this->Description
); }
64 function getLanguage() {
65 global $wgContLanguageCode;
66 return $wgContLanguageCode;
68 function getDate() { return $this->Date
; }
69 function getAuthor() { return $this->xmlEncode( $this->Author
); }
70 function getComments() { return $this->xmlEncode( $this->Comments
); }
75 * @todo document (needs one-sentence top-level class description).
77 class ChannelFeed
extends FeedItem
{
79 * Abstract function, override!
84 * Generate Header of the feed
86 function outHeader() {
94 function outItem( $item ) {
95 # print "<item>...</item>";
99 * Generate Footer of the feed
101 function outFooter() {
107 * Setup and send HTTP headers. Don't send any content;
108 * content might end up being cached and re-sent with
109 * these same headers later.
111 * This should be called from the outHeader() method,
112 * but can also be called separately.
116 function httpHeaders() {
119 # We take over from $wgOut, excepting its cache header info
121 $mimetype = $this->contentType();
122 header( "Content-type: $mimetype; charset=UTF-8" );
123 $wgOut->sendCacheControl();
128 * Return an internet media type to be sent in the headers.
133 function contentType() {
135 $ctype = $wgRequest->getVal('ctype','application/xml');
136 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
137 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
141 * Output the initial XML headers with a stylesheet for legibility
142 * if someone finds it in a browser.
145 function outXmlHeader() {
146 global $wgStylePath, $wgStyleVersion;
148 $this->httpHeaders();
149 echo '<?xml version="1.0"?>' . "\n";
150 echo '<?xml-stylesheet type="text/css" href="' .
151 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
157 * Generate a RSS feed
159 class RSSFeed
extends ChannelFeed
{
162 * Format a date given a timestamp
163 * @param integer $ts Timestamp
164 * @return string Date string
166 function formatTime( $ts ) {
167 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
171 * Ouput an RSS 2.0 header
173 function outHeader() {
176 $this->outXmlHeader();
177 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
179 <title
><?php
print $this->getTitle() ?
></title
>
180 <link
><?php
print $this->getUrl() ?
></link
>
181 <description
><?php
print $this->getDescription() ?
></description
>
182 <language
><?php
print $this->getLanguage() ?
></language
>
183 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
184 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
189 * Output an RSS 2.0 item
190 * @param FeedItem item to be output
192 function outItem( $item ) {
195 <title
><?php
print $item->getTitle() ?
></title
>
196 <link
><?php
print $item->getUrl() ?
></link
>
197 <description
><?php
print $item->getDescription() ?
></description
>
198 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
199 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
200 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
206 * Ouput an RSS 2.0 footer
208 function outFooter() {
216 * Generate an Atom feed
218 class AtomFeed
extends ChannelFeed
{
222 function formatTime( $ts ) {
223 // need to use RFC 822 time format at least for rss2.0
224 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
228 * Outputs a basic header for Atom 1.0 feeds.
230 function outHeader() {
233 $this->outXmlHeader();
234 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
235 <id
><?php
print $this->getFeedId() ?
></id
>
236 <title
><?php
print $this->getTitle() ?
></title
>
237 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
238 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
239 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
240 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
241 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
247 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
248 * for every feed we create. For now just use the URL, but who
249 * can tell if that's right? If we put options on the feed, do we
250 * have to change the id? Maybe? Maybe not.
255 function getFeedId() {
256 return $this->getSelfUrl();
260 * Atom 1.0 requests a self-reference to the feed.
264 function getSelfUrl() {
266 return htmlspecialchars( $wgRequest->getFullRequestURL() );
270 * Output a given item.
273 function outItem( $item ) {
277 <id
><?php
print $item->getUrl() ?
></id
>
278 <title
><?php
print $item->getTitle() ?
></title
>
279 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
280 <?php
if( $item->getDate() ) { ?
>
281 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
284 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
285 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
288 <?php
/* FIXME need to add comments
289 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
294 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
296 function outFooter() {?
>