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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 * Contain a feed class as well as classes to build rss / atom ... feeds
28 * Available feeds objects
30 $wgFeedClasses = array(
45 var $Description = '';
54 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
55 $this->Title
= $Title;
56 $this->Description
= $Description;
59 $this->Author
= $Author;
60 $this->Comments
= $Comments;
67 function xmlEncode( $string ) {
68 global $wgInputEncoding, $wgLang;
69 $string = str_replace( "\r\n", "\n", $string );
70 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
71 $string = $wgLang->iconv( $wgInputEncoding, 'utf-8', $string );
73 return htmlspecialchars( $string );
79 function getTitle() { return $this->xmlEncode( $this->Title
); }
83 function getUrl() { return $this->xmlEncode( $this->Url
); }
87 function getDescription() { return $this->xmlEncode( $this->Description
); }
91 function getLanguage() {
92 global $wgLanguageCode;
93 return $wgLanguageCode;
98 function getDate() { return $this->Date
; }
102 function getAuthor() { return $this->xmlEncode( $this->Author
); }
106 function getComments() { return $this->xmlEncode( $this->Comments
); }
113 class ChannelFeed
extends FeedItem
{
115 * Abstract function, override!
120 * Generate Header of the feed
122 function outHeader() {
130 function outItem( $item ) {
131 # print "<item>...</item>";
135 * Generate Footer of the feed
137 function outFooter() {
144 * @param string $mimetype (optional) type of output
146 function outXmlHeader( $mimetype='application/xml' ) {
147 global $wgServer, $wgStylePath, $wgOut;
149 # We take over from $wgOut, excepting its cache header info
151 header( "Content-type: $mimetype; charset=UTF-8" );
152 $wgOut->sendCacheControl();
154 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
155 print '<' . '?xml-stylesheet type="text/css" href="' .
156 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
161 * Generate a RSS feed
165 class RSSFeed
extends ChannelFeed
{
168 * Format a date given a timestamp
169 * @param integer $ts Timestamp
170 * @return string Date string
172 function formatTime( $ts ) {
173 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp2Unix( $ts ) );
177 * Ouput an RSS 2.0 header
179 function outHeader() {
182 $this->outXmlHeader();
183 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
185 <title
><?php
print $this->getTitle() ?
></title
>
186 <link
><?php
print $this->getUrl() ?
></link
>
187 <description
><?php
print $this->getDescription() ?
></description
>
188 <language
><?php
print $this->getLanguage() ?
></language
>
189 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
190 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
195 * Output an RSS 2.0 item
196 * @param FeedItem item to be output
198 function outItem( $item ) {
201 <title
><?php
print $item->getTitle() ?
></title
>
202 <link
><?php
print $item->getUrl() ?
></link
>
203 <description
><?php
print $item->getDescription() ?
></description
>
204 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
205 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
206 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
212 * Ouput an RSS 2.0 footer
214 function outFooter() {
222 * Generate an Atom feed
226 class AtomFeed
extends ChannelFeed
{
230 function formatTime( $ts ) {
231 // need to use RFC 822 time format at least for rss2.0
232 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp2Unix( $ts ) );
238 function outHeader() {
239 global $wgVersion, $wgOut;
241 $this->outXmlHeader();
242 ?
><feed version
="0.3" xml
:lang
="<?php print $this->getLanguage() ?>">
243 <title
><?php
print $this->getTitle() ?
></title
>
244 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
245 <modified
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</modified
>
246 <tagline
><?php
print $this->getDescription() ?
></tagline
>
247 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
255 function outItem( $item ) {
259 <title
><?php
print $item->getTitle() ?
></title
>
260 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
261 <?php
if( $item->getDate() ) { ?
>
262 <modified
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</modified
>
263 <issued
><?php
print $this->formatTime( $item->getDate() ) ?
></issued
>
264 <created
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</created
><?php
} ?
>
266 <summary type
="text/plain"><?php
print $item->getDescription() ?
></summary
>
267 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
><!-- <url
></url
><email
></email
> --></author
><?php
}?
>
268 <comment
>foobar
</comment
>
271 <?php
/* FIXME need to add comments
272 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
279 function outFooter() {?
>