MediaWiki master
PageDataRequestHandler.php
Go to the documentation of this file.
1<?php
8
18
26
39 public function canHandleRequest( $subPage, WebRequest $request ) {
40 if ( $subPage === '' || $subPage === null ) {
41 return $request->getText( 'target' ) !== '';
42 }
43
44 $parts = explode( '/', $subPage, 2 );
45 $slot = $parts[0];
46 $title = $parts[1] ?? '';
47 return ( $slot === SlotRecord::MAIN || $slot === '' ) && $title !== '';
48 }
49
65 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
66 // No matter what: The response is always public
67 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
68
69 if ( !$this->canHandleRequest( $subPage, $request ) ) {
70 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
71 }
72
73 $revision = 0;
74
75 if ( $subPage !== '' ) {
76 $parts = explode( '/', $subPage, 2 );
77 $titleText = $parts[1] ?? '';
78 } else {
79 $titleText = $request->getText( 'target' );
80 }
81
82 $revision = $request->getInt( 'oldid', $revision );
83 $revision = $request->getInt( 'revision', $revision );
84
85 if ( $titleText === null || $titleText === '' ) {
86 // TODO: different error message?
87 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', (string)$titleText ) );
88 }
89
90 try {
91 $title = MediaWikiServices::getInstance()->getTitleFactory()->newFromTextThrow( $titleText );
92 } catch ( MalformedTitleException ) {
93 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $titleText ) );
94 }
95
96 $this->httpContentNegotiation( $request, $output, $title, $revision );
97 }
98
111 public function httpContentNegotiation(
112 WebRequest $request,
113 OutputPage $output,
114 Title $title,
115 $revision = 0
116 ) {
117 $mimeTypes = MediaWikiServices::getInstance()
118 ->getContentHandlerFactory()
119 ->getContentHandler( $title->getContentModel() )
120 ->getSupportedFormats();
121
122 $acceptHeader = $request->getHeader( 'Accept' );
123 if ( $acceptHeader !== false ) {
124 $parser = new HttpAcceptParser();
125 $accept = $parser->parseWeights( $acceptHeader );
126 } else {
127 // anything goes
128 $accept = [
129 '*' => 0.1 // just to make extra sure
130 ];
131 // prefer the default
132 $accept[$mimeTypes[0]] = 1;
133 }
134
135 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
136 $format = $negotiator->getBestSupportedKey( $accept );
137
138 if ( $format === null ) {
139 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
140 }
141
142 if ( $format === null ) {
143 throw new HttpError( 406, wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) ) );
144 }
145
146 $url = $this->getDocUrl( $title, $format, $revision );
147 $output->redirect( $url, 303 );
148 }
149
158 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
159 $params = [];
160
161 if ( $revision > 0 ) {
162 $params['oldid'] = $revision;
163 }
164
165 if ( $format === 'text/html' ) {
166 return $title->getFullURL( $params );
167 }
168
169 $params[ 'action' ] = 'raw';
170
171 return $title->getFullURL( $params );
172 }
173
174}
175
177class_alias( PageDataRequestHandler::class, 'PageDataRequestHandler' );
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:23
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:70
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition Title.php:424
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2153
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition Title.php:1060
Utility for negotiating a value from a set of supported values using a preference list.