2020-12-14 12:26:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use LogicException;
|
2022-06-13 09:31:50 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-12-14 12:26:02 +00:00
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\Response;
|
|
|
|
|
use MediaWiki\Rest\SimpleHandler;
|
|
|
|
|
use MediaWiki\Rest\StringStream;
|
|
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A handler that returns Parsoid HTML for the following routes:
|
|
|
|
|
* - /revision/{revision}/html,
|
|
|
|
|
* - /revision/{revision}/with_html
|
|
|
|
|
*
|
|
|
|
|
* Class RevisionHTMLHandler
|
|
|
|
|
* @package MediaWiki\Rest\Handler
|
|
|
|
|
*/
|
|
|
|
|
class RevisionHTMLHandler extends SimpleHandler {
|
|
|
|
|
|
2022-09-06 09:07:45 +00:00
|
|
|
/** @var HtmlOutputRendererHelper */
|
2020-12-14 12:26:02 +00:00
|
|
|
private $htmlHelper;
|
|
|
|
|
|
|
|
|
|
/** @var RevisionContentHelper */
|
|
|
|
|
private $contentHelper;
|
|
|
|
|
|
2022-11-18 16:55:18 +00:00
|
|
|
public function __construct( PageRestHelperFactory $helperFactory ) {
|
|
|
|
|
$this->contentHelper = $helperFactory->newRevisionContentHelper();
|
|
|
|
|
$this->htmlHelper = $helperFactory->newHtmlOutputRendererHelper();
|
2020-12-14 12:26:02 +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-14 12:26:02 +00:00
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
$page = $this->contentHelper->getPage();
|
2020-12-14 12:26:02 +00:00
|
|
|
$revision = $this->contentHelper->getTargetRevision();
|
|
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
if ( $page && $revision ) {
|
2022-11-22 12:58:57 +00:00
|
|
|
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user, $revision );
|
2022-10-30 07:10:22 +00:00
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
|
|
|
|
if ( $acceptLanguage ) {
|
|
|
|
|
$this->htmlHelper->setVariantConversionLanguage( $acceptLanguage );
|
|
|
|
|
}
|
2020-12-14 12:26:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Response
|
|
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
|
|
|
|
public function run(): Response {
|
|
|
|
|
$this->contentHelper->checkAccess();
|
|
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
$page = $this->contentHelper->getPage();
|
2020-12-14 12:26:02 +00:00
|
|
|
$revisionRecord = $this->contentHelper->getTargetRevision();
|
|
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
// The call to $this->contentHelper->getPage() should not return null if
|
2020-12-14 12:26:02 +00:00
|
|
|
// $this->contentHelper->checkAccess() did not throw.
|
2021-05-04 20:45:30 +00:00
|
|
|
Assert::invariant( $page !== null, 'Page should be known' );
|
2020-12-14 12:26:02 +00:00
|
|
|
|
|
|
|
|
// The call to $this->contentHelper->getTargetRevision() should not return null if
|
|
|
|
|
// $this->contentHelper->checkAccess() did not throw.
|
|
|
|
|
Assert::invariant( $revisionRecord !== null, 'Revision should be known' );
|
|
|
|
|
|
|
|
|
|
$outputMode = $this->getOutputMode();
|
2022-10-30 07:10:22 +00:00
|
|
|
$setContentLanguageHeader = true;
|
2020-12-14 12:26:02 +00:00
|
|
|
switch ( $outputMode ) {
|
|
|
|
|
case 'html':
|
|
|
|
|
$parserOutput = $this->htmlHelper->getHtml();
|
|
|
|
|
$response = $this->getResponseFactory()->create();
|
|
|
|
|
// TODO: need to respect content-type returned by Parsoid.
|
|
|
|
|
$response->setHeader( 'Content-Type', 'text/html' );
|
2022-10-30 07:10:22 +00:00
|
|
|
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
|
2020-12-14 12:26:02 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
|
|
|
|
$response->setBody( new StringStream( $parserOutput->getText() ) );
|
|
|
|
|
break;
|
|
|
|
|
case 'with_html':
|
|
|
|
|
$parserOutput = $this->htmlHelper->getHtml();
|
|
|
|
|
$body = $this->contentHelper->constructMetadata();
|
|
|
|
|
$body['html'] = $parserOutput->getText();
|
|
|
|
|
$response = $this->getResponseFactory()->createJson( $body );
|
2022-10-30 07:10:22 +00:00
|
|
|
// For JSON content, it doesn't make sense to set content language header
|
|
|
|
|
$this->htmlHelper->putHeaders( $response, !$setContentLanguageHeader );
|
2020-12-14 12:26:02 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new LogicException( "Unknown HTML type $outputMode" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 {
|
|
|
|
|
if ( !$this->contentHelper->isAccessible() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:13:42 +00:00
|
|
|
// Vary eTag based on output mode
|
|
|
|
|
return $this->htmlHelper->getETag( $this->getOutputMode() );
|
2020-12-14 12:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getLastModified(): ?string {
|
|
|
|
|
if ( !$this->contentHelper->isAccessible() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->htmlHelper->getLastModified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getOutputMode(): string {
|
|
|
|
|
return $this->getConfig()['format'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function needsWriteAccess(): bool {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamSettings(): array {
|
2022-05-16 16:56:20 +00:00
|
|
|
return array_merge(
|
|
|
|
|
$this->contentHelper->getParamSettings(),
|
|
|
|
|
$this->htmlHelper->getParamSettings()
|
|
|
|
|
);
|
2020-12-14 12:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function hasRepresentation() {
|
|
|
|
|
return $this->contentHelper->hasContent();
|
|
|
|
|
}
|
|
|
|
|
}
|