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 = '';
45 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
46 $this->Title
= $Title;
47 $this->Description
= $Description;
50 $this->Author
= $Author;
51 $this->Comments
= $Comments;
57 function xmlEncode( $string ) {
58 $string = str_replace( "\r\n", "\n", $string );
59 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
60 return htmlspecialchars( $string );
63 function getTitle() { return $this->xmlEncode( $this->Title
); }
64 function getUrl() { return $this->xmlEncode( $this->Url
); }
65 function getDescription() { return $this->xmlEncode( $this->Description
); }
66 function getLanguage() {
67 global $wgContLanguageCode;
68 return $wgContLanguageCode;
70 function getDate() { return $this->Date
; }
71 function getAuthor() { return $this->xmlEncode( $this->Author
); }
72 function getComments() { return $this->xmlEncode( $this->Comments
); }
77 * @todo document (needs one-sentence top-level class description).
79 class ChannelFeed
extends FeedItem
{
81 * Abstract function, override!
86 * Generate Header of the feed
88 function outHeader() {
96 function outItem( $item ) {
97 # print "<item>...</item>";
101 * Generate Footer of the feed
103 function outFooter() {
109 * Setup and send HTTP headers. Don't send any content;
110 * content might end up being cached and re-sent with
111 * these same headers later.
113 * This should be called from the outHeader() method,
114 * but can also be called separately.
118 function httpHeaders() {
121 # We take over from $wgOut, excepting its cache header info
123 $mimetype = $this->contentType();
124 header( "Content-type: $mimetype; charset=UTF-8" );
125 $wgOut->sendCacheControl();
130 * Return an internet media type to be sent in the headers.
135 function contentType() {
137 $ctype = $wgRequest->getVal('ctype','application/xml');
138 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
139 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
143 * Output the initial XML headers with a stylesheet for legibility
144 * if someone finds it in a browser.
147 function outXmlHeader() {
148 global $wgServer, $wgStylePath, $wgStyleVersion;
150 $this->httpHeaders();
151 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
152 echo '<?xml-stylesheet type="text/css" href="' .
153 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css?$wgStyleVersion" ) . '"?' . ">\n";
158 * Generate a RSS feed
160 class RSSFeed
extends ChannelFeed
{
163 * Format a date given a timestamp
164 * @param integer $ts Timestamp
165 * @return string Date string
167 function formatTime( $ts ) {
168 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
172 * Ouput an RSS 2.0 header
174 function outHeader() {
177 $this->outXmlHeader();
178 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
180 <title
><?php
print $this->getTitle() ?
></title
>
181 <link
><?php
print $this->getUrl() ?
></link
>
182 <description
><?php
print $this->getDescription() ?
></description
>
183 <language
><?php
print $this->getLanguage() ?
></language
>
184 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
185 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
190 * Output an RSS 2.0 item
191 * @param FeedItem item to be output
193 function outItem( $item ) {
196 <title
><?php
print $item->getTitle() ?
></title
>
197 <link
><?php
print $item->getUrl() ?
></link
>
198 <description
><?php
print $item->getDescription() ?
></description
>
199 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
200 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
201 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
207 * Ouput an RSS 2.0 footer
209 function outFooter() {
217 * Generate an Atom feed
219 class AtomFeed
extends ChannelFeed
{
223 function formatTime( $ts ) {
224 // need to use RFC 822 time format at least for rss2.0
225 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
229 * Outputs a basic header for Atom 1.0 feeds.
231 function outHeader() {
234 $this->outXmlHeader();
235 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
236 <id
><?php
print $this->getFeedId() ?
></id
>
237 <title
><?php
print $this->getTitle() ?
></title
>
238 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
239 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
240 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
241 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
242 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
248 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
249 * for every feed we create. For now just use the URL, but who
250 * can tell if that's right? If we put options on the feed, do we
251 * have to change the id? Maybe? Maybe not.
256 function getFeedId() {
257 return $this->getSelfUrl();
261 * Atom 1.0 requests a self-reference to the feed.
265 function getSelfUrl() {
267 return htmlspecialchars( $wgRequest->getFullRequestURL() );
271 * Output a given item.
274 function outItem( $item ) {
278 <id
><?php
print $item->getUrl() ?
></id
>
279 <title
><?php
print $item->getTitle() ?
></title
>
280 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
281 <?php
if( $item->getDate() ) { ?
>
282 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
285 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
286 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
289 <?php
/* FIXME need to add comments
290 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
295 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
297 function outFooter() {?
>