MediaWiki master
RSSFeed.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Feed;
25
31class RSSFeed extends ChannelFeed {
32
39 private function formatTime( $ts ) {
40 if ( $ts ) {
41 return gmdate( 'D, d M Y H:i:s \G\M\T', (int)wfTimestamp( TS_UNIX, $ts ) );
42 }
43 return null;
44 }
45
49 public function outHeader() {
50 $this->outXmlHeader();
51 // Manually escaping rather than letting Mustache do it because Mustache
52 // uses htmlentities, which does not work with XML
53 $templateParams = [
54 'title' => $this->getTitle(),
55 'url' => $this->xmlEncode(
56 $this->urlUtils->expand( $this->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
57 ),
58 'description' => $this->getDescription(),
59 'language' => $this->xmlEncode( $this->getLanguage() ),
60 'version' => $this->xmlEncode( MW_VERSION ),
61 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
62 ];
63 print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
64 }
65
70 public function outItem( $item ) {
71 // Manually escaping rather than letting Mustache do it because Mustache
72 // uses htmlentities, which does not work with XML
73 $templateParams = [
74 "title" => $item->getTitle(),
75 "url" => $this->xmlEncode(
76 $this->urlUtils->expand( $item->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
77 ),
78 "permalink" => $item->rssIsPermalink,
79 "uniqueID" => $item->getUniqueID(),
80 "description" => $item->getDescription(),
81 "date" => $this->xmlEncodeNullable( $this->formatTime( $item->getDate() ) ),
82 "author" => $item->getAuthor()
83 ];
84 $comments = $item->getCommentsUnescaped();
85 if ( $comments ) {
86 $commentsEscaped = $this->xmlEncode(
87 $this->urlUtils->expand( $comments, PROTO_CURRENT ) ?? ''
88 );
89 $templateParams["comments"] = $commentsEscaped;
90 }
91 print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
92 }
93
97 public function outFooter() {
98 print "</channel></rss>";
99 }
100}
const PROTO_CURRENT
Definition Defines.php:236
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:37
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.
outXmlHeader()
Output the initial XML headers.
xmlEncodeNullable(?string $string)
Encode $string so that it can be safely embedded in a XML document, returning null if $string was nul...
Definition FeedItem.php:107
getTitle()
Get the title of this item; already xml-encoded.
Definition FeedItem.php:152
getDescription()
Get the description of this item; already xml-encoded.
Definition FeedItem.php:178
getLanguage()
Get the language of this item.
Definition FeedItem.php:196
xmlEncode( $string)
Encode $string so that it can be safely embedded in a XML document.
Definition FeedItem.php:95
Generate an RSS feed.
Definition RSSFeed.php:31
outFooter()
Output an RSS 2.0 footer.
Definition RSSFeed.php:97
outItem( $item)
Output an RSS 2.0 item.
Definition RSSFeed.php:70
outHeader()
Output an RSS 2.0 header.
Definition RSSFeed.php:49