| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\LinkedData; |
| 22 |
|
| 23 |
use HttpError; |
| 24 |
use MediaWiki\MediaWikiServices; |
| 25 |
use MediaWiki\Output\OutputPage; |
| 26 |
use MediaWiki\Request\WebRequest; |
| 27 |
use MediaWiki\Revision\SlotRecord; |
| 28 |
use MediaWiki\Title\MalformedTitleException; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use Wikimedia\Http\HttpAcceptNegotiator; |
| 31 |
use Wikimedia\Http\HttpAcceptParser; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class PageDataRequestHandler { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) { |
| 80 |
|
| 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 |
|
| 101 |
throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 142 |
$accept = [ |
| 143 |
'*' => 0.1 |
| 144 |
]; |
| 145 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 190 |
|
| 191 |
class_alias( PageDataRequestHandler::class, 'PageDataRequestHandler' ); |