wiki.techinc.nl/includes/Rest/Handler/RevisionSourceHandler.php
bpirkle 3f614a0898 Add content.v1 REST module with relevant content endpoints
T366837 added the ability to group REST endpoints into modules.
This change now introduces the first use of a REST module.
The existing endpoints remain accessible at their original paths,
and mocha tests are run against both old and new paths.

Bug: T370430
Change-Id: I83a5cdbdd359e4d3e5f70dd3930783616b556738
2024-08-07 16:51:09 -05:00

117 lines
3.1 KiB
PHP

<?php
namespace MediaWiki\Rest\Handler;
use LogicException;
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
use MediaWiki\Rest\Handler\Helper\RevisionContentHelper;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Revision\RevisionRecord;
/**
* A handler that returns page source and metadata for the following routes:
* - /revision/{revision}
* - /revision/{revision}/bare
*/
class RevisionSourceHandler extends SimpleHandler {
private RevisionContentHelper $contentHelper;
public function __construct( PageRestHelperFactory $helperFactory ) {
$this->contentHelper = $helperFactory->newRevisionContentHelper();
}
protected function postValidationSetup() {
$this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
}
/**
* @param RevisionRecord $rev
* @return string
*/
private function constructHtmlUrl( RevisionRecord $rev ): string {
// TODO: once legacy "v1" routes are removed, just use the path prefix from the module.
$pathPrefix = $this->getModule()->getPathPrefix();
if ( strlen( $pathPrefix ) == 0 ) {
$pathPrefix = 'v1';
}
return $this->getRouter()->getRouteUrl(
'/' . $pathPrefix . '/revision/{id}/html',
[ 'id' => $rev->getId() ]
);
}
/**
* @return Response
* @throws LocalizedHttpException
*/
public function run() {
$this->contentHelper->checkAccess();
$outputMode = $this->getOutputMode();
switch ( $outputMode ) {
case 'bare':
$revisionRecord = $this->contentHelper->getTargetRevision();
$body = $this->contentHelper->constructMetadata();
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable revisionRecord is set when used
$body['html_url'] = $this->constructHtmlUrl( $revisionRecord );
$response = $this->getResponseFactory()->createJson( $body );
$this->contentHelper->setCacheControl( $response );
break;
case 'source':
$content = $this->contentHelper->getContent();
$body = $this->contentHelper->constructMetadata();
$body['source'] = $content->getText();
break;
default:
throw new LogicException( "Unknown output mode $outputMode" );
}
$response = $this->getResponseFactory()->createJson( $body );
$this->contentHelper->setCacheControl( $response );
return $response;
}
protected function getResponseBodySchema(): array {
$schema = $this->contentHelper->getResponseBodySchema();
// TODO: add fields based on the output mode
return $schema;
}
/**
* @return string|null
*/
protected function getETag(): ?string {
return $this->contentHelper->getETag();
}
/**
* @return string|null
*/
protected function getLastModified(): ?string {
return $this->contentHelper->getLastModified();
}
private function getOutputMode(): string {
return $this->getConfig()['format'];
}
public function needsWriteAccess(): bool {
return false;
}
public function getParamSettings(): array {
return $this->contentHelper->getParamSettings();
}
/**
* @return bool
*/
protected function hasRepresentation() {
return $this->contentHelper->hasContent();
}
}