2 # Basic support for outputting syndication feeds in RSS, other formats
4 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * Available feeds are defined in Defines.php
39 var $Description = '';
48 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
49 $this->Title
= $Title;
50 $this->Description
= $Description;
53 $this->Author
= $Author;
54 $this->Comments
= $Comments;
60 function xmlEncode( $string ) {
61 $string = str_replace( "\r\n", "\n", $string );
62 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
63 return htmlspecialchars( $string );
66 function getTitle() { return $this->xmlEncode( $this->Title
); }
67 function getUrl() { return $this->xmlEncode( $this->Url
); }
68 function getDescription() { return $this->xmlEncode( $this->Description
); }
69 function getLanguage() {
70 global $wgContLanguageCode;
71 return $wgContLanguageCode;
73 function getDate() { return $this->Date
; }
74 function getAuthor() { return $this->xmlEncode( $this->Author
); }
75 function getComments() { return $this->xmlEncode( $this->Comments
); }
83 class ChannelFeed
extends FeedItem
{
85 * Abstract function, override!
90 * Generate Header of the feed
92 function outHeader() {
100 function outItem( $item ) {
101 # print "<item>...</item>";
105 * Generate Footer of the feed
107 function outFooter() {
113 * Setup and send HTTP headers. Don't send any content;
114 * content might end up being cached and re-sent with
115 * these same headers later.
117 * This should be called from the outHeader() method,
118 * but can also be called separately.
122 function httpHeaders() {
125 # We take over from $wgOut, excepting its cache header info
127 $mimetype = $this->contentType();
128 header( "Content-type: $mimetype; charset=UTF-8" );
129 $wgOut->sendCacheControl();
134 * Return an internet media type to be sent in the headers.
139 function contentType() {
141 $ctype = $wgRequest->getVal('ctype','application/xml');
142 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
143 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
147 * Output the initial XML headers with a stylesheet for legibility
148 * if someone finds it in a browser.
151 function outXmlHeader() {
152 global $wgServer, $wgStylePath;
154 $this->httpHeaders();
155 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
156 echo '<?xml-stylesheet type="text/css" href="' .
157 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
162 * Generate a RSS feed
166 class RSSFeed
extends ChannelFeed
{
169 * Format a date given a timestamp
170 * @param integer $ts Timestamp
171 * @return string Date string
173 function formatTime( $ts ) {
174 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
178 * Ouput an RSS 2.0 header
180 function outHeader() {
183 $this->outXmlHeader();
184 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
186 <title
><?php
print $this->getTitle() ?
></title
>
187 <link
><?php
print $this->getUrl() ?
></link
>
188 <description
><?php
print $this->getDescription() ?
></description
>
189 <language
><?php
print $this->getLanguage() ?
></language
>
190 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
191 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
196 * Output an RSS 2.0 item
197 * @param FeedItem item to be output
199 function outItem( $item ) {
202 <title
><?php
print $item->getTitle() ?
></title
>
203 <link
><?php
print $item->getUrl() ?
></link
>
204 <description
><?php
print $item->getDescription() ?
></description
>
205 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
206 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
207 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
213 * Ouput an RSS 2.0 footer
215 function outFooter() {
223 * Generate an Atom feed
227 class AtomFeed
extends ChannelFeed
{
231 function formatTime( $ts ) {
232 // need to use RFC 822 time format at least for rss2.0
233 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
237 * Outputs a basic header for Atom 1.0 feeds.
239 function outHeader() {
242 $this->outXmlHeader();
243 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
244 <id
><?php
print $this->getFeedId() ?
></id
>
245 <title
><?php
print $this->getTitle() ?
></title
>
246 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
247 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
248 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
249 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
250 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
256 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
257 * for every feed we create. For now just use the URL, but who
258 * can tell if that's right? If we put options on the feed, do we
259 * have to change the id? Maybe? Maybe not.
264 function getFeedId() {
265 return $this->getSelfUrl();
269 * Atom 1.0 requests a self-reference to the feed.
273 function getSelfUrl() {
275 return htmlspecialchars( $wgRequest->getFullRequestURL() );
279 * Output a given item.
282 function outItem( $item ) {
286 <id
><?php
print $item->getUrl() ?
></id
>
287 <title
><?php
print $item->getTitle() ?
></title
>
288 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
289 <?php
if( $item->getDate() ) { ?
>
290 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
293 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
294 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
297 <?php
/* FIXME need to add comments
298 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
303 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
305 function outFooter() {?
>