Merge "Introduce HtmlMessageOutputHelper for system messages"
This commit is contained in:
commit
d11a785c3b
11 changed files with 419 additions and 119 deletions
|
|
@ -1684,6 +1684,8 @@ $wgAutoloadLocalClasses = [
|
|||
'MediaWiki\\Rest\\Handler\\CreationHandler' => __DIR__ . '/includes/Rest/Handler/CreationHandler.php',
|
||||
'MediaWiki\\Rest\\Handler\\EditHandler' => __DIR__ . '/includes/Rest/Handler/EditHandler.php',
|
||||
'MediaWiki\\Rest\\Handler\\HtmlInputTransformHelper' => __DIR__ . '/includes/Rest/Handler/HtmlInputTransformHelper.php',
|
||||
'MediaWiki\\Rest\\Handler\\HtmlMessageOutputHelper' => __DIR__ . '/includes/Rest/Handler/HtmlMessageOutputHelper.php',
|
||||
'MediaWiki\\Rest\\Handler\\HtmlOutputHelper' => __DIR__ . '/includes/Rest/Handler/HtmlOutputHelper.php',
|
||||
'MediaWiki\\Rest\\Handler\\HtmlOutputRendererHelper' => __DIR__ . '/includes/Rest/Handler/HtmlOutputRendererHelper.php',
|
||||
'MediaWiki\\Rest\\Handler\\LanguageLinksHandler' => __DIR__ . '/includes/Rest/Handler/LanguageLinksHandler.php',
|
||||
'MediaWiki\\Rest\\Handler\\MediaFileHandler' => __DIR__ . '/includes/Rest/Handler/MediaFileHandler.php',
|
||||
|
|
|
|||
123
includes/Rest/Handler/HtmlMessageOutputHelper.php
Normal file
123
includes/Rest/Handler/HtmlMessageOutputHelper.php
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
namespace MediaWiki\Rest\Handler;
|
||||
|
||||
use LanguageCode;
|
||||
use MediaWiki\Page\PageIdentity;
|
||||
use MediaWiki\Rest\ResponseInterface;
|
||||
use Message;
|
||||
use ParserOutput;
|
||||
use Title;
|
||||
use Wikimedia\Parsoid\Utils\ContentUtils;
|
||||
use Wikimedia\Parsoid\Utils\DOMUtils;
|
||||
|
||||
/**
|
||||
* @since 1.40
|
||||
* @unstable
|
||||
*/
|
||||
class HtmlMessageOutputHelper implements HtmlOutputHelper {
|
||||
|
||||
private PageIdentity $page;
|
||||
|
||||
/**
|
||||
* Initializes the helper with the given parameters like the page
|
||||
* we're dealing with.
|
||||
*
|
||||
* @param PageIdentity $page
|
||||
*/
|
||||
public function init( PageIdentity $page ): void {
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Message|null
|
||||
*/
|
||||
private function getDefaultSystemMessage(): ?Message {
|
||||
$title = Title::castFromPageIdentity( $this->page );
|
||||
|
||||
return $title ? $title->getDefaultSystemMessage() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHtml(): ParserOutput {
|
||||
$message = $this->getDefaultSystemMessage();
|
||||
|
||||
// NOTE: This class should be used only for system messages,
|
||||
// so failing hard here is fine if we're not dealing with one.
|
||||
$messageDom = DOMUtils::parseHTML( $message->parse() );
|
||||
DOMUtils::appendToHead( $messageDom, 'meta', [
|
||||
'http-equiv' => 'content-language',
|
||||
'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ),
|
||||
] );
|
||||
|
||||
$messageDocHtml = ContentUtils::toXML( $messageDom );
|
||||
|
||||
return new ParserOutput( $messageDocHtml );
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getETag( string $suffix = '' ): ?string {
|
||||
// XXX: We end up generating the HTML twice. Would be nice to avoid that.
|
||||
// But messages are small, and not hit a lot...
|
||||
$output = $this->getHtml();
|
||||
|
||||
return '"message/' . sha1( $output->getRawText() ) . '/' . $suffix . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @note This is guaranteed to always return NULL since
|
||||
* proper system messages (with no DB entry) have no
|
||||
* revision, so they should have no last modified time.
|
||||
*/
|
||||
public function getLastModified(): ?string {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getParamSettings(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setVariantConversionLanguage(
|
||||
string $targetLanguageCode,
|
||||
?string $sourceLanguageCode = null
|
||||
): void {
|
||||
// TODO: Set language in the response headers.
|
||||
}
|
||||
|
||||
public function putHeaders(
|
||||
ResponseInterface $response,
|
||||
bool $forHtml = true
|
||||
): void {
|
||||
// TODO: Set language in the response headers.
|
||||
}
|
||||
|
||||
}
|
||||
98
includes/Rest/Handler/HtmlOutputHelper.php
Normal file
98
includes/Rest/Handler/HtmlOutputHelper.php
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
namespace MediaWiki\Rest\Handler;
|
||||
|
||||
use MediaWiki\Rest\LocalizedHttpException;
|
||||
use MediaWiki\Rest\ResponseInterface;
|
||||
use ParserOutput;
|
||||
use Wikimedia\Parsoid\Core\ClientError;
|
||||
|
||||
/**
|
||||
* @since 1.40
|
||||
* @unstable
|
||||
*/
|
||||
interface HtmlOutputHelper {
|
||||
|
||||
/**
|
||||
* Fetch the HTML for rendering of a given page. If the rendering is
|
||||
* available in parsoid parser cache, return that. Otherwise, perform
|
||||
* a parse and return the result while caching it in the parser cache.
|
||||
*
|
||||
* NOTE: Caching can be explicitly disabled or a force parse action
|
||||
* can be issued. Stashing and rate limiting on stashing also applies
|
||||
* here if specified.
|
||||
*
|
||||
* @return ParserOutput a tuple with html and content-type
|
||||
* @throws LocalizedHttpException
|
||||
* @throws ClientError
|
||||
*/
|
||||
public function getHtml(): ParserOutput;
|
||||
|
||||
/**
|
||||
* Returns an ETag uniquely identifying the HTML output.
|
||||
*
|
||||
* @see Handler::getETag()
|
||||
*
|
||||
* @param string $suffix A suffix to attach to the etag.
|
||||
*
|
||||
* @return string|null We return null when there is no etag.
|
||||
*/
|
||||
public function getETag( string $suffix = '' ): ?string;
|
||||
|
||||
/**
|
||||
* Returns the time at which the HTML was rendered.
|
||||
*
|
||||
* @see Handler::getLastModified()
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getLastModified(): ?string;
|
||||
|
||||
/**
|
||||
* Gets the request parameters of this request.
|
||||
*
|
||||
* @see Handler::getParamSettings()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParamSettings(): array;
|
||||
|
||||
/**
|
||||
* Set the language to be used for variant conversion.
|
||||
*
|
||||
* @param string $targetLanguageCode
|
||||
* @param null|string $sourceLanguageCode
|
||||
*/
|
||||
public function setVariantConversionLanguage(
|
||||
string $targetLanguageCode,
|
||||
?string $sourceLanguageCode = null
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Set the HTTP headers based on the response generated
|
||||
*
|
||||
* @param ResponseInterface $response
|
||||
* @param bool $forHtml Whether the response will be HTML (rather than JSON)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function putHeaders( ResponseInterface $response, bool $forHtml = true ): void;
|
||||
|
||||
}
|
||||
|
|
@ -20,6 +20,7 @@
|
|||
namespace MediaWiki\Rest\Handler;
|
||||
|
||||
use Content;
|
||||
use HttpError;
|
||||
use IBufferingStatsdDataFactory;
|
||||
use Language;
|
||||
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
|
||||
|
|
@ -50,7 +51,6 @@ use User;
|
|||
use Wikimedia\Assert\Assert;
|
||||
use Wikimedia\Message\MessageValue;
|
||||
use Wikimedia\ParamValidator\ParamValidator;
|
||||
use Wikimedia\Parsoid\Core\ClientError;
|
||||
use Wikimedia\Parsoid\Core\PageBundle;
|
||||
use Wikimedia\Parsoid\Parsoid;
|
||||
use Wikimedia\Parsoid\Utils\ContentUtils;
|
||||
|
|
@ -63,7 +63,7 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
|
|||
*
|
||||
* @unstable Pending consolidation of the Parsoid extension with core code.
|
||||
*/
|
||||
class HtmlOutputRendererHelper {
|
||||
class HtmlOutputRendererHelper implements HtmlOutputHelper {
|
||||
/**
|
||||
* @internal
|
||||
* @var string[]
|
||||
|
|
@ -271,6 +271,11 @@ class HtmlOutputRendererHelper {
|
|||
*/
|
||||
public function setRevision( $revisionOrId ): void {
|
||||
Assert::parameterType( [ RevisionRecord::class, 'integer' ], $revisionOrId, '$revision' );
|
||||
|
||||
if ( is_int( $revisionOrId ) && $revisionOrId <= 0 ) {
|
||||
throw new HttpError( 400, "Bad revision ID: $revisionOrId" );
|
||||
}
|
||||
|
||||
$this->revisionOrId = $revisionOrId;
|
||||
|
||||
if ( $this->getRevisionId() === null ) {
|
||||
|
|
@ -335,11 +340,15 @@ class HtmlOutputRendererHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initializes the helper with the given parameters like the page
|
||||
* we're dealing with, parameters gotten from the request inputs,
|
||||
* and the revision if any is available.
|
||||
*
|
||||
* @param PageIdentity $page
|
||||
* @param array $parameters
|
||||
* @param User $user
|
||||
* @param RevisionRecord|int|null $revision DEPRECATED, use setRevision()
|
||||
* @param Language|null $pageLanguage DEPRECATED, use setPageLanguage()
|
||||
* @param RevisionRecord|int|null $revision
|
||||
* @param Language|null $pageLanguage
|
||||
*/
|
||||
public function init(
|
||||
PageIdentity $page,
|
||||
|
|
@ -350,10 +359,16 @@ class HtmlOutputRendererHelper {
|
|||
) {
|
||||
$this->page = $page;
|
||||
$this->user = $user;
|
||||
$this->revisionOrId = $revision;
|
||||
$this->pageLanguage = $pageLanguage;
|
||||
$this->stash = $parameters['stash'] ?? false;
|
||||
|
||||
if ( $revision !== null ) {
|
||||
$this->setRevision( $revision );
|
||||
}
|
||||
|
||||
if ( $pageLanguage !== null ) {
|
||||
$this->setPageLanguage( $pageLanguage );
|
||||
}
|
||||
|
||||
if ( $this->stash ) {
|
||||
$this->setFlavor( 'stash' );
|
||||
} else {
|
||||
|
|
@ -362,19 +377,18 @@ class HtmlOutputRendererHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the language to be used for variant conversion
|
||||
* @param string $targetLanguageCode
|
||||
* @param null|string $sourceLanguageCode
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setVariantConversionLanguage( string $targetLanguageCode, ?string $sourceLanguageCode = null ) {
|
||||
public function setVariantConversionLanguage(
|
||||
string $targetLanguageCode,
|
||||
?string $sourceLanguageCode = null
|
||||
): void {
|
||||
$this->targetLanguageCode = $targetLanguageCode;
|
||||
$this->sourceLanguageCode = $sourceLanguageCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParserOutput a tuple with html and content-type
|
||||
* @throws LocalizedHttpException
|
||||
* @throws ClientError
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHtml(): ParserOutput {
|
||||
if ( $this->processedParserOutput ) {
|
||||
|
|
@ -442,11 +456,7 @@ class HtmlOutputRendererHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns an ETag uniquely identifying the HTML output.
|
||||
*
|
||||
* @param string $suffix A suffix to attach to the etag.
|
||||
*
|
||||
* @return string|null
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getETag( string $suffix = '' ): ?string {
|
||||
$parserOutput = $this->getParserOutput();
|
||||
|
|
@ -467,16 +477,14 @@ class HtmlOutputRendererHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the time at which the HTML was rendered.
|
||||
*
|
||||
* @return string|null
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLastModified(): ?string {
|
||||
return $this->getParserOutput()->getCacheTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getParamSettings(): array {
|
||||
return [
|
||||
|
|
@ -566,14 +574,9 @@ class HtmlOutputRendererHelper {
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the HTTP headers based on the response generated
|
||||
*
|
||||
* @param ResponseInterface $response
|
||||
* @param bool $forHtml Whether the response will be HTML (rather than JSON)
|
||||
*
|
||||
* @return void
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function putHeaders( ResponseInterface $response, bool $forHtml = true ) {
|
||||
public function putHeaders( ResponseInterface $response, bool $forHtml = true ): void {
|
||||
if ( $forHtml ) {
|
||||
// For HTML we want to set the Content-Language. For JSON, we probably don't.
|
||||
$response->setHeader( 'Content-Language', $this->getHtmlOutputContentLanguage() );
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace MediaWiki\Rest\Handler;
|
||||
|
||||
use LanguageCode;
|
||||
use LogicException;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Page\RedirectStore;
|
||||
|
|
@ -10,11 +9,8 @@ use MediaWiki\Rest\LocalizedHttpException;
|
|||
use MediaWiki\Rest\Response;
|
||||
use MediaWiki\Rest\SimpleHandler;
|
||||
use MediaWiki\Rest\StringStream;
|
||||
use ParserOutput;
|
||||
use TitleFormatter;
|
||||
use Wikimedia\Assert\Assert;
|
||||
use Wikimedia\Parsoid\Utils\ContentUtils;
|
||||
use Wikimedia\Parsoid\Utils\DOMUtils;
|
||||
|
||||
/**
|
||||
* A handler that returns Parsoid HTML for the following routes:
|
||||
|
|
@ -26,7 +22,7 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
|
|||
class PageHTMLHandler extends SimpleHandler {
|
||||
use PageRedirectHandlerTrait;
|
||||
|
||||
/** @var HtmlOutputRendererHelper */
|
||||
/** @var HtmlOutputHelper */
|
||||
private $htmlHelper;
|
||||
|
||||
/** @var PageContentHelper */
|
||||
|
|
@ -38,6 +34,8 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
/** @var RedirectStore */
|
||||
private $redirectStore;
|
||||
|
||||
private PageRestHelperFactory $helperFactory;
|
||||
|
||||
public function __construct(
|
||||
TitleFormatter $titleFormatter,
|
||||
RedirectStore $redirectStore,
|
||||
|
|
@ -46,6 +44,7 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
$this->titleFormatter = $titleFormatter;
|
||||
$this->redirectStore = $redirectStore;
|
||||
$this->contentHelper = $helperFactory->newPageContentHelper();
|
||||
$this->helperFactory = $helperFactory;
|
||||
$this->htmlHelper = $helperFactory->newHtmlOutputRendererHelper();
|
||||
}
|
||||
|
||||
|
|
@ -57,19 +56,23 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
$this->contentHelper->init( $user, $this->getValidatedParams() );
|
||||
|
||||
$page = $this->contentHelper->getPageIdentity();
|
||||
$isSystemMessage = $this->contentHelper->useDefaultSystemMessage();
|
||||
|
||||
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
||||
// We can't use the helper object with system messages.
|
||||
// TODO: We should have an implementation of HtmlOutputRendererHelper
|
||||
// for system messages in the future.
|
||||
// Currently NO OP.
|
||||
} elseif ( $page ) {
|
||||
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user );
|
||||
if ( $page ) {
|
||||
if ( $isSystemMessage ) {
|
||||
$this->htmlHelper = $this->helperFactory->newHtmlMessageOutputHelper();
|
||||
$this->htmlHelper->init( $page );
|
||||
} else {
|
||||
$revision = $this->contentHelper->getTargetRevision();
|
||||
// NOTE: We know that $this->htmlHelper is an instance of HtmlOutputRendererHelper
|
||||
// because we set it in the constructor.
|
||||
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user, $revision );
|
||||
|
||||
$request = $this->getRequest();
|
||||
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
||||
if ( $acceptLanguage ) {
|
||||
$this->htmlHelper->setVariantConversionLanguage( $acceptLanguage );
|
||||
$request = $this->getRequest();
|
||||
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
||||
if ( $acceptLanguage ) {
|
||||
$this->htmlHelper->setVariantConversionLanguage( $acceptLanguage );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -80,8 +83,7 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
*/
|
||||
public function run(): Response {
|
||||
$this->contentHelper->checkAccess();
|
||||
$page = $this->contentHelper->getPage();
|
||||
$isSystemMessage = $this->contentHelper->useDefaultSystemMessage();
|
||||
$page = $this->contentHelper->getPageIdentity();
|
||||
$params = $this->getRequest()->getQueryParams();
|
||||
|
||||
if ( array_key_exists( 'redirect', $params ) ) {
|
||||
|
|
@ -92,30 +94,22 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
|
||||
// The call to $this->contentHelper->getPage() should not return null if
|
||||
// $this->contentHelper->checkAccess() did not throw.
|
||||
Assert::invariant(
|
||||
$page !== null || $isSystemMessage,
|
||||
'Page should be known or be a valid system message page'
|
||||
Assert::invariant( $page !== null, 'Page should be known' );
|
||||
|
||||
$redirectResponse = $this->createRedirectResponseIfNeeded(
|
||||
$page,
|
||||
$followWikiRedirects,
|
||||
$this->contentHelper->getTitleText(),
|
||||
$this->titleFormatter,
|
||||
$this->redirectStore
|
||||
);
|
||||
|
||||
if ( $isSystemMessage ) {
|
||||
$parserOutput = $this->getSystemMessageOutput();
|
||||
} else {
|
||||
'@phan-var \MediaWiki\Page\ExistingPageRecord $page';
|
||||
$redirectResponse = $this->createRedirectResponseIfNeeded(
|
||||
$page,
|
||||
$followWikiRedirects,
|
||||
$this->contentHelper->getTitleText(),
|
||||
$this->titleFormatter,
|
||||
$this->redirectStore
|
||||
);
|
||||
|
||||
if ( $redirectResponse !== null ) {
|
||||
return $redirectResponse;
|
||||
}
|
||||
|
||||
$parserOutput = $this->htmlHelper->getHtml();
|
||||
if ( $redirectResponse !== null ) {
|
||||
return $redirectResponse;
|
||||
}
|
||||
|
||||
$parserOutput = $this->htmlHelper->getHtml();
|
||||
|
||||
// Do not de-duplicate styles, Parsoid already does it in a slightly different way (T300325)
|
||||
$parserOutputHtml = $parserOutput->getText( [ 'deduplicateStyles' => false ] );
|
||||
|
||||
|
|
@ -130,17 +124,12 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
$body = $this->contentHelper->constructMetadata();
|
||||
$body['html'] = $parserOutputHtml;
|
||||
|
||||
if ( $page ) {
|
||||
// If param redirect=no is present, that means this page can be a redirect
|
||||
// check for a redirectTargetUrl and send it to the body as `redirect_target`
|
||||
'@phan-var \MediaWiki\Page\ExistingPageRecord $page';
|
||||
$redirectTargetUrl = $this->getWikiRedirectTargetUrl(
|
||||
$page, $this->redirectStore, $this->titleFormatter
|
||||
);
|
||||
$redirectTargetUrl = $this->getWikiRedirectTargetUrl(
|
||||
$page, $this->redirectStore, $this->titleFormatter
|
||||
);
|
||||
|
||||
if ( $redirectTargetUrl ) {
|
||||
$body['redirect_target'] = $redirectTargetUrl;
|
||||
}
|
||||
if ( $redirectTargetUrl ) {
|
||||
$body['redirect_target'] = $redirectTargetUrl;
|
||||
}
|
||||
|
||||
$response = $this->getResponseFactory()->createJson( $body );
|
||||
|
|
@ -150,29 +139,12 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
throw new LogicException( "Unknown HTML type $outputMode" );
|
||||
}
|
||||
|
||||
if ( !$isSystemMessage ) {
|
||||
$setContentLanguageHeader = ( $outputMode === 'html' );
|
||||
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
|
||||
}
|
||||
$setContentLanguageHeader = ( $outputMode === 'html' );
|
||||
$this->htmlHelper->putHeaders( $response, $setContentLanguageHeader );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function getSystemMessageOutput(): ParserOutput {
|
||||
$message = $this->contentHelper->getDefaultSystemMessage();
|
||||
|
||||
$messageDom = DOMUtils::parseHTML( $message->parse() );
|
||||
DOMUtils::appendToHead( $messageDom, 'meta', [
|
||||
'http-equiv' => 'content-language',
|
||||
'content' => LanguageCode::bcp47( $message->getLanguage()->getCode() ),
|
||||
] );
|
||||
|
||||
$messageDocHtml = ContentUtils::toXML( $messageDom );
|
||||
|
||||
// TODO: Set language in the response headers.
|
||||
return new ParserOutput( $messageDocHtml );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
|
|
@ -184,13 +156,6 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
return null;
|
||||
}
|
||||
|
||||
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
||||
// XXX: We end up generating the HTML twice. Would be nice to avoid that.
|
||||
// But messages are small, and not hit a lot...
|
||||
$output = $this->getSystemMessageOutput();
|
||||
return '"message/' . sha1( $output->getRawText() ) . '/' . $this->getOutputMode() . '"';
|
||||
}
|
||||
|
||||
// Vary eTag based on output mode
|
||||
return $this->htmlHelper->getETag( $this->getOutputMode() );
|
||||
}
|
||||
|
|
@ -203,10 +168,6 @@ class PageHTMLHandler extends SimpleHandler {
|
|||
return null;
|
||||
}
|
||||
|
||||
if ( $this->contentHelper->useDefaultSystemMessage() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->htmlHelper->getLastModified();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -99,6 +99,10 @@ class PageRestHelperFactory {
|
|||
);
|
||||
}
|
||||
|
||||
public function newHtmlMessageOutputHelper(): HtmlMessageOutputHelper {
|
||||
return new HtmlMessageOutputHelper();
|
||||
}
|
||||
|
||||
public function newHtmlInputTransformHelper( $envOptions = [] ): HtmlInputTransformHelper {
|
||||
return new HtmlInputTransformHelper(
|
||||
$this->stats,
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ class RevisionHTMLHandler extends SimpleHandler {
|
|||
$revision = $this->contentHelper->getTargetRevision();
|
||||
|
||||
if ( $page && $revision ) {
|
||||
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user );
|
||||
$this->htmlHelper->setRevision( $revision );
|
||||
$this->htmlHelper->init( $page, $this->getValidatedParams(), $user, $revision );
|
||||
|
||||
$request = $this->getRequest();
|
||||
$acceptLanguage = $request->getHeaderLine( 'Accept-Language' ) ?: null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Rest\Helper;
|
||||
|
||||
use MediaWiki\Rest\Handler\HtmlMessageOutputHelper;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper
|
||||
* @group Database
|
||||
*/
|
||||
class HtmlMessageOutputHelperTest extends MediaWikiIntegrationTestCase {
|
||||
private function newHelper(): HtmlMessageOutputHelper {
|
||||
return new HtmlMessageOutputHelper();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::init
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::getHtml
|
||||
*/
|
||||
public function testGetHtml() {
|
||||
$page = $this->getNonexistingTestPage( 'MediaWiki:Logouttext' );
|
||||
|
||||
$helper = $this->newHelper();
|
||||
$helper->init( $page );
|
||||
|
||||
$this->assertSame( 0, $page->getLatest() );
|
||||
|
||||
$htmlresult = $helper->getHtml()->getRawText();
|
||||
|
||||
$this->assertStringContainsString( 'You are now logged out', $htmlresult );
|
||||
// Check that we have a full HTML document in English
|
||||
$this->assertStringContainsString( '<html', $htmlresult );
|
||||
$this->assertStringContainsString( 'content="en"', $htmlresult );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::init
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::getETag
|
||||
*/
|
||||
public function testGetETag() {
|
||||
$page = $this->getNonexistingTestPage( 'MediaWiki:Logouttext' );
|
||||
|
||||
$helper = $this->newHelper();
|
||||
$helper->init( $page );
|
||||
|
||||
$etag = $helper->getETag();
|
||||
|
||||
$this->assertStringContainsString( '"message/', $etag );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::init
|
||||
* @covers \MediaWiki\Rest\Handler\HtmlMessageOutputHelper::getHtml
|
||||
*/
|
||||
public function testGetHtmlWithLanguageCode() {
|
||||
$page = $this->getNonexistingTestPage( 'MediaWiki:Logouttext/de' );
|
||||
|
||||
$helper = $this->newHelper();
|
||||
$helper->init( $page );
|
||||
|
||||
$this->assertSame( 0, $page->getLatest() );
|
||||
|
||||
$htmlresult = $helper->getHtml()->getRawText();
|
||||
|
||||
$this->assertStringContainsString( 'Du bist nun abgemeldet', $htmlresult );
|
||||
// Check that we have a full HTML document in English
|
||||
$this->assertStringContainsString( '<html', $htmlresult );
|
||||
$this->assertStringContainsString( 'content="de"', $htmlresult );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Rest\Handler;
|
||||
|
||||
use MediaWiki\Rest\Handler\HtmlMessageOutputHelper;
|
||||
use MediaWiki\Rest\Handler\HtmlOutputHelper;
|
||||
use MediaWiki\Rest\Handler\HtmlOutputRendererHelper;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\PageRestHelperFactory
|
||||
*/
|
||||
class PageRestHelperFactoryTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\Rest\Handler\PageRestHelperFactory::newHtmlMessageOutputHelper
|
||||
* @covers \MediaWiki\Rest\Handler\PageRestHelperFactory::newHtmlOutputRendererHelper
|
||||
*/
|
||||
public function testNewHtmlOutputHelpers() {
|
||||
$helperFactory = $this->getServiceContainer()->getPageRestHelperFactory();
|
||||
|
||||
$helper = $helperFactory->newHtmlMessageOutputHelper();
|
||||
|
||||
$this->assertInstanceOf( HtmlMessageOutputHelper::class, $helper );
|
||||
$this->assertInstanceOf( HtmlOutputHelper::class, $helper );
|
||||
|
||||
$helper = $helperFactory->newHtmlOutputRendererHelper();
|
||||
|
||||
$this->assertInstanceOf( HtmlOutputRendererHelper::class, $helper );
|
||||
$this->assertInstanceOf( HtmlOutputHelper::class, $helper );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1965,7 +1965,7 @@ class ParsoidHandlerTest extends MediaWikiIntegrationTestCase {
|
|||
* @dataProvider provideWt2html
|
||||
*
|
||||
* @param array $attribs
|
||||
* @param string $text
|
||||
* @param string|null $text
|
||||
* @param string[] $expectedData
|
||||
* @param string[] $unexpectedHtml
|
||||
* @param string[] $expectedHeaders
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use MediaWiki\MainConfigNames;
|
|||
use MediaWiki\MainConfigSchema;
|
||||
use MediaWiki\Parser\ParserCacheFactory;
|
||||
use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
|
||||
use MediaWiki\Rest\Handler\HtmlMessageOutputHelper;
|
||||
use MediaWiki\Rest\Handler\HtmlOutputRendererHelper;
|
||||
use MediaWiki\Rest\Handler\LanguageLinksHandler;
|
||||
use MediaWiki\Rest\Handler\PageContentHelper;
|
||||
|
|
@ -83,7 +84,7 @@ trait PageHandlerTestTrait {
|
|||
|
||||
$helperFactory = $this->createNoOpMock(
|
||||
PageRestHelperFactory::class,
|
||||
[ 'newPageContentHelper', 'newHtmlOutputRendererHelper' ]
|
||||
[ 'newPageContentHelper', 'newHtmlOutputRendererHelper', 'newHtmlMessageOutputHelper' ]
|
||||
);
|
||||
|
||||
$helperFactory->method( 'newPageContentHelper' )
|
||||
|
|
@ -94,15 +95,20 @@ trait PageHandlerTestTrait {
|
|||
$services->getPageStore()
|
||||
) );
|
||||
|
||||
$parsoidOutputStash = $this->getParsoidOutputStash();
|
||||
$helperFactory->method( 'newHtmlOutputRendererHelper' )
|
||||
->willReturn( new HtmlOutputRendererHelper(
|
||||
$this->getParsoidOutputStash(),
|
||||
$services->getStatsdDataFactory(),
|
||||
$parsoidOutputAccess,
|
||||
$services->getHtmlTransformFactory(),
|
||||
$services->getContentHandlerFactory(),
|
||||
$services->getLanguageFactory()
|
||||
) );
|
||||
->willReturn(
|
||||
new HtmlOutputRendererHelper(
|
||||
$parsoidOutputStash,
|
||||
$services->getStatsdDataFactory(),
|
||||
$parsoidOutputAccess,
|
||||
$services->getHtmlTransformFactory(),
|
||||
$services->getContentHandlerFactory(),
|
||||
$services->getLanguageFactory()
|
||||
)
|
||||
);
|
||||
$helperFactory->method( 'newHtmlMessageOutputHelper' )
|
||||
->willReturn( new HtmlMessageOutputHelper() );
|
||||
|
||||
return new PageHTMLHandler(
|
||||
$services->getTitleFormatter(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue