MediaWiki master
PageDataRequestHandler.php
Go to the documentation of this file.
1<?php
22
23use HttpError;
32
40
53 public function canHandleRequest( $subPage, WebRequest $request ) {
54 if ( $subPage === '' || $subPage === null ) {
55 return $request->getText( 'target' ) !== '';
56 }
57
58 $parts = explode( '/', $subPage, 2 );
59 $slot = $parts[0];
60 $title = $parts[1] ?? '';
61 return ( $slot === SlotRecord::MAIN || $slot === '' ) && $title !== '';
62 }
63
79 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
80 // No matter what: The response is always public
81 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
82
83 if ( !$this->canHandleRequest( $subPage, $request ) ) {
84 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
85 }
86
87 $revision = 0;
88
89 if ( $subPage !== '' ) {
90 $parts = explode( '/', $subPage, 2 );
91 $title = $parts[1] ?? '';
92 } else {
93 $title = $request->getText( 'target' );
94 }
95
96 $revision = $request->getInt( 'oldid', $revision );
97 $revision = $request->getInt( 'revision', $revision );
98
99 if ( $title === null || $title === '' ) {
100 // TODO: different error message?
101 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', (string)$title ) );
102 }
103
104 try {
105 $title = MediaWikiServices::getInstance()->getTitleFactory()->newFromTextThrow( $title );
106 } catch ( MalformedTitleException $ex ) {
107 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
108 }
109
110 $this->httpContentNegotiation( $request, $output, $title, $revision );
111 }
112
125 public function httpContentNegotiation(
126 WebRequest $request,
127 OutputPage $output,
128 Title $title,
129 $revision = 0
130 ) {
131 $mimeTypes = MediaWikiServices::getInstance()
132 ->getContentHandlerFactory()
133 ->getContentHandler( $title->getContentModel() )
134 ->getSupportedFormats();
135
136 $acceptHeader = $request->getHeader( 'Accept' );
137 if ( $acceptHeader !== false ) {
138 $parser = new HttpAcceptParser();
139 $accept = $parser->parseWeights( $acceptHeader );
140 } else {
141 // anything goes
142 $accept = [
143 '*' => 0.1 // just to make extra sure
144 ];
145 // prefer the default
146 $accept[$mimeTypes[0]] = 1;
147 }
148
149 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
150 $format = $negotiator->getBestSupportedKey( $accept );
151
152 if ( $format === null ) {
153 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
154 }
155
156 if ( $format === null ) {
157 throw new HttpError( 406, wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) ) );
158 }
159
160 $url = $this->getDocUrl( $title, $format, $revision );
161 $output->redirect( $url, 303 );
162 }
163
172 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
173 $params = [];
174
175 if ( $revision > 0 ) {
176 $params['oldid'] = $revision;
177 }
178
179 if ( $format === 'text/html' ) {
180 return $title->getFullURL( $params );
181 }
182
183 $params[ 'action' ] = 'raw';
184
185 return $title->getFullURL( $params );
186 }
187
188}
189
191class_alias( PageDataRequestHandler::class, 'PageDataRequestHandler' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
Show an error that looks like an HTTP server error.
Definition HttpError.php:33
Request handler implementing a data interface for mediawiki pages.
httpContentNegotiation(WebRequest $request, OutputPage $output, Title $title, $revision=0)
Applies HTTP content negotiation.
handleRequest( $subPage, WebRequest $request, OutputPage $output)
Main method for handling requests.
canHandleRequest( $subPage, WebRequest $request)
Checks whether the request is complete, i.e.
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.
redirect( $url, $responsecode='302')
Redirect to $url rather than displaying the normal page.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
getText( $name, $default='')
Fetch a text string from this web request's $_GET, $_POST or path router vars and return it in normal...
getHeader( $name, $flags=0)
Get a request header, or false if it isn't set.
getInt( $name, $default=0)
Fetch an integer value from this web request's $_GET, $_POST or path router vars, or return $default ...
Value object representing a content slot associated with a page revision.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Represents a title within MediaWiki.
Definition Title.php:78
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition Title.php:428
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2130
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition Title.php:1065
Utility for negotiating a value from a set of supported values using a preference list.