Debian Codegraph @

includes/Rest/Handler/RevisionHTMLHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Rest\Handler;
4
5 use LogicException;
6 use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper;
7 use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
8 use MediaWiki\Rest\Handler\Helper\RevisionContentHelper;
9 use MediaWiki\Rest\LocalizedHttpException;
10 use MediaWiki\Rest\Response;
11 use MediaWiki\Rest\SimpleHandler;
12 use MediaWiki\Rest\StringStream;
13 use Wikimedia\Assert\Assert;
14
15 /**
16 * A handler that returns Parsoid HTML for the following routes:
17 * - /revision/{revision}/html,
18 * - /revision/{revision}/with_html
19 */
20 class RevisionHTMLHandler extends SimpleHandler {
21
22 private ?HtmlOutputRendererHelper $htmlHelper = null;
23 private PageRestHelperFactory $helperFactory;
24 private RevisionContentHelper $contentHelper;
25
26 public function __construct( PageRestHelperFactory $helperFactory ) {
27 $this->helperFactory = $helperFactory;
28 $this->contentHelper = $helperFactory->newRevisionContentHelper();
29 }
30
31 protected function postValidationSetup() {
32 $authority = $this->getAuthority();
33 $this->contentHelper->init( $authority, $this->getValidatedParams() );
34
35 $page = $this->contentHelper->getPage();
36 $revision = $this->contentHelper->getTargetRevision();
37
38 if ( $page && $revision ) {
39 $this->htmlHelper = $this->helperFactory->newHtmlOutputRendererHelper(
40 $page, $this->getValidatedParams(), $authority, $revision
41 );
42
43 $request = $this->getRequest();
44 $acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
45 if ( $acceptLanguage ) {
46 $this->htmlHelper->setVariantConversionLanguage(
47 $acceptLanguage
48 );
49 }
50 }
51 }
52
53 /**
54 * @return Response
55 * @throws LocalizedHttpException
56 */
57 public function run(): Response {
58 $this->contentHelper->checkAccess();
59
60 $page = $this->contentHelper->getPage();
61 $revisionRecord = $this->contentHelper->getTargetRevision();
62
63 // The call to $this->contentHelper->getPage() should not return null if
64 // $this->contentHelper->checkAccess() did not throw.
65 Assert::invariant( $page !== null, 'Page should be known' );
66
67 // The call to $this->contentHelper->getTargetRevision() should not return null if
68 // $this->contentHelper->checkAccess() did not throw.
69 Assert::invariant( $revisionRecord !== null, 'Revision should be known' );
70
71 $outputMode = $this->getOutputMode();
72 $setContentLanguageHeader = true;
73 switch ( $outputMode ) {
74 case 'html':
75 $parserOutput = $this->htmlHelper->getHtml();
76 $response = $this->getResponseFactory()->create();
77 // TODO: need to respect content-type returned by Parsoid.
78 $response->setHeader( 'Content-Type', 'text/html' );
79 $this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
80 $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
81 $response->setBody( new StringStream( $parserOutput->getRawText() ) );
82 break;
83 case 'with_html':
84 $parserOutput = $this->htmlHelper->getHtml();
85 $body = $this->contentHelper->constructMetadata();
86 $body['html'] = $parserOutput->getRawText();
87 $response = $this->getResponseFactory()->createJson( $body );
88 // For JSON content, it doesn't make sense to set content language header
89 $this->htmlHelper->putHeaders( $response, !$setContentLanguageHeader );
90 $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
91 break;
92 default:
93 throw new LogicException( "Unknown HTML type $outputMode" );
94 }
95
96 return $response;
97 }
98
99 /**
100 * Returns an ETag representing a page's source. The ETag assumes a page's source has changed
101 * if the latest revision of a page has been made private, un-readable for another reason,
102 * or a newer revision exists.
103 * @return string|null
104 */
105 protected function getETag(): ?string {
106 if ( !$this->contentHelper->isAccessible() ) {
107 return null;
108 }
109
110 // Vary eTag based on output mode
111 return $this->htmlHelper->getETag( $this->getOutputMode() );
112 }
113
114 /**
115 * @return string|null
116 */
117 protected function getLastModified(): ?string {
118 if ( !$this->contentHelper->isAccessible() ) {
119 return null;
120 }
121
122 return $this->htmlHelper->getLastModified();
123 }
124
125 private function getOutputMode(): string {
126 return $this->getConfig()['format'];
127 }
128
129 public function needsWriteAccess(): bool {
130 return false;
131 }
132
133 public function getParamSettings(): array {
134 return array_merge(
135 $this->contentHelper->getParamSettings(),
136 HtmlOutputRendererHelper::getParamSettings()
137 );
138 }
139
140 /**
141 * @return bool
142 */
143 protected function hasRepresentation() {
144 return $this->contentHelper->hasContent();
145 }
146 }