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
49 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
50 $this->Title
= $Title;
51 $this->Description
= $Description;
54 $this->Author
= $Author;
55 $this->Comments
= $Comments;
61 function xmlEncode( $string ) {
62 $string = str_replace( "\r\n", "\n", $string );
63 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
64 return htmlspecialchars( $string );
67 function getTitle() { return $this->xmlEncode( $this->Title
); }
68 function getUrl() { return $this->xmlEncode( $this->Url
); }
69 function getDescription() { return $this->xmlEncode( $this->Description
); }
70 function getLanguage() {
71 global $wgContLanguageCode;
72 return $wgContLanguageCode;
74 function getDate() { return $this->Date
; }
75 function getAuthor() { return $this->xmlEncode( $this->Author
); }
76 function getComments() { return $this->xmlEncode( $this->Comments
); }
84 class ChannelFeed
extends FeedItem
{
86 * Abstract function, override!
91 * Generate Header of the feed
93 function outHeader() {
101 function outItem( $item ) {
102 # print "<item>...</item>";
106 * Generate Footer of the feed
108 function outFooter() {
114 * Setup and send HTTP headers. Don't send any content;
115 * content might end up being cached and re-sent with
116 * these same headers later.
118 * This should be called from the outHeader() method,
119 * but can also be called separately.
123 function httpHeaders() {
126 # We take over from $wgOut, excepting its cache header info
128 $mimetype = $this->contentType();
129 header( "Content-type: $mimetype; charset=UTF-8" );
130 $wgOut->sendCacheControl();
135 * Return an internet media type to be sent in the headers.
140 function contentType() {
142 $ctype = $wgRequest->getVal('ctype','application/xml');
143 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
144 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
148 * Output the initial XML headers with a stylesheet for legibility
149 * if someone finds it in a browser.
152 function outXmlHeader() {
153 global $wgServer, $wgStylePath;
155 $this->httpHeaders();
156 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
157 echo '<?xml-stylesheet type="text/css" href="' .
158 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css" ) . '"?' . ">\n";
163 * Generate a RSS feed
167 class RSSFeed
extends ChannelFeed
{
170 * Format a date given a timestamp
171 * @param integer $ts Timestamp
172 * @return string Date string
174 function formatTime( $ts ) {
175 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
179 * Ouput an RSS 2.0 header
181 function outHeader() {
184 $this->outXmlHeader();
185 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
187 <title
><?php
print $this->getTitle() ?
></title
>
188 <link
><?php
print $this->getUrl() ?
></link
>
189 <description
><?php
print $this->getDescription() ?
></description
>
190 <language
><?php
print $this->getLanguage() ?
></language
>
191 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
192 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
197 * Output an RSS 2.0 item
198 * @param FeedItem item to be output
200 function outItem( $item ) {
203 <title
><?php
print $item->getTitle() ?
></title
>
204 <link
><?php
print $item->getUrl() ?
></link
>
205 <description
><?php
print $item->getDescription() ?
></description
>
206 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
207 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
208 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
214 * Ouput an RSS 2.0 footer
216 function outFooter() {
224 * Generate an Atom feed
228 class AtomFeed
extends ChannelFeed
{
232 function formatTime( $ts ) {
233 // need to use RFC 822 time format at least for rss2.0
234 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
238 * Outputs a basic header for Atom 1.0 feeds.
240 function outHeader() {
243 $this->outXmlHeader();
244 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
245 <id
><?php
print $this->getFeedId() ?
></id
>
246 <title
><?php
print $this->getTitle() ?
></title
>
247 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
248 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
249 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
250 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
251 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
257 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
258 * for every feed we create. For now just use the URL, but who
259 * can tell if that's right? If we put options on the feed, do we
260 * have to change the id? Maybe? Maybe not.
265 function getFeedId() {
266 return $this->getSelfUrl();
270 * Atom 1.0 requests a self-reference to the feed.
274 function getSelfUrl() {
276 return htmlspecialchars( $wgRequest->getFullRequestURL() );
280 * Output a given item.
283 function outItem( $item ) {
287 <id
><?php
print $item->getUrl() ?
></id
>
288 <title
><?php
print $item->getTitle() ?
></title
>
289 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
290 <?php
if( $item->getDate() ) { ?
>
291 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
294 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
295 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
298 <?php
/* FIXME need to add comments
299 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
304 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
306 function outFooter() {?
>