Debian Codegraph @

includes/Feed/ChannelFeed.php

raw symbols SCIP
1 <?php
2
3 /**
4 * Copyright © 2004 Brooke Vibber <[email protected]>
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
25 namespace MediaWiki\Feed;
26
27 use MediaWiki\Html\TemplateParser;
28 use MediaWiki\MainConfigNames;
29 use MediaWiki\MediaWikiServices;
30 use 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 */
38 abstract 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 */
148 class_alias( ChannelFeed::class, 'ChannelFeed' );