changes: Split Feed.php into a class per file
[lhc/web/wiklou.git] / includes / changes / RSSFeed.php
1 <?php
2 /**
3 * Copyright © 2004 Brion Vibber <brion@pobox.com>
4 * https://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Generate an RSS feed.
26 *
27 * @ingroup Feed
28 */
29 class RSSFeed extends ChannelFeed {
30
31 /**
32 * Format a date given a timestamp. If a timestamp is not given, nothing is returned
33 *
34 * @param int|null $ts Timestamp
35 * @return string|null Date string
36 */
37 function formatTime( $ts ) {
38 if ( $ts ) {
39 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
40 }
41 }
42
43 /**
44 * Output an RSS 2.0 header
45 */
46 function outHeader() {
47 global $wgVersion;
48
49 $this->outXmlHeader();
50 // Manually escaping rather than letting Mustache do it because Mustache
51 // uses htmlentities, which does not work with XML
52 $templateParams = [
53 'title' => $this->getTitle(),
54 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
55 'description' => $this->getDescription(),
56 'language' => $this->xmlEncode( $this->getLanguage() ),
57 'version' => $this->xmlEncode( $wgVersion ),
58 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
59 ];
60 print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
61 }
62
63 /**
64 * Output an RSS 2.0 item
65 * @param FeedItem $item Item to be output
66 */
67 function outItem( $item ) {
68 // Manually escaping rather than letting Mustache do it because Mustache
69 // uses htmlentities, which does not work with XML
70 $templateParams = [
71 "title" => $item->getTitle(),
72 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
73 "permalink" => $item->rssIsPermalink,
74 "uniqueID" => $item->getUniqueID(),
75 "description" => $item->getDescription(),
76 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
77 "author" => $item->getAuthor()
78 ];
79 $comments = $item->getCommentsUnescaped();
80 if ( $comments ) {
81 $commentsEscaped = $this->xmlEncode( wfExpandUrl( $comments, PROTO_CURRENT ) );
82 $templateParams["comments"] = $commentsEscaped;
83 }
84 print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
85 }
86
87 /**
88 * Output an RSS 2.0 footer
89 */
90 function outFooter() {
91 print "</channel></rss>";
92 }
93 }