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.
43 var $Description = '';
53 * @param $Title String|Title Item's title
54 * @param $Description String
55 * @param $Url String: URL uniquely designating the item.
56 * @param $Date String: Item's date
57 * @param $Author String: Author's user name
58 * @param $Comments String
60 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
61 $this->Title
= $Title;
62 $this->Description
= $Description;
64 $this->UniqueId
= $Url;
65 $this->RSSIsPermalink
= false;
67 $this->Author
= $Author;
68 $this->Comments
= $Comments;
72 * Get the last touched timestamp
74 * @return String last-touched timestamp
76 public function getLastMod() {
77 return $this->Title
->getTouched();
81 * Encode $string so that it can be safely embedded in a XML document
83 * @param $string String: string to encode
86 public function xmlEncode( $string ) {
87 $string = str_replace( "\r\n", "\n", $string );
88 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
89 return htmlspecialchars( $string );
93 * Get the unique id of this item
97 public function getUniqueId() {
98 if ( $this->UniqueId
) {
99 return $this->xmlEncode( $this->UniqueId
);
104 * set the unique id of an item
106 * @param $uniqueId String: unique id for the item
107 * @param $RSSisPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
109 public function setUniqueId($uniqueId, $RSSisPermalink = false) {
110 $this->UniqueId
= $uniqueId;
111 $this->RSSIsPermalink
= $RSSisPermalink;
115 * Get the title of this item; already xml-encoded
119 public function getTitle() {
120 return $this->xmlEncode( $this->Title
);
124 * Get the DB prefixed title
126 * @return String the prefixed title, with underscores and
127 * any interwiki and namespace prefixes
129 public function getDBPrefixedTitle() {
130 return $this->Title
->getPrefixedDBKey();
134 * Get the URL of this item; already xml-encoded
138 public function getUrl() {
139 return $this->xmlEncode( $this->Url
);
143 * Get the description of this item; already xml-encoded
147 public function getDescription() {
148 return $this->xmlEncode( $this->Description
);
152 * Get the language of this item
156 public function getLanguage() {
157 global $wgLanguageCode;
158 return $wgLanguageCode;
162 * Get the title of this item
166 public function getDate() {
171 * Get the author of this item; already xml-encoded
175 public function getAuthor() {
176 return $this->xmlEncode( $this->Author
);
180 * Get the comment of this item; already xml-encoded
184 public function getComments() {
185 return $this->xmlEncode( $this->Comments
);
189 * Quickie hack... strip out wikilinks to more legible form from the comment.
191 * @param $text String: wikitext
194 public static function stripComment( $text ) {
195 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
201 * @todo document (needs one-sentence top-level class description).
204 class ChannelFeed
extends FeedItem
{
206 * Abstract function, override!
211 * Generate Header of the feed
213 function outHeader() {
221 function outItem( $item ) {
222 # print "<item>...</item>";
226 * Generate Footer of the feed
228 function outFooter() {
234 * Setup and send HTTP headers. Don't send any content;
235 * content might end up being cached and re-sent with
236 * these same headers later.
238 * This should be called from the outHeader() method,
239 * but can also be called separately.
241 public function httpHeaders() {
242 global $wgOut, $wgVaryOnXFP;
244 # We take over from $wgOut, excepting its cache header info
246 $mimetype = $this->contentType();
247 header( "Content-type: $mimetype; charset=UTF-8" );
248 if ( $wgVaryOnXFP ) {
249 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
251 $wgOut->sendCacheControl();
256 * Return an internet media type to be sent in the headers.
261 function contentType() {
263 $ctype = $wgRequest->getVal('ctype','application/xml');
264 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
265 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
269 * Output the initial XML headers with a stylesheet for legibility
270 * if someone finds it in a browser.
273 function outXmlHeader() {
274 global $wgStylePath, $wgStyleVersion;
276 $this->httpHeaders();
277 echo '<?xml version="1.0"?>' . "\n";
278 echo '<?xml-stylesheet type="text/css" href="' .
279 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT
) ) .
285 * Generate a RSS feed
289 class RSSFeed
extends ChannelFeed
{
292 * Format a date given a timestamp
294 * @param $ts Integer: timestamp
295 * @return String: date string
297 function formatTime( $ts ) {
298 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
302 * Ouput an RSS 2.0 header
304 function outHeader() {
307 $this->outXmlHeader();
308 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
310 <title
><?php
print $this->getTitle() ?
></title
>
311 <link
><?php
print wfExpandUrl( $this->getUrl(), PROTO_CURRENT
) ?
></link
>
312 <description
><?php
print $this->getDescription() ?
></description
>
313 <language
><?php
print $this->getLanguage() ?
></language
>
314 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
315 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
320 * Output an RSS 2.0 item
321 * @param $item FeedItem: item to be output
323 function outItem( $item ) {
326 <title
><?php
print $item->getTitle() ?
></title
>
327 <link
><?php
print wfExpandUrl( $item->getUrl(), PROTO_CURRENT
) ?
></link
>
328 <guid
<?php
if( !$item->RSSIsPermalink
) print ' isPermaLink="false"' ?
>><?php
print $item->getUniqueId() ?
></guid
>
329 <description
><?php
print $item->getDescription() ?
></description
>
330 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
331 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
332 <?php
if( $item->getComments() ) { ?
><comments
><?php
print wfExpandUrl( $item->getComments(), PROTO_CURRENT
) ?
></comments
><?php
}?
>
338 * Ouput an RSS 2.0 footer
340 function outFooter() {
348 * Generate an Atom feed
352 class AtomFeed
extends ChannelFeed
{
356 function formatTime( $ts ) {
357 // need to use RFC 822 time format at least for rss2.0
358 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
362 * Outputs a basic header for Atom 1.0 feeds.
364 function outHeader() {
367 $this->outXmlHeader();
368 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
369 <id
><?php
print $this->getFeedId() ?
></id
>
370 <title
><?php
print $this->getTitle() ?
></title
>
371 <link rel
="self" type
="application/atom+xml" href
="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
372 <link rel
="alternate" type
="text/html" href
="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
373 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
374 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
375 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
381 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
382 * for every feed we create. For now just use the URL, but who
383 * can tell if that's right? If we put options on the feed, do we
384 * have to change the id? Maybe? Maybe not.
389 function getFeedId() {
390 return $this->getSelfUrl();
394 * Atom 1.0 requests a self-reference to the feed.
398 function getSelfUrl() {
400 return htmlspecialchars( $wgRequest->getFullRequestURL() );
404 * Output a given item.
407 function outItem( $item ) {
411 <id
><?php
print $item->getUniqueId() ?
></id
>
412 <title
><?php
print $item->getTitle() ?
></title
>
413 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ) ?>"/>
414 <?php
if( $item->getDate() ) { ?
>
415 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
418 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
419 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
422 <?php
/* @todo FIXME: Need to add comments
423 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
428 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
430 function outFooter() {?
>