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 * https://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
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
54 public $rssIsPermalink = false;
57 * @param string|Title $title Item's title
58 * @param string $description
59 * @param string $url URL uniquely designating the item.
60 * @param string $date Item's date
61 * @param string $author Author's user name
62 * @param string $comments
64 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
65 $this->title
= $title;
66 $this->description
= $description;
68 $this->uniqueId
= $url;
70 $this->author
= $author;
71 $this->comments
= $comments;
75 * Encode $string so that it can be safely embedded in a XML document
77 * @param string $string String to encode
80 public function xmlEncode( $string ) {
81 $string = str_replace( "\r\n", "\n", $string );
82 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
83 return htmlspecialchars( $string );
87 * Get the unique id of this item; already xml-encoded
90 public function getUniqueID() {
91 $id = $this->getUniqueIDUnescaped();
93 return $this->xmlEncode( $id );
98 * Get the unique id of this item, without any escaping
101 public function getUniqueIdUnescaped() {
102 if ( $this->uniqueId
) {
103 return wfExpandUrl( $this->uniqueId
, PROTO_CURRENT
);
108 * Set the unique id of an item
110 * @param string $uniqueId Unique id for the item
111 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
113 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
114 $this->uniqueId
= $uniqueId;
115 $this->rssIsPermalink
= $rssIsPermalink;
119 * Get the title of this item; already xml-encoded
123 public function getTitle() {
124 return $this->xmlEncode( $this->title
);
128 * Get the URL of this item; already xml-encoded
132 public function getUrl() {
133 return $this->xmlEncode( $this->url
);
136 /** Get the URL of this item without any escaping
140 public function getUrlUnescaped() {
145 * Get the description of this item; already xml-encoded
149 public function getDescription() {
150 return $this->xmlEncode( $this->description
);
154 * Get the description of this item without any escaping
157 public function getDescriptionUnescaped() {
158 return $this->description
;
162 * Get the language of this item
166 public function getLanguage() {
167 global $wgLanguageCode;
168 return LanguageCode
::bcp47( $wgLanguageCode );
172 * Get the date of this item
176 public function getDate() {
181 * Get the author of this item; already xml-encoded
185 public function getAuthor() {
186 return $this->xmlEncode( $this->author
);
190 * Get the author of this item without any escaping
194 public function getAuthorUnescaped() {
195 return $this->author
;
199 * Get the comment of this item; already xml-encoded
203 public function getComments() {
204 return $this->xmlEncode( $this->comments
);
208 * Get the comment of this item without any escaping
212 public function getCommentsUnescaped() {
213 return $this->comments
;
217 * Quickie hack... strip out wikilinks to more legible form from the comment.
219 * @param string $text Wikitext
222 public static function stripComment( $text ) {
223 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
229 * Class to support the outputting of syndication feeds in Atom and RSS format.
233 abstract class ChannelFeed
extends FeedItem
{
235 /** @var TemplateParser */
236 protected $templateParser;
239 * @param string|Title $title Feed's title
240 * @param string $description
241 * @param string $url URL uniquely designating the feed.
242 * @param string $date Feed's date
243 * @param string $author Author's user name
244 * @param string $comments
246 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
247 parent
::__construct( $title, $description, $url, $date, $author, $comments );
248 $this->templateParser
= new TemplateParser();
252 * Generate Header of the feed
258 abstract public function outHeader();
264 * print "<item>...</item>";
266 * @param FeedItem $item
268 abstract public function outItem( $item );
271 * Generate Footer of the feed
277 abstract public function outFooter();
280 * Setup and send HTTP headers. Don't send any content;
281 * content might end up being cached and re-sent with
282 * these same headers later.
284 * This should be called from the outHeader() method,
285 * but can also be called separately.
287 public function httpHeaders() {
288 global $wgOut, $wgVaryOnXFP;
290 # We take over from $wgOut, excepting its cache header info
292 $mimetype = $this->contentType();
293 header( "Content-type: $mimetype; charset=UTF-8" );
295 // Set a sane filename
296 $exts = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer()
297 ->getExtensionsForType( $mimetype );
298 $ext = $exts ?
strtok( $exts, ' ' ) : 'xml';
299 header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
301 if ( $wgVaryOnXFP ) {
302 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
304 $wgOut->sendCacheControl();
308 * Return an internet media type to be sent in the headers.
312 private function contentType() {
315 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
319 'application/rss+xml',
320 'application/atom+xml'
323 return ( in_array( $ctype, $allowedctypes ) ?
$ctype : 'application/xml' );
327 * Output the initial XML headers.
329 protected function outXmlHeader() {
330 $this->httpHeaders();
331 echo '<?xml version="1.0"?>' . "\n";
336 * Generate a RSS feed
340 class RSSFeed
extends ChannelFeed
{
343 * Format a date given a timestamp. If a timestamp is not given, nothing is returned
345 * @param int|null $ts Timestamp
346 * @return string|null Date string
348 function formatTime( $ts ) {
350 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
355 * Output an RSS 2.0 header
357 function outHeader() {
360 $this->outXmlHeader();
361 // Manually escaping rather than letting Mustache do it because Mustache
362 // uses htmlentities, which does not work with XML
364 'title' => $this->getTitle(),
365 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT
) ),
366 'description' => $this->getDescription(),
367 'language' => $this->xmlEncode( $this->getLanguage() ),
368 'version' => $this->xmlEncode( $wgVersion ),
369 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
371 print $this->templateParser
->processTemplate( 'RSSHeader', $templateParams );
375 * Output an RSS 2.0 item
376 * @param FeedItem $item Item to be output
378 function outItem( $item ) {
379 // Manually escaping rather than letting Mustache do it because Mustache
380 // uses htmlentities, which does not work with XML
382 "title" => $item->getTitle(),
383 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT
) ),
384 "permalink" => $item->rssIsPermalink
,
385 "uniqueID" => $item->getUniqueId(),
386 "description" => $item->getDescription(),
387 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
388 "author" => $item->getAuthor()
390 $comments = $item->getCommentsUnescaped();
392 $commentsEscaped = $this->xmlEncode( wfExpandUrl( $comments, PROTO_CURRENT
) );
393 $templateParams["comments"] = $commentsEscaped;
395 print $this->templateParser
->processTemplate( 'RSSItem', $templateParams );
399 * Output an RSS 2.0 footer
401 function outFooter() {
402 print "</channel></rss>";
407 * Generate an Atom feed
411 class AtomFeed
extends ChannelFeed
{
413 * Format a date given timestamp, if one is given.
415 * @param string|int|null $timestamp
416 * @return string|null
418 function formatTime( $timestamp ) {
420 // need to use RFC 822 time format at least for rss2.0
421 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $timestamp ) );
426 * Outputs a basic header for Atom 1.0 feeds.
428 function outHeader() {
430 $this->outXmlHeader();
431 // Manually escaping rather than letting Mustache do it because Mustache
432 // uses htmlentities, which does not work with XML
434 'language' => $this->xmlEncode( $this->getLanguage() ),
435 'feedID' => $this->getFeedID(),
436 'title' => $this->getTitle(),
437 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT
) ),
438 'selfUrl' => $this->getSelfUrl(),
439 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
440 'description' => $this->getDescription(),
441 'version' => $this->xmlEncode( $wgVersion ),
443 print $this->templateParser
->processTemplate( 'AtomHeader', $templateParams );
447 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
448 * for every feed we create. For now just use the URL, but who
449 * can tell if that's right? If we put options on the feed, do we
450 * have to change the id? Maybe? Maybe not.
454 private function getFeedId() {
455 return $this->getSelfUrl();
459 * Atom 1.0 requests a self-reference to the feed.
462 private function getSelfUrl() {
464 return htmlspecialchars( $wgRequest->getFullRequestURL() );
468 * Output a given item.
469 * @param FeedItem $item
471 function outItem( $item ) {
473 // Manually escaping rather than letting Mustache do it because Mustache
474 // uses htmlentities, which does not work with XML
476 "uniqueID" => $item->getUniqueId(),
477 "title" => $item->getTitle(),
478 "mimeType" => $this->xmlEncode( $wgMimeType ),
479 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT
) ),
480 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
481 "description" => $item->getDescription(),
482 "author" => $item->getAuthor()
484 print $this->templateParser
->processTemplate( 'AtomItem', $templateParams );
488 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
490 function outFooter() {