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 * https://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 string $description
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 string $comments
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 bool $rssIsPermalink 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 date 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
194 abstract public function outHeader();
200 * print "<item>...</item>";
202 * @param FeedItem $item
204 abstract public function outItem( $item );
207 * Generate Footer of the feed
213 abstract public function outFooter();
216 * Setup and send HTTP headers. Don't send any content;
217 * content might end up being cached and re-sent with
218 * these same headers later.
220 * This should be called from the outHeader() method,
221 * but can also be called separately.
223 public function httpHeaders() {
224 global $wgOut, $wgVaryOnXFP;
226 # We take over from $wgOut, excepting its cache header info
228 $mimetype = $this->contentType();
229 header( "Content-type: $mimetype; charset=UTF-8" );
230 if ( $wgVaryOnXFP ) {
231 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
233 $wgOut->sendCacheControl();
238 * Return an internet media type to be sent in the headers.
243 function contentType() {
245 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
246 $allowedctypes = array( 'application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml' );
247 return ( in_array( $ctype, $allowedctypes ) ?
$ctype : 'application/xml' );
251 * Output the initial XML headers with a stylesheet for legibility
252 * if someone finds it in a browser.
255 function outXmlHeader() {
256 global $wgStylePath, $wgStyleVersion;
258 $this->httpHeaders();
259 echo '<?xml version="1.0"?>' . "\n";
260 echo '<?xml-stylesheet type="text/css" href="' .
261 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT
) ) .
267 * Generate a RSS feed
271 class RSSFeed
extends ChannelFeed
{
274 * Format a date given a timestamp
276 * @param int $ts Timestamp
277 * @return string Date string
279 function formatTime( $ts ) {
280 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
284 * Output an RSS 2.0 header
286 function outHeader() {
289 $this->outXmlHeader();
290 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
292 <title
><?php
print $this->getTitle() ?
></title
>
293 <link
><?php
print wfExpandUrl( $this->getUrl(), PROTO_CURRENT
) ?
></link
>
294 <description
><?php
print $this->getDescription() ?
></description
>
295 <language
><?php
print $this->getLanguage() ?
></language
>
296 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
297 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
302 * Output an RSS 2.0 item
303 * @param FeedItem $item Item to be output
305 function outItem( $item ) {
308 <title
><?php
print $item->getTitle(); ?
></title
>
309 <link
><?php
print wfExpandUrl( $item->getUrl(), PROTO_CURRENT
); ?
></link
>
310 <guid
<?php
if ( !$item->rssIsPermalink
) { print ' isPermaLink="false"'; } ?
>><?php
print $item->getUniqueId(); ?
></guid
>
311 <description
><?php
print $item->getDescription() ?
></description
>
312 <?php
if ( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ); ?
></pubDate
><?php
} ?
>
313 <?php
if ( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor(); ?
></dc
:creator
><?php
}?
>
314 <?php
if ( $item->getComments() ) { ?
><comments
><?php
print wfExpandUrl( $item->getComments(), PROTO_CURRENT
); ?
></comments
><?php
}?
>
320 * Output an RSS 2.0 footer
322 function outFooter() {
330 * Generate an Atom feed
334 class AtomFeed
extends ChannelFeed
{
339 function formatTime( $ts ) {
340 // need to use RFC 822 time format at least for rss2.0
341 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
345 * Outputs a basic header for Atom 1.0 feeds.
347 function outHeader() {
350 $this->outXmlHeader();
351 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
352 <id
><?php
print $this->getFeedId() ?
></id
>
353 <title
><?php
print $this->getTitle() ?
></title
>
354 <link rel
="self" type
="application/atom+xml" href
="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
355 <link rel
="alternate" type
="text/html" href
="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
356 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
357 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
358 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
364 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
365 * for every feed we create. For now just use the URL, but who
366 * can tell if that's right? If we put options on the feed, do we
367 * have to change the id? Maybe? Maybe not.
372 function getFeedId() {
373 return $this->getSelfUrl();
377 * Atom 1.0 requests a self-reference to the feed.
381 function getSelfUrl() {
383 return htmlspecialchars( $wgRequest->getFullRequestURL() );
387 * Output a given item.
388 * @param FeedItem $item
390 function outItem( $item ) {
394 <id
><?php
print $item->getUniqueId(); ?
></id
>
395 <title
><?php
print $item->getTitle(); ?
></title
>
396 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
397 <?php
if ( $item->getDate() ) { ?
>
398 <updated
><?php
print $this->formatTime( $item->getDate() ); ?
>Z
</updated
>
401 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
402 <?php
if ( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor(); ?
></name
></author
><?php
}?
>
405 <?php
/* @todo FIXME: Need to add comments
406 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
411 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
413 function outFooter() {?
>