0289a6b916d6aaabe4f8396c792bd6971483c3c5
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 = '';
51 * @param $Title String: Item's title
52 * @param $Description String
53 * @param $Url String: URL uniquely designating the item.
54 * @param $Date String: Item's date
55 * @param $Author String: Author's user name
56 * @param $Comments String
58 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
59 $this->Title
= $Title;
60 $this->Description
= $Description;
63 $this->Author
= $Author;
64 $this->Comments
= $Comments;
68 * Encode $string so that it can be safely embedded in a XML document
70 * @param $string String: string to encode
73 public function xmlEncode( $string ) {
74 $string = str_replace( "\r\n", "\n", $string );
75 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
76 return htmlspecialchars( $string );
80 * Get the title of this item; already xml-encoded
84 public function getTitle() {
85 return $this->xmlEncode( $this->Title
);
89 * Get the URL of this item; already xml-encoded
93 public function getUrl() {
94 return $this->xmlEncode( $this->Url
);
98 * Get the description of this item; already xml-encoded
102 public function getDescription() {
103 return $this->xmlEncode( $this->Description
);
107 * Get the language of this item
111 public function getLanguage() {
112 global $wgContLanguageCode;
113 return $wgContLanguageCode;
117 * Get the title of this item
121 public function getDate() {
126 * Get the author of this item; already xml-encoded
130 public function getAuthor() {
131 return $this->xmlEncode( $this->Author
);
135 * Get the comment of this item; already xml-encoded
139 public function getComments() {
140 return $this->xmlEncode( $this->Comments
);
144 * Quickie hack... strip out wikilinks to more legible form from the comment.
146 * @param $text String: wikitext
149 public static function stripComment( $text ) {
150 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
156 * @todo document (needs one-sentence top-level class description).
159 class ChannelFeed
extends FeedItem
{
161 * Abstract function, override!
166 * Generate Header of the feed
168 function outHeader() {
176 function outItem( $item ) {
177 # print "<item>...</item>";
181 * Generate Footer of the feed
183 function outFooter() {
189 * Setup and send HTTP headers. Don't send any content;
190 * content might end up being cached and re-sent with
191 * these same headers later.
193 * This should be called from the outHeader() method,
194 * but can also be called separately.
196 public function httpHeaders() {
199 # We take over from $wgOut, excepting its cache header info
201 $mimetype = $this->contentType();
202 header( "Content-type: $mimetype; charset=UTF-8" );
203 $wgOut->sendCacheControl();
208 * Return an internet media type to be sent in the headers.
213 function contentType() {
215 $ctype = $wgRequest->getVal('ctype','application/xml');
216 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
217 return (in_array($ctype, $allowedctypes) ?
$ctype : 'application/xml');
221 * Output the initial XML headers with a stylesheet for legibility
222 * if someone finds it in a browser.
225 function outXmlHeader() {
226 global $wgStylePath, $wgStyleVersion;
228 $this->httpHeaders();
229 echo '<?xml version="1.0"?>' . "\n";
230 echo '<?xml-stylesheet type="text/css" href="' .
231 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
237 * Generate a RSS feed
241 class RSSFeed
extends ChannelFeed
{
244 * Format a date given a timestamp
246 * @param $ts Integer: timestamp
247 * @return String: date string
249 function formatTime( $ts ) {
250 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
254 * Ouput an RSS 2.0 header
256 function outHeader() {
259 $this->outXmlHeader();
260 ?
><rss version
="2.0" xmlns
:dc
="http://purl.org/dc/elements/1.1/">
262 <title
><?php
print $this->getTitle() ?
></title
>
263 <link
><?php
print $this->getUrl() ?
></link
>
264 <description
><?php
print $this->getDescription() ?
></description
>
265 <language
><?php
print $this->getLanguage() ?
></language
>
266 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
267 <lastBuildDate
><?php
print $this->formatTime( wfTimestampNow() ) ?
></lastBuildDate
>
272 * Output an RSS 2.0 item
273 * @param $item FeedItem: item to be output
275 function outItem( $item ) {
278 <title
><?php
print $item->getTitle() ?
></title
>
279 <link
><?php
print $item->getUrl() ?
></link
>
280 <description
><?php
print $item->getDescription() ?
></description
>
281 <?php
if( $item->getDate() ) { ?
><pubDate
><?php
print $this->formatTime( $item->getDate() ) ?
></pubDate
><?php
} ?
>
282 <?php
if( $item->getAuthor() ) { ?
><dc
:creator
><?php
print $item->getAuthor() ?
></dc
:creator
><?php
}?
>
283 <?php
if( $item->getComments() ) { ?
><comments
><?php
print $item->getComments() ?
></comments
><?php
}?
>
289 * Ouput an RSS 2.0 footer
291 function outFooter() {
299 * Generate an Atom feed
303 class AtomFeed
extends ChannelFeed
{
307 function formatTime( $ts ) {
308 // need to use RFC 822 time format at least for rss2.0
309 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $ts ) );
313 * Outputs a basic header for Atom 1.0 feeds.
315 function outHeader() {
318 $this->outXmlHeader();
319 ?
><feed xmlns
="http://www.w3.org/2005/Atom" xml
:lang
="<?php print $this->getLanguage() ?>">
320 <id
><?php
print $this->getFeedId() ?
></id
>
321 <title
><?php
print $this->getTitle() ?
></title
>
322 <link rel
="self" type
="application/atom+xml" href
="<?php print $this->getSelfUrl() ?>"/>
323 <link rel
="alternate" type
="text/html" href
="<?php print $this->getUrl() ?>"/>
324 <updated
><?php
print $this->formatTime( wfTimestampNow() ) ?
>Z
</updated
>
325 <subtitle
><?php
print $this->getDescription() ?
></subtitle
>
326 <generator
>MediaWiki
<?php
print $wgVersion ?
></generator
>
332 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
333 * for every feed we create. For now just use the URL, but who
334 * can tell if that's right? If we put options on the feed, do we
335 * have to change the id? Maybe? Maybe not.
340 function getFeedId() {
341 return $this->getSelfUrl();
345 * Atom 1.0 requests a self-reference to the feed.
349 function getSelfUrl() {
351 return htmlspecialchars( $wgRequest->getFullRequestURL() );
355 * Output a given item.
358 function outItem( $item ) {
362 <id
><?php
print $item->getUrl() ?
></id
>
363 <title
><?php
print $item->getTitle() ?
></title
>
364 <link rel
="alternate" type
="<?php print $wgMimeType ?>" href
="<?php print $item->getUrl() ?>"/>
365 <?php
if( $item->getDate() ) { ?
>
366 <updated
><?php
print $this->formatTime( $item->getDate() ) ?
>Z
</updated
>
369 <summary type
="html"><?php
print $item->getDescription() ?
></summary
>
370 <?php
if( $item->getAuthor() ) { ?
><author
><name
><?php
print $item->getAuthor() ?
></name
></author
><?php
}?
>
373 <?php
/* FIXME need to add comments
374 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
379 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
381 function outFooter() {?
>