2020-01-16 23:40:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use LogicException;
|
2023-01-13 21:30:21 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\HtmlOutputHelper;
|
2024-06-05 15:49:49 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper;
|
2023-01-13 21:30:21 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\PageContentHelper;
|
2023-06-11 16:56:29 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\PageRedirectHelper;
|
2023-01-13 21:30:21 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
|
2020-01-16 23:40:58 +00:00
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\Response;
|
2020-12-03 17:53:55 +00:00
|
|
|
use MediaWiki\Rest\SimpleHandler;
|
2020-01-16 23:40:58 +00:00
|
|
|
use MediaWiki\Rest\StringStream;
|
2020-12-03 17:53:55 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2020-01-16 23:40:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A handler that returns Parsoid HTML for the following routes:
|
|
|
|
|
* - /page/{title}/html,
|
|
|
|
|
* - /page/{title}/with_html
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki\Rest\Handler
|
|
|
|
|
*/
|
2020-12-03 17:53:55 +00:00
|
|
|
class PageHTMLHandler extends SimpleHandler {
|
2022-11-07 15:00:26 +00:00
|
|
|
|
2023-06-11 16:56:29 +00:00
|
|
|
private HtmlOutputHelper $htmlHelper;
|
|
|
|
|
private PageContentHelper $contentHelper;
|
2022-11-22 12:58:57 +00:00
|
|
|
private PageRestHelperFactory $helperFactory;
|
|
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
public function __construct(
|
2022-11-18 16:55:18 +00:00
|
|
|
PageRestHelperFactory $helperFactory
|
2020-01-16 23:40:58 +00:00
|
|
|
) {
|
2022-11-18 16:55:18 +00:00
|
|
|
$this->contentHelper = $helperFactory->newPageContentHelper();
|
2022-11-22 12:58:57 +00:00
|
|
|
$this->helperFactory = $helperFactory;
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-11 16:56:29 +00:00
|
|
|
private function getRedirectHelper(): PageRedirectHelper {
|
|
|
|
|
return $this->helperFactory->newPageRedirectHelper(
|
|
|
|
|
$this->getResponseFactory(),
|
|
|
|
|
$this->getRouter(),
|
|
|
|
|
$this->getPath(),
|
|
|
|
|
$this->getRequest()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
protected function postValidationSetup() {
|
2023-11-09 13:32:23 +00:00
|
|
|
$authority = $this->getAuthority();
|
|
|
|
|
$this->contentHelper->init( $authority, $this->getValidatedParams() );
|
2020-12-03 17:53:55 +00:00
|
|
|
|
2022-11-14 13:23:43 +00:00
|
|
|
$page = $this->contentHelper->getPageIdentity();
|
2022-11-22 12:58:57 +00:00
|
|
|
$isSystemMessage = $this->contentHelper->useDefaultSystemMessage();
|
2022-11-14 13:23:43 +00:00
|
|
|
|
2022-11-22 12:58:57 +00:00
|
|
|
if ( $page ) {
|
|
|
|
|
if ( $isSystemMessage ) {
|
2024-06-05 15:49:49 +00:00
|
|
|
$this->htmlHelper = $this->helperFactory->newHtmlMessageOutputHelper( $page );
|
2022-11-22 12:58:57 +00:00
|
|
|
} else {
|
|
|
|
|
$revision = $this->contentHelper->getTargetRevision();
|
2024-06-05 15:49:49 +00:00
|
|
|
$this->htmlHelper = $this->helperFactory->newHtmlOutputRendererHelper(
|
|
|
|
|
$page, $this->getValidatedParams(), $authority, $revision
|
|
|
|
|
);
|
2022-11-22 12:58:57 +00:00
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
|
|
|
|
if ( $acceptLanguage ) {
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$this->htmlHelper->setVariantConversionLanguage(
|
|
|
|
|
$acceptLanguage
|
|
|
|
|
);
|
2022-11-22 12:58:57 +00:00
|
|
|
}
|
2022-10-10 14:46:54 +00:00
|
|
|
}
|
2020-12-03 17:53:55 +00:00
|
|
|
}
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 23:32:27 +00:00
|
|
|
/**
|
2020-01-16 23:40:58 +00:00
|
|
|
* @return Response
|
|
|
|
|
* @throws LocalizedHttpException
|
|
|
|
|
*/
|
2020-12-03 17:53:55 +00:00
|
|
|
public function run(): Response {
|
2023-06-16 18:56:48 +00:00
|
|
|
$this->contentHelper->checkAccessPermission();
|
2022-11-22 12:58:57 +00:00
|
|
|
$page = $this->contentHelper->getPageIdentity();
|
2022-11-16 19:08:58 +00:00
|
|
|
$params = $this->getRequest()->getQueryParams();
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( 'redirect', $params ) ) {
|
|
|
|
|
$followWikiRedirects = $params['redirect'] !== 'no';
|
|
|
|
|
} else {
|
|
|
|
|
$followWikiRedirects = true;
|
|
|
|
|
}
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2021-05-04 20:45:30 +00:00
|
|
|
// The call to $this->contentHelper->getPage() should not return null if
|
2020-12-03 17:53:55 +00:00
|
|
|
// $this->contentHelper->checkAccess() did not throw.
|
2022-11-22 12:58:57 +00:00
|
|
|
Assert::invariant( $page !== null, 'Page should be known' );
|
|
|
|
|
|
2023-06-11 16:56:29 +00:00
|
|
|
$redirectHelper = $this->getRedirectHelper();
|
|
|
|
|
$redirectHelper->setFollowWikiRedirects( $followWikiRedirects );
|
2023-06-16 18:56:48 +00:00
|
|
|
// Should treat variant redirects a special case as wiki redirects
|
|
|
|
|
// if ?redirect=no language variant should do nothing and fall into the 404 path
|
2023-06-11 16:56:29 +00:00
|
|
|
$redirectResponse = $redirectHelper->createRedirectResponseIfNeeded(
|
2022-11-22 12:58:57 +00:00
|
|
|
$page,
|
2023-06-11 16:56:29 +00:00
|
|
|
$this->contentHelper->getTitleText()
|
2022-11-14 13:23:43 +00:00
|
|
|
);
|
2020-01-16 23:40:58 +00:00
|
|
|
|
2022-11-22 12:58:57 +00:00
|
|
|
if ( $redirectResponse !== null ) {
|
|
|
|
|
return $redirectResponse;
|
2022-11-07 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 18:56:48 +00:00
|
|
|
// We could have a missing page at this point, check and return 404 if that's the case
|
|
|
|
|
$this->contentHelper->checkHasContent();
|
|
|
|
|
|
2022-11-22 12:58:57 +00:00
|
|
|
$parserOutput = $this->htmlHelper->getHtml();
|
2022-05-27 16:38:32 +00:00
|
|
|
$parserOutputHtml = $parserOutput->getRawText();
|
2022-01-28 22:38:34 +00:00
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
$outputMode = $this->getOutputMode();
|
|
|
|
|
switch ( $outputMode ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
case 'html':
|
|
|
|
|
$response = $this->getResponseFactory()->create();
|
2020-12-03 17:53:55 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
2022-01-28 22:38:34 +00:00
|
|
|
$response->setBody( new StringStream( $parserOutputHtml ) );
|
2020-01-16 23:40:58 +00:00
|
|
|
break;
|
|
|
|
|
case 'with_html':
|
2020-12-03 17:53:55 +00:00
|
|
|
$body = $this->contentHelper->constructMetadata();
|
2022-01-28 22:38:34 +00:00
|
|
|
$body['html'] = $parserOutputHtml;
|
2022-11-16 19:08:58 +00:00
|
|
|
|
2023-06-11 16:56:29 +00:00
|
|
|
$redirectTargetUrl = $redirectHelper->getWikiRedirectTargetUrl( $page );
|
2022-11-22 12:58:57 +00:00
|
|
|
|
|
|
|
|
if ( $redirectTargetUrl ) {
|
|
|
|
|
$body['redirect_target'] = $redirectTargetUrl;
|
2022-11-16 19:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
$response = $this->getResponseFactory()->createJson( $body );
|
2020-12-03 17:53:55 +00:00
|
|
|
$this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
|
2020-01-16 23:40:58 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-09 19:30:06 +00:00
|
|
|
throw new LogicException( "Unknown HTML type $outputMode" );
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-22 12:58:57 +00:00
|
|
|
$setContentLanguageHeader = ( $outputMode === 'html' );
|
|
|
|
|
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
|
2022-11-14 13:23:43 +00:00
|
|
|
|
2020-01-16 23:40:58 +00:00
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an ETag representing a page's source. The ETag assumes a page's source has changed
|
|
|
|
|
* if the latest revision of a page has been made private, un-readable for another reason,
|
|
|
|
|
* or a newer revision exists.
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getETag(): ?string {
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-05-24 21:13:42 +00:00
|
|
|
|
|
|
|
|
// Vary eTag based on output mode
|
|
|
|
|
return $this->htmlHelper->getETag( $this->getOutputMode() );
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getLastModified(): ?string {
|
2022-11-14 13:23:43 +00:00
|
|
|
if ( !$this->contentHelper->isAccessible() || !$this->contentHelper->hasContent() ) {
|
2020-01-16 23:40:58 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-14 13:23:43 +00:00
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
return $this->htmlHelper->getLastModified();
|
2020-11-19 18:53:05 +00:00
|
|
|
}
|
2020-10-19 23:12:51 +00:00
|
|
|
|
2020-12-09 19:30:06 +00:00
|
|
|
private function getOutputMode(): string {
|
2020-11-19 18:53:05 +00:00
|
|
|
return $this->getConfig()['format'];
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
public function needsWriteAccess(): bool {
|
|
|
|
|
return false;
|
2020-12-02 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 17:53:55 +00:00
|
|
|
public function getParamSettings(): array {
|
2022-05-16 16:56:20 +00:00
|
|
|
return array_merge(
|
|
|
|
|
$this->contentHelper->getParamSettings(),
|
2024-06-05 15:49:49 +00:00
|
|
|
// Note that postValidation we might end up using
|
|
|
|
|
// a HtmlMessageOutputHelper, but the param settings
|
|
|
|
|
// for that are a subset of those for HtmlOutputRendererHelper
|
|
|
|
|
HtmlOutputRendererHelper::getParamSettings()
|
2022-05-16 16:56:20 +00:00
|
|
|
);
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|
2024-10-30 04:25:25 +00:00
|
|
|
|
|
|
|
|
protected function generateResponseSpec( string $method ): array {
|
|
|
|
|
$spec = parent::generateResponseSpec( $method );
|
|
|
|
|
|
|
|
|
|
// TODO: Consider if we prefer something like:
|
|
|
|
|
// text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"
|
|
|
|
|
// That would be more specific, but fragile when the profile version changes. It could
|
|
|
|
|
// also be inaccurate if the page content was not in fact produced by Parsoid.
|
|
|
|
|
if ( $this->getOutputMode() == 'html' ) {
|
|
|
|
|
unset( $spec['200']['content']['application/json'] );
|
|
|
|
|
$spec['200']['content']['text/html']['schema']['type'] = 'string';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $spec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getResponseBodySchemaFileName( string $method ): ?string {
|
|
|
|
|
return 'includes/Rest/Handler/Schema/ExistingPageHtml.json';
|
|
|
|
|
}
|
2020-01-16 23:40:58 +00:00
|
|
|
}
|