MediaWiki master
RSSFeed.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Feed;
11
12use Wikimedia\Timestamp\TimestampFormat as TS;
13
19class RSSFeed extends ChannelFeed {
20
27 private function formatTime( $ts ) {
28 if ( $ts ) {
29 return gmdate( 'D, d M Y H:i:s \G\M\T', (int)wfTimestamp( TS::UNIX, $ts ) );
30 }
31 return null;
32 }
33
38 public function outputHeader( $output ): void {
39 $this->outputXmlHeader( $output );
40 // Manually escaping rather than letting Mustache do it because Mustache
41 // uses htmlentities, which does not work with XML
42 $templateParams = [
43 'title' => $this->getTitle(),
44 'url' => $this->xmlEncode(
45 $this->urlUtils->expand( $this->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
46 ),
47 'description' => $this->getDescription(),
48 'language' => $this->xmlEncode( $this->getLanguage() ),
49 'version' => $this->xmlEncode( MW_VERSION ),
50 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
51 ];
52 print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
53 }
54
59 public function outputItem( FeedItem $item, $output ): void {
60 // Manually escaping rather than letting Mustache do it because Mustache
61 // uses htmlentities, which does not work with XML
62 $templateParams = [
63 "title" => $item->getTitle(),
64 "url" => $this->xmlEncode(
65 $this->urlUtils->expand( $item->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
66 ),
67 "permalink" => $item->rssIsPermalink,
68 "uniqueID" => $item->getUniqueID(),
69 "description" => $item->getDescription(),
70 "date" => $this->xmlEncodeNullable( $this->formatTime( $item->getDate() ) ),
71 "author" => $item->getAuthor()
72 ];
73 $comments = $item->getCommentsUnescaped();
74 if ( $comments ) {
75 $commentsEscaped = $this->xmlEncode(
76 $this->urlUtils->expand( $comments, PROTO_CURRENT ) ?? ''
77 );
78 $templateParams["comments"] = $commentsEscaped;
79 }
80 print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
81 }
82
87 public function outputFooter( $output ): void {
88 print "</channel></rss>";
89 }
90}
const PROTO_CURRENT
Definition Defines.php:222
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:23
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class to support the outputting of syndication feeds in Atom and RSS format.
outputXmlHeader( $output)
Output the initial XML headers.
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:26
getTitle()
Get the title of this item; already xml-encoded.
Definition FeedItem.php:138
getDescription()
Get the description of this item; already xml-encoded.
Definition FeedItem.php:164
getLanguage()
Get the language of this item.
Definition FeedItem.php:182
getCommentsUnescaped()
Get the comment of this item without any escaping.
Definition FeedItem.php:229
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition FeedItem.php:81
Generate an RSS feed.
Definition RSSFeed.php:19
outputItem(FeedItem $item, $output)
Output an RSS 2.0 item Generate an item.Example: print "<item>...</item>"; to override 1....
Definition RSSFeed.php:59
outputFooter( $output)
Output an RSS 2.0 footer Generate Footer of the feed.Example: print "</feed>"; to override 1....
Definition RSSFeed.php:87
outputHeader( $output)
Output an RSS 2.0 header Generate Header of the feed.Example: print "<feed>"; to override 1....
Definition RSSFeed.php:38