MediaWiki master
ChannelFeed.php
Go to the documentation of this file.
1<?php
2
10
11namespace MediaWiki\Feed;
12
13use MediaWiki\Html\TemplateParser;
14use MediaWiki\MainConfigNames;
15use MediaWiki\MediaWikiServices;
16use MediaWiki\Output\OutputPage;
17use MediaWiki\Request\WebRequest;
18use Wikimedia\Http\HttpHeader;
19
26abstract class ChannelFeed extends FeedItem {
27
29 protected $templateParser;
30
41 public function __construct(
42 $title, $description, $url, $date = '', $author = '', $comments = ''
43 ) {
44 parent::__construct( $title, $description, $url, $date, $author, $comments );
45 $this->templateParser = new TemplateParser();
46 }
47
56 abstract public function outputHeader( $output ): void;
57
67 abstract public function outputItem( FeedItem $item, $output ): void;
68
77 abstract public function outputFooter( $output ): void;
78
87 public function sendHttpHeaders( $output ): void {
88 $varyOnXFP = MediaWikiServices::getInstance()->getMainConfig()
89 ->get( MainConfigNames::VaryOnXFP );
90 # We take over from $wgOut, excepting its cache header info
91 $output->disable();
92 $mimetype = $this->contentType( $output->getRequest() );
93 HttpHeader::send( "Content-type: $mimetype; charset=UTF-8" );
94 // @todo Maybe set a CSP header here at some point as defense in depth.
95 // need to figure out how that interacts with browser display of article
96 // snippets.
97
98 // Set a sensible filename
99 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
100 $ext = $mimeAnalyzer->getExtensionFromMimeTypeOrNull( $mimetype ) ?? 'xml';
101 HttpHeader::send( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
102
103 if ( $varyOnXFP ) {
104 $output->addVaryHeader( 'X-Forwarded-Proto' );
105 }
106 $output->sendCacheControl();
107 }
108
114 private function contentType( $request ): string {
115 $ctype = $request->getVal( 'ctype', 'application/xml' );
116 $allowedctypes = [
117 'application/xml',
118 'text/xml',
119 'application/rss+xml',
120 'application/atom+xml'
121 ];
122
123 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
124 }
125
132 protected function outputXmlHeader( $output ): void {
133 $this->sendHttpHeaders( $output );
134 echo '<?xml version="1.0"?>' . "\n";
135 }
136}
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.