| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Rest\Handler\Helper\HtmlOutputHelper; |
| 7 |
use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper; |
| 8 |
use MediaWiki\Rest\Handler\Helper\PageContentHelper; |
| 9 |
use MediaWiki\Rest\Handler\Helper\PageRedirectHelper; |
| 10 |
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory; |
| 11 |
use MediaWiki\Rest\LocalizedHttpException; |
| 12 |
use MediaWiki\Rest\Response; |
| 13 |
use MediaWiki\Rest\SimpleHandler; |
| 14 |
use MediaWiki\Rest\StringStream; |
| 15 |
use Wikimedia\Assert\Assert; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class PageHTMLHandler extends SimpleHandler { |
| 25 |
|
| 26 |
private HtmlOutputHelper $htmlHelper; |
| 27 |
private PageContentHelper $contentHelper; |
| 28 |
private PageRestHelperFactory $helperFactory; |
| 29 |
|
| 30 |
public function __construct( |
| 31 |
PageRestHelperFactory $helperFactory |
| 32 |
) { |
| 33 |
$this->contentHelper = $helperFactory->newPageContentHelper(); |
| 34 |
$this->helperFactory = $helperFactory; |
| 35 |
} |
| 36 |
|
| 37 |
private function getRedirectHelper(): PageRedirectHelper { |
| 38 |
return $this->helperFactory->newPageRedirectHelper( |
| 39 |
$this->getResponseFactory(), |
| 40 |
$this->getRouter(), |
| 41 |
$this->getPath(), |
| 42 |
$this->getRequest() |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
protected function postValidationSetup() { |
| 47 |
$authority = $this->getAuthority(); |
| 48 |
$this->contentHelper->init( $authority, $this->getValidatedParams() ); |
| 49 |
|
| 50 |
$page = $this->contentHelper->getPageIdentity(); |
| 51 |
$isSystemMessage = $this->contentHelper->useDefaultSystemMessage(); |
| 52 |
|
| 53 |
if ( $page ) { |
| 54 |
if ( $isSystemMessage ) { |
| 55 |
$this->htmlHelper = $this->helperFactory->newHtmlMessageOutputHelper( $page ); |
| 56 |
} else { |
| 57 |
$revision = $this->contentHelper->getTargetRevision(); |
| 58 |
$this->htmlHelper = $this->helperFactory->newHtmlOutputRendererHelper( |
| 59 |
$page, $this->getValidatedParams(), $authority, $revision |
| 60 |
); |
| 61 |
|
| 62 |
$request = $this->getRequest(); |
| 63 |
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null; |
| 64 |
if ( $acceptLanguage ) { |
| 65 |
$this->htmlHelper->setVariantConversionLanguage( |
| 66 |
$acceptLanguage |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function run(): Response { |
| 78 |
$this->contentHelper->checkAccessPermission(); |
| 79 |
$page = $this->contentHelper->getPageIdentity(); |
| 80 |
$params = $this->getRequest()->getQueryParams(); |
| 81 |
|
| 82 |
if ( array_key_exists( 'redirect', $params ) ) { |
| 83 |
$followWikiRedirects = $params['redirect'] !== 'no'; |
| 84 |
} else { |
| 85 |
$followWikiRedirects = true; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
Assert::invariant( $page !== null, 'Page should be known' ); |
| 91 |
|
| 92 |
$redirectHelper = $this->getRedirectHelper(); |
| 93 |
$redirectHelper->setFollowWikiRedirects( $followWikiRedirects ); |
| 94 |
|
| 95 |
|
| 96 |
$redirectResponse = $redirectHelper->createRedirectResponseIfNeeded( |
| 97 |
$page, |
| 98 |
$this->contentHelper->getTitleText() |
| 99 |
); |
| 100 |
|
| 101 |
if ( $redirectResponse !== null ) { |
| 102 |
$redirectResponse->setHeader( 'Cache-Control', 'max-age=60' ); |
| 103 |
return $redirectResponse; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$this->contentHelper->checkHasContent(); |
| 108 |
|
| 109 |
$parserOutput = $this->htmlHelper->getHtml(); |
| 110 |
$parserOutputHtml = $parserOutput->getRawText(); |
| 111 |
|
| 112 |
$outputMode = $this->getOutputMode(); |
| 113 |
switch ( $outputMode ) { |
| 114 |
case 'html': |
| 115 |
$response = $this->getResponseFactory()->create(); |
| 116 |
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() ); |
| 117 |
$response->setBody( new StringStream( $parserOutputHtml ) ); |
| 118 |
break; |
| 119 |
case 'with_html': |
| 120 |
$body = $this->contentHelper->constructMetadata(); |
| 121 |
$body['html'] = $parserOutputHtml; |
| 122 |
|
| 123 |
$redirectTargetUrl = $redirectHelper->getWikiRedirectTargetUrl( $page ); |
| 124 |
|
| 125 |
if ( $redirectTargetUrl ) { |
| 126 |
$body['redirect_target'] = $redirectTargetUrl; |
| 127 |
} |
| 128 |
|
| 129 |
$response = $this->getResponseFactory()->createJson( $body ); |
| 130 |
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() ); |
| 131 |
break; |
| 132 |
default: |
| 133 |
throw new LogicException( "Unknown HTML type $outputMode" ); |
| 134 |
} |
| 135 |
|
| 136 |
$setContentLanguageHeader = ( $outputMode === 'html' ); |
| 137 |
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader ); |
| 138 |
|
| 139 |
return $response; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
protected function getETag(): ?string { |
| 149 |
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) { |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
return $this->htmlHelper->getETag( $this->getOutputMode() ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
protected function getLastModified(): ?string { |
| 161 |
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) { |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
return $this->htmlHelper->getLastModified(); |
| 166 |
} |
| 167 |
|
| 168 |
private function getOutputMode(): string { |
| 169 |
return $this->getConfig()['format']; |
| 170 |
} |
| 171 |
|
| 172 |
public function needsWriteAccess(): bool { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
public function getParamSettings(): array { |
| 177 |
return array_merge( |
| 178 |
$this->contentHelper->getParamSettings(), |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
HtmlOutputRendererHelper::getParamSettings() |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
protected function generateResponseSpec( string $method ): array { |
| 187 |
$spec = parent::generateResponseSpec( $method ); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if ( $this->getOutputMode() == 'html' ) { |
| 194 |
unset( $spec['200']['content']['application/json'] ); |
| 195 |
$spec['200']['content']['text/html']['schema']['type'] = 'string'; |
| 196 |
} |
| 197 |
|
| 198 |
return $spec; |
| 199 |
} |
| 200 |
|
| 201 |
public function getResponseBodySchemaFileName( string $method ): ?string { |
| 202 |
return 'includes/Rest/Handler/Schema/ExistingPageHtml.json'; |
| 203 |
} |
| 204 |
} |