2020-01-16 23:40:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest\Handler;
|
|
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
use LanguageCode;
|
2020-01-16 23:40:58 +00:00
|
|
|
use LogicException;
|
2022-06-13 09:31:50 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-11-07 15:00:26 +00:00
|
|
|
use MediaWiki\Page\ExistingPageRecord;
|
|
|
|
|
use MediaWiki\Page\RedirectStore;
|
2020-01-16 23:40:58 +00:00
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\Response;
|
2020-12-03 17:53:55 +00:00
|
|
|
use MediaWiki\Rest\SimpleHandler;
|
2020-01-16 23:40:58 +00:00
|
|
|
use MediaWiki\Rest\StringStream;
|
2022-11-14 13:23:43 +00:00
|
|
|
use ParserOutput;
|
2020-01-16 23:40:58 +00:00
|
|
|
use TitleFormatter;
|
2020-12-03 17:53:55 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2022-11-14 13:23:43 +00:00
|
|
|
use Wikimedia\Parsoid\Utils\ContentUtils;
|
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
2020-01-16 23:40:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A handler that returns Parsoid HTML for the following routes:
|
|
|
|
|
* - /page/{title}/html,
|
|
|
|
|
* - /page/{title}/with_html
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki\Rest\Handler
|
|
|
|
|
*/
|
2020-12-03 17:53:55 +00:00
|
|
|
class PageHTMLHandler extends SimpleHandler {
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2022-09-06 09:07:45 +00:00
|
|
|
/** @var HtmlOutputRendererHelper */
|
2020-12-03 17:53:55 +00:00
|
|
|
private $htmlHelper;
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
/** @var PageContentHelper */
|
|
|
|
|
private $contentHelper;
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2022-11-07 15:00:26 +00:00
|
|
|
/** @var TitleFormatter */
|
|
|
|
|
private $titleFormatter;
|
|
|
|
|
|
|
|
|
|
/** @var RedirectStore */
|
|
|
|
|
private $redirectStore;
|
|
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
public function __construct(
|
|
|
|
|
TitleFormatter $titleFormatter,
|
2022-11-18 16:55:18 +00:00
|
|
|
RedirectStore $redirectStore,
|
|
|
|
|
PageRestHelperFactory $helperFactory
|
2020-01-16 23:40:58 +00:00
|
|
|
) {
|
2022-11-07 15:00:26 +00:00
|
|
|
$this->titleFormatter = $titleFormatter;
|
|
|
|
|
$this->redirectStore = $redirectStore;
|
2022-11-18 16:55:18 +00:00
|
|
|
$this->contentHelper = $helperFactory->newPageContentHelper();
|
|
|
|
|
$this->htmlHelper = $helperFactory->newHtmlOutputRendererHelper();
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
protected function postValidationSetup() {
|
2022-06-13 09:31:50 +00:00
|
|
|
// TODO: Once Authority supports rate limit (T310476), just inject the Authority.
|
|
|
|
|
$user = MediaWikiServices::getInstance()->getUserFactory()
|
|
|
|
|
->newFromUserIdentity( $this->getAuthority()->getUser() );
|
|
|
|
|
|
|
|
|
|
$this->contentHelper->init( $user, $this->getValidatedParams() );
|
2020-12-03 17:53:55 +00:00
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
$page = $this->contentHelper->getPageIdentity();
|
|
|
|
|
|
|
|
|
|
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
|
|
|
|
// We can't use the helper object with system messages.
|
|
|
|
|
// TODO: We should have an implementation of HtmlOutputRendererHelper
|
|
|
|
|
// for system messages in the future.
|
|
|
|
|
// Currently NO OP.
|
|
|
|
|
} elseif ( $page ) {
|
2022-06-13 09:31:50 +00:00
|
|
|
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user );
|
2022-10-10 14:46:54 +00:00
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
|
|
|
|
if ( $acceptLanguage ) {
|
|
|
|
|
$this->htmlHelper->setVariantConversionLanguage( $acceptLanguage );
|
|
|
|
|
}
|
2020-12-03 17:53:55 +00:00
|
|
|
}
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 23:32:27 +00:00
|
|
|
/**
|
2020-01-16 23:40:58 +00:00
|
|
|
* @return Response
|
|
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
2020-12-03 17:53:55 +00:00
|
|
|
public function run(): Response {
|
|
|
|
|
$this->contentHelper->checkAccess();
|
2021-05-04 20:45:30 +00:00
|
|
|
$page = $this->contentHelper->getPage();
|
2022-11-14 13:23:43 +00:00
|
|
|
$isSystemMessage = $this->contentHelper->useDefaultSystemMessage();
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
// The call to $this->contentHelper->getPage() should not return null if
|
2020-12-03 17:53:55 +00:00
|
|
|
// $this->contentHelper->checkAccess() did not throw.
|
2022-11-14 13:23:43 +00:00
|
|
|
Assert::invariant(
|
|
|
|
|
$page !== null || $isSystemMessage,
|
|
|
|
|
'Page should be known or be a valid system message page'
|
|
|
|
|
);
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( $isSystemMessage ) {
|
|
|
|
|
$parserOutput = $this->getSystemMessageOutput();
|
|
|
|
|
} else {
|
|
|
|
|
'@phan-var ExistingPageRecord $page';
|
|
|
|
|
$pageRedirectResponse = $this->createPageRedirectResponse( $page );
|
2022-11-07 15:00:26 +00:00
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( $pageRedirectResponse !== null ) {
|
|
|
|
|
return $pageRedirectResponse;
|
|
|
|
|
}
|
|
|
|
|
$parserOutput = $this->htmlHelper->getHtml();
|
2022-11-07 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-28 22:38:34 +00:00
|
|
|
// Do not de-duplicate styles, Parsoid already does it in a slightly different way (T300325)
|
|
|
|
|
$parserOutputHtml = $parserOutput->getText( [ 'deduplicateStyles' => false ] );
|
|
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
$outputMode = $this->getOutputMode();
|
|
|
|
|
switch ( $outputMode ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
case 'html':
|
|
|
|
|
$response = $this->getResponseFactory()->create();
|
2020-11-19 18:53:05 +00:00
|
|
|
$response->setHeader( 'Content-Type', 'text/html' );
|
2020-12-03 17:53:55 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
2022-01-28 22:38:34 +00:00
|
|
|
$response->setBody( new StringStream( $parserOutputHtml ) );
|
2020-01-16 23:40:58 +00:00
|
|
|
break;
|
|
|
|
|
case 'with_html':
|
2020-12-03 17:53:55 +00:00
|
|
|
$body = $this->contentHelper->constructMetadata();
|
2022-01-28 22:38:34 +00:00
|
|
|
$body['html'] = $parserOutputHtml;
|
2020-01-16 23:40:58 +00:00
|
|
|
$response = $this->getResponseFactory()->createJson( $body );
|
2020-12-03 17:53:55 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
2020-01-16 23:40:58 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-09 19:30:06 +00:00
|
|
|
throw new LogicException( "Unknown HTML type $outputMode" );
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( !$isSystemMessage ) {
|
|
|
|
|
$setContentLanguageHeader = ( $outputMode === 'html' );
|
|
|
|
|
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 15:00:26 +00:00
|
|
|
/**
|
|
|
|
|
* Check for Page Redirects and create a Redirect Response
|
|
|
|
|
* @param ExistingPageRecord $page
|
|
|
|
|
* @return Response|null
|
|
|
|
|
*/
|
|
|
|
|
private function createPageRedirectResponse( ExistingPageRecord $page ): ?Response {
|
|
|
|
|
$titleAsRequested = $this->contentHelper->getTitleText();
|
|
|
|
|
$normalizedTitle = $this->titleFormatter->getPrefixedDBkey( $page );
|
|
|
|
|
|
|
|
|
|
// Check for normalization redirects
|
|
|
|
|
if ( $titleAsRequested !== $normalizedTitle ) {
|
|
|
|
|
$redirectTargetUrl = $this->getRouteUrl( [
|
|
|
|
|
"title" => $normalizedTitle
|
|
|
|
|
] );
|
|
|
|
|
return $this->getResponseFactory()->createPermanentRedirect( $redirectTargetUrl );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = $this->getRequest()->getQueryParams();
|
|
|
|
|
$redirectParam = $params['redirect'] ?? null;
|
|
|
|
|
$redirectTarget = $this->redirectStore->getRedirectTarget( $page );
|
|
|
|
|
|
|
|
|
|
if ( $redirectTarget === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if page is a redirect
|
|
|
|
|
if ( $page->isRedirect() && $redirectParam !== 'no' ) {
|
|
|
|
|
$redirectTargetUrl = $this->getRouteUrl( [
|
|
|
|
|
"title" => $this->titleFormatter->getPrefixedDBkey(
|
|
|
|
|
$redirectTarget
|
|
|
|
|
)
|
|
|
|
|
] );
|
|
|
|
|
return $this->getResponseFactory()->createTemporaryRedirect( $redirectTargetUrl );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
private function getSystemMessageOutput(): ParserOutput {
|
|
|
|
|
$message = $this->contentHelper->getDefaultSystemMessage();
|
|
|
|
|
|
|
|
|
|
$messageDom = DOMUtils::parseHTML( $message->parse() );
|
|
|
|
|
DOMUtils::appendToHead( $messageDom, 'meta', [
|
|
|
|
|
'http-equiv' => 'content-language',
|
|
|
|
|
'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$messageDocHtml = ContentUtils::toXML( $messageDom );
|
|
|
|
|
|
|
|
|
|
// TODO: Set language in the response headers.
|
|
|
|
|
return new ParserOutput( $messageDocHtml );
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an ETag representing a page's source. The ETag assumes a page's source has changed
|
|
|
|
|
* if the latest revision of a page has been made private, un-readable for another reason,
|
|
|
|
|
* or a newer revision exists.
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getETag(): ?string {
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-05-24 21:13:42 +00:00
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
|
|
|
|
// XXX: We end up generating the HTML twice. Would be nice to avoid that.
|
|
|
|
|
// But messages are small, and not hit a lot...
|
|
|
|
|
$output = $this->getSystemMessageOutput();
|
|
|
|
|
return '"message/' . sha1( $output->getRawText() ) . '/' . $this->getOutputMode() . '"';
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:13:42 +00:00
|
|
|
// Vary eTag based on output mode
|
|
|
|
|
return $this->htmlHelper->getETag( $this->getOutputMode() );
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getLastModified(): ?string {
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-14 13:23:43 +00:00
|
|
|
|
|
|
|
|
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
return $this->htmlHelper->getLastModified();
|
2020-11-19 18:53:05 +00:00
|
|
|
}
|
2020-10-19 23:12:51 +00:00
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
private function getOutputMode(): string {
|
2020-11-19 18:53:05 +00:00
|
|
|
return $this->getConfig()['format'];
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
public function needsWriteAccess(): bool {
|
|
|
|
|
return false;
|
2020-12-02 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
public function getParamSettings(): array {
|
2022-05-16 16:56:20 +00:00
|
|
|
return array_merge(
|
|
|
|
|
$this->contentHelper->getParamSettings(),
|
|
|
|
|
$this->htmlHelper->getParamSettings()
|
|
|
|
|
);
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
}
|