wiki.techinc.nl/includes/Rest/Handler/RevisionHTMLHandler.php

167 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Rest\Handler;
use Config;
use LogicException;
use MediaWiki\Edit\ParsoidOutputStash;
use MediaWiki\Page\PageLookup;
use MediaWiki\Parser\ParserCacheFactory;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\StringStream;
use MediaWiki\Revision\RevisionLookup;
use TitleFormatter;
use Wikimedia\Assert\Assert;
use Wikimedia\UUID\GlobalIdGenerator;
/**
* 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 {
/** @var ParsoidHTMLHelper */
private $htmlHelper;
/** @var RevisionContentHelper */
private $contentHelper;
/**
* @param Config $config
* @param RevisionLookup $revisionLookup
* @param TitleFormatter $titleFormatter
* @param ParserCacheFactory $parserCacheFactory
* @param GlobalIdGenerator $globalIdGenerator
* @param PageLookup $pageLookup
* @param ParsoidOutputStash $parsoidOutputStash
*/
public function __construct(
Config $config,
RevisionLookup $revisionLookup,
TitleFormatter $titleFormatter,
ParserCacheFactory $parserCacheFactory,
GlobalIdGenerator $globalIdGenerator,
PageLookup $pageLookup,
ParsoidOutputStash $parsoidOutputStash
) {
$this->contentHelper = new RevisionContentHelper(
$config,
$revisionLookup,
$titleFormatter,
$pageLookup
);
$this->htmlHelper = new ParsoidHTMLHelper(
$parserCacheFactory->getParserCache( 'parsoid' ),
$parserCacheFactory->getRevisionOutputCache( 'parsoid' ),
$globalIdGenerator,
$parsoidOutputStash
);
}
protected function postValidationSetup() {
$this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
$page = $this->contentHelper->getPage();
$revision = $this->contentHelper->getTargetRevision();
if ( $page && $revision ) {
$this->htmlHelper->init( $page, $this->getValidatedParams(), $revision );
}
}
/**
* @return Response
* @throws LocalizedHttpException
*/
public function run(): Response {
$this->contentHelper->checkAccess();
$page = $this->contentHelper->getPage();
$revisionRecord = $this->contentHelper->getTargetRevision();
// The call to $this->contentHelper->getPage() should not return null if
// $this->contentHelper->checkAccess() did not throw.
Assert::invariant( $page !== null, 'Page should be known' );
// 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();
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' );
$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 );
$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;
}
return $this->htmlHelper->getETag();
}
/**
* @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 {
return array_merge(
$this->contentHelper->getParamSettings(),
$this->htmlHelper->getParamSettings()
);
}
/**
* @return bool
*/
protected function hasRepresentation() {
return $this->contentHelper->hasContent();
}
}