Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / Feed.php
blob635b04e4d45641dc813748b16867dc987b56fb3f
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 Title
42 var $title;
44 var $description;
45 var $url;
46 var $date;
47 var $author;
48 var $uniqueId;
49 var $comments;
50 var $rssIsPermalink = false;
52 /**
53 * Constructor
55 * @param string|Title $title Item's title
56 * @param $description String
57 * @param string $url URL uniquely designating the item.
58 * @param string $date Item's date
59 * @param string $author 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->date = $date;
68 $this->author = $author;
69 $this->comments = $comments;
72 /**
73 * Encode $string so that it can be safely embedded in a XML document
75 * @param string $string string to encode
76 * @return String
78 public function xmlEncode( $string ) {
79 $string = str_replace( "\r\n", "\n", $string );
80 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
81 return htmlspecialchars( $string );
84 /**
85 * Get the unique id of this item
87 * @return String
89 public function getUniqueId() {
90 if ( $this->uniqueId ) {
91 return $this->xmlEncode( $this->uniqueId );
95 /**
96 * set the unique id of an item
98 * @param string $uniqueId unique id for the item
99 * @param $rssIsPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
101 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
102 $this->uniqueId = $uniqueId;
103 $this->rssIsPermalink = $rssIsPermalink;
107 * Get the title of this item; already xml-encoded
109 * @return String
111 public function getTitle() {
112 return $this->xmlEncode( $this->title );
116 * Get the URL of this item; already xml-encoded
118 * @return String
120 public function getUrl() {
121 return $this->xmlEncode( $this->url );
125 * Get the description of this item; already xml-encoded
127 * @return String
129 public function getDescription() {
130 return $this->xmlEncode( $this->description );
134 * Get the language of this item
136 * @return String
138 public function getLanguage() {
139 global $wgLanguageCode;
140 return $wgLanguageCode;
144 * Get the title of this item
146 * @return String
148 public function getDate() {
149 return $this->date;
153 * Get the author of this item; already xml-encoded
155 * @return String
157 public function getAuthor() {
158 return $this->xmlEncode( $this->author );
162 * Get the comment of this item; already xml-encoded
164 * @return String
166 public function getComments() {
167 return $this->xmlEncode( $this->comments );
171 * Quickie hack... strip out wikilinks to more legible form from the comment.
173 * @param string $text wikitext
174 * @return String
176 public static function stripComment( $text ) {
177 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
179 /**#@-*/
183 * @todo document (needs one-sentence top-level class description).
184 * @ingroup Feed
186 abstract class ChannelFeed extends FeedItem {
188 * Generate Header of the feed
189 * @par Example:
190 * @code
191 * print "<feed>";
192 * @endcode
193 * @param $item
195 abstract public function outHeader();
198 * Generate an item
199 * @par Example:
200 * @code
201 * print "<item>...</item>";
202 * @endcode
203 * @param $item
205 abstract public function outItem( $item );
208 * Generate Footer of the feed
209 * @par Example:
210 * @code
211 * print "</feed>";
212 * @endcode
214 abstract public function outFooter();
217 * Setup and send HTTP headers. Don't send any content;
218 * content might end up being cached and re-sent with
219 * these same headers later.
221 * This should be called from the outHeader() method,
222 * but can also be called separately.
224 public function httpHeaders() {
225 global $wgOut, $wgVaryOnXFP;
227 # We take over from $wgOut, excepting its cache header info
228 $wgOut->disable();
229 $mimetype = $this->contentType();
230 header( "Content-type: $mimetype; charset=UTF-8" );
231 if ( $wgVaryOnXFP ) {
232 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
234 $wgOut->sendCacheControl();
239 * Return an internet media type to be sent in the headers.
241 * @return string
242 * @private
244 function contentType() {
245 global $wgRequest;
246 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
247 $allowedctypes = array( 'application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml' );
248 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
252 * Output the initial XML headers with a stylesheet for legibility
253 * if someone finds it in a browser.
254 * @private
256 function outXmlHeader() {
257 global $wgStylePath, $wgStyleVersion;
259 $this->httpHeaders();
260 echo '<?xml version="1.0"?>' . "\n";
261 echo '<?xml-stylesheet type="text/css" href="' .
262 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT ) ) .
263 '"?' . ">\n";
268 * Generate a RSS feed
270 * @ingroup Feed
272 class RSSFeed extends ChannelFeed {
275 * Format a date given a timestamp
277 * @param $ts Integer: timestamp
278 * @return String: date string
280 function formatTime( $ts ) {
281 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
285 * Output an RSS 2.0 header
287 function outHeader() {
288 global $wgVersion;
290 $this->outXmlHeader();
291 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
292 <channel>
293 <title><?php print $this->getTitle() ?></title>
294 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
295 <description><?php print $this->getDescription() ?></description>
296 <language><?php print $this->getLanguage() ?></language>
297 <generator>MediaWiki <?php print $wgVersion ?></generator>
298 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
299 <?php
303 * Output an RSS 2.0 item
304 * @param $item FeedItem: item to be output
306 function outItem( $item ) {
308 <item>
309 <title><?php print $item->getTitle(); ?></title>
310 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
311 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
312 <description><?php print $item->getDescription() ?></description>
313 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
314 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
315 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
316 </item>
317 <?php
321 * Output an RSS 2.0 footer
323 function outFooter() {
325 </channel>
326 </rss><?php
331 * Generate an Atom feed
333 * @ingroup Feed
335 class AtomFeed extends ChannelFeed {
337 * @todo document
338 * @return string
340 function formatTime( $ts ) {
341 // need to use RFC 822 time format at least for rss2.0
342 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
346 * Outputs a basic header for Atom 1.0 feeds.
348 function outHeader() {
349 global $wgVersion;
351 $this->outXmlHeader();
352 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
353 <id><?php print $this->getFeedId() ?></id>
354 <title><?php print $this->getTitle() ?></title>
355 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
356 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
357 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
358 <subtitle><?php print $this->getDescription() ?></subtitle>
359 <generator>MediaWiki <?php print $wgVersion ?></generator>
361 <?php
365 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
366 * for every feed we create. For now just use the URL, but who
367 * can tell if that's right? If we put options on the feed, do we
368 * have to change the id? Maybe? Maybe not.
370 * @return string
371 * @private
373 function getFeedId() {
374 return $this->getSelfUrl();
378 * Atom 1.0 requests a self-reference to the feed.
379 * @return string
380 * @private
382 function getSelfUrl() {
383 global $wgRequest;
384 return htmlspecialchars( $wgRequest->getFullRequestURL() );
388 * Output a given item.
389 * @param $item
391 function outItem( $item ) {
392 global $wgMimeType;
394 <entry>
395 <id><?php print $item->getUniqueId(); ?></id>
396 <title><?php print $item->getTitle(); ?></title>
397 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
398 <?php if ( $item->getDate() ) { ?>
399 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
400 <?php } ?>
402 <summary type="html"><?php print $item->getDescription() ?></summary>
403 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
404 </entry>
406 <?php /* @todo FIXME: Need to add comments
407 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
412 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
414 function outFooter() {?>
415 </feed><?php