3 * Basic support for outputting syndication feeds in RSS, other formats.
5 * Contain a feed class as well as classes to build rss / atom ... feeds
6 * Available feeds are defined in Defines.php
8 * Copyright © 2004 Brion Vibber <brion@pobox.com>
9 * http://www.mediawiki.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
50 var $rssIsPermalink = false;
55 * @param string|Title $title Item's title
56 * @param $description String
57 * @param string $url URL uniquely designating the item.
58 * @param string $date Item's date
59 * @param string $author Author's user name
60 * @param $comments String
62 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
63 $this->title
= $title;
64 $this->description
= $description;
66 $this->uniqueId
= $url;
68 $this->author
= $author;
69 $this->comments
= $comments;
73 * Encode $string so that it can be safely embedded in a XML document
75 * @param string $string string to encode
78 public function xmlEncode( $string ) {
79 $string = str_replace( "\r\n", "\n", $string );
80 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
81 return htmlspecialchars( $string );
85 * Get the unique id of this item
89 public function getUniqueId() {
90 if ( $this->uniqueId
) {
91 return $this->xmlEncode( $this->uniqueId
);
96 * set the unique id of an item
98 * @param string $uniqueId unique id for the item
99 * @param $rssIsPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
101 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
102 $this->uniqueId
= $uniqueId;
103 $this->rssIsPermalink
= $rssIsPermalink;
107 * Get the title of this item; already xml-encoded
111 public function getTitle() {
112 return $this->xmlEncode( $this->title
);
116 * Get the URL of this item; already xml-encoded
120 public function getUrl() {
121 return $this->xmlEncode( $this->url
);
125 * Get the description of this item; already xml-encoded
129 public function getDescription() {
130 return $this->xmlEncode( $this->description
);
134 * Get the language of this item
138 public function getLanguage() {
139 global $wgLanguageCode;
140 return $wgLanguageCode;
144 * Get the title of this item
148 public function getDate() {
153 * Get the author of this item; already xml-encoded
157 public function getAuthor() {
158 return $this->xmlEncode( $this->author
);
162 * Get the comment of this item; already xml-encoded
166 public function getComments() {
167 return $this->xmlEncode( $this->comments
);
171 * Quickie hack... strip out wikilinks to more legible form from the comment.
173 * @param string $text wikitext
176 public static function stripComment( $text ) {
177 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
183 * @todo document (needs one-sentence top-level class description).
186 abstract class ChannelFeed
extends FeedItem
{
188 * Generate Header of the feed
195 abstract public function outHeader();
201 * print "<item>...</item>";
205 abstract public function outItem( $item );
208 * Generate Footer of the feed
214 abstract public function outFooter();
217 * Setup and send HTTP headers. Don't send any content;
218 * content might end up being cached and re-sent with
219 * these same headers later.
221 * This should be called from the outHeader() method,
222 * but can also be called separately.
224 public function httpHeaders() {
225 global $wgOut, $wgVaryOnXFP;
227 # We take over from $wgOut, excepting its cache header info
229 $mimetype = $this->contentType();
230 header( "Content-type: $mimetype; charset=UTF-8" );
231 if ( $wgVaryOnXFP ) {
232 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
234 $wgOut->sendCacheControl();
239 * Return an internet media type to be sent in the headers.
244 function contentType() {
246 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
247 $allowedctypes = array( 'application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml' );
248 return ( in_array( $ctype, $allowedctypes ) ?
$ctype : 'application/xml' );
252 * Output the initial XML headers with a stylesheet for legibility
253 * if someone finds it in a browser.
256 function outXmlHeader() {
257 global $wgStylePath, $wgStyleVersion;
259 $this->httpHeaders();
260 echo '<?xml version="1.0"?>' . "\n";
261 echo '<?xml-stylesheet type="text/css" href="' .
262 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT
) ) .
268 * Generate a RSS feed
272 class RSSFeed
extends ChannelFeed
{
275 * Format a date given a timestamp
277 * @param $ts Integer: timestamp
278 * @return String: date string
280 function formatTime( $ts ) {
281 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
285 * Output an RSS 2.0 header
287 function outHeader() {
290 $this->outXmlHeader();
291 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
293 <title
><?php
print $this->getTitle() ?
></title
>
294 <link
><?php
print wfExpandUrl( $this->getUrl(), PROTO_CURRENT
) ?
></link
>
295 <description
><?php
print $this->getDescription() ?
></description
>
296 <language
><?php
print $this->getLanguage() ?
></language
>
297 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
298 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
303 * Output an RSS 2.0 item
304 * @param $item FeedItem: item to be output
306 function outItem( $item ) {
309 <title
><?php
print $item->getTitle(); ?
></title
>
310 <link
><?php
print wfExpandUrl( $item->getUrl(), PROTO_CURRENT
); ?
></link
>
311 <guid
<?php
if ( !$item->rssIsPermalink
) { print ' isPermaLink="false"'; } ?
>><?php
print $item->getUniqueId(); ?
></guid
>
312 <description
><?php
print $item->getDescription() ?
></description
>
313 <?php
if ( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ); ?
></pubDate
><?php
} ?
>
314 <?php
if ( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor(); ?
></dc
:creator
><?php
}?
>
315 <?php
if ( $item->getComments() ) { ?
><comments
><?php
print wfExpandUrl( $item->getComments(), PROTO_CURRENT
); ?
></comments
><?php
}?
>
321 * Output an RSS 2.0 footer
323 function outFooter() {
331 * Generate an Atom feed
335 class AtomFeed
extends ChannelFeed
{
340 function formatTime( $ts ) {
341 // need to use RFC 822 time format at least for rss2.0
342 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
346 * Outputs a basic header for Atom 1.0 feeds.
348 function outHeader() {
351 $this->outXmlHeader();
352 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
353 <id
><?php
print $this->getFeedId() ?
></id
>
354 <title
><?php
print $this->getTitle() ?
></title
>
355 <link rel
="self" type
="application/atom+xml" href
="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
356 <link rel
="alternate" type
="text/html" href
="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
357 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
358 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
359 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
365 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
366 * for every feed we create. For now just use the URL, but who
367 * can tell if that's right? If we put options on the feed, do we
368 * have to change the id? Maybe? Maybe not.
373 function getFeedId() {
374 return $this->getSelfUrl();
378 * Atom 1.0 requests a self-reference to the feed.
382 function getSelfUrl() {
384 return htmlspecialchars( $wgRequest->getFullRequestURL() );
388 * Output a given item.
391 function outItem( $item ) {
395 <id
><?php
print $item->getUniqueId(); ?
></id
>
396 <title
><?php
print $item->getTitle(); ?
></title
>
397 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
398 <?php
if ( $item->getDate() ) { ?
>
399 <updated
><?php
print $this->formatTime( $item->getDate() ); ?
>Z
</updated
>
402 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
403 <?php
if ( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor(); ?
></name
></author
><?php
}?
>
406 <?php
/* @todo FIXME: Need to add comments
407 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
412 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
414 function outFooter() {?
>