| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory; |
| 7 |
use MediaWiki\Rest\Handler\Helper\RevisionContentHelper; |
| 8 |
use MediaWiki\Rest\LocalizedHttpException; |
| 9 |
use MediaWiki\Rest\Response; |
| 10 |
use MediaWiki\Rest\SimpleHandler; |
| 11 |
use MediaWiki\Revision\RevisionRecord; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class RevisionSourceHandler extends SimpleHandler { |
| 19 |
|
| 20 |
private RevisionContentHelper $contentHelper; |
| 21 |
|
| 22 |
public function __construct( PageRestHelperFactory $helperFactory ) { |
| 23 |
$this->contentHelper = $helperFactory->newRevisionContentHelper(); |
| 24 |
} |
| 25 |
|
| 26 |
protected function postValidationSetup() { |
| 27 |
$this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private function constructHtmlUrl( RevisionRecord $rev ): string { |
| 35 |
|
| 36 |
$pathPrefix = $this->getModule()->getPathPrefix(); |
| 37 |
if ( strlen( $pathPrefix ) == 0 ) { |
| 38 |
$pathPrefix = 'v1'; |
| 39 |
} |
| 40 |
|
| 41 |
return $this->getRouter()->getRouteUrl( |
| 42 |
'/' . $pathPrefix . '/revision/{id}/html', |
| 43 |
[ 'id' => $rev->getId() ] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function run() { |
| 52 |
$this->contentHelper->checkAccess(); |
| 53 |
|
| 54 |
$outputMode = $this->getOutputMode(); |
| 55 |
switch ( $outputMode ) { |
| 56 |
case 'restbase': |
| 57 |
$body = [ 'items' => [ $this->contentHelper->constructRestbaseCompatibleMetadata() ] ]; |
| 58 |
break; |
| 59 |
case 'bare': |
| 60 |
$revisionRecord = $this->contentHelper->getTargetRevision(); |
| 61 |
$body = $this->contentHelper->constructMetadata(); |
| 62 |
|
| 63 |
$body['html_url'] = $this->constructHtmlUrl( $revisionRecord ); |
| 64 |
$response = $this->getResponseFactory()->createJson( $body ); |
| 65 |
$this->contentHelper->setCacheControl( $response ); |
| 66 |
break; |
| 67 |
case 'source': |
| 68 |
$content = $this->contentHelper->getContent(); |
| 69 |
$body = $this->contentHelper->constructMetadata(); |
| 70 |
$body['source'] = $content->getText(); |
| 71 |
break; |
| 72 |
default: |
| 73 |
throw new LogicException( "Unknown output mode $outputMode" ); |
| 74 |
} |
| 75 |
|
| 76 |
$response = $this->getResponseFactory()->createJson( $body ); |
| 77 |
$this->contentHelper->setCacheControl( $response ); |
| 78 |
|
| 79 |
return $response; |
| 80 |
} |
| 81 |
|
| 82 |
protected function getResponseBodySchemaFileName( string $method ): ?string { |
| 83 |
|
| 84 |
return 'includes/Rest/Handler/Schema/RevisionMetaData.json'; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected function getETag(): ?string { |
| 91 |
return $this->contentHelper->getETag(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
protected function getLastModified(): ?string { |
| 98 |
return $this->contentHelper->getLastModified(); |
| 99 |
} |
| 100 |
|
| 101 |
private function getOutputMode(): string { |
| 102 |
if ( $this->getRouter()->isRestbaseCompatEnabled( $this->getRequest() ) ) { |
| 103 |
return 'restbase'; |
| 104 |
} |
| 105 |
return $this->getConfig()['format']; |
| 106 |
} |
| 107 |
|
| 108 |
public function needsWriteAccess(): bool { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
public function getParamSettings(): array { |
| 113 |
return $this->contentHelper->getParamSettings(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
protected function hasRepresentation() { |
| 120 |
return $this->contentHelper->hasContent(); |
| 121 |
} |
| 122 |
} |