3 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
24 * Basic support for outputting syndication feeds in RSS, other formats.
25 * Contain a feed class as well as classes to build rss / atom ... feeds
26 * Available feeds are defined in Defines.php
32 * A base class for basic support for outputting syndication feeds in RSS and other formats.
42 var $Description = '';
53 * @param $Title String: 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 * Encode $string so that it can be safely embedded in a XML document
74 * @param $string String: string to encode
77 public function xmlEncode( $string ) {
78 $string = str_replace( "\r\n", "\n", $string );
79 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
80 return htmlspecialchars( $string );
84 * Get the unique id of this item
88 public function getUniqueId() {
89 if ( $this->UniqueId
) {
90 return $this->xmlEncode( $this->UniqueId
);
95 * set the unique id of an item
97 * @param $uniqueId String: unique id for the item
98 * @param $RSSisPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
100 public function setUniqueId($uniqueId, $RSSisPermalink = False) {
101 $this->UniqueId
= $uniqueId;
102 $this->RSSIsPermalink
= $isPermalink;
106 * Get the title of this item; already xml-encoded
110 public function getTitle() {
111 return $this->xmlEncode( $this->Title
);
115 * Get the URL of this item; already xml-encoded
119 public function getUrl() {
120 return $this->xmlEncode( $this->Url
);
124 * Get the description of this item; already xml-encoded
128 public function getDescription() {
129 return $this->xmlEncode( $this->Description
);
133 * Get the language of this item
137 public function getLanguage() {
138 global $wgContLanguageCode;
139 return $wgContLanguageCode;
143 * Get the title of this item
147 public function getDate() {
152 * Get the author of this item; already xml-encoded
156 public function getAuthor() {
157 return $this->xmlEncode( $this->Author
);
161 * Get the comment of this item; already xml-encoded
165 public function getComments() {
166 return $this->xmlEncode( $this->Comments
);
170 * Quickie hack... strip out wikilinks to more legible form from the comment.
172 * @param $text String: wikitext
175 public static function stripComment( $text ) {
176 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
182 * @todo document (needs one-sentence top-level class description).
185 class ChannelFeed
extends FeedItem
{
187 * Abstract function, override!
192 * Generate Header of the feed
194 function outHeader() {
202 function outItem( $item ) {
203 # print "<item>...</item>";
207 * Generate Footer of the feed
209 function outFooter() {
215 * Setup and send HTTP headers. Don't send any content;
216 * content might end up being cached and re-sent with
217 * these same headers later.
219 * This should be called from the outHeader() method,
220 * but can also be called separately.
222 public function httpHeaders() {
225 # We take over from $wgOut, excepting its cache header info
227 $mimetype = $this->contentType();
228 header( "Content-type: $mimetype; charset=UTF-8" );
229 $wgOut->sendCacheControl();
234 * Return an internet media type to be sent in the headers.
239 function contentType() {
241 $ctype = $wgRequest->getVal('ctype','application/xml');
242 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
243 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
247 * Output the initial XML headers with a stylesheet for legibility
248 * if someone finds it in a browser.
251 function outXmlHeader() {
252 global $wgStylePath, $wgStyleVersion;
254 $this->httpHeaders();
255 echo '<?xml version="1.0"?>' . "\n";
256 echo '<?xml-stylesheet type="text/css" href="' .
257 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
263 * Generate a RSS feed
267 class RSSFeed
extends ChannelFeed
{
270 * Format a date given a timestamp
272 * @param $ts Integer: timestamp
273 * @return String: date string
275 function formatTime( $ts ) {
276 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
280 * Ouput an RSS 2.0 header
282 function outHeader() {
285 $this->outXmlHeader();
286 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
288 <title
><?php
print $this->getTitle() ?
></title
>
289 <link
><?php
print $this->getUrl() ?
></link
>
290 <description
><?php
print $this->getDescription() ?
></description
>
291 <language
><?php
print $this->getLanguage() ?
></language
>
292 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
293 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
298 * Output an RSS 2.0 item
299 * @param $item FeedItem: item to be output
301 function outItem( $item ) {
304 <title
><?php
print $item->getTitle() ?
></title
>
305 <link
><?php
print $item->getUrl() ?
></link
>
306 <guid
<?php
if( $item->RSSIsPermalink
) print ' isPermaLink="true"' ?
>><?php
print $item->getUniqueId() ?
></guid
>
307 <description
><?php
print $item->getDescription() ?
></description
>
308 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
309 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
310 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
316 * Ouput an RSS 2.0 footer
318 function outFooter() {
326 * Generate an Atom feed
330 class AtomFeed
extends ChannelFeed
{
334 function formatTime( $ts ) {
335 // need to use RFC 822 time format at least for rss2.0
336 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
340 * Outputs a basic header for Atom 1.0 feeds.
342 function outHeader() {
345 $this->outXmlHeader();
346 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
347 <id
><?php
print $this->getFeedId() ?
></id
>
348 <title
><?php
print $this->getTitle() ?
></title
>
349 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
350 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
351 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
352 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
353 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
359 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
360 * for every feed we create. For now just use the URL, but who
361 * can tell if that's right? If we put options on the feed, do we
362 * have to change the id? Maybe? Maybe not.
367 function getFeedId() {
368 return $this->getSelfUrl();
372 * Atom 1.0 requests a self-reference to the feed.
376 function getSelfUrl() {
378 return htmlspecialchars( $wgRequest->getFullRequestURL() );
382 * Output a given item.
385 function outItem( $item ) {
389 <id
><?php
print $item->getUniqueId() ?
></id
>
390 <title
><?php
print $item->getTitle() ?
></title
>
391 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
392 <?php
if( $item->getDate() ) { ?
>
393 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
396 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
397 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
400 <?php
/* FIXME need to add comments
401 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
406 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
408 function outFooter() {?
>