wiki.techinc.nl/includes/Rest/Handler/RevisionSourceHandler.php
Bill Pirkle fc7acbee64 Promote experimental revision rest endpoints to official
These three endpoints have been experimental for many months:
  revision/{id}
  revision/{id}/html
  revision/{id}/with_html
Promote them to officially released. This completes the
basic "revision" endpoint support, and helps clear out
the coreDevelopmentRoutes.json file for unrelated
experiments.

This also modifies the existing revision/{id}/bare endpoint
response, which previously pointed callers to the experimental
endpoint for html. It now points callers to the official one.

Bug: T305506
Change-Id: Iee8d1723e98dd3e3e389a0514dde28799914b2fd
2022-04-13 11:30:53 -05:00

124 lines
3 KiB
PHP

<?php
namespace MediaWiki\Rest\Handler;
use Config;
use LogicException;
use MediaWiki\Page\PageLookup;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use TitleFormatter;
/**
* A handler that returns page source and metadata for the following routes:
* - /revision/{revision}
* - /revision/{revision}/bare
*/
class RevisionSourceHandler extends SimpleHandler {
/** @var RevisionContentHelper */
private $contentHelper;
/**
* @param Config $config
* @param RevisionLookup $revisionLookup
* @param TitleFormatter $titleFormatter
* @param PageLookup $pageLookup
*/
public function __construct(
Config $config,
RevisionLookup $revisionLookup,
TitleFormatter $titleFormatter,
PageLookup $pageLookup
) {
$this->contentHelper = new RevisionContentHelper(
$config,
$revisionLookup,
$titleFormatter,
$pageLookup
);
}
protected function postValidationSetup() {
$this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
}
/**
* @param RevisionRecord $rev
* @return string
*/
private function constructHtmlUrl( RevisionRecord $rev ): string {
return $this->getRouter()->getRouteUrl(
'/v1/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;
}
/**
* @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();
}
}