MediaWiki master
ChannelFeed.php
Go to the documentation of this file.
1<?php
2
11namespace MediaWiki\Feed;
12
18
25abstract class ChannelFeed extends FeedItem {
26
28 protected $templateParser;
29
40 public function __construct(
41 $title, $description, $url, $date = '', $author = '', $comments = ''
42 ) {
43 parent::__construct( $title, $description, $url, $date, $author, $comments );
44 $this->templateParser = new TemplateParser();
45 }
46
55 abstract public function outputHeader( $output ): void;
56
66 abstract public function outputItem( FeedItem $item, $output ): void;
67
76 abstract public function outputFooter( $output ): void;
77
86 public function sendHttpHeaders( $output ): void {
87 $varyOnXFP = MediaWikiServices::getInstance()->getMainConfig()
88 ->get( MainConfigNames::VaryOnXFP );
89 # We take over from $wgOut, excepting its cache header info
90 $output->disable();
91 $mimetype = $this->contentType( $output->getRequest() );
92 header( "Content-type: $mimetype; charset=UTF-8" );
93 // @todo Maybe set a CSP header here at some point as defense in depth.
94 // need to figure out how that interacts with browser display of article
95 // snippets.
96
97 // Set a sensible filename
98 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
99 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $mimetype ) ?? 'xml';
100 header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
101
102 if ( $varyOnXFP ) {
103 $output->addVaryHeader( 'X-Forwarded-Proto' );
104 }
105 $output->sendCacheControl();
106 }
107
113 private function contentType( $request ): string {
114 $ctype = $request->getVal( 'ctype', 'application/xml' );
115 $allowedctypes = [
116 'application/xml',
117 'text/xml',
118 'application/rss+xml',
119 'application/atom+xml'
120 ];
121
122 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
123 }
124
131 protected function outputXmlHeader( $output ): void {
132 $this->sendHttpHeaders( $output );
133 echo '<?xml version="1.0"?>' . "\n";
134 }
135}
Class to support the outputting of syndication feeds in Atom and RSS format.
TemplateParser $templateParser
__construct( $title, $description, $url, $date='', $author='', $comments='')
outputHeader( $output)
Generate Header of the feed.
outputXmlHeader( $output)
Output the initial XML headers.
outputFooter( $output)
Generate Footer of the feed.
outputItem(FeedItem $item, $output)
Generate an item.
sendHttpHeaders( $output)
Setup and send HTTP headers.
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:26
Handles compiling Mustache templates into PHP rendering functions.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This is one of the Core classes and should be read at least once by any new developers.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...