Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChannelFeed
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 outHeader
n/a
0 / 0
n/a
0 / 0
0
 outItem
n/a
0 / 0
n/a
0 / 0
0
 outFooter
n/a
0 / 0
n/a
0 / 0
0
 httpHeaders
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 contentType
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 outXmlHeader
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org>
5 * https://www.mediawiki.org/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25namespace MediaWiki\Feed;
26
27use MediaWiki\Html\TemplateParser;
28use MediaWiki\MainConfigNames;
29use MediaWiki\MediaWikiServices;
30use MediaWiki\Title\Title;
31
32/**
33 * Class to support the outputting of syndication feeds in Atom and RSS format.
34 *
35 * @stable to extend
36 * @ingroup Feed
37 */
38abstract class ChannelFeed extends FeedItem {
39
40    /** @var TemplateParser */
41    protected $templateParser;
42
43    /**
44     * @stable to call
45     *
46     * @param string|Title $title Feed's title
47     * @param string $description
48     * @param string $url URL uniquely designating the feed.
49     * @param string $date Feed's date
50     * @param string $author Author's user name
51     * @param string $comments
52     *
53     */
54    public function __construct(
55        $title, $description, $url, $date = '', $author = '', $comments = ''
56    ) {
57        parent::__construct( $title, $description, $url, $date, $author, $comments );
58        $this->templateParser = new TemplateParser();
59    }
60
61    /**
62     * Generate Header of the feed
63     * @par Example:
64     * @code
65     * print "<feed>";
66     * @endcode
67     */
68    abstract public function outHeader();
69
70    /**
71     * Generate an item
72     * @par Example:
73     * @code
74     * print "<item>...</item>";
75     * @endcode
76     * @param FeedItem $item
77     */
78    abstract public function outItem( $item );
79
80    /**
81     * Generate Footer of the feed
82     * @par Example:
83     * @code
84     * print "</feed>";
85     * @endcode
86     */
87    abstract public function outFooter();
88
89    /**
90     * Setup and send HTTP headers. Don't send any content;
91     * content might end up being cached and re-sent with
92     * these same headers later.
93     *
94     * This should be called from the outHeader() method,
95     * but can also be called separately.
96     */
97    public function httpHeaders() {
98        global $wgOut;
99        $varyOnXFP = MediaWikiServices::getInstance()->getMainConfig()
100            ->get( MainConfigNames::VaryOnXFP );
101        # We take over from $wgOut, excepting its cache header info
102        $wgOut->disable();
103        $mimetype = $this->contentType();
104        header( "Content-type: $mimetype; charset=UTF-8" );
105
106        // Set a sensible filename
107        $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
108        $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $mimetype ) ?? 'xml';
109        header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
110
111        if ( $varyOnXFP ) {
112            $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
113        }
114        $wgOut->sendCacheControl();
115    }
116
117    /**
118     * Return an internet media type to be sent in the headers.
119     *
120     * @stable to override
121     *
122     * @return string
123     */
124    private function contentType() {
125        global $wgRequest;
126
127        $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
128        $allowedctypes = [
129            'application/xml',
130            'text/xml',
131            'application/rss+xml',
132            'application/atom+xml'
133        ];
134
135        return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
136    }
137
138    /**
139     * Output the initial XML headers.
140     */
141    protected function outXmlHeader() {
142        $this->httpHeaders();
143        echo '<?xml version="1.0"?>' . "\n";
144    }
145}
146
147/** @deprecated class alias since 1.40 */
148class_alias( ChannelFeed::class, 'ChannelFeed' );