MediaWiki REL1_41
PageDataRequestHandler.php
Go to the documentation of this file.
1<?php
29
37
50 public function canHandleRequest( $subPage, WebRequest $request ) {
51 if ( $subPage === '' || $subPage === null ) {
52 return $request->getText( 'target' ) !== '';
53 }
54
55 $parts = explode( '/', $subPage, 2 );
56 $slot = $parts[0];
57 $title = $parts[1] ?? '';
58 return ( $slot === SlotRecord::MAIN || $slot === '' ) && $title !== '';
59 }
60
76 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
77 // No matter what: The response is always public
78 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
79
80 if ( !$this->canHandleRequest( $subPage, $request ) ) {
81 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
82 }
83
84 $revision = 0;
85
86 if ( $subPage !== '' ) {
87 $parts = explode( '/', $subPage, 2 );
88 $title = $parts[1] ?? '';
89 } else {
90 $title = $request->getText( 'target' );
91 }
92
93 $revision = $request->getInt( 'oldid', $revision );
94 $revision = $request->getInt( 'revision', $revision );
95
96 if ( $title === null || $title === '' ) {
97 // TODO: different error message?
98 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
99 }
100
101 try {
102 $title = MediaWikiServices::getInstance()->getTitleFactory()->newFromTextThrow( $title );
103 } catch ( MalformedTitleException $ex ) {
104 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
105 }
106
107 $this->httpContentNegotiation( $request, $output, $title, $revision );
108 }
109
122 public function httpContentNegotiation(
123 WebRequest $request,
124 OutputPage $output,
125 Title $title,
126 $revision = 0
127 ) {
128 $mimeTypes = MediaWikiServices::getInstance()
129 ->getContentHandlerFactory()
130 ->getContentHandler( $title->getContentModel() )
131 ->getSupportedFormats();
132
133 $acceptHeader = $request->getHeader( 'Accept' );
134 if ( $acceptHeader !== false ) {
135 $parser = new HttpAcceptParser();
136 $accept = $parser->parseWeights( $acceptHeader );
137 } else {
138 // anything goes
139 $accept = [
140 '*' => 0.1 // just to make extra sure
141 ];
142 // prefer the default
143 $accept[$mimeTypes[0]] = 1;
144 }
145
146 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
147 $format = $negotiator->getBestSupportedKey( $accept );
148
149 if ( $format === null ) {
150 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
151 }
152
153 if ( $format === null ) {
154 throw new HttpError( 406, wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) ) );
155 }
156
157 $url = $this->getDocUrl( $title, $format, $revision );
158 $output->redirect( $url, 303 );
159 }
160
169 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
170 $params = [];
171
172 if ( $revision > 0 ) {
173 $params['oldid'] = $revision;
174 }
175
176 if ( $format === 'text/html' ) {
177 return $title->getFullURL( $params );
178 }
179
180 $params[ 'action' ] = 'raw';
181
182 return $title->getFullURL( $params );
183 }
184
185}
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
Service locator for MediaWiki core services.
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 stripping il...
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.
getInt( $name, $default=0)
Fetch an integer value from the input or return $default if not set.
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:76
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition Title.php:435
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2135
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition Title.php:1080
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.
Utility for negotiating a value from a set of supported values using a preference list.