3 # Basic support for outputting syndication feeds in RSS, other formats
5 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
6 # http://www.mediawiki.org/
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # http://www.gnu.org/copyleft/gpl.html
24 * Contain a feed class as well as classes to build rss / atom ... feeds
25 * Available feeds are defined in Defines.php
40 var $Description = '';
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;
62 function xmlEncode( $string ) {
63 global $wgInputEncoding, $wgContLang;
64 $string = str_replace( "\r\n", "\n", $string );
65 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
66 $string = $wgContLang->iconv( $wgInputEncoding, 'utf-8', $string );
68 return htmlspecialchars( $string );
74 function getTitle() { return $this->xmlEncode( $this->Title
); }
78 function getUrl() { return $this->xmlEncode( $this->Url
); }
82 function getDescription() { return $this->xmlEncode( $this->Description
); }
86 function getLanguage() {
87 global $wgContLanguageCode;
88 return $wgContLanguageCode;
93 function getDate() { return $this->Date
; }
97 function getAuthor() { return $this->xmlEncode( $this->Author
); }
101 function getComments() { return $this->xmlEncode( $this->Comments
); }
108 class ChannelFeed
extends FeedItem
{
110 * Abstract function, override!
115 * Generate Header of the feed
117 function outHeader() {
125 function outItem( $item ) {
126 # print "<item>...</item>";
130 * Generate Footer of the feed
132 function outFooter() {
139 * @param string $mimetype (optional) type of output
141 function outXmlHeader( $mimetype='application/xml' ) {
142 global $wgServer, $wgStylePath, $wgOut;
144 # We take over from $wgOut, excepting its cache header info
146 header( "Content-type: $mimetype; charset=UTF-8" );
147 $wgOut->sendCacheControl();
149 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
150 print '<' . '?xml-stylesheet type="text/css" href="' .
151 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
156 * 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
221 class AtomFeed
extends ChannelFeed
{
225 function formatTime( $ts ) {
226 // need to use RFC 822 time format at least for rss2.0
227 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
233 function outHeader() {
234 global $wgVersion, $wgOut;
236 $this->outXmlHeader();
237 ?
><feed version
="0.3" xml
:lang
="<?php print $this->getLanguage() ?>">
238 <title
><?php
print $this->getTitle() ?
></title
>
239 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
240 <modified
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</modified
>
241 <tagline
><?php
print $this->getDescription() ?
></tagline
>
242 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
250 function outItem( $item ) {
254 <title
><?php
print $item->getTitle() ?
></title
>
255 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
256 <?php
if( $item->getDate() ) { ?
>
257 <modified
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</modified
>
258 <issued
><?php
print $this->formatTime( $item->getDate() ) ?
></issued
>
259 <created
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</created
><?php
} ?
>
261 <summary type
="text/plain"><?php
print $item->getDescription() ?
></summary
>
262 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
><!-- <url
></url
><email
></email
> --></author
><?php
}?
>
263 <comment
>foobar
</comment
>
266 <?php
/* FIXME need to add comments
267 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
274 function outFooter() {?
>