* Fix db->makeList() spacing
[mediawiki.git] / includes / Feed.php
blobe22ed9383bfb67dcc932b83be4782bcc333f63ca
1 <?php
2 /**
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
26 * @file
29 /**
30 * @defgroup Feed Feed
33 /**
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
36 * @ingroup Feed
38 class FeedItem {
39 /**#@+
40 * @var string
41 * @private
43 var $Title = 'Wiki';
44 var $Description = '';
45 var $Url = '';
46 var $Date = '';
47 var $Author = '';
48 var $UniqueId = '';
49 var $RSSIsPermalink;
50 /**#@-*/
52 /**
53 * Constructor
55 * @param $Title String: Item's title
56 * @param $Description String
57 * @param $Url String: URL uniquely designating the item.
58 * @param $Date String: Item's date
59 * @param $Author String: 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;
65 $this->Url = $Url;
66 $this->UniqueId = $Url;
67 $this->RSSIsPermalink = false;
68 $this->Date = $Date;
69 $this->Author = $Author;
70 $this->Comments = $Comments;
73 /**
74 * Get the last touched timestamp
76 * @return String last-touched timestamp
78 public function getLastMod() {
79 return $this->Title->getTouched();
82 /**
83 * Encode $string so that it can be safely embedded in a XML document
85 * @param $string String: string to encode
86 * @return String
88 public function xmlEncode( $string ) {
89 $string = str_replace( "\r\n", "\n", $string );
90 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
91 return htmlspecialchars( $string );
94 /**
95 * Get the unique id of this item
97 * @return String
99 public function getUniqueId() {
100 if ( $this->UniqueId ) {
101 return $this->xmlEncode( $this->UniqueId );
106 * set the unique id of an item
108 * @param $uniqueId String: unique id for the item
109 * @param $RSSisPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
111 public function setUniqueId($uniqueId, $RSSisPermalink = false) {
112 $this->UniqueId = $uniqueId;
113 $this->RSSIsPermalink = $RSSisPermalink;
117 * Get the title of this item; already xml-encoded
119 * @return String
121 public function getTitle() {
122 return $this->xmlEncode( $this->Title );
126 * Get the DB prefixed title
128 * @return String the prefixed title, with underscores and
129 * any interwiki and namespace prefixes
131 public function getDBPrefixedTitle() {
132 return $this->Title->getPrefixedDBKey();
136 * Get the URL of this item; already xml-encoded
138 * @return String
140 public function getUrl() {
141 return $this->xmlEncode( $this->Url );
145 * Get the description of this item; already xml-encoded
147 * @return String
149 public function getDescription() {
150 return $this->xmlEncode( $this->Description );
154 * Get the language of this item
156 * @return String
158 public function getLanguage() {
159 global $wgLanguageCode;
160 return $wgLanguageCode;
164 * Get the title of this item
166 * @return String
168 public function getDate() {
169 return $this->Date;
173 * Get the author of this item; already xml-encoded
175 * @return String
177 public function getAuthor() {
178 return $this->xmlEncode( $this->Author );
182 * Get the comment of this item; already xml-encoded
184 * @return String
186 public function getComments() {
187 return $this->xmlEncode( $this->Comments );
191 * Quickie hack... strip out wikilinks to more legible form from the comment.
193 * @param $text String: wikitext
194 * @return String
196 public static function stripComment( $text ) {
197 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
199 /**#@-*/
203 * @todo document (needs one-sentence top-level class description).
204 * @ingroup Feed
206 class ChannelFeed extends FeedItem {
207 /**#@+
208 * Abstract function, override!
209 * @abstract
213 * Generate Header of the feed
215 function outHeader() {
216 # print "<feed>";
220 * Generate an item
221 * @param $item
223 function outItem( $item ) {
224 # print "<item>...</item>";
228 * Generate Footer of the feed
230 function outFooter() {
231 # print "</feed>";
233 /**#@-*/
236 * Setup and send HTTP headers. Don't send any content;
237 * content might end up being cached and re-sent with
238 * these same headers later.
240 * This should be called from the outHeader() method,
241 * but can also be called separately.
243 public function httpHeaders() {
244 global $wgOut;
246 # We take over from $wgOut, excepting its cache header info
247 $wgOut->disable();
248 $mimetype = $this->contentType();
249 header( "Content-type: $mimetype; charset=UTF-8" );
250 $wgOut->sendCacheControl();
255 * Return an internet media type to be sent in the headers.
257 * @return string
258 * @private
260 function contentType() {
261 global $wgRequest;
262 $ctype = $wgRequest->getVal('ctype','application/xml');
263 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
264 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
268 * Output the initial XML headers with a stylesheet for legibility
269 * if someone finds it in a browser.
270 * @private
272 function outXmlHeader() {
273 global $wgStylePath, $wgStyleVersion;
275 $this->httpHeaders();
276 echo '<?xml version="1.0"?>' . "\n";
277 echo '<?xml-stylesheet type="text/css" href="' .
278 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
279 '"?' . ">\n";
284 * Generate a RSS feed
286 * @ingroup Feed
288 class RSSFeed extends ChannelFeed {
291 * Format a date given a timestamp
293 * @param $ts Integer: timestamp
294 * @return String: date string
296 function formatTime( $ts ) {
297 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
301 * Ouput an RSS 2.0 header
303 function outHeader() {
304 global $wgVersion;
306 $this->outXmlHeader();
307 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
308 <channel>
309 <title><?php print $this->getTitle() ?></title>
310 <link><?php print $this->getUrl() ?></link>
311 <description><?php print $this->getDescription() ?></description>
312 <language><?php print $this->getLanguage() ?></language>
313 <generator>MediaWiki <?php print $wgVersion ?></generator>
314 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
315 <?php
319 * Output an RSS 2.0 item
320 * @param $item FeedItem: item to be output
322 function outItem( $item ) {
324 <item>
325 <title><?php print $item->getTitle() ?></title>
326 <link><?php print $item->getUrl() ?></link>
327 <guid<?php if( !$item->RSSIsPermalink ) print ' isPermaLink="false"' ?>><?php print $item->getUniqueId() ?></guid>
328 <description><?php print $item->getDescription() ?></description>
329 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
330 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
331 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
332 </item>
333 <?php
337 * Ouput an RSS 2.0 footer
339 function outFooter() {
341 </channel>
342 </rss><?php
347 * Generate an Atom feed
349 * @ingroup Feed
351 class AtomFeed extends ChannelFeed {
353 * @todo document
355 function formatTime( $ts ) {
356 // need to use RFC 822 time format at least for rss2.0
357 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
361 * Outputs a basic header for Atom 1.0 feeds.
363 function outHeader() {
364 global $wgVersion;
366 $this->outXmlHeader();
367 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
368 <id><?php print $this->getFeedId() ?></id>
369 <title><?php print $this->getTitle() ?></title>
370 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
371 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
372 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
373 <subtitle><?php print $this->getDescription() ?></subtitle>
374 <generator>MediaWiki <?php print $wgVersion ?></generator>
376 <?php
380 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
381 * for every feed we create. For now just use the URL, but who
382 * can tell if that's right? If we put options on the feed, do we
383 * have to change the id? Maybe? Maybe not.
385 * @return string
386 * @private
388 function getFeedId() {
389 return $this->getSelfUrl();
393 * Atom 1.0 requests a self-reference to the feed.
394 * @return string
395 * @private
397 function getSelfUrl() {
398 global $wgRequest;
399 return htmlspecialchars( $wgRequest->getFullRequestURL() );
403 * Output a given item.
404 * @param $item
406 function outItem( $item ) {
407 global $wgMimeType;
409 <entry>
410 <id><?php print $item->getUniqueId() ?></id>
411 <title><?php print $item->getTitle() ?></title>
412 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
413 <?php if( $item->getDate() ) { ?>
414 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
415 <?php } ?>
417 <summary type="html"><?php print $item->getDescription() ?></summary>
418 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
419 </entry>
421 <?php /* @todo FIXME: Need to add comments
422 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
427 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
429 function outFooter() {?>
430 </feed><?php