Merge "api: Update QueryFilearchive to provide information to everyone"
[lhc/web/wiklou.git] / includes / changes / ChannelFeed.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 * Class to support the outputting of syndication feeds in Atom and RSS format.
26 *
27 * @ingroup Feed
28 */
29 abstract class ChannelFeed extends FeedItem {
30
31 /** @var TemplateParser */
32 protected $templateParser;
33
34 /**
35 * @param string|Title $title Feed's title
36 * @param string $description
37 * @param string $url URL uniquely designating the feed.
38 * @param string $date Feed's date
39 * @param string $author Author's user name
40 * @param string $comments
41 */
42 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
43 parent::__construct( $title, $description, $url, $date, $author, $comments );
44 $this->templateParser = new TemplateParser();
45 }
46
47 /**
48 * Generate Header of the feed
49 * @par Example:
50 * @code
51 * print "<feed>";
52 * @endcode
53 */
54 abstract public function outHeader();
55
56 /**
57 * Generate an item
58 * @par Example:
59 * @code
60 * print "<item>...</item>";
61 * @endcode
62 * @param FeedItem $item
63 */
64 abstract public function outItem( $item );
65
66 /**
67 * Generate Footer of the feed
68 * @par Example:
69 * @code
70 * print "</feed>";
71 * @endcode
72 */
73 abstract public function outFooter();
74
75 /**
76 * Setup and send HTTP headers. Don't send any content;
77 * content might end up being cached and re-sent with
78 * these same headers later.
79 *
80 * This should be called from the outHeader() method,
81 * but can also be called separately.
82 */
83 public function httpHeaders() {
84 global $wgOut, $wgVaryOnXFP;
85
86 # We take over from $wgOut, excepting its cache header info
87 $wgOut->disable();
88 $mimetype = $this->contentType();
89 header( "Content-type: $mimetype; charset=UTF-8" );
90
91 // Set a sane filename
92 $exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
93 ->getExtensionsForType( $mimetype );
94 $ext = $exts ? strtok( $exts, ' ' ) : 'xml';
95 header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
96
97 if ( $wgVaryOnXFP ) {
98 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
99 }
100 $wgOut->sendCacheControl();
101 }
102
103 /**
104 * Return an internet media type to be sent in the headers.
105 *
106 * @return string
107 */
108 private function contentType() {
109 global $wgRequest;
110
111 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
112 $allowedctypes = [
113 'application/xml',
114 'text/xml',
115 'application/rss+xml',
116 'application/atom+xml'
117 ];
118
119 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
120 }
121
122 /**
123 * Output the initial XML headers.
124 */
125 protected function outXmlHeader() {
126 $this->httpHeaders();
127 echo '<?xml version="1.0"?>' . "\n";
128 }
129 }