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;
58 function xmlEncode( $string ) {
59 $string = str_replace( "\r\n", "\n", $string );
60 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
61 return htmlspecialchars( $string );
64 function getTitle() { return $this->xmlEncode( $this->Title
); }
65 function getUrl() { return $this->xmlEncode( $this->Url
); }
66 function getDescription() { return $this->xmlEncode( $this->Description
); }
67 function getLanguage() {
68 global $wgContLanguageCode;
69 return $wgContLanguageCode;
71 function getDate() { return $this->Date
; }
72 function getAuthor() { return $this->xmlEncode( $this->Author
); }
73 function getComments() { return $this->xmlEncode( $this->Comments
); }
78 * @todo document (needs one-sentence top-level class description).
80 class ChannelFeed
extends FeedItem
{
82 * Abstract function, override!
87 * Generate Header of the feed
89 function outHeader() {
97 function outItem( $item ) {
98 # print "<item>...</item>";
102 * Generate Footer of the feed
104 function outFooter() {
110 * Setup and send HTTP headers. Don't send any content;
111 * content might end up being cached and re-sent with
112 * these same headers later.
114 * This should be called from the outHeader() method,
115 * but can also be called separately.
119 function httpHeaders() {
122 # We take over from $wgOut, excepting its cache header info
124 $mimetype = $this->contentType();
125 header( "Content-type: $mimetype; charset=UTF-8" );
126 $wgOut->sendCacheControl();
131 * Return an internet media type to be sent in the headers.
136 function contentType() {
138 $ctype = $wgRequest->getVal('ctype','application/xml');
139 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
140 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
144 * Output the initial XML headers with a stylesheet for legibility
145 * if someone finds it in a browser.
148 function outXmlHeader() {
149 global $wgServer, $wgStylePath, $wgStyleVersion;
151 $this->httpHeaders();
152 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
153 echo '<?xml-stylesheet type="text/css" href="' .
154 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css?$wgStyleVersion" ) . '"?' . ">\n";
159 * Generate a RSS feed
161 class RSSFeed
extends ChannelFeed
{
164 * Format a date given a timestamp
165 * @param integer $ts Timestamp
166 * @return string Date string
168 function formatTime( $ts ) {
169 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
173 * Ouput an RSS 2.0 header
175 function outHeader() {
178 $this->outXmlHeader();
179 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
181 <title
><?php
print $this->getTitle() ?
></title
>
182 <link
><?php
print $this->getUrl() ?
></link
>
183 <description
><?php
print $this->getDescription() ?
></description
>
184 <language
><?php
print $this->getLanguage() ?
></language
>
185 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
186 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
191 * Output an RSS 2.0 item
192 * @param FeedItem item to be output
194 function outItem( $item ) {
197 <title
><?php
print $item->getTitle() ?
></title
>
198 <link
><?php
print $item->getUrl() ?
></link
>
199 <description
><?php
print $item->getDescription() ?
></description
>
200 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
201 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
202 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
208 * Ouput an RSS 2.0 footer
210 function outFooter() {
218 * Generate an Atom feed
220 class AtomFeed
extends ChannelFeed
{
224 function formatTime( $ts ) {
225 // need to use RFC 822 time format at least for rss2.0
226 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
230 * Outputs a basic header for Atom 1.0 feeds.
232 function outHeader() {
235 $this->outXmlHeader();
236 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
237 <id
><?php
print $this->getFeedId() ?
></id
>
238 <title
><?php
print $this->getTitle() ?
></title
>
239 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
240 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
241 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
242 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
243 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
249 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
250 * for every feed we create. For now just use the URL, but who
251 * can tell if that's right? If we put options on the feed, do we
252 * have to change the id? Maybe? Maybe not.
257 function getFeedId() {
258 return $this->getSelfUrl();
262 * Atom 1.0 requests a self-reference to the feed.
266 function getSelfUrl() {
268 return htmlspecialchars( $wgRequest->getFullRequestURL() );
272 * Output a given item.
275 function outItem( $item ) {
279 <id
><?php
print $item->getUrl() ?
></id
>
280 <title
><?php
print $item->getTitle() ?
></title
>
281 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
282 <?php
if( $item->getDate() ) { ?
>
283 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
286 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
287 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
290 <?php
/* FIXME need to add comments
291 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
296 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
298 function outFooter() {?
>