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
37 public function outHeader() {
38 $this->outXmlHeader();
39 // Manually escaping rather than letting Mustache do it because Mustache
40 // uses htmlentities, which does not work with XML
41 $templateParams = [
42 'title' => $this->getTitle(),
43 'url' => $this->xmlEncode(
44 $this->urlUtils->expand( $this->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
45 ),
46 'description' => $this->getDescription(),
47 'language' => $this->xmlEncode( $this->getLanguage() ),
48 'version' => $this->xmlEncode( MW_VERSION ),
49 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
50 ];
51 print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
52 }
53
58 public function outItem( $item ) {
59 // Manually escaping rather than letting Mustache do it because Mustache
60 // uses htmlentities, which does not work with XML
61 $templateParams = [
62 "title" => $item->getTitle(),
63 "url" => $this->xmlEncode(
64 $this->urlUtils->expand( $item->getUrlUnescaped(), PROTO_CURRENT ) ?? ''
65 ),
66 "permalink" => $item->rssIsPermalink,
67 "uniqueID" => $item->getUniqueID(),
68 "description" => $item->getDescription(),
69 "date" => $this->xmlEncodeNullable( $this->formatTime( $item->getDate() ) ),
70 "author" => $item->getAuthor()
71 ];
72 $comments = $item->getCommentsUnescaped();
73 if ( $comments ) {
74 $commentsEscaped = $this->xmlEncode(
75 $this->urlUtils->expand( $comments, PROTO_CURRENT ) ?? ''
76 );
77 $templateParams["comments"] = $commentsEscaped;
78 }
79 print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
80 }
81
85 public function outFooter() {
86 print "</channel></rss>";
87 }
88}
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.
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:93
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
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
outFooter()
Output an RSS 2.0 footer.
Definition RSSFeed.php:85
outItem( $item)
Output an RSS 2.0 item.
Definition RSSFeed.php:58
outHeader()
Output an RSS 2.0 header.
Definition RSSFeed.php:37