* Added Id to "deleted only" check to make label work
[mediawiki.git] / includes / Feed.php
blob782b64283b1adc61a03bb6b29283474a142df779
1 <?php
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
21 /**
22 * @defgroup Feed Feed
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
28 * @file
31 /**
32 * A base class for basic support for outputting syndication feeds in RSS and other formats.
34 * @ingroup Feed
36 class FeedItem {
37 /**#@+
38 * @var string
39 * @private
41 var $Title = 'Wiki';
42 var $Description = '';
43 var $Url = '';
44 var $Date = '';
45 var $Author = '';
46 var $UniqueId = '';
47 var $RSSIsPermalink;
48 /**#@-*/
50 /**
51 * Constructor
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;
63 $this->Url = $Url;
64 $this->UniqueId = $Url;
65 $this->RSSIsPermalink = false;
66 $this->Date = $Date;
67 $this->Author = $Author;
68 $this->Comments = $Comments;
71 /**
72 * Encode $string so that it can be safely embedded in a XML document
74 * @param $string String: string to encode
75 * @return String
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 );
83 /**
84 * Get the unique id of this item
86 * @return String
88 public function getUniqueId() {
89 if ( $this->UniqueId ) {
90 return $this->xmlEncode( $this->UniqueId );
94 /**
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
108 * @return String
110 public function getTitle() {
111 return $this->xmlEncode( $this->Title );
115 * Get the URL of this item; already xml-encoded
117 * @return String
119 public function getUrl() {
120 return $this->xmlEncode( $this->Url );
124 * Get the description of this item; already xml-encoded
126 * @return String
128 public function getDescription() {
129 return $this->xmlEncode( $this->Description );
133 * Get the language of this item
135 * @return String
137 public function getLanguage() {
138 global $wgContLanguageCode;
139 return $wgContLanguageCode;
143 * Get the title of this item
145 * @return String
147 public function getDate() {
148 return $this->Date;
152 * Get the author of this item; already xml-encoded
154 * @return String
156 public function getAuthor() {
157 return $this->xmlEncode( $this->Author );
161 * Get the comment of this item; already xml-encoded
163 * @return String
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
173 * @return String
175 public static function stripComment( $text ) {
176 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
178 /**#@-*/
182 * @todo document (needs one-sentence top-level class description).
183 * @ingroup Feed
185 class ChannelFeed extends FeedItem {
186 /**#@+
187 * Abstract function, override!
188 * @abstract
192 * Generate Header of the feed
194 function outHeader() {
195 # print "<feed>";
199 * Generate an item
200 * @param $item
202 function outItem( $item ) {
203 # print "<item>...</item>";
207 * Generate Footer of the feed
209 function outFooter() {
210 # print "</feed>";
212 /**#@-*/
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() {
223 global $wgOut;
225 # We take over from $wgOut, excepting its cache header info
226 $wgOut->disable();
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.
236 * @return string
237 * @private
239 function contentType() {
240 global $wgRequest;
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.
249 * @private
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" ) ) .
258 '"?' . ">\n";
263 * Generate a RSS feed
265 * @ingroup 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() {
283 global $wgVersion;
285 $this->outXmlHeader();
286 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
287 <channel>
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>
294 <?php
298 * Output an RSS 2.0 item
299 * @param $item FeedItem: item to be output
301 function outItem( $item ) {
303 <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 }?>
311 </item>
312 <?php
316 * Ouput an RSS 2.0 footer
318 function outFooter() {
320 </channel>
321 </rss><?php
326 * Generate an Atom feed
328 * @ingroup Feed
330 class AtomFeed extends ChannelFeed {
332 * @todo document
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() {
343 global $wgVersion;
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>
355 <?php
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.
364 * @return string
365 * @private
367 function getFeedId() {
368 return $this->getSelfUrl();
372 * Atom 1.0 requests a self-reference to the feed.
373 * @return string
374 * @private
376 function getSelfUrl() {
377 global $wgRequest;
378 return htmlspecialchars( $wgRequest->getFullRequestURL() );
382 * Output a given item.
383 * @param $item
385 function outItem( $item ) {
386 global $wgMimeType;
388 <entry>
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>
394 <?php } ?>
396 <summary type="html"><?php print $item->getDescription() ?></summary>
397 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
398 </entry>
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() {?>
409 </feed><?php