Debian Codegraph @

includes/Rest/Handler/PageHTMLHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Rest\Handler;
4
5 use LogicException;
6 use MediaWiki\Rest\Handler\Helper\HtmlOutputHelper;
7 use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper;
8 use MediaWiki\Rest\Handler\Helper\PageContentHelper;
9 use MediaWiki\Rest\Handler\Helper\PageRedirectHelper;
10 use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
11 use MediaWiki\Rest\LocalizedHttpException;
12 use MediaWiki\Rest\Response;
13 use MediaWiki\Rest\SimpleHandler;
14 use MediaWiki\Rest\StringStream;
15 use Wikimedia\Assert\Assert;
16
17 /**
18 * A handler that returns Parsoid HTML for the following routes:
19 * - /page/{title}/html,
20 * - /page/{title}/with_html
21 *
22 * @package MediaWiki\Rest\Handler
23 */
24 class PageHTMLHandler extends SimpleHandler {
25
26 private HtmlOutputHelper $htmlHelper;
27 private PageContentHelper $contentHelper;
28 private PageRestHelperFactory $helperFactory;
29
30 public function __construct(
31 PageRestHelperFactory $helperFactory
32 ) {
33 $this->contentHelper = $helperFactory->newPageContentHelper();
34 $this->helperFactory = $helperFactory;
35 }
36
37 private function getRedirectHelper(): PageRedirectHelper {
38 return $this->helperFactory->newPageRedirectHelper(
39 $this->getResponseFactory(),
40 $this->getRouter(),
41 $this->getPath(),
42 $this->getRequest()
43 );
44 }
45
46 protected function postValidationSetup() {
47 $authority = $this->getAuthority();
48 $this->contentHelper->init( $authority, $this->getValidatedParams() );
49
50 $page = $this->contentHelper->getPageIdentity();
51 $isSystemMessage = $this->contentHelper->useDefaultSystemMessage();
52
53 if ( $page ) {
54 if ( $isSystemMessage ) {
55 $this->htmlHelper = $this->helperFactory->newHtmlMessageOutputHelper( $page );
56 } else {
57 $revision = $this->contentHelper->getTargetRevision();
58 $this->htmlHelper = $this->helperFactory->newHtmlOutputRendererHelper(
59 $page, $this->getValidatedParams(), $authority, $revision
60 );
61
62 $request = $this->getRequest();
63 $acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
64 if ( $acceptLanguage ) {
65 $this->htmlHelper->setVariantConversionLanguage(
66 $acceptLanguage
67 );
68 }
69 }
70 }
71 }
72
73 /**
74 * @return Response
75 * @throws LocalizedHttpException
76 */
77 public function run(): Response {
78 $this->contentHelper->checkAccessPermission();
79 $page = $this->contentHelper->getPageIdentity();
80 $params = $this->getRequest()->getQueryParams();
81
82 if ( array_key_exists( 'redirect', $params ) ) {
83 $followWikiRedirects = $params['redirect'] !== 'no';
84 } else {
85 $followWikiRedirects = true;
86 }
87
88 // The call to $this->contentHelper->getPage() should not return null if
89 // $this->contentHelper->checkAccess() did not throw.
90 Assert::invariant( $page !== null, 'Page should be known' );
91
92 $redirectHelper = $this->getRedirectHelper();
93 $redirectHelper->setFollowWikiRedirects( $followWikiRedirects );
94 // Should treat variant redirects a special case as wiki redirects
95 // if ?redirect=no language variant should do nothing and fall into the 404 path
96 $redirectResponse = $redirectHelper->createRedirectResponseIfNeeded(
97 $page,
98 $this->contentHelper->getTitleText()
99 );
100
101 if ( $redirectResponse !== null ) {
102 $redirectResponse->setHeader( 'Cache-Control', 'max-age=60' );
103 return $redirectResponse;
104 }
105
106 // We could have a missing page at this point, check and return 404 if that's the case
107 $this->contentHelper->checkHasContent();
108
109 $parserOutput = $this->htmlHelper->getHtml();
110 $parserOutputHtml = $parserOutput->getRawText();
111
112 $outputMode = $this->getOutputMode();
113 switch ( $outputMode ) {
114 case 'html':
115 $response = $this->getResponseFactory()->create();
116 $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
117 $response->setBody( new StringStream( $parserOutputHtml ) );
118 break;
119 case 'with_html':
120 $body = $this->contentHelper->constructMetadata();
121 $body['html'] = $parserOutputHtml;
122
123 $redirectTargetUrl = $redirectHelper->getWikiRedirectTargetUrl( $page );
124
125 if ( $redirectTargetUrl ) {
126 $body['redirect_target'] = $redirectTargetUrl;
127 }
128
129 $response = $this->getResponseFactory()->createJson( $body );
130 $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
131 break;
132 default:
133 throw new LogicException( "Unknown HTML type $outputMode" );
134 }
135
136 $setContentLanguageHeader = ( $outputMode === 'html' );
137 $this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
138
139 return $response;
140 }
141
142 /**
143 * Returns an ETag representing a page's source. The ETag assumes a page's source has changed
144 * if the latest revision of a page has been made private, un-readable for another reason,
145 * or a newer revision exists.
146 * @return string|null
147 */
148 protected function getETag(): ?string {
149 if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
150 return null;
151 }
152
153 // Vary eTag based on output mode
154 return $this->htmlHelper->getETag( $this->getOutputMode() );
155 }
156
157 /**
158 * @return string|null
159 */
160 protected function getLastModified(): ?string {
161 if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
162 return null;
163 }
164
165 return $this->htmlHelper->getLastModified();
166 }
167
168 private function getOutputMode(): string {
169 return $this->getConfig()['format'];
170 }
171
172 public function needsWriteAccess(): bool {
173 return false;
174 }
175
176 public function getParamSettings(): array {
177 return array_merge(
178 $this->contentHelper->getParamSettings(),
179 // Note that postValidation we might end up using
180 // a HtmlMessageOutputHelper, but the param settings
181 // for that are a subset of those for HtmlOutputRendererHelper
182 HtmlOutputRendererHelper::getParamSettings()
183 );
184 }
185
186 protected function generateResponseSpec( string $method ): array {
187 $spec = parent::generateResponseSpec( $method );
188
189 // TODO: Consider if we prefer something like:
190 // text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"
191 // That would be more specific, but fragile when the profile version changes. It could
192 // also be inaccurate if the page content was not in fact produced by Parsoid.
193 if ( $this->getOutputMode() == 'html' ) {
194 unset( $spec['200']['content']['application/json'] );
195 $spec['200']['content']['text/html']['schema']['type'] = 'string';
196 }
197
198 return $spec;
199 }
200
201 public function getResponseBodySchemaFileName( string $method ): ?string {
202 return 'includes/Rest/Handler/Schema/ExistingPageHtml.json';
203 }
204 }