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