MediaWiki REL1_40
PageDataRequestHandler.php
Go to the documentation of this file.
1<?php
26
34
47 public function canHandleRequest( $subPage, WebRequest $request ) {
48 if ( $subPage === '' || $subPage === null ) {
49 return $request->getText( 'target' ) !== '';
50 }
51
52 $parts = explode( '/', $subPage, 2 );
53 $slot = $parts[0];
54 $title = $parts[1] ?? '';
55 return ( $slot === SlotRecord::MAIN || $slot === '' ) && $title !== '';
56 }
57
73 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
74 // No matter what: The response is always public
75 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
76
77 if ( !$this->canHandleRequest( $subPage, $request ) ) {
78 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
79 }
80
81 $revision = 0;
82
83 if ( $subPage !== '' ) {
84 $parts = explode( '/', $subPage, 2 );
85 $title = $parts[1] ?? '';
86 } else {
87 $title = $request->getText( 'target' );
88 }
89
90 $revision = $request->getInt( 'oldid', $revision );
91 $revision = $request->getInt( 'revision', $revision );
92
93 if ( $title === null || $title === '' ) {
94 // TODO: different error message?
95 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
96 }
97
98 try {
99 $title = Title::newFromTextThrow( $title );
100 } catch ( MalformedTitleException $ex ) {
101 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
102 }
103
104 $this->httpContentNegotiation( $request, $output, $title, $revision );
105 }
106
119 public function httpContentNegotiation(
120 WebRequest $request,
121 OutputPage $output,
123 $revision = 0
124 ) {
125 $mimeTypes = MediaWikiServices::getInstance()
126 ->getContentHandlerFactory()
127 ->getContentHandler( $title->getContentModel() )
128 ->getSupportedFormats();
129
130 $acceptHeader = $request->getHeader( 'Accept' );
131 if ( $acceptHeader !== false ) {
132 $parser = new HttpAcceptParser();
133 $accept = $parser->parseWeights( $acceptHeader );
134 } else {
135 // anything goes
136 $accept = [
137 '*' => 0.1 // just to make extra sure
138 ];
139 // prefer the default
140 $accept[$mimeTypes[0]] = 1;
141 }
142
143 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
144 $format = $negotiator->getBestSupportedKey( $accept );
145
146 if ( $format === null ) {
147 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
148 }
149
150 if ( $format === null ) {
151 throw new HttpError( 406, wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) ) );
152 }
153
154 $url = $this->getDocUrl( $title, $format, $revision );
155 $output->redirect( $url, 303 );
156 }
157
166 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
167 $params = [];
168
169 if ( $revision > 0 ) {
170 $params['oldid'] = $revision;
171 }
172
173 if ( $format === 'text/html' ) {
174 return $title->getFullURL( $params );
175 }
176
177 $params[ 'action' ] = 'raw';
178
179 return $title->getFullURL( $params );
180 }
181
182}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Show an error that looks like an HTTP server error.
Definition HttpError.php:32
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Service locator for MediaWiki core services.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:82
This is one of the Core classes and should be read at least once by any new developers.
redirect( $url, $responsecode='302')
Redirect to $url rather than displaying the normal page.
Request handler implementing a data interface for mediawiki pages.
canHandleRequest( $subPage, WebRequest $request)
Checks whether the request is complete, i.e.
handleRequest( $subPage, WebRequest $request, OutputPage $output)
Main method for handling requests.
httpContentNegotiation(WebRequest $request, OutputPage $output, Title $title, $revision=0)
Applies HTTP content negotiation.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getInt( $name, $default=0)
Fetch an integer value from the input or return $default if not set.
getText( $name, $default='')
Fetch a text string and return it in normalized form.
getHeader( $name, $flags=0)
Get a request header, or false if it isn't set.
Utility for negotiating a value from a set of supported values using a preference list.