| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper; |
| 7 |
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory; |
| 8 |
use MediaWiki\Rest\Handler\Helper\RevisionContentHelper; |
| 9 |
use MediaWiki\Rest\LocalizedHttpException; |
| 10 |
use MediaWiki\Rest\Response; |
| 11 |
use MediaWiki\Rest\SimpleHandler; |
| 12 |
use MediaWiki\Rest\StringStream; |
| 13 |
use Wikimedia\Assert\Assert; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class RevisionHTMLHandler extends SimpleHandler { |
| 21 |
|
| 22 |
private ?HtmlOutputRendererHelper $htmlHelper = null; |
| 23 |
private PageRestHelperFactory $helperFactory; |
| 24 |
private RevisionContentHelper $contentHelper; |
| 25 |
|
| 26 |
public function __construct( PageRestHelperFactory $helperFactory ) { |
| 27 |
$this->helperFactory = $helperFactory; |
| 28 |
$this->contentHelper = $helperFactory->newRevisionContentHelper(); |
| 29 |
} |
| 30 |
|
| 31 |
protected function postValidationSetup() { |
| 32 |
$authority = $this->getAuthority(); |
| 33 |
$this->contentHelper->init( $authority, $this->getValidatedParams() ); |
| 34 |
|
| 35 |
$page = $this->contentHelper->getPage(); |
| 36 |
$revision = $this->contentHelper->getTargetRevision(); |
| 37 |
|
| 38 |
if ( $page && $revision ) { |
| 39 |
$this->htmlHelper = $this->helperFactory->newHtmlOutputRendererHelper( |
| 40 |
$page, $this->getValidatedParams(), $authority, $revision |
| 41 |
); |
| 42 |
|
| 43 |
$request = $this->getRequest(); |
| 44 |
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null; |
| 45 |
if ( $acceptLanguage ) { |
| 46 |
$this->htmlHelper->setVariantConversionLanguage( |
| 47 |
$acceptLanguage |
| 48 |
); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function run(): Response { |
| 58 |
$this->contentHelper->checkAccess(); |
| 59 |
|
| 60 |
$page = $this->contentHelper->getPage(); |
| 61 |
$revisionRecord = $this->contentHelper->getTargetRevision(); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Assert::invariant( $page !== null, 'Page should be known' ); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Assert::invariant( $revisionRecord !== null, 'Revision should be known' ); |
| 70 |
|
| 71 |
$outputMode = $this->getOutputMode(); |
| 72 |
$setContentLanguageHeader = true; |
| 73 |
switch ( $outputMode ) { |
| 74 |
case 'html': |
| 75 |
$parserOutput = $this->htmlHelper->getHtml(); |
| 76 |
$response = $this->getResponseFactory()->create(); |
| 77 |
|
| 78 |
$response->setHeader( 'Content-Type', 'text/html' ); |
| 79 |
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader ); |
| 80 |
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() ); |
| 81 |
$response->setBody( new StringStream( $parserOutput->getRawText() ) ); |
| 82 |
break; |
| 83 |
case 'with_html': |
| 84 |
$parserOutput = $this->htmlHelper->getHtml(); |
| 85 |
$body = $this->contentHelper->constructMetadata(); |
| 86 |
$body['html'] = $parserOutput->getRawText(); |
| 87 |
$response = $this->getResponseFactory()->createJson( $body ); |
| 88 |
|
| 89 |
$this->htmlHelper->putHeaders( $response, !$setContentLanguageHeader ); |
| 90 |
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() ); |
| 91 |
break; |
| 92 |
default: |
| 93 |
throw new LogicException( "Unknown HTML type $outputMode" ); |
| 94 |
} |
| 95 |
|
| 96 |
return $response; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
protected function getETag(): ?string { |
| 106 |
if ( !$this->contentHelper->isAccessible() ) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
return $this->htmlHelper->getETag( $this->getOutputMode() ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected function getLastModified(): ?string { |
| 118 |
if ( !$this->contentHelper->isAccessible() ) { |
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
return $this->htmlHelper->getLastModified(); |
| 123 |
} |
| 124 |
|
| 125 |
private function getOutputMode(): string { |
| 126 |
return $this->getConfig()['format']; |
| 127 |
} |
| 128 |
|
| 129 |
public function needsWriteAccess(): bool { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
public function getParamSettings(): array { |
| 134 |
return array_merge( |
| 135 |
$this->contentHelper->getParamSettings(), |
| 136 |
HtmlOutputRendererHelper::getParamSettings() |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
protected function hasRepresentation() { |
| 144 |
return $this->contentHelper->hasContent(); |
| 145 |
} |
| 146 |
} |