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
158 public function getDescriptionUnescaped() {
159 return $this->description
;
163 * Get the language of this item
167 public function getLanguage() {
168 global $wgLanguageCode;
169 return LanguageCode
::bcp47( $wgLanguageCode );
173 * Get the date of this item
177 public function getDate() {
182 * Get the author of this item; already xml-encoded
186 public function getAuthor() {
187 return $this->xmlEncode( $this->author
);
191 * Get the author of this item without any escaping
195 public function getAuthorUnescaped() {
196 return $this->author
;
200 * Get the comment of this item; already xml-encoded
204 public function getComments() {
205 return $this->xmlEncode( $this->comments
);
209 * Get the comment of this item without any escaping
213 public function getCommentsUnescaped() {
214 return $this->comments
;
218 * Quickie hack... strip out wikilinks to more legible form from the comment.
220 * @param string $text Wikitext
223 public static function stripComment( $text ) {
224 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
230 * Class to support the outputting of syndication feeds in Atom and RSS format.
234 abstract class ChannelFeed
extends FeedItem
{
236 /** @var TemplateParser */
237 protected $templateParser;
240 * @param string|Title $title Feed's title
241 * @param string $description
242 * @param string $url URL uniquely designating the feed.
243 * @param string $date Feed's date
244 * @param string $author Author's user name
245 * @param string $comments
247 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
248 parent
::__construct( $title, $description, $url, $date, $author, $comments );
249 $this->templateParser
= new TemplateParser();
253 * Generate Header of the feed
259 abstract public function outHeader();
265 * print "<item>...</item>";
267 * @param FeedItem $item
269 abstract public function outItem( $item );
272 * Generate Footer of the feed
278 abstract public function outFooter();
281 * Setup and send HTTP headers. Don't send any content;
282 * content might end up being cached and re-sent with
283 * these same headers later.
285 * This should be called from the outHeader() method,
286 * but can also be called separately.
288 public function httpHeaders() {
289 global $wgOut, $wgVaryOnXFP;
291 # We take over from $wgOut, excepting its cache header info
293 $mimetype = $this->contentType();
294 header( "Content-type: $mimetype; charset=UTF-8" );
296 // Set a sane filename
297 $exts = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer()
298 ->getExtensionsForType( $mimetype );
299 $ext = $exts ?
strtok( $exts, ' ' ) : 'xml';
300 header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
302 if ( $wgVaryOnXFP ) {
303 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
305 $wgOut->sendCacheControl();
309 * Return an internet media type to be sent in the headers.
313 private function contentType() {
316 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
320 'application/rss+xml',
321 'application/atom+xml'
324 return ( in_array( $ctype, $allowedctypes ) ?
$ctype : 'application/xml' );
328 * Output the initial XML headers.
330 protected function outXmlHeader() {
331 $this->httpHeaders();
332 echo '<?xml version="1.0"?>' . "\n";
337 * Generate a RSS feed
341 class RSSFeed
extends ChannelFeed
{
344 * Format a date given a timestamp. If a timestamp is not given, nothing is returned
346 * @param int|null $ts Timestamp
347 * @return string|null Date string
349 function formatTime( $ts ) {
351 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX
, $ts ) );
356 * Output an RSS 2.0 header
358 function outHeader() {
361 $this->outXmlHeader();
362 // Manually escaping rather than letting Mustache do it because Mustache
363 // uses htmlentities, which does not work with XML
365 'title' => $this->getTitle(),
366 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT
) ),
367 'description' => $this->getDescription(),
368 'language' => $this->xmlEncode( $this->getLanguage() ),
369 'version' => $this->xmlEncode( $wgVersion ),
370 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
372 print $this->templateParser
->processTemplate( 'RSSHeader', $templateParams );
376 * Output an RSS 2.0 item
377 * @param FeedItem $item Item to be output
379 function outItem( $item ) {
380 // Manually escaping rather than letting Mustache do it because Mustache
381 // uses htmlentities, which does not work with XML
383 "title" => $item->getTitle(),
384 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT
) ),
385 "permalink" => $item->rssIsPermalink
,
386 "uniqueID" => $item->getUniqueID(),
387 "description" => $item->getDescription(),
388 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
389 "author" => $item->getAuthor()
391 $comments = $item->getCommentsUnescaped();
393 $commentsEscaped = $this->xmlEncode( wfExpandUrl( $comments, PROTO_CURRENT
) );
394 $templateParams["comments"] = $commentsEscaped;
396 print $this->templateParser
->processTemplate( 'RSSItem', $templateParams );
400 * Output an RSS 2.0 footer
402 function outFooter() {
403 print "</channel></rss>";
408 * Generate an Atom feed
412 class AtomFeed
extends ChannelFeed
{
414 * Format a date given timestamp, if one is given.
416 * @param string|int|null $timestamp
417 * @return string|null
419 function formatTime( $timestamp ) {
421 // need to use RFC 822 time format at least for rss2.0
422 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX
, $timestamp ) );
427 * Outputs a basic header for Atom 1.0 feeds.
429 function outHeader() {
431 $this->outXmlHeader();
432 // Manually escaping rather than letting Mustache do it because Mustache
433 // uses htmlentities, which does not work with XML
435 'language' => $this->xmlEncode( $this->getLanguage() ),
436 'feedID' => $this->getFeedId(),
437 'title' => $this->getTitle(),
438 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT
) ),
439 'selfUrl' => $this->getSelfUrl(),
440 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
441 'description' => $this->getDescription(),
442 'version' => $this->xmlEncode( $wgVersion ),
444 print $this->templateParser
->processTemplate( 'AtomHeader', $templateParams );
448 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
449 * for every feed we create. For now just use the URL, but who
450 * can tell if that's right? If we put options on the feed, do we
451 * have to change the id? Maybe? Maybe not.
455 private function getFeedId() {
456 return $this->getSelfUrl();
460 * Atom 1.0 requests a self-reference to the feed.
463 private function getSelfUrl() {
465 return htmlspecialchars( $wgRequest->getFullRequestURL() );
469 * Output a given item.
470 * @param FeedItem $item
472 function outItem( $item ) {
474 // Manually escaping rather than letting Mustache do it because Mustache
475 // uses htmlentities, which does not work with XML
477 "uniqueID" => $item->getUniqueID(),
478 "title" => $item->getTitle(),
479 "mimeType" => $this->xmlEncode( $wgMimeType ),
480 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT
) ),
481 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
482 "description" => $item->getDescription(),
483 "author" => $item->getAuthor()
485 print $this->templateParser
->processTemplate( 'AtomItem', $templateParams );
489 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
491 function outFooter() {