Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RSSFeed
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 formatTime
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 outHeader
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 outItem
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 outFooter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org>
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
24namespace MediaWiki\Feed;
25
26/**
27 * Generate an RSS feed.
28 *
29 * @ingroup Feed
30 */
31class RSSFeed extends ChannelFeed {
32
33    /**
34     * Format a date given a timestamp. If a timestamp is not given, nothing is returned
35     *
36     * @param string|int|null $ts Timestamp
37     * @return string|null Date string
38     */
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
46    /**
47     * Output an RSS 2.0 header
48     */
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
66    /**
67     * Output an RSS 2.0 item
68     * @param FeedItem $item Item to be output
69     */
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
94    /**
95     * Output an RSS 2.0 footer
96     */
97    public function outFooter() {
98        print "</channel></rss>";
99    }
100}